Update app.py
Browse files
app.py
CHANGED
|
@@ -64,6 +64,8 @@ file_share_name = "climateqa"
|
|
| 64 |
service = ShareServiceClient(account_url=account_url, credential=credential)
|
| 65 |
share_client = service.get_share_client(file_share_name)
|
| 66 |
|
|
|
|
|
|
|
| 67 |
user_id = create_user_id()
|
| 68 |
|
| 69 |
|
|
@@ -213,74 +215,87 @@ async def chat(query,history,audience,sources,reports):
|
|
| 213 |
# print(f"Error in fallback iterator: {e}")
|
| 214 |
# raise gr.Error(f"ClimateQ&A Error: {e}\nThe error has been noted, try another question and if the error remains, you can contact us :)")
|
| 215 |
|
| 216 |
-
|
| 217 |
-
|
| 218 |
|
| 219 |
-
op = op.ops[0]
|
| 220 |
-
# print("ITERATION",op)
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
output_query = op["value"]["question"]
|
| 225 |
-
|
| 226 |
-
elif op['path'] == retriever_path_id: # documents
|
| 227 |
-
try:
|
| 228 |
-
docs = op['value']['documents'] # List[Document]
|
| 229 |
-
docs_html = []
|
| 230 |
-
for i, d in enumerate(docs, 1):
|
| 231 |
-
docs_html.append(make_html_source(d, i))
|
| 232 |
-
docs_html = "".join(docs_html)
|
| 233 |
-
except TypeError:
|
| 234 |
-
print("No documents found")
|
| 235 |
-
print("op: ",op)
|
| 236 |
-
continue
|
| 237 |
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
# elif op['path'] == final_output_path_id:
|
| 246 |
-
# final_output = op['value']
|
| 247 |
-
|
| 248 |
-
# if "answer" in final_output:
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
"prompt": prompt,
|
| 271 |
-
"query": prompt,
|
| 272 |
-
"question":output_query,
|
| 273 |
-
"docs":serialize_docs(docs),
|
| 274 |
-
"answer": history[-1][1],
|
| 275 |
-
"time": timestamp,
|
| 276 |
-
}
|
| 277 |
-
log_on_azure(file, logs, share_client)
|
| 278 |
|
|
|
|
|
|
|
| 279 |
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
yield history,docs_html,output_query,output_language,gallery
|
| 286 |
|
|
|
|
| 64 |
service = ShareServiceClient(account_url=account_url, credential=credential)
|
| 65 |
share_client = service.get_share_client(file_share_name)
|
| 66 |
|
| 67 |
+
print("YO",account_url,credential)
|
| 68 |
+
|
| 69 |
user_id = create_user_id()
|
| 70 |
|
| 71 |
|
|
|
|
| 215 |
# print(f"Error in fallback iterator: {e}")
|
| 216 |
# raise gr.Error(f"ClimateQ&A Error: {e}\nThe error has been noted, try another question and if the error remains, you can contact us :)")
|
| 217 |
|
| 218 |
+
try:
|
| 219 |
+
async for op in result:
|
| 220 |
|
|
|
|
|
|
|
| 221 |
|
| 222 |
+
op = op.ops[0]
|
| 223 |
+
# print("ITERATION",op)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
+
if op['path'] == reformulated_question_path_id: # reforulated question
|
| 226 |
+
try:
|
| 227 |
+
output_language = op['value']["language"] # str
|
| 228 |
+
output_query = op["value"]["question"]
|
| 229 |
+
except Exception as e:
|
| 230 |
+
raise gr.Error(f"ClimateQ&A Error: {e}\nThe error has been noted, try another question and if the error remains, you can contact us :)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
+
elif op['path'] == retriever_path_id: # documents
|
| 233 |
+
try:
|
| 234 |
+
docs = op['value']['documents'] # List[Document]
|
| 235 |
+
docs_html = []
|
| 236 |
+
for i, d in enumerate(docs, 1):
|
| 237 |
+
docs_html.append(make_html_source(d, i))
|
| 238 |
+
docs_html = "".join(docs_html)
|
| 239 |
+
except TypeError:
|
| 240 |
+
print("No documents found")
|
| 241 |
+
print("op: ",op)
|
| 242 |
+
continue
|
| 243 |
+
|
| 244 |
+
elif op['path'] == streaming_output_path_id: # final answer
|
| 245 |
+
new_token = op['value'] # str
|
| 246 |
+
time.sleep(0.01)
|
| 247 |
+
answer_yet = history[-1][1] + new_token
|
| 248 |
+
answer_yet = parse_output_llm_with_sources(answer_yet)
|
| 249 |
+
history[-1] = (query,answer_yet)
|
| 250 |
|
| 251 |
+
|
| 252 |
+
# elif op['path'] == final_output_path_id:
|
| 253 |
+
# final_output = op['value']
|
| 254 |
|
| 255 |
+
# if "answer" in final_output:
|
| 256 |
+
|
| 257 |
+
# final_output = final_output["answer"]
|
| 258 |
+
# print(final_output)
|
| 259 |
+
# answer = history[-1][1] + final_output
|
| 260 |
+
# answer = parse_output_llm_with_sources(answer)
|
| 261 |
+
# history[-1] = (query,answer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
+
else:
|
| 264 |
+
continue
|
| 265 |
|
| 266 |
+
history = [tuple(x) for x in history]
|
| 267 |
+
yield history,docs_html,output_query,output_language,gallery
|
| 268 |
+
|
| 269 |
+
except Exception as e:
|
| 270 |
+
print(f"Error in fallback iterator: {e}")
|
| 271 |
+
raise gr.Error(f"ClimateQ&A Error: {e}\nThe error has been noted, try another question and if the error remains, you can contact us :)")
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
try:
|
| 275 |
+
# Log answer on Azure Blob Storage
|
| 276 |
+
if os.getenv("GRADIO_ENV") != "local":
|
| 277 |
+
timestamp = str(datetime.now().timestamp())
|
| 278 |
+
file = timestamp + ".json"
|
| 279 |
+
prompt = history[-1][0]
|
| 280 |
+
logs = {
|
| 281 |
+
"user_id": str(user_id),
|
| 282 |
+
"prompt": prompt,
|
| 283 |
+
"query": prompt,
|
| 284 |
+
"question":output_query,
|
| 285 |
+
"docs":serialize_docs(docs),
|
| 286 |
+
"answer": history[-1][1],
|
| 287 |
+
"time": timestamp,
|
| 288 |
+
}
|
| 289 |
+
log_on_azure(file, logs, share_client)
|
| 290 |
+
except Exception as e:
|
| 291 |
+
print(f"Error logging on Azure Blob Storage: {e}")
|
| 292 |
+
raise gr.Error(f"ClimateQ&A Error: {str(e)[:100]}\nThe error has been noted, try another question and if the error remains, you can contact us :)")
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
# gallery = [x.metadata["image_path"] for x in docs if (len(x.metadata["image_path"]) > 0 and "IAS" in x.metadata["image_path"])]
|
| 296 |
+
# if len(gallery) > 0:
|
| 297 |
+
# gallery = list(set("|".join(gallery).split("|")))
|
| 298 |
+
# gallery = [get_image_from_azure_blob_storage(x) for x in gallery]
|
| 299 |
|
| 300 |
yield history,docs_html,output_query,output_language,gallery
|
| 301 |
|