Display image in a parallel task
Browse files
app.py
CHANGED
|
@@ -177,10 +177,7 @@ async def chat(query,history,audience,sources,reports,current_graphs):
|
|
| 177 |
print(f"Error getting documents: {e}")
|
| 178 |
print(event)
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
used_figures, figures, gallery = process_figures(docs, figures, gallery, used_figures)
|
| 183 |
-
|
| 184 |
|
| 185 |
elif event["name"] in steps_display.keys() and event["event"] == "on_chain_start": #display steps
|
| 186 |
event_description,display_output = steps_display[node]
|
|
@@ -245,7 +242,7 @@ async def chat(query,history,audience,sources,reports,current_graphs):
|
|
| 245 |
if event["name"] == "categorize_intent" and event["event"] == "on_chain_start":
|
| 246 |
print("X")
|
| 247 |
|
| 248 |
-
yield history, docs_html, output_query, output_language, gallery, figures,
|
| 249 |
|
| 250 |
except Exception as e:
|
| 251 |
print(event, "has failed")
|
|
@@ -274,17 +271,19 @@ async def chat(query,history,audience,sources,reports,current_graphs):
|
|
| 274 |
raise gr.Error(f"ClimateQ&A Error: {str(e)[:100]} - The error has been noted, try another question and if the error remains, you can contact us :)")
|
| 275 |
|
| 276 |
|
| 277 |
-
|
| 278 |
-
# figures, gallery = process_figures(docs, figures, gallery)
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
-
yield history, docs_html, output_query, output_language, gallery, figures, graphs_html#,output_query,output_keywords
|
| 285 |
|
| 286 |
-
def process_figures(docs, figures, gallery, used_figures):
|
| 287 |
-
|
|
|
|
|
|
|
|
|
|
| 288 |
docs_figures = [d for d in docs if d.metadata["chunk_type"] == "image"]
|
| 289 |
for i, doc in enumerate(docs_figures):
|
| 290 |
if doc.metadata["chunk_type"] == "image":
|
|
@@ -315,7 +314,7 @@ def process_figures(docs, figures, gallery, used_figures):
|
|
| 315 |
except Exception as e:
|
| 316 |
print(f"Skipped adding image {i} because of {e}")
|
| 317 |
|
| 318 |
-
return
|
| 319 |
|
| 320 |
def save_feedback(feed: str, user_id):
|
| 321 |
if len(feed) > 1:
|
|
@@ -469,6 +468,8 @@ with gr.Blocks(title="Climate Q&A", css_paths=os.getcwd()+ "/style.css", theme=t
|
|
| 469 |
|
| 470 |
|
| 471 |
with gr.Tab("Figures",elem_id = "tab-figures",id = 3) as tab_figures:
|
|
|
|
|
|
|
| 472 |
with Modal(visible=False, elem_id="modal_figure_galery") as modal:
|
| 473 |
gallery_component = gr.Gallery(object_fit='scale-down',elem_id="gallery-component", height="80vh")
|
| 474 |
|
|
@@ -661,25 +662,21 @@ with gr.Blocks(title="Climate Q&A", css_paths=os.getcwd()+ "/style.css", theme=t
|
|
| 661 |
sources_notif_label = f"Sources ({sources_number})"
|
| 662 |
figures_notif_label = f"Figures ({figures_number})"
|
| 663 |
graphs_notif_label = f"Recommended content ({graphs_number})"
|
| 664 |
-
|
| 665 |
-
# figures_notif_label = f"Figures (π)"
|
| 666 |
-
# graphs_notif_label = f"Recommended content (π)"
|
| 667 |
-
|
| 668 |
return gr.update(label = sources_notif_label), gr.update(label = figures_notif_label), gr.update(label = graphs_notif_label)
|
| 669 |
|
| 670 |
(textbox
|
| 671 |
.submit(start_chat, [textbox,chatbot], [textbox,tabs,chatbot],queue = False,api_name = "start_chat_textbox")
|
| 672 |
-
.then(chat, [textbox,chatbot,dropdown_audience, dropdown_sources,dropdown_reports, current_graphs],
|
| 673 |
.then(finish_chat, None, [textbox],api_name = "finish_chat_textbox")
|
| 674 |
.then(update_sources_number_display, [sources_textbox, figures_cards, current_graphs],[tab_sources, tab_figures, tab_recommended_content] )
|
| 675 |
)
|
| 676 |
|
| 677 |
(examples_hidden
|
| 678 |
.change(start_chat, [examples_hidden,chatbot], [textbox,tabs,chatbot],queue = False,api_name = "start_chat_examples")
|
| 679 |
-
.then(chat, [examples_hidden,chatbot,dropdown_audience, dropdown_sources,dropdown_reports, current_graphs],
|
| 680 |
.then(finish_chat, None, [textbox],api_name = "finish_chat_examples")
|
| 681 |
.then(update_sources_number_display, [sources_textbox, figures_cards, current_graphs],[tab_sources, tab_figures, tab_recommended_content] )
|
| 682 |
-
|
| 683 |
)
|
| 684 |
|
| 685 |
|
|
@@ -690,6 +687,10 @@ with gr.Blocks(title="Climate Q&A", css_paths=os.getcwd()+ "/style.css", theme=t
|
|
| 690 |
return [gr.update(visible=visible_bools[i]) for i in range(len(samples))]
|
| 691 |
|
| 692 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 693 |
|
| 694 |
dropdown_samples.change(change_sample_questions,dropdown_samples,samples)
|
| 695 |
|
|
|
|
| 177 |
print(f"Error getting documents: {e}")
|
| 178 |
print(event)
|
| 179 |
|
| 180 |
+
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
elif event["name"] in steps_display.keys() and event["event"] == "on_chain_start": #display steps
|
| 183 |
event_description,display_output = steps_display[node]
|
|
|
|
| 242 |
if event["name"] == "categorize_intent" and event["event"] == "on_chain_start":
|
| 243 |
print("X")
|
| 244 |
|
| 245 |
+
yield history, docs_html, output_query, output_language, docs , graphs_html#gallery, figures, #,output_query,output_keywords
|
| 246 |
|
| 247 |
except Exception as e:
|
| 248 |
print(event, "has failed")
|
|
|
|
| 271 |
raise gr.Error(f"ClimateQ&A Error: {str(e)[:100]} - The error has been noted, try another question and if the error remains, you can contact us :)")
|
| 272 |
|
| 273 |
|
| 274 |
+
|
|
|
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
+
yield history, docs_html, output_query, output_language, docs, graphs_html # gallery, figures, graphs_html#,output_query,output_keywords
|
| 281 |
|
| 282 |
+
# def process_figures(docs, figures, gallery, used_figures =[]):
|
| 283 |
+
def process_figures(docs):
|
| 284 |
+
gallery=[]
|
| 285 |
+
used_figures =[]
|
| 286 |
+
figures = '<div class="figures-container"><p></p> </div>'
|
| 287 |
docs_figures = [d for d in docs if d.metadata["chunk_type"] == "image"]
|
| 288 |
for i, doc in enumerate(docs_figures):
|
| 289 |
if doc.metadata["chunk_type"] == "image":
|
|
|
|
| 314 |
except Exception as e:
|
| 315 |
print(f"Skipped adding image {i} because of {e}")
|
| 316 |
|
| 317 |
+
return figures, gallery
|
| 318 |
|
| 319 |
def save_feedback(feed: str, user_id):
|
| 320 |
if len(feed) > 1:
|
|
|
|
| 468 |
|
| 469 |
|
| 470 |
with gr.Tab("Figures",elem_id = "tab-figures",id = 3) as tab_figures:
|
| 471 |
+
sources_raw = gr.State()
|
| 472 |
+
|
| 473 |
with Modal(visible=False, elem_id="modal_figure_galery") as modal:
|
| 474 |
gallery_component = gr.Gallery(object_fit='scale-down',elem_id="gallery-component", height="80vh")
|
| 475 |
|
|
|
|
| 662 |
sources_notif_label = f"Sources ({sources_number})"
|
| 663 |
figures_notif_label = f"Figures ({figures_number})"
|
| 664 |
graphs_notif_label = f"Recommended content ({graphs_number})"
|
| 665 |
+
|
|
|
|
|
|
|
|
|
|
| 666 |
return gr.update(label = sources_notif_label), gr.update(label = figures_notif_label), gr.update(label = graphs_notif_label)
|
| 667 |
|
| 668 |
(textbox
|
| 669 |
.submit(start_chat, [textbox,chatbot], [textbox,tabs,chatbot],queue = False,api_name = "start_chat_textbox")
|
| 670 |
+
.then(chat, [textbox,chatbot,dropdown_audience, dropdown_sources,dropdown_reports, current_graphs] ,[chatbot,sources_textbox,output_query,output_language, sources_raw, current_graphs],concurrency_limit = 8,api_name = "chat_textbox")
|
| 671 |
.then(finish_chat, None, [textbox],api_name = "finish_chat_textbox")
|
| 672 |
.then(update_sources_number_display, [sources_textbox, figures_cards, current_graphs],[tab_sources, tab_figures, tab_recommended_content] )
|
| 673 |
)
|
| 674 |
|
| 675 |
(examples_hidden
|
| 676 |
.change(start_chat, [examples_hidden,chatbot], [textbox,tabs,chatbot],queue = False,api_name = "start_chat_examples")
|
| 677 |
+
.then(chat, [examples_hidden,chatbot,dropdown_audience, dropdown_sources,dropdown_reports, current_graphs] ,[chatbot,sources_textbox,output_query,output_language, sources_raw, current_graphs],concurrency_limit = 8,api_name = "chat_textbox")
|
| 678 |
.then(finish_chat, None, [textbox],api_name = "finish_chat_examples")
|
| 679 |
.then(update_sources_number_display, [sources_textbox, figures_cards, current_graphs],[tab_sources, tab_figures, tab_recommended_content] )
|
|
|
|
| 680 |
)
|
| 681 |
|
| 682 |
|
|
|
|
| 687 |
return [gr.update(visible=visible_bools[i]) for i in range(len(samples))]
|
| 688 |
|
| 689 |
|
| 690 |
+
sources_raw.change(process_figures, inputs=[sources_raw], outputs=[figures_cards, gallery_component])
|
| 691 |
+
sources_textbox.change(update_sources_number_display, [sources_textbox, figures_cards, current_graphs],[tab_sources, tab_figures, tab_recommended_content])
|
| 692 |
+
figures_cards.change(update_sources_number_display, [sources_textbox, figures_cards, current_graphs],[tab_sources, tab_figures, tab_recommended_content])
|
| 693 |
+
current_graphs.change(update_sources_number_display, [sources_textbox, figures_cards, current_graphs],[tab_sources, tab_figures, tab_recommended_content])
|
| 694 |
|
| 695 |
dropdown_samples.change(change_sample_questions,dropdown_samples,samples)
|
| 696 |
|