SVashishta1 commited on
Commit
80bf7c1
·
1 Parent(s): e06f3ce
Files changed (1) hide show
  1. app.py +56 -7
app.py CHANGED
@@ -10,6 +10,7 @@ from langchain_groq import ChatGroq
10
  import plotly.express as px
11
  import time
12
  import plotly.io as pio
 
13
 
14
  # Load environment variables
15
  load_dotenv()
@@ -212,25 +213,37 @@ def process_text_query(query, history):
212
  x_col = result_df.columns[0]
213
  y_cols = numeric_cols[:3] # Use up to 3 numeric columns
214
 
 
 
215
  # Create appropriate plot based on data characteristics
216
- if 'month' in result_df.columns or 'date' in result_df.columns or 'year' in result_df.columns:
217
  # Time series data - use line chart
218
  fig = px.line(result_df, x=x_col, y=y_cols, title="Time Series Analysis")
219
  else:
220
  # Regular data - use bar chart
221
  fig = px.bar(result_df, x=x_col, y=y_cols[0], title="Data Visualization")
222
 
 
 
 
 
 
 
 
 
223
  # Store the figure for display
224
  current_plot = fig
 
225
 
226
  # Add note about visualization
227
  response += "\n\n**A visualization has been generated and is displayed below.**"
228
-
229
- # After creating the plot
230
- print(f"Plot created: {current_plot is not None}")
231
-
232
  except Exception as viz_error:
233
  print(f"Visualization error: {str(viz_error)}")
 
234
  # Continue without visualization if there's an error
235
 
236
  except Exception as e:
@@ -417,6 +430,26 @@ def text_to_speech_output(text):
417
  # Since we don't have VoiceAssistant, return None
418
  return None
419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
420
  # Create Gradio interface
421
  with gr.Blocks(title="AI Document Analysis & Voice Assistant") as demo:
422
  gr.Markdown("# 🤖 AI Document Analysis & Voice Assistant")
@@ -425,8 +458,14 @@ with gr.Blocks(title="AI Document Analysis & Voice Assistant") as demo:
425
  with gr.Tab("Chat"):
426
  chatbot = gr.Chatbot(height=400, type="messages")
427
 
428
- # Add a plot component
429
- plot_output = gr.Plot(label="Data Visualization", visible=False)
 
 
 
 
 
 
430
 
431
  with gr.Row():
432
  with gr.Column(scale=8):
@@ -532,6 +571,16 @@ with gr.Blocks(title="AI Document Analysis & Voice Assistant") as demo:
532
  inputs=[api_key],
533
  outputs=[gr.Textbox(label="Status")]
534
  )
 
 
 
 
 
 
 
 
 
 
535
 
536
  # Launch the app
537
  if __name__ == "__main__":
 
10
  import plotly.express as px
11
  import time
12
  import plotly.io as pio
13
+ import traceback
14
 
15
  # Load environment variables
16
  load_dotenv()
 
213
  x_col = result_df.columns[0]
214
  y_cols = numeric_cols[:3] # Use up to 3 numeric columns
215
 
216
+ print(f"Creating plot with x={x_col}, y={y_cols}")
217
+
218
  # Create appropriate plot based on data characteristics
219
+ if 'month' in result_df.columns or 'date' in result_df.columns or 'year' in result_df.columns or any('date' in col.lower() for col in result_df.columns):
220
  # Time series data - use line chart
221
  fig = px.line(result_df, x=x_col, y=y_cols, title="Time Series Analysis")
222
  else:
223
  # Regular data - use bar chart
224
  fig = px.bar(result_df, x=x_col, y=y_cols[0], title="Data Visualization")
225
 
226
+ # Ensure the figure is properly configured
227
+ fig.update_layout(
228
+ autosize=True,
229
+ width=800,
230
+ height=500,
231
+ margin=dict(l=50, r=50, b=100, t=100, pad=4)
232
+ )
233
+
234
  # Store the figure for display
235
  current_plot = fig
236
+ print(f"Plot created successfully: {current_plot is not None}")
237
 
238
  # Add note about visualization
239
  response += "\n\n**A visualization has been generated and is displayed below.**"
240
+ else:
241
+ print("Not enough numeric columns or data points for visualization")
242
+ else:
243
+ print("Not enough columns for visualization")
244
  except Exception as viz_error:
245
  print(f"Visualization error: {str(viz_error)}")
246
+ traceback.print_exc() # Add this to get a full traceback
247
  # Continue without visualization if there's an error
248
 
249
  except Exception as e:
 
430
  # Since we don't have VoiceAssistant, return None
431
  return None
432
 
433
+ def create_test_visualization():
434
+ """Create a test visualization to verify plotting works"""
435
+ # Create sample data
436
+ data = pd.DataFrame({
437
+ 'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
438
+ 'Value': [10, 15, 13, 17, 20, 25]
439
+ })
440
+
441
+ # Create a simple bar chart
442
+ fig = px.bar(data, x='Month', y='Value', title='Test Visualization')
443
+
444
+ # Configure the figure
445
+ fig.update_layout(
446
+ autosize=True,
447
+ width=800,
448
+ height=500
449
+ )
450
+
451
+ return fig
452
+
453
  # Create Gradio interface
454
  with gr.Blocks(title="AI Document Analysis & Voice Assistant") as demo:
455
  gr.Markdown("# 🤖 AI Document Analysis & Voice Assistant")
 
458
  with gr.Tab("Chat"):
459
  chatbot = gr.Chatbot(height=400, type="messages")
460
 
461
+ # Add a plot component with explicit sizing
462
+ plot_output = gr.Plot(
463
+ label="Data Visualization",
464
+ visible=False,
465
+ elem_id="visualization_plot", # Add an element ID for easier debugging
466
+ container=True, # Ensure it has a container
467
+ min_width=700 # Set minimum width
468
+ )
469
 
470
  with gr.Row():
471
  with gr.Column(scale=8):
 
571
  inputs=[api_key],
572
  outputs=[gr.Textbox(label="Status")]
573
  )
574
+
575
+ gr.Markdown("## Debugging")
576
+ test_viz_btn = gr.Button("Test Visualization")
577
+ test_viz_output = gr.Plot(label="Test Visualization")
578
+
579
+ test_viz_btn.click(
580
+ create_test_visualization,
581
+ inputs=[],
582
+ outputs=[test_viz_output]
583
+ )
584
 
585
  # Launch the app
586
  if __name__ == "__main__":