Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,7 +26,42 @@ embeddings = HuggingFaceEmbeddings(
|
|
| 26 |
db = FAISS.load_local("IPCC_index_e5_1000_all", embeddings)
|
| 27 |
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def generate_response(input_text):
|
| 31 |
docs = db.similarity_search(input_text,k=5)
|
| 32 |
|
|
@@ -44,16 +79,14 @@ def generate_response(input_text):
|
|
| 44 |
|
| 45 |
{question}
|
| 46 |
|
| 47 |
-
Generate a comprehensive and informative answer to the general audience of 100 words or less for the \
|
| 48 |
given question based solely on the provided search results (hyperlink and source). You must \
|
| 49 |
only use information from the provided search results. Use an unbiased and \
|
| 50 |
journalistic tone. Combine search results together into a coherent answer. Do not \
|
| 51 |
repeat text. Only use \
|
| 52 |
relevant results that answer the question accurately. list these sources at the end of your answer \
|
| 53 |
-
in a section named "source".
|
| 54 |
-
|
| 55 |
-
After the "source" section, add a "highlight" section\
|
| 56 |
-
And add a short highlight of your answer in humor and make sure no more than 5 words.\
|
| 57 |
|
| 58 |
Format your answer in markdown format
|
| 59 |
|
|
@@ -96,7 +129,7 @@ def generate_response(input_text):
|
|
| 96 |
llm = ChatOpenAI(
|
| 97 |
model_name="gpt-3.5-turbo-16k",
|
| 98 |
temperature=0.05,
|
| 99 |
-
max_tokens=
|
| 100 |
openai_api_key=openai_api_key
|
| 101 |
)
|
| 102 |
|
|
@@ -126,55 +159,59 @@ st.caption("π¬ A Climate Change chatbot powered by OpenAI LLM and IPCC documen
|
|
| 126 |
|
| 127 |
|
| 128 |
if "messages" not in st.session_state:
|
| 129 |
-
st.session_state["messages"] = [{"role": "assistant", "content": "Any question about the climate change?"}]
|
| 130 |
|
| 131 |
for msg in st.session_state.messages:
|
| 132 |
st.chat_message(msg["role"]).write(msg["content"])
|
| 133 |
|
|
|
|
|
|
|
|
|
|
| 134 |
if prompt := st.chat_input():
|
|
|
|
|
|
|
| 135 |
if not openai_api_key:
|
| 136 |
st.info("Please add your OpenAI API key to continue.")
|
| 137 |
st.stop()
|
| 138 |
|
| 139 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 140 |
st.chat_message("user").write(prompt)
|
| 141 |
-
|
| 142 |
-
result_r = result["result"]
|
| 143 |
-
index = result_r.find("Highlight:")
|
| 144 |
-
|
| 145 |
-
# Extract everything after "Highlight:"
|
| 146 |
-
match = re.search(r"Highlight: (.+)", result_r)
|
| 147 |
-
if match:
|
| 148 |
-
highlighted_text = match.group(1)
|
| 149 |
-
else:
|
| 150 |
-
highlighted_text="hello world"
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
|
| 155 |
-
# Display assistant response in chat message container
|
| 156 |
with st.chat_message("assistant"):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
message_placeholder = st.empty()
|
| 158 |
full_response = ""
|
| 159 |
assistant_response = result_r
|
| 160 |
-
|
| 161 |
for chunk in assistant_response.split():
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
message_placeholder.write(result_r)
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
st.session_state.messages.append({"role": "assistant", "content": result_r})
|
| 169 |
|
| 170 |
-
#st.session_state.messages.append({"role": "assistant", "content": result["result"]})
|
| 171 |
-
#st.chat_message("assistant").write(result_r)
|
| 172 |
-
#display_typing_effect(st.chat_message("assistant"), result_r)
|
| 173 |
-
#st.markdown(result['source_documents'][0])
|
| 174 |
-
#st.markdown(result['source_documents'][1])
|
| 175 |
-
#st.markdown(result['source_documents'][2])
|
| 176 |
-
#st.markdown(result['source_documents'][3])
|
| 177 |
-
#st.markdown(result['source_documents'][4])
|
| 178 |
|
| 179 |
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
db = FAISS.load_local("IPCC_index_e5_1000_all", embeddings)
|
| 27 |
|
| 28 |
|
| 29 |
+
question1 = 'Why does temperature increase?'
|
| 30 |
+
question2 = 'What evidence we have of climate change?'
|
| 31 |
+
question3 = 'What is the link between health and climate change?'
|
| 32 |
+
|
| 33 |
+
def click_button(button_text):
|
| 34 |
+
if prompt := button_text:
|
| 35 |
+
|
| 36 |
+
#if prompt := st.text_input(label="Your quesiton:",value=st.session_state.button_text if 'button_text' in st.session_state else 'Text your question'):
|
| 37 |
+
if not openai_api_key:
|
| 38 |
+
st.info("Please add your OpenAI API key to continue.")
|
| 39 |
+
st.stop()
|
| 40 |
+
|
| 41 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 42 |
+
st.chat_message("user").write(prompt)
|
| 43 |
+
with st.spinner("Thinking..."):
|
| 44 |
+
result = generate_response(prompt)
|
| 45 |
+
result_r = result["result"]
|
| 46 |
+
index = result_r.find("Highlight:")
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# Display assistant response in chat message container
|
| 51 |
+
with st.chat_message("assistant"):
|
| 52 |
+
message_placeholder = st.empty()
|
| 53 |
+
full_response = ""
|
| 54 |
+
assistant_response = result_r
|
| 55 |
+
# Simulate stream of response with milliseconds delay
|
| 56 |
+
for chunk in assistant_response.split():
|
| 57 |
+
full_response += chunk + " "
|
| 58 |
+
time.sleep(0.05)
|
| 59 |
+
# Add a blinking cursor to simulate typing
|
| 60 |
+
message_placeholder.write(full_response + "β")
|
| 61 |
+
message_placeholder.write(result_r)
|
| 62 |
+
# Add assistant response to chat history
|
| 63 |
+
st.session_state.messages.append({"role": "assistant", "content": result_r})
|
| 64 |
+
|
| 65 |
def generate_response(input_text):
|
| 66 |
docs = db.similarity_search(input_text,k=5)
|
| 67 |
|
|
|
|
| 79 |
|
| 80 |
{question}
|
| 81 |
|
| 82 |
+
Generate a comprehensive and informative answer and three next questions to the general audience of 100 words or less for the \
|
| 83 |
given question based solely on the provided search results (hyperlink and source). You must \
|
| 84 |
only use information from the provided search results. Use an unbiased and \
|
| 85 |
journalistic tone. Combine search results together into a coherent answer. Do not \
|
| 86 |
repeat text. Only use \
|
| 87 |
relevant results that answer the question accurately. list these sources at the end of your answer \
|
| 88 |
+
in a section named "source". After the "source" section, makre sure provide three next questions in the section of predicted \
|
| 89 |
+
\
|
|
|
|
|
|
|
| 90 |
|
| 91 |
Format your answer in markdown format
|
| 92 |
|
|
|
|
| 129 |
llm = ChatOpenAI(
|
| 130 |
model_name="gpt-3.5-turbo-16k",
|
| 131 |
temperature=0.05,
|
| 132 |
+
max_tokens=2500,
|
| 133 |
openai_api_key=openai_api_key
|
| 134 |
)
|
| 135 |
|
|
|
|
| 159 |
|
| 160 |
|
| 161 |
if "messages" not in st.session_state:
|
| 162 |
+
st.session_state["messages"] = [{"role": "assistant", "content": "Any question about the climate change? Here are some examples:"}]
|
| 163 |
|
| 164 |
for msg in st.session_state.messages:
|
| 165 |
st.chat_message(msg["role"]).write(msg["content"])
|
| 166 |
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
|
| 170 |
if prompt := st.chat_input():
|
| 171 |
+
|
| 172 |
+
#if prompt := st.text_input(label="Your quesiton:",value=st.session_state.button_text if 'button_text' in st.session_state else 'Text your question'):
|
| 173 |
if not openai_api_key:
|
| 174 |
st.info("Please add your OpenAI API key to continue.")
|
| 175 |
st.stop()
|
| 176 |
|
| 177 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 178 |
st.chat_message("user").write(prompt)
|
| 179 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
|
|
|
| 181 |
with st.chat_message("assistant"):
|
| 182 |
+
with st.spinner("thinking..."):
|
| 183 |
+
result = generate_response(prompt)
|
| 184 |
+
result_r = result["result"]
|
| 185 |
+
index = result_r.find("Highlight:")
|
| 186 |
message_placeholder = st.empty()
|
| 187 |
full_response = ""
|
| 188 |
assistant_response = result_r
|
| 189 |
+
# Simulate stream of response with milliseconds delay
|
| 190 |
for chunk in assistant_response.split():
|
| 191 |
+
full_response += chunk + " "
|
| 192 |
+
time.sleep(0.05)
|
| 193 |
+
# Add a blinking cursor to simulate typing
|
| 194 |
+
message_placeholder.write(full_response + "β")
|
| 195 |
message_placeholder.write(result_r)
|
| 196 |
+
|
| 197 |
+
if re.search(r'Next Questions?:', result_r, flags=re.IGNORECASE):
|
| 198 |
+
|
| 199 |
+
questions_text = re.split(r'Next Questions?:', result_r, flags=re.IGNORECASE)[-1]
|
| 200 |
+
|
| 201 |
+
pattern = r'\d+\.\s*([^?]+\?)'
|
| 202 |
+
|
| 203 |
+
# Use re.findall to find all matches of the pattern in the questions_text
|
| 204 |
+
matches = re.findall(pattern, questions_text)
|
| 205 |
+
|
| 206 |
+
question1 = matches[0].strip()
|
| 207 |
+
question2 = matches[1].strip()
|
| 208 |
+
question3 = matches[2].strip()
|
| 209 |
+
# Add assistant response to chat history
|
| 210 |
st.session_state.messages.append({"role": "assistant", "content": result_r})
|
| 211 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
|
| 214 |
+
button_col1, button_col2, button_col3= st.columns([5, 5, 5])
|
| 215 |
+
st.code(question1)
|
| 216 |
+
st.code(question2)
|
| 217 |
+
st.code(question3)
|