dolphinium commited on
Commit
c3741ac
Β·
1 Parent(s): bdcb123

add solr data display to analysis flow and update yield statements for consistency

Browse files
Files changed (1) hide show
  1. app.py +22 -16
app.py CHANGED
@@ -423,47 +423,50 @@ def process_analysis_flow(user_input, history, state):
423
  state = {'query_count': 0, 'last_suggestions': []}
424
 
425
  # Reset UI components for the new analysis
426
- yield (history, state, gr.update(value=None, visible=False), gr.update(value=None, visible=False), gr.update(value=None, visible=False))
427
 
428
  query_context = user_input.strip()
429
  if not query_context:
430
  history.append((user_input, "Please enter a question to analyze."))
431
- yield (history, state, None, None, None)
432
  return
433
 
434
  # 1. Acknowledge and start the process
435
  history.append((user_input, f"Analyzing: '{query_context}'\n\n*Generating Solr query...*"))
436
- yield (history, state, None, None, None)
437
 
438
  # 2. Generate Solr Query
439
  llm_solr_obj = llm_generate_solr_query(query_context, field_metadata)
440
  if not llm_solr_obj or 'query' not in llm_solr_obj or 'json.facet' not in llm_solr_obj:
441
  history.append((None, "I'm sorry, I couldn't generate a valid Solr query for that request. Please try rephrasing your question."))
442
- yield (history, state, None, None, None)
443
  return
444
 
445
  solr_q, solr_facet = llm_solr_obj.get('query'), llm_solr_obj.get('json.facet')
446
  history.append((None, "βœ… Solr query generated!"))
447
  formatted_query = f"**Query:**\n```\n{solr_q}\n```\n\n**Facet JSON:**\n```json\n{json.dumps(solr_facet, indent=2)}\n```"
448
- yield (history, state, None, None, gr.update(value=formatted_query, visible=True))
449
 
450
  # 3. Execute Query
451
  try:
452
  history.append((None, "*Executing query against the database...*"))
453
- yield (history, state, None, None, gr.update(value=formatted_query, visible=True))
454
 
455
  search_params = {"rows": 0, "json.facet": json.dumps(solr_facet)}
456
  results = solr_client.search(q=solr_q, **search_params)
457
  facet_data = results.raw_response.get("facets", {})
 
 
 
458
 
459
  if not facet_data or facet_data.get('count', 0) == 0:
460
  history.append((None, "No data was found for your query. Please try a different question."))
461
- yield (history, state, None, None, gr.update(value=formatted_query, visible=True))
462
  return
463
 
464
  # 4. Generate Visualization
465
  history.append((None, "βœ… Data retrieved. Generating visualization..."))
466
- yield (history, state, None, None, gr.update(value=formatted_query, visible=True))
467
 
468
  viz_code = llm_generate_visualization_code(query_context, facet_data)
469
  plot_path = execute_viz_code_and_get_path(viz_code, facet_data)
@@ -471,17 +474,17 @@ def process_analysis_flow(user_input, history, state):
471
  output_plot = gr.update(value=plot_path, visible=True) if plot_path else gr.update(visible=False)
472
  if not plot_path:
473
  history.append((None, "*I was unable to generate a plot for this data.*\n"))
474
- yield (history, state, output_plot, None, gr.update(value=formatted_query, visible=True))
475
 
476
  # 5. Generate and Stream Report
477
  history.append((None, "βœ… Plot created. Streaming final report..."))
478
  output_report = gr.update(value="", visible=True) # Make it visible before streaming
479
- yield (history, state, output_plot, output_report, gr.update(value=formatted_query, visible=True))
480
 
481
  report_text = ""
482
  for chunk in llm_generate_summary_and_suggestions_stream(query_context, facet_data):
483
  report_text += chunk
484
- yield (history, state, output_plot, report_text, gr.update(value=formatted_query, visible=True))
485
 
486
  # 6. Finalize and prompt for next action
487
  state['query_count'] += 1
@@ -489,13 +492,13 @@ def process_analysis_flow(user_input, history, state):
489
 
