Spaces:
Runtime error
Runtime error
query reformulation
Browse files
app.py
CHANGED
@@ -13,22 +13,16 @@ import numpy as np
|
|
13 |
from datetime import datetime
|
14 |
from azure.storage.fileshare import ShareServiceClient
|
15 |
|
16 |
-
from dotenv import load_dotenv
|
17 |
|
18 |
-
# Load the environment variables from the .env file
|
19 |
-
load_dotenv()
|
20 |
-
print(os.environ)
|
21 |
-
|
22 |
-
# for key in ["CONTENT","API_KEY","SOURCES","RESSOURCE_ENDPOINT"]:
|
23 |
-
# os.environ[key.lower()] = os.environ[key]
|
24 |
|
|
|
25 |
|
26 |
theme = gr.themes.Soft(
|
27 |
primary_hue="sky",
|
28 |
-
font=[gr.themes.GoogleFont(
|
29 |
)
|
30 |
|
31 |
-
|
32 |
system_template = {"role": "system", "content": os.environ["content"]}
|
33 |
|
34 |
openai.api_type = "azure"
|
@@ -59,14 +53,10 @@ credential = {
|
|
59 |
"account_name": os.environ["account_name"],
|
60 |
}
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
share_client = service.get_share_client(file_share_name)
|
67 |
-
except:
|
68 |
-
print("Skipped logging")
|
69 |
-
|
70 |
user_id = create_user_id(10)
|
71 |
|
72 |
|
@@ -96,24 +86,34 @@ def chat(
|
|
96 |
else:
|
97 |
raise Exception("report_type arg should be in (All available, IPCC only)")
|
98 |
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
messages = history + [{"role": "user", "content": query}]
|
102 |
sources = "\n\n".join(
|
103 |
-
f"
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
106 |
)
|
107 |
|
108 |
if sources:
|
109 |
-
messages.append(
|
110 |
-
{"role": "system", "content": f"{os.environ['sources']}\n\n{sources}"}
|
111 |
-
)
|
112 |
|
113 |
response = openai.Completion.create(
|
114 |
engine="climateGPT",
|
115 |
prompt=to_completion(messages),
|
116 |
-
temperature=0
|
117 |
stream=True,
|
118 |
max_tokens=1024,
|
119 |
)
|
@@ -133,33 +133,23 @@ def chat(
|
|
133 |
"answer": messages[-1]["content"],
|
134 |
"time": timestamp,
|
135 |
}
|
136 |
-
|
137 |
-
log_on_azure(file, logs, share_client)
|
138 |
-
except:
|
139 |
-
pass
|
140 |
-
|
141 |
|
142 |
for chunk in response:
|
143 |
-
if (
|
144 |
-
chunk_message := chunk["choices"][0].get("text")
|
145 |
-
) and chunk_message != "<|im_end|>":
|
146 |
complete_response += chunk_message
|
147 |
messages[-1]["content"] = complete_response
|
148 |
gradio_format = make_pairs([a["content"] for a in messages[1:]])
|
149 |
yield gradio_format, messages, sources
|
150 |
|
151 |
-
|
152 |
else:
|
153 |
sources = "⚠️ No relevant passages found in the climate science reports"
|
154 |
complete_response = "**⚠️ No relevant passages found in the climate science reports, you may want to ask a more specific question (specifying your question on climate issues).**"
|
155 |
-
|
156 |
messages.append({"role": "assistant", "content": complete_response})
|
157 |
-
|
158 |
gradio_format = make_pairs([a["content"] for a in messages[1:]])
|
159 |
yield gradio_format, messages, sources
|
160 |
|
161 |
|
162 |
-
|
163 |
def save_feedback(feed: str, user_id):
|
164 |
if len(feed) > 1:
|
165 |
timestamp = str(datetime.now().timestamp())
|
@@ -182,15 +172,12 @@ def log_on_azure(file, logs, share_client):
|
|
182 |
file_client.upload_file(str(logs))
|
183 |
|
184 |
|
185 |
-
with gr.Blocks(title="🌍 Climate Q&A", css="style.css",theme
|
186 |
-
|
187 |
user_id_state = gr.State([user_id])
|
188 |
|
189 |
# Gradio
|
190 |
gr.Markdown("<h1><center>Climate Q&A 🌍</center></h1>")
|
191 |
-
gr.Markdown(
|
192 |
-
"<h4><center>Ask climate-related questions to the IPCC reports</center></h4>"
|
193 |
-
)
|
194 |
with gr.Row():
|
195 |
with gr.Column(scale=1):
|
196 |
gr.Markdown(
|
@@ -215,13 +202,11 @@ Version 0.2-beta - This tool is under active development
|
|
215 |
|
216 |
with gr.Column(scale=1):
|
217 |
gr.Markdown("![](https://i.postimg.cc/fLvsvMzM/Untitled-design-5.png)")
|
218 |
-
gr.Markdown(
|
219 |
-
"*Source : IPCC AR6 - Synthesis Report of the IPCC 6th assessment report (AR6)*"
|
220 |
-
)
|
221 |
|
222 |
with gr.Row():
|
223 |
with gr.Column(scale=2):
|
224 |
-
chatbot = gr.Chatbot(elem_id="chatbot",label
|
225 |
state = gr.State([system_template])
|
226 |
|
227 |
with gr.Row():
|
@@ -229,7 +214,7 @@ Version 0.2-beta - This tool is under active development
|
|
229 |
show_label=False,
|
230 |
placeholder="Ask here your climate-related question and press enter",
|
231 |
).style(container=False)
|
232 |
-
ask_examples_hidden = gr.Textbox(elem_id
|
233 |
|
234 |
examples_questions = gr.Examples(
|
235 |
[
|
@@ -266,14 +251,13 @@ Version 0.2-beta - This tool is under active development
|
|
266 |
"Is climate change really happening or is it just a natural fluctuation in Earth's temperature?",
|
267 |
"Is the scientific consensus on climate change really as strong as it is claimed to be?",
|
268 |
],
|
269 |
-
[ask_examples_hidden],
|
|
|
270 |
)
|
271 |
|
272 |
with gr.Column(scale=1, variant="panel"):
|
273 |
gr.Markdown("### Sources")
|
274 |
-
sources_textbox = gr.Textbox(
|
275 |
-
interactive=False, show_label=False, max_lines=50
|
276 |
-
)
|
277 |
# reports_select = gr.Dropdown(
|
278 |
# ["IPCC only", "All available"],
|
279 |
# default="All available",
|
@@ -304,7 +288,7 @@ Version 0.2-beta - This tool is under active development
|
|
304 |
],
|
305 |
outputs=[chatbot, state, sources_textbox],
|
306 |
)
|
307 |
-
|
308 |
gr.Markdown("## How to use ClimateQ&A")
|
309 |
with gr.Row():
|
310 |
with gr.Column(scale=1):
|
@@ -319,7 +303,6 @@ Version 0.2-beta - This tool is under active development
|
|
319 |
"""
|
320 |
)
|
321 |
with gr.Column(scale=1):
|
322 |
-
|
323 |
gr.Markdown(
|
324 |
"""
|
325 |
### ⚠️ Limitations
|
|
|
13 |
from datetime import datetime
|
14 |
from azure.storage.fileshare import ShareServiceClient
|
15 |
|
16 |
+
# from dotenv import load_dotenv
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# load_dotenv()
|
20 |
|
21 |
theme = gr.themes.Soft(
|
22 |
primary_hue="sky",
|
23 |
+
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
|
24 |
)
|
25 |
|
|
|
26 |
system_template = {"role": "system", "content": os.environ["content"]}
|
27 |
|
28 |
openai.api_type = "azure"
|
|
|
53 |
"account_name": os.environ["account_name"],
|
54 |
}
|
55 |
|
56 |
+
account_url = os.environ["account_url"]
|
57 |
+
file_share_name = "climategpt"
|
58 |
+
service = ShareServiceClient(account_url=account_url, credential=credential)
|
59 |
+
share_client = service.get_share_client(file_share_name)
|
|
|
|
|
|
|
|
|
60 |
user_id = create_user_id(10)
|
61 |
|
62 |
|
|
|
86 |
else:
|
87 |
raise Exception("report_type arg should be in (All available, IPCC only)")
|
88 |
|
89 |
+
reformulated_query = openai.Completion.create(
|
90 |
+
engine="climateGPT",
|
91 |
+
prompt=f"Reformulate the following user message to be a short standalone question in English, in the context of an educationnal discussion about climate change.\n---\nquery: La technologie nous sauvera-t-elle ?\nstandalone question: Can technology help humanity mitigate the effects of climate change?\n---\nquery: what are our reserves in fossil fuel?\nstandalone question: What are the current reserves of fossil fuels and how long will they last?\n---\nquery: {query}\nstandalone question: ",
|
92 |
+
temperature=0,
|
93 |
+
max_tokens=128,
|
94 |
+
stop=["\n---\n", "<|im_end|>"],
|
95 |
+
)
|
96 |
+
reformulated_query = reformulated_query["choices"][0]["text"]
|
97 |
+
|
98 |
+
docs = retriever.retrieve(query=reformulated_query, top_k=10)
|
99 |
|
100 |
messages = history + [{"role": "user", "content": query}]
|
101 |
sources = "\n\n".join(
|
102 |
+
[f"query used for retrieval:\n{reformulated_query}"]
|
103 |
+
+ [
|
104 |
+
f"doc {i}: {d.meta['file_name']} page {d.meta['page_number']}\n{d.content}"
|
105 |
+
for i, d in enumerate(docs, 1)
|
106 |
+
if d.score > threshold
|
107 |
+
]
|
108 |
)
|
109 |
|
110 |
if sources:
|
111 |
+
messages.append({"role": "system", "content": f"{os.environ['sources']}\n\n{sources}"})
|
|
|
|
|
112 |
|
113 |
response = openai.Completion.create(
|
114 |
engine="climateGPT",
|
115 |
prompt=to_completion(messages),
|
116 |
+
temperature=0, # deterministic
|
117 |
stream=True,
|
118 |
max_tokens=1024,
|
119 |
)
|
|
|
133 |
"answer": messages[-1]["content"],
|
134 |
"time": timestamp,
|
135 |
}
|
136 |
+
log_on_azure(file, logs, share_client)
|
|
|
|
|
|
|
|
|
137 |
|
138 |
for chunk in response:
|
139 |
+
if (chunk_message := chunk["choices"][0].get("text")) and chunk_message != "<|im_end|>":
|
|
|
|
|
140 |
complete_response += chunk_message
|
141 |
messages[-1]["content"] = complete_response
|
142 |
gradio_format = make_pairs([a["content"] for a in messages[1:]])
|
143 |
yield gradio_format, messages, sources
|
144 |
|
|
|
145 |
else:
|
146 |
sources = "⚠️ No relevant passages found in the climate science reports"
|
147 |
complete_response = "**⚠️ No relevant passages found in the climate science reports, you may want to ask a more specific question (specifying your question on climate issues).**"
|
|
|
148 |
messages.append({"role": "assistant", "content": complete_response})
|
|
|
149 |
gradio_format = make_pairs([a["content"] for a in messages[1:]])
|
150 |
yield gradio_format, messages, sources
|
151 |
|
152 |
|
|
|
153 |
def save_feedback(feed: str, user_id):
|
154 |
if len(feed) > 1:
|
155 |
timestamp = str(datetime.now().timestamp())
|
|
|
172 |
file_client.upload_file(str(logs))
|
173 |
|
174 |
|
175 |
+
with gr.Blocks(title="🌍 Climate Q&A", css="style.css", theme=theme) as demo:
|
|
|
176 |
user_id_state = gr.State([user_id])
|
177 |
|
178 |
# Gradio
|
179 |
gr.Markdown("<h1><center>Climate Q&A 🌍</center></h1>")
|
180 |
+
gr.Markdown("<h4><center>Ask climate-related questions to the IPCC reports</center></h4>")
|
|
|
|
|
181 |
with gr.Row():
|
182 |
with gr.Column(scale=1):
|
183 |
gr.Markdown(
|
|
|
202 |
|
203 |
with gr.Column(scale=1):
|
204 |
gr.Markdown("![](https://i.postimg.cc/fLvsvMzM/Untitled-design-5.png)")
|
205 |
+
gr.Markdown("*Source : IPCC AR6 - Synthesis Report of the IPCC 6th assessment report (AR6)*")
|
|
|
|
|
206 |
|
207 |
with gr.Row():
|
208 |
with gr.Column(scale=2):
|
209 |
+
chatbot = gr.Chatbot(elem_id="chatbot", label="ClimateQ&A chatbot")
|
210 |
state = gr.State([system_template])
|
211 |
|
212 |
with gr.Row():
|
|
|
214 |
show_label=False,
|
215 |
placeholder="Ask here your climate-related question and press enter",
|
216 |
).style(container=False)
|
217 |
+
ask_examples_hidden = gr.Textbox(elem_id="hidden-message")
|
218 |
|
219 |
examples_questions = gr.Examples(
|
220 |
[
|
|
|
251 |
"Is climate change really happening or is it just a natural fluctuation in Earth's temperature?",
|
252 |
"Is the scientific consensus on climate change really as strong as it is claimed to be?",
|
253 |
],
|
254 |
+
[ask_examples_hidden],
|
255 |
+
examples_per_page=15,
|
256 |
)
|
257 |
|
258 |
with gr.Column(scale=1, variant="panel"):
|
259 |
gr.Markdown("### Sources")
|
260 |
+
sources_textbox = gr.Textbox(interactive=False, show_label=False, max_lines=50)
|
|
|
|
|
261 |
# reports_select = gr.Dropdown(
|
262 |
# ["IPCC only", "All available"],
|
263 |
# default="All available",
|
|
|
288 |
],
|
289 |
outputs=[chatbot, state, sources_textbox],
|
290 |
)
|
291 |
+
|
292 |
gr.Markdown("## How to use ClimateQ&A")
|
293 |
with gr.Row():
|
294 |
with gr.Column(scale=1):
|
|
|
303 |
"""
|
304 |
)
|
305 |
with gr.Column(scale=1):
|
|
|
306 |
gr.Markdown(
|
307 |
"""
|
308 |
### ⚠️ Limitations
|