SVashishta1 commited on
Commit
a8c9793
·
1 Parent(s): 79d18e9

Error Fix

Browse files
Files changed (2) hide show
  1. app.py +75 -31
  2. requirements.txt +2 -1
app.py CHANGED
@@ -13,6 +13,9 @@ import plotly.io as pio
13
  import traceback
14
  import base64
15
  from io import BytesIO
 
 
 
16
 
17
  # Load environment variables
18
  load_dotenv()
@@ -129,11 +132,8 @@ interpret_prompt = ChatPromptTemplate.from_messages(
129
 
130
  def process_text_query(query, history):
131
  """Process a text query and update chat history"""
132
- global current_plot
133
- current_plot = None # Reset the plot
134
-
135
  if not query:
136
- return "", history, None
137
 
138
  # Add the user's query to history
139
  history.append({"role": "user", "content": query})
@@ -225,11 +225,13 @@ def process_text_query(query, history):
225
  # Regular data - use bar chart
226
  fig = px.bar(result_df, x=x_col, y=y_cols[0], title="Data Visualization")
227
 
228
- # Convert the figure to HTML and embed it directly in the response
229
- plot_html = pio.to_html(fig, full_html=False, include_plotlyjs='cdn')
 
 
230
 
231
- # Add the HTML directly to the response
232
- response += f"\n\n<div style='width:100%;height:400px;'>{plot_html}</div>"
233
 
234
  # Add note about visualization
235
  response += "\n\n**A visualization has been generated and is displayed above.**"
@@ -263,7 +265,7 @@ def process_text_query(query, history):
263
  # Add the response to history
264
  history.append({"role": "assistant", "content": response})
265
 
266
- return "", history, current_plot
267
 
268
  def process_file_upload(files):
269
  """Process uploaded files and index them"""
@@ -404,8 +406,25 @@ def process_voice_input(audio_path):
404
  if audio_path is None:
405
  return "No audio recorded"
406
 
407
- # Since we don't have VoiceAssistant, return a placeholder message
408
- return "Voice transcription is not available"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
409
 
410
  def text_to_speech_output(text):
411
  """Convert text to speech"""
@@ -422,8 +441,25 @@ def text_to_speech_output(text):
422
  if not last_message:
423
  return None
424
 
425
- # Since we don't have VoiceAssistant, return None
426
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
427
 
428
  def create_test_visualization():
429
  """Create a test visualization to verify plotting works"""
@@ -508,10 +544,19 @@ with gr.Blocks(title="AI Document Analysis & Voice Assistant") as demo:
508
  gr.Markdown("Upload documents, ask questions, and get voice responses!")
509
 
510
  with gr.Tab("Chat"):
511
- chatbot = gr.Chatbot(height=400, type="messages")
 
 
 
 
 
 
 
 
 
 
512
 
513
- # Add a plot component
514
- plot_output = gr.HTML(label="Data Visualization", visible=True, elem_id="plot_container")
515
 
516
  with gr.Row():
517
  with gr.Column(scale=8):
@@ -540,18 +585,16 @@ with gr.Blocks(title="AI Document Analysis & Voice Assistant") as demo:
540
  submit_btn.click(
541
  process_text_query,
542
  inputs=[msg, chatbot],
543
- outputs=[msg, chatbot, plot_output],
544
- postprocess=lambda _, __, fig: gr.update(value=fig, visible=fig is not None)
545
  )
546
 
547
  msg.submit(
548
  process_text_query,
549
  inputs=[msg, chatbot],
550
- outputs=[msg, chatbot, plot_output],
551
- postprocess=lambda _, __, fig: gr.update(value=fig, visible=fig is not None)
552
  )
553
 
554
- clear_btn.click(lambda: [None, None, gr.update(visible=False)], None, [chatbot, plot_output, plot_output], queue=False)
555
  clear_context_btn.click(clear_context, inputs=[], outputs=[chatbot])
556
 
557
  voice_btn.click(
@@ -575,16 +618,15 @@ with gr.Blocks(title="AI Document Analysis & Voice Assistant") as demo:
575
  )
576
 
577
  with gr.Tab("Document Upload"):
578
- file_upload = gr.File(
579
- label="Upload Documents",
580
- file_types=[".pdf", ".txt", ".docx", ".csv", ".xlsx"],
581
- file_count="multiple"
582
- )
583
-
584
  with gr.Row():
585
- upload_button = gr.Button("Process & Index Documents", scale=2)
586
- flush_db_btn_doc = gr.Button("🗑️ Flush All Databases", variant="stop", scale=1)
 
 
 
 
587
 
 
588
  upload_output = gr.Textbox(label="Upload Status")
589
 
590
  upload_button.click(
@@ -609,8 +651,10 @@ with gr.Blocks(title="AI Document Analysis & Voice Assistant") as demo:
609
  )
610
 
611
  with gr.Tab("Settings"):
612
- gr.Markdown("## Database Management")
613
- flush_db_btn = gr.Button("🗑️ Flush All Databases", variant="stop", scale=1, min_width=200)
 
 
614
  flush_result = gr.Textbox(label="Flush Result")
615
 
616
  flush_db_btn.click(
 
13
  import traceback
14
  import base64
15
  from io import BytesIO
16
+ import speech_recognition as sr
17
+ from gtts import gTTS
18
+ import re
19
 
20
  # Load environment variables
21
  load_dotenv()
 
132
 
133
  def process_text_query(query, history):
134
  """Process a text query and update chat history"""
 
 
 
135
  if not query:
136
+ return "", history
137
 
138
  # Add the user's query to history
139
  history.append({"role": "user", "content": query})
 
225
  # Regular data - use bar chart
226
  fig = px.bar(result_df, x=x_col, y=y_cols[0], title="Data Visualization")
227
 
228
+ # Convert the figure to an image and encode it as base64
229
+ img_bytes = fig.to_image(format="png", width=800, height=500)
230
+ encoded = base64.b64encode(img_bytes).decode("ascii")
231
+ img_src = f"data:image/png;base64,{encoded}"
232
 
233
+ # Add the image directly to the response
234
+ response += f"\n\n<img src='{img_src}' width='100%' />"
235
 
236
  # Add note about visualization
237
  response += "\n\n**A visualization has been generated and is displayed above.**"
 
265
  # Add the response to history
266
  history.append({"role": "assistant", "content": response})
267
 
268
+ return "", history
269
 
270
  def process_file_upload(files):
271
  """Process uploaded files and index them"""
 
406
  if audio_path is None:
407
  return "No audio recorded"
408
 
409
+ try:
410
+ # Initialize recognizer
411
+ r = sr.Recognizer()
412
+
413
+ # Load the audio file
414
+ with sr.AudioFile(audio_path) as source:
415
+ # Read the audio data
416
+ audio_data = r.record(source)
417
+
418
+ # Recognize speech using Google Speech Recognition
419
+ text = r.recognize_google(audio_data)
420
+
421
+ return text
422
+ except sr.UnknownValueError:
423
+ return "Could not understand audio"
424
+ except sr.RequestError as e:
425
+ return f"Error with speech recognition service: {e}"
426
+ except Exception as e:
427
+ return f"Error processing audio: {str(e)}"
428
 
429
  def text_to_speech_output(text):
430
  """Convert text to speech"""
 
441
  if not last_message:
442
  return None
443
 
444
+ try:
445
+ # Clean the text (remove markdown and HTML)
446
+ clean_text = re.sub(r'<.*?>', '', last_message) # Remove HTML tags
447
+ clean_text = re.sub(r'\*\*(.*?)\*\*', r'\1', clean_text) # Remove bold markdown
448
+ clean_text = re.sub(r'\n\n', ' ', clean_text) # Replace double newlines with space
449
+ clean_text = re.sub(r'```.*?```', 'Code block removed for speech.', clean_text, flags=re.DOTALL) # Replace code blocks
450
+
451
+ # Create a temporary file
452
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
453
+ temp_file.close()
454
+
455
+ # Generate speech
456
+ tts = gTTS(text=clean_text, lang='en', slow=False)
457
+ tts.save(temp_file.name)
458
+
459
+ return temp_file.name
460
+ except Exception as e:
461
+ print(f"Error generating speech: {str(e)}")
462
+ return None
463
 
464
  def create_test_visualization():
465
  """Create a test visualization to verify plotting works"""
 
544
  gr.Markdown("Upload documents, ask questions, and get voice responses!")
545
 
546
  with gr.Tab("Chat"):
547
+ # Use a custom CSS to ensure images are displayed properly
548
+ gr.HTML("""
549
+ <style>
550
+ .chatbot-container img {
551
+ max-width: 100%;
552
+ height: auto;
553
+ display: block;
554
+ margin: 10px 0;
555
+ }
556
+ </style>
557
+ """)
558
 
559
+ chatbot = gr.Chatbot(height=500, type="messages", elem_classes="chatbot-container")
 
560
 
561
  with gr.Row():
562
  with gr.Column(scale=8):
 
585
  submit_btn.click(
586
  process_text_query,
587
  inputs=[msg, chatbot],
588
+ outputs=[msg, chatbot]
 
589
  )
590
 
591
  msg.submit(
592
  process_text_query,
593
  inputs=[msg, chatbot],
594
+ outputs=[msg, chatbot]
 
595
  )
596
 
597
+ clear_btn.click(lambda: None, None, [chatbot], queue=False)
598
  clear_context_btn.click(clear_context, inputs=[], outputs=[chatbot])
599
 
600
  voice_btn.click(
 
618
  )
619
 
620
  with gr.Tab("Document Upload"):
 
 
 
 
 
 
621
  with gr.Row():
622
+ file_upload = gr.File(
623
+ label="Upload Documents",
624
+ file_types=[".pdf", ".txt", ".docx", ".csv", ".xlsx"],
625
+ file_count="multiple"
626
+ )
627
+ flush_db_btn_doc = gr.Button("🗑️ Flush All Databases", variant="stop")
628
 
629
+ upload_button = gr.Button("Process & Index Documents")
630
  upload_output = gr.Textbox(label="Upload Status")
631
 
632
  upload_button.click(
 
651
  )
652
 
653
  with gr.Tab("Settings"):
654
+ with gr.Row():
655
+ gr.Markdown("## Database Management")
656
+ flush_db_btn = gr.Button("🗑️ Flush All Databases", variant="stop", scale=1)
657
+
658
  flush_result = gr.Textbox(label="Flush Result")
659
 
660
  flush_db_btn.click(
requirements.txt CHANGED
@@ -10,4 +10,5 @@ langchain-community>=0.0.10
10
  langchain-groq>=0.0.5
11
  plotly>=5.14.0
12
  gtts>=2.3.1
13
- SpeechRecognition>=3.10.0
 
 
10
  langchain-groq>=0.0.5
11
  plotly>=5.14.0
12
  gtts>=2.3.1
13
+ SpeechRecognition>=3.10.0
14
+ kaleido>=0.2.1