490
  next_prompt = "Analysis complete. What would you like to explore next? You can ask a follow-up question, pick a suggestion, or ask something new."
491
  history.append((None, next_prompt))
492
- yield (history, state, output_plot, report_text, gr.update(value=formatted_query, visible=True))
493
 
494
  except Exception as e:
495
  error_message = f"An unexpected error occurred during analysis: {e}"
496
  history.append((None, error_message))
497
  print(f"Error during analysis execution: {e}")
498
- yield (history, state, None, None, gr.update(value=formatted_query, visible=True))
499
 
500
  with gr.Blocks(theme=gr.themes.Soft(), css="footer {display: none !important}") as demo:
501
  state = gr.State()
@@ -517,6 +520,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css="footer {display: none !important}")
517
  with gr.Column(scale=2):
518
  with gr.Accordion("Generated Solr Query", open=False):
519
  solr_query_display = gr.Markdown("Query will appear here...", visible=True)
 
 
520
  plot_display = gr.Image(label="Visualization", type="filepath", visible=False)
521
  report_display = gr.Markdown("Report will be streamed here...", visible=False)
522
 
@@ -527,7 +532,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css="footer {display: none !important}")
527
  msg_textbox.submit(
528
  fn=process_analysis_flow,
529
  inputs=[msg_textbox, chatbot, state],
530
- outputs=[chatbot, state, plot_display, report_display, solr_query_display]
531
  )
532
 
533
  def reset_all():
@@ -538,10 +543,11 @@ with gr.Blocks(theme=gr.themes.Soft(), css="footer {display: none !important}")
538
  "", # msg_textbox
539
  gr.update(value=None, visible=False), # plot_display
540
  gr.update(value=None, visible=False), # report_display
541
- gr.update(value=None, visible=False) # solr_query_display
 
542
  )
543
 
544
- clear_button.click(fn=reset_all, inputs=None, outputs=[chatbot, state, msg_textbox, plot_display, report_display, solr_query_display], queue=False)
545
 
546
  if is_initialized:
547
  demo.queue().launch(debug=True, share=True)
 
423
  state = {'query_count': 0, 'last_suggestions': []}
424
 
425
  # Reset UI components for the new analysis
426
+ yield (history, state, gr.update(value=None, visible=False), gr.update(value=None, visible=False), gr.update(value=None, visible=False), gr.update(value=None, visible=False))
427
 
428
  query_context = user_input.strip()
429
  if not query_context:
430
  history.append((user_input, "Please enter a question to analyze."))
431
+ yield (history, state, None, None, None, None)
432
  return
433
 
434
  # 1. Acknowledge and start the process
435
  history.append((user_input, f"Analyzing: '{query_context}'\n\n*Generating Solr query...*"))
436
+ yield (history, state, None, None, None, None)
437
 
438
  # 2. Generate Solr Query
439
  llm_solr_obj = llm_generate_solr_query(query_context, field_metadata)
440
  if not llm_solr_obj or 'query' not in llm_solr_obj or 'json.facet' not in llm_solr_obj:
441
  history.append((None, "I'm sorry, I couldn't generate a valid Solr query for that request. Please try rephrasing your question."))
442
+ yield (history, state, None, None, None, None)
443
  return
444
 
445
  solr_q, solr_facet = llm_solr_obj.get('query'), llm_solr_obj.get('json.facet')
446
  history.append((None, "βœ… Solr query generated!"))
447
  formatted_query = f"**Query:**\n```\n{solr_q}\n```\n\n**Facet JSON:**\n```json\n{json.dumps(solr_facet, indent=2)}\n```"
448
+ yield (history, state, None, None, gr.update(value=formatted_query, visible=True), None)
449
 
450
  # 3. Execute Query
451
  try:
452
  history.append((None, "*Executing query against the database...*"))
453
+ yield (history, state, None, None, gr.update(value=formatted_query, visible=True), None)
454
 
455
  search_params = {"rows": 0, "json.facet": json.dumps(solr_facet)}
456
  results = solr_client.search(q=solr_q, **search_params)
457
  facet_data = results.raw_response.get("facets", {})
