SVashishta1 commited on
Commit
a535552
·
1 Parent(s): b8099ec

Fix: Update chat format and UI layout

Browse files
Files changed (1) hide show
  1. app.py +50 -70
app.py CHANGED
@@ -177,7 +177,7 @@ def process_text_query(query, history):
177
  return "", history
178
 
179
  # Add the user's query to history
180
- history.append({"role": "user", "content": query})
181
 
182
  start_time = time.time()
183
 
@@ -276,7 +276,7 @@ def process_text_query(query, history):
276
  viz_html = generate_visualization(result_df, query)
277
  if viz_html:
278
  # Add the visualization to history
279
- history.append({"role": "assistant", "content": viz_html})
280
  return viz_html, history
281
 
282
  # If no visualization or visualization failed, generate text response
@@ -288,23 +288,23 @@ def process_text_query(query, history):
288
  )).content
289
 
290
  # Add the response to history
291
- history.append({"role": "assistant", "content": response})
292
  return response, history
293
 
294
  except Exception as e:
295
  error_msg = f"Error processing query: {str(e)}"
296
- history.append({"role": "assistant", "content": error_msg})
297
  return error_msg, history
298
 
299
  else:
300
  # Handle non-CSV queries (document queries)
301
  try:
302
  response = document_assistant.process_query(query)
303
- history.append({"role": "assistant", "content": response})
304
  return response, history
305
  except Exception as e:
306
  error_msg = f"Error processing query: {str(e)}"
307
- history.append({"role": "assistant", "content": error_msg})
308
  return error_msg, history
309
 
310
  def process_file_upload(files):
@@ -418,9 +418,9 @@ def clear_context():
418
  "table_name": None
419
  }
420
 
421
- return [{"role": "assistant", "content": "Context cleared. You can now upload new documents or CSV files."}]
422
  except Exception as e:
423
- return [{"role": "assistant", "content": f"Error clearing context: {str(e)}"}]
424
 
425
  # I am making a function for voice input but we are not using it in this version(still in development phase)
426
  """
@@ -790,7 +790,7 @@ with gr.Blocks(title="LLM Powered Database Chatbot") as demo:
790
  # Add a global variable to store the current visualization
791
  current_visualization = gr.State(None)
792
 
793
- with gr.Tab("Chat"):
794
  # Use a custom CSS to ensure images are displayed properly
795
  gr.HTML("""
796
  <style>
@@ -800,62 +800,8 @@ with gr.Blocks(title="LLM Powered Database Chatbot") as demo:
800
  display: block;
801
  margin: 10px 0;
802
  }
