datawithsuman
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -24,16 +24,16 @@ import nest_asyncio
|
|
24 |
nest_asyncio.apply()
|
25 |
|
26 |
# OpenAI credentials
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
|
31 |
# key = os.getenv('MISTRAL_API_KEY')
|
32 |
# os.environ["MISTRAL_API_KEY"] = key
|
33 |
|
34 |
# Anthropic credentials
|
35 |
-
key = os.getenv('CLAUDE_API_KEY')
|
36 |
-
os.environ["ANTHROPIC_API_KEY"] = key
|
37 |
|
38 |
# Streamlit UI
|
39 |
st.title("Prompt Optimization for a Policy Bot")
|
@@ -46,7 +46,6 @@ if uploaded_files:
|
|
46 |
f.write(uploaded_file.getbuffer())
|
47 |
reader = SimpleDirectoryReader(input_files=[f"./data/{uploaded_file.name}"])
|
48 |
documents = reader.load_data()
|
49 |
-
st.write(documents)
|
50 |
st.success("File uploaded...")
|
51 |
|
52 |
# # Indexing
|
@@ -144,6 +143,35 @@ if uploaded_files:
|
|
144 |
return [response.usage.prompt_tokens, response.usage.completion_tokens, response.usage.total_tokens, response.choices[0].message.content]
|
145 |
|
146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
# Initialize session state for token summary, evaluation details, and chat messages
|
148 |
if "token_summary" not in st.session_state:
|
149 |
st.session_state.token_summary = []
|
@@ -155,9 +183,6 @@ if uploaded_files:
|
|
155 |
with st.chat_message(message["role"]):
|
156 |
st.markdown(message["content"])
|
157 |
|
158 |
-
# Summarize
|
159 |
-
full_prompt = "\n\n".join([context + prompt])
|
160 |
-
orig_res = res(full_prompt)
|
161 |
|
162 |
# Accept user input
|
163 |
if prompt := st.chat_input("Enter your query:"):
|
|
|
24 |
nest_asyncio.apply()
|
25 |
|
26 |
# OpenAI credentials
|
27 |
+
key = os.getenv('OPENAI_API_KEY')
|
28 |
+
openai.api_key = key
|
29 |
+
os.environ["OPENAI_API_KEY"] = key
|
30 |
|
31 |
# key = os.getenv('MISTRAL_API_KEY')
|
32 |
# os.environ["MISTRAL_API_KEY"] = key
|
33 |
|
34 |
# Anthropic credentials
|
35 |
+
# key = os.getenv('CLAUDE_API_KEY')
|
36 |
+
# os.environ["ANTHROPIC_API_KEY"] = key
|
37 |
|
38 |
# Streamlit UI
|
39 |
st.title("Prompt Optimization for a Policy Bot")
|
|
|
46 |
f.write(uploaded_file.getbuffer())
|
47 |
reader = SimpleDirectoryReader(input_files=[f"./data/{uploaded_file.name}"])
|
48 |
documents = reader.load_data()
|
|
|
49 |
st.success("File uploaded...")
|
50 |
|
51 |
# # Indexing
|
|
|
143 |
return [response.usage.prompt_tokens, response.usage.completion_tokens, response.usage.total_tokens, response.choices[0].message.content]
|
144 |
|
145 |
|
146 |
+
# Summary
|
147 |
+
def summary(prompt):
|
148 |
+
|
149 |
+
response = openai.chat.completions.create(
|
150 |
+
model=model,
|
151 |
+
messages=[
|
152 |
+
{"role":"system",
|
153 |
+
"content":"Summarize the following context:"
|
154 |
+
},
|
155 |
+
{"role": "user",
|
156 |
+
"content": prompt,
|
157 |
+
}
|
158 |
+
]
|
159 |
+
)
|
160 |
+
return response.choices[0].message.content
|
161 |
+
|
162 |
+
|
163 |
+
full_prompt = documents[0].text
|
164 |
+
st.success("Input text")
|
165 |
+
st.markdown(full_prompt)
|
166 |
+
|
167 |
+
st.success("Generated summary")
|
168 |
+
gen_summ = summary(full_prompt)
|
169 |
+
st.markdown(gen_summ)
|
170 |
+
st.success("Reference summary")
|
171 |
+
ref_summ = summary(full_prompt)
|
172 |
+
st.markdown(ref_summ)
|
173 |
+
|
174 |
+
|
175 |
# Initialize session state for token summary, evaluation details, and chat messages
|
176 |
if "token_summary" not in st.session_state:
|
177 |
st.session_state.token_summary = []
|
|
|
183 |
with st.chat_message(message["role"]):
|
184 |
st.markdown(message["content"])
|
185 |
|
|
|
|
|
|
|
186 |
|
187 |
# Accept user input
|
188 |
if prompt := st.chat_input("Enter your query:"):
|