458
+
459
+ formatted_data = f"**Facet Data:**\n```json\n{json.dumps(facet_data, indent=2)}\n```"
460
+
461
 
462
  if not facet_data or facet_data.get('count', 0) == 0:
463
  history.append((None, "No data was found for your query. Please try a different question."))
464
+ yield (history, state, None, None, gr.update(value=formatted_query, visible=True), gr.update(value=formatted_data, visible=True))
465
  return
466
 
467
  # 4. Generate Visualization
468
  history.append((None, "βœ… Data retrieved. Generating visualization..."))
469
+ yield (history, state, None, None, gr.update(value=formatted_query, visible=True), gr.update(value=formatted_data, visible=True))
470
 
471
  viz_code = llm_generate_visualization_code(query_context, facet_data)
472
  plot_path = execute_viz_code_and_get_path(viz_code, facet_data)
 
474
  output_plot = gr.update(value=plot_path, visible=True) if plot_path else gr.update(visible=False)
475
  if not plot_path:
476
  history.append((None, "*I was unable to generate a plot for this data.*\n"))
477
+ yield (history, state, output_plot, None, gr.update(value=formatted_query, visible=True), gr.update(value=formatted_data, visible=True))
478
 
479
  # 5. Generate and Stream Report
480
  history.append((None, "βœ… Plot created. Streaming final report..."))
481
  output_report = gr.update(value="", visible=True) # Make it visible before streaming
482
+ yield (history, state, output_plot, output_report, gr.update(value=formatted_query, visible=True), gr.update(value=formatted_data, visible=True))
483
 
484
  report_text = ""
485
  for chunk in llm_generate_summary_and_suggestions_stream(query_context, facet_data):
486
  report_text += chunk
487
+ yield (history, state, output_plot, report_text, gr.update(value=formatted_query, visible=True), gr.update(value=formatted_data, visible=True))
488
 
489
  # 6. Finalize and prompt for next action
490
  state['query_count'] += 1
 
492
 
493
  next_prompt = "Analysis complete. What would you like to explore next? You can ask a follow-up question, pick a suggestion, or ask something new."
494
  history.append((None, next_prompt))
495
+ yield (history, state, output_plot, report_text, gr.update(value=formatted_query, visible=True), gr.update(value=formatted_data, visible=True))
496
 
497
  except Exception as e:
498
  error_message = f"An unexpected error occurred during analysis: {e}"
499
  history.append((None, error_message))
500
  print(f"Error during analysis execution: {e}")
501
+ yield (history, state, None, None, gr.update(value=formatted_query, visible=True), None)
502
 
503
  with gr.Blocks(theme=gr.themes.Soft(), css="footer {display: none !important}") as demo:
504
  state = gr.State()
 
520
  with gr.Column(scale=2):
521
  with gr.Accordion("Generated Solr Query", open=False):
522
  solr_query_display = gr.Markdown("Query will appear here...", visible=True)
523
+ with gr.Accordion("Retrieved Solr Data", open=False):
524
+ solr_data_display = gr.Markdown("Data will appear here...", visible=False)
525
  plot_display = gr.Image(label="Visualization", type="filepath", visible=False)
526
  report_display = gr.Markdown("Report will be streamed here...", visible=False)
527
 
 
532
  msg_textbox.submit(
533
  fn=process_analysis_flow,
534
  inputs=[msg_textbox, chatbot, state],
535
+ outputs=[chatbot, state, plot_display, report_display, solr_query_display, solr_data_display]
536
  )
537
 
538
  def reset_all():
 
543
  "", # msg_textbox
544
  gr.update(value=None, visible=False), # plot_display
545
  gr.update(value=None, visible=False), # report_display
546
+ gr.update(value=None, visible=False), # solr_query_display
547
+ gr.update(value=None, visible=False) # solr_data_display
548
  )
549
 
550
+ clear_button.click(fn=reset_all, inputs=None, outputs=[chatbot, state, msg_textbox, plot_display, report_display, solr_query_display, solr_data_display], queue=False)
551
 
552
  if is_initialized:
553
  demo.queue().launch(debug=True, share=True)