Redmind commited on
Commit
2d2aa0c
·
verified ·
1 Parent(s): a451a4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -26
app.py CHANGED
@@ -497,7 +497,6 @@ iterations = 0
497
 
498
 
499
  def answer_question(user_question, chatbot, audio=None):
500
-
501
  global iterations
502
  iterations = 0
503
  # Ensure the temporary chart directory exists
@@ -527,9 +526,9 @@ def answer_question(user_question, chatbot, audio=None):
527
  except sr.RequestError:
528
  user_question = "Could not request results from Google Speech Recognition service."
529
 
530
- while iterations < max_iterations:
531
 
532
- """if "send email to" in user_question:
533
  email_match = re.search(r"send email to ([\w\.-]+@[\w\.-]+)", user_question)
534
  if email_match:
535
  user_email = email_match.group(1).strip()
@@ -537,43 +536,40 @@ def answer_question(user_question, chatbot, audio=None):
537
  user_question = f"{user_question}:{user_email}"
538
  """
539
 
540
- response = agent_executor.invoke({"input": user_question}, config={"callbacks": [langfuse_handler]})
541
 
542
- if isinstance(response, dict):
543
- response_text = response.get("output", "")
544
- else:
545
- response_text = response
546
- if "invalid" not in response_text.lower():
547
- break
548
- iterations += 1
549
 
550
- if iterations == max_iterations:
551
- return "The agent could not generate a valid response within the iteration limit."
552
 
553
  if os.getenv("IMAGE_PATH") in response_text:
554
  # Open the image file
555
  img = Image.open(os.getenv("IMAGE_PATH"))
556
-
557
  # Convert the PIL Image to a base64 encoded string
558
  buffered = BytesIO()
559
  img.save(buffered, format="PNG")
560
  img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
561
-
562
  img = f'<img src="data:image/png;base64,{img_str}" style="width:500px; height:400px;">'
563
  # image = gr.Image(value=img_str)
564
  chatbot.append((user_question, img))
565
-
566
  email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
567
  match = re.search(email_pattern, user_question)
568
  if match:
569
- user_email = match.group() # Return the matched email
570
-
571
- # email send
572
- if len(user_email)>0:
573
- import asyncio
574
-
575
  # Send email with the chart image attached
576
- #coro=
577
  send_email_with_attachment(
578
  recipient_email=user_email,
579
  subject="Warehouse Inventory Report",
@@ -582,9 +578,6 @@ def answer_question(user_question, chatbot, audio=None):
582
  attachment_path=os.getenv("IMAGE_PATH")
583
  )
584
 
585
- #asyncio.run(coro)
586
-
587
-
588
  if "send email to" in user_question:
589
  try:
590
  os.remove(img) # Clean up the temporary image file
@@ -604,6 +597,7 @@ def answer_question(user_question, chatbot, audio=None):
604
  # response_text = response_text.replace('\n', ' ').replace(' ', ' ').strip()
605
  # return response_text
606
 
 
607
  css = """
608
  .gradio-container {
609
  width: 90%; /* Set the width as a percentage of the viewport */
 
497
 
498
 
499
  def answer_question(user_question, chatbot, audio=None):
 
500
  global iterations
501
  iterations = 0
502
  # Ensure the temporary chart directory exists
 
526
  except sr.RequestError:
527
  user_question = "Could not request results from Google Speech Recognition service."
528
 
529
+ while iterations < max_iterations:
530
 
531
+ """if "send email to" in user_question:
532
  email_match = re.search(r"send email to ([\w\.-]+@[\w\.-]+)", user_question)
533
  if email_match:
534
  user_email = email_match.group(1).strip()
 
536
  user_question = f"{user_question}:{user_email}"
537
  """
538
 
539
+ response = agent_executor.invoke({"input": user_question}, config={"callbacks": [langfuse_handler]})
540
 
541
+ if isinstance(response, dict):
542
+ response_text = response.get("output", "")
543
+ else:
544
+ response_text = response
545
+ if "invalid" not in response_text.lower():
546
+ break
547
+ iterations += 1
548
 
549
+ if iterations == max_iterations:
550
+ return "The agent could not generate a valid response within the iteration limit."
551
 
552
  if os.getenv("IMAGE_PATH") in response_text:
553
  # Open the image file
554
  img = Image.open(os.getenv("IMAGE_PATH"))
555
+
556
  # Convert the PIL Image to a base64 encoded string
557
  buffered = BytesIO()
558
  img.save(buffered, format="PNG")
559
  img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
560
+
561
  img = f'<img src="data:image/png;base64,{img_str}" style="width:500px; height:400px;">'
562
  # image = gr.Image(value=img_str)
563
  chatbot.append((user_question, img))
564
+
565
  email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
566
  match = re.search(email_pattern, user_question)
567
  if match:
568
+ user_email = match.group() # Return the matched email
569
+
570
+ # email send
571
+ if len(user_email) > 0:
 
 
572
  # Send email with the chart image attached
 
573
  send_email_with_attachment(
574
  recipient_email=user_email,
575
  subject="Warehouse Inventory Report",
 
578
  attachment_path=os.getenv("IMAGE_PATH")
579
  )
580
 
 
 
 
581
  if "send email to" in user_question:
582
  try:
583
  os.remove(img) # Clean up the temporary image file
 
597
  # response_text = response_text.replace('\n', ' ').replace(' ', ' ').strip()
598
  # return response_text
599
 
600
+
601
  css = """
602
  .gradio-container {
603
  width: 90%; /* Set the width as a percentage of the viewport */