803
- </style>
804
- """)
805
-
806
- chatbot = gr.Chatbot(height=500, elem_classes="chatbot-container")
807
-
808
- # Add information about capabilities
809
- gr.Markdown("""
810
- ### Capabilities:
811
- - **Data Analysis**: Ask questions about your data and get detailed responses
812
- - **Visualization**: Request and view graphs and charts of your data
813
- - **Multiple File Types**: Upload PDFs, TXT, DOCX, CSV, and XLSX files for analysis
814
- - **Natural Language Queries**: Ask questions in plain English about your documents
815
- """)
816
-
817
- with gr.Row():
818
- with gr.Column(scale=8):
819
- msg = gr.Textbox(
820
- placeholder="Ask a question about your documents...",
821
- show_label=False
822
- )
823
- with gr.Column(scale=1):
824
- pass
825
-
826
- with gr.Row():
827
- submit_btn = gr.Button("Submit")
828
- clear_btn = gr.Button("Clear")
829
- clear_context_btn = gr.Button("Clear Context")
830
-
831
- with gr.Tab("Visualizations"):
832
- gr.Markdown("## 📊 Data Visualizations")
833
-
834
- with gr.Row():
835
- with gr.Column(scale=3):
836
- visualization_output = gr.HTML(
837
- label="Current Visualization",
838
- elem_classes="visualization-container"
839
- )
840
- with gr.Column(scale=1):
841
- with gr.Group():
842
- clear_viz_btn = gr.Button("🗑️ Clear Visualization", variant="stop")
843
- save_viz_btn = gr.Button("💾 Save Visualization")
844
- save_status = gr.Textbox(label="Save Status", visible=False)
845
-
846
- gr.Markdown("""
847
- ### How to use:
848
- 1. Ask a question about your data in the Chat tab
849
- 2. If your question involves visualization, the plot will appear here
850
- 3. You can switch between Chat and Visualizations tabs to see both the conversation and the plots
851
- 4. Use the buttons above to clear or save the current visualization
852
- """)
853
-
854
- # Add custom CSS for better visualization display
855
- gr.HTML("""
856
- <style>
857
  .visualization-container {
858
- min-height: 600px;
859
  max-height: 800px;
860
  overflow: auto;
861
  padding: 20px;
@@ -871,6 +817,45 @@ with gr.Blocks(title="LLM Powered Database Chatbot") as demo:
871
  </style>
872
  """)
873
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
874
  def clear_visualization():
875
  return "", ""
876
 
@@ -918,19 +903,14 @@ with gr.Blocks(title="LLM Powered Database Chatbot") as demo:
918
  viz_html = response
919
  # Update the visualization state
920
  current_viz = viz_html
921
- # Remove the visualization from the chat response
922
- response = "I've created a visualization for your query. Please check the 'Visualizations' tab to see it."
923
 
924
- return response, new_history, current_viz
925
 
926
  # Update the button click handlers
927
  submit_btn.click(
928
  process_text_query_with_visualization,
929
  inputs=[msg, chatbot, current_visualization],
930
  outputs=[msg, chatbot, current_visualization]
931
- ).then(
932
- lambda: None, # Clear the input
933
- outputs=[msg]
934
  ).then(
935
  lambda viz: viz if viz else "", # Update visualization tab
936
  inputs=[current_visualization],
 
177
  return "", history
178
 
179
  # Add the user's query to history
180
+ history.append([query, None])
181
 
182
  start_time = time.time()
183
 
 
276
  viz_html = generate_visualization(result_df, query)
277
  if viz_html:
278
  # Add the visualization to history
279
+ history[-1][1] = viz_html
280
  return viz_html, history
281
 
282
  # If no visualization or visualization failed, generate text response
 
288
  )).content
289
 
290
  # Add the response to history
291
+ history[-1][1] = response
292
  return response, history
293
 
294
  except Exception as e:
295
  error_msg = f"Error processing query: {str(e)}"
296
+ history[-1][1] = error_msg
297
  return error_msg, history
298
 
299
  else:
300
  # Handle non-CSV queries (document queries)
301
  try:
302
  response = document_assistant.process_query(query)
303
+ history[-1][1] = response
304
  return response, history
305
  except Exception as e:
306
  error_msg = f"Error processing query: {str(e)}"
307
+ history[-1][1] = error_msg
308
  return error_msg, history
309
 
310
  def process_file_upload(files):
 
418
  "table_name": None
419
  }
420
 
421
+ return [["Context cleared. You can now upload new documents or CSV files.", None]]
422
  except Exception as e:
423
+ return [[f"Error clearing context: {str(e)}", None]]
424
 
425
  # I am making a function for voice input but we are not using it in this version(still in development phase)
426
  """
 
790
  # Add a global variable to store the current visualization
791
  current_visualization = gr.State(None)
792
 
793
+ with gr.Tab("Chat & Visualizations"):
794
  # Use a custom CSS to ensure images are displayed properly
795
  gr.HTML("""
796
  <style>
 
800
  display: block;
801
  margin: 10px 0;
802
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
803
  .visualization-container {
804
+ min-height: 500px;
805
  max-height: 800px;
806
  overflow: auto;
807
  padding: 20px;
 
817
  </style>
818
  """)
819
 
820
+ with gr.Row():
821
+ with gr.Column(scale=1):
822
+ chatbot = gr.Chatbot(height=500, elem_classes="chatbot-container")
823
+
824
+ with gr.Row():
825
+ with gr.Column(scale=8):
826
+ msg = gr.Textbox(
827
+ placeholder="Ask a question about your documents...",
828
+ show_label=False
829
+ )
830
+ with gr.Column(scale=1):
831
+ pass
832
+
833
+ with gr.Row():
834
+ submit_btn = gr.Button("Submit")
835
+ clear_btn = gr.Button("Clear")
836
+ clear_context_btn = gr.Button("Clear Context")
837
+
838
+ with gr.Column(scale=1):
839
+ visualization_output = gr.HTML(
840
+ label="Visualization",
841
+ elem_classes="visualization-container"
842
+ )
843
+
844
+ with gr.Row():
845
+ clear_viz_btn = gr.Button("🗑️ Clear Visualization")
846
+ save_viz_btn = gr.Button("💾 Save Visualization")
847
+
848
+ save_status = gr.Textbox(label="Save Status", visible=False)
849
+
850
+ # Add information about capabilities
851
+ gr.Markdown("""
852
+ ### Capabilities:
853
+ - **Data Analysis**: Ask questions about your data and get detailed responses
854
+ - **Visualization**: Request and view graphs and charts of your data
855
+ - **Multiple File Types**: Upload PDFs, TXT, DOCX, CSV, and XLSX files for analysis
856
+ - **Natural Language Queries**: Ask questions in plain English about your documents
857
+ """)
858
+
859
  def clear_visualization():
860
  return "", ""
861
 
 
903
  viz_html = response
904
  # Update the visualization state
905
  current_viz = viz_html
 
 
906
 
907
+ return "", new_history, current_viz
908
 
909
  # Update the button click handlers
910
  submit_btn.click(
911
  process_text_query_with_visualization,
912
  inputs=[msg, chatbot, current_visualization],
913
  outputs=[msg, chatbot, current_visualization]
 
 
 
914
  ).then(
915
  lambda viz: viz if viz else "", # Update visualization tab
916
  inputs=[current_visualization],