Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,16 +11,18 @@ import time
|
|
11 |
import shutil
|
12 |
import json
|
13 |
import nltk
|
|
|
14 |
# audio package
|
15 |
-
import speech_recognition as sr
|
16 |
from pydub import AudioSegment
|
17 |
-
from pydub.playback import play
|
|
|
18 |
# email library
|
19 |
-
import smtplib, ssl
|
20 |
from email.mime.multipart import MIMEMultipart
|
21 |
from email.mime.text import MIMEText
|
22 |
from email.mime.base import MIMEBase
|
23 |
-
from email import encoders
|
24 |
# langchain
|
25 |
from langchain_core.prompts import ChatPromptTemplate
|
26 |
from langchain_core.output_parsers import StrOutputParser
|
@@ -51,15 +53,20 @@ load_dotenv()
|
|
51 |
# langfuse analytics
|
52 |
from langfuse.callback import CallbackHandler
|
53 |
|
|
|
|
|
|
|
54 |
#forcefully stop the agent execution
|
55 |
import concurrent.futures
|
|
|
56 |
|
57 |
-
|
|
|
|
|
58 |
|
59 |
-
# Define global variables for managing the thread and
|
60 |
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
|
61 |
-
|
62 |
-
import threading
|
63 |
stop_event = threading.Event()
|
64 |
|
65 |
# LangFuse API keys and host settings
|
@@ -81,9 +88,11 @@ db_uri = os.getenv("POSTGRESQL_CONNECTION")
|
|
81 |
# Database setup
|
82 |
|
83 |
db = SQLDatabase.from_uri(db_uri)
|
|
|
84 |
user_email = ""
|
85 |
warehouse_name = ""
|
86 |
warehouse_id = ""
|
|
|
87 |
inventory_date = datetime.today().strftime('%Y-%m-%d')
|
88 |
apis = [
|
89 |
# fetch warehouse ID
|
@@ -193,9 +202,6 @@ def get_ASN_data(question):
|
|
193 |
except Exception as err:
|
194 |
print(f"An error occurred: {err}")
|
195 |
|
196 |
-
|
197 |
-
# get_ASN_data("ASN24072400001")
|
198 |
-
|
199 |
def load_and_split_pdf(pdf_path):
|
200 |
reader = PdfReader(pdf_path)
|
201 |
text = ''
|
@@ -248,15 +254,12 @@ def document_data_tool(question):
|
|
248 |
# summarized_response = summarize_document(query_response)
|
249 |
return query_response
|
250 |
|
251 |
-
#mailjet API
|
252 |
-
def send_email_with_attachment_mailjet(recipient_email, subject, body,
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
# Replace these with your Mailjet API credentials
|
258 |
-
api_key = '9ce0aa1f8eb21d9e2ed2433a5702185b'
|
259 |
-
api_secret = '434e366db9b8c421eef99f6f92129fad'
|
260 |
|
261 |
# Initialize the Mailjet client
|
262 |
mailjet = Client(auth=(api_key, api_secret), version='v3.1')
|
@@ -283,7 +286,7 @@ def send_email_with_attachment_mailjet(recipient_email, subject, body, attachmen
|
|
283 |
{
|
284 |
"ContentType": "image/png", # Replace with the correct MIME type of your image
|
285 |
"Filename": "inventory_report.png", # Name of the image as it will appear in the email
|
286 |
-
"Base64Content":
|
287 |
}
|
288 |
]
|
289 |
|
@@ -293,7 +296,7 @@ def send_email_with_attachment_mailjet(recipient_email, subject, body, attachmen
|
|
293 |
|
294 |
# Send the email
|
295 |
result = mailjet.send.create(data=data)
|
296 |
-
|
297 |
# Check if the email was sent successfully
|
298 |
if result.status_code == 200:
|
299 |
print("Email sent successfully with attachment!")
|
@@ -381,40 +384,43 @@ def inventory_report(question):
|
|
381 |
if "warehouseId" in api["params"]:
|
382 |
api["params"]["warehouseId"] = warehouse_id
|
383 |
|
384 |
-
data1 = make_api_request(apis[1]["url"], apis[1]["params"])
|
385 |
-
|
386 |
-
headers = ["S.No", "Warehouse Code", "Warehouse Name", "Customer Code", "Customer Name", "Item Code", "Item Name",
|
387 |
-
"Currency", "EAN", "UOM", "Quantity", "Gross Weight", "Volume", "Total Value"]
|
388 |
-
table_data = []
|
389 |
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
408 |
|
409 |
-
|
410 |
-
|
411 |
|
412 |
-
|
413 |
|
414 |
-
|
415 |
-
|
416 |
|
417 |
-
|
|
|
|
|
418 |
|
419 |
|
420 |
# inventory_report("WH:can you give me a bar chart with item name and quantity for the warehouse WH")
|
@@ -506,39 +512,39 @@ def clean_gradio_tmp_dir():
|
|
506 |
max_iterations = 5
|
507 |
iterations = 0
|
508 |
|
|
|
509 |
def handle_query(user_question, chatbot, audio=None):
|
510 |
|
511 |
"""
|
512 |
Function to handle the processing of user input with `AgentExecutor.invoke()`.
|
513 |
"""
|
514 |
-
global
|
515 |
|
516 |
-
# Clear previous stop event and
|
517 |
stop_event.clear()
|
518 |
-
|
519 |
-
#if
|
520 |
# chatbot.append(("","A query is already being processed. Please stop it before starting a new one."))
|
521 |
# return gr.update(value=chatbot)
|
522 |
|
523 |
-
print(user_question)
|
524 |
# Start the processing in a new thread
|
525 |
-
|
526 |
|
527 |
-
# Periodically check if
|
528 |
-
while not
|
529 |
if stop_event.is_set():
|
530 |
-
|
531 |
chatbot.append((user_question, "Sorry, we encountered an error while processing your request. Please try after some time."))
|
532 |
return gr.update(value=chatbot)
|
533 |
|
534 |
time.sleep(1) # Wait for 1 second before checking again
|
535 |
|
536 |
-
if
|
537 |
chatbot.append((user_question, "Sorry, we encountered an error while processing your request. Please try after some time."))
|
538 |
return gr.update(value=chatbot)
|
539 |
else:
|
540 |
try:
|
541 |
-
user_question1, response_text1 =
|
542 |
print("output")
|
543 |
print(user_question1)
|
544 |
print(response_text1)
|
@@ -554,10 +560,10 @@ def stop_processing(chatbot):
|
|
554 |
"""
|
555 |
Stops the current processing if it's running.
|
556 |
"""
|
557 |
-
global
|
558 |
-
if
|
559 |
stop_event.set() # Signal the process to stop
|
560 |
-
|
561 |
chatbot.append((user_question, "Sorry, we encountered an error while processing your request. Please try after some time."))
|
562 |
return gr.update(value=chatbot)
|
563 |
|
@@ -571,6 +577,7 @@ def answer_question_thread(user_question, chatbot, audio=None):
|
|
571 |
# Clean the /tmp/gradio/ directory
|
572 |
# clean_gradio_tmp_dir()
|
573 |
# Handle audio input if provided
|
|
|
574 |
if audio is not None:
|
575 |
sample_rate, audio_data = audio
|
576 |
audio_segment = AudioSegment(
|
@@ -592,6 +599,7 @@ def answer_question_thread(user_question, chatbot, audio=None):
|
|
592 |
user_question = "Sorry, I could not understand the audio."
|
593 |
except sr.RequestError:
|
594 |
user_question = "Could not request results from Google Speech Recognition service."
|
|
|
595 |
|
596 |
while iterations < max_iterations:
|
597 |
|
@@ -626,8 +634,7 @@ def answer_question_thread(user_question, chatbot, audio=None):
|
|
626 |
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
627 |
|
628 |
img = f'<img src="data:image/png;base64,{img_str}" style="width:450px; height:400px;">'
|
629 |
-
|
630 |
-
#chatbot.append((user_question, img))
|
631 |
response_text = response.get("output", "").split(".")[0] + img
|
632 |
|
633 |
email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
|
@@ -643,17 +650,8 @@ def answer_question_thread(user_question, chatbot, audio=None):
|
|
643 |
subject="Warehouse Inventory Report",
|
644 |
body=response.get("output", "").split(".")[0],
|
645 |
# attachment_path=chart_path
|
646 |
-
|
647 |
|
648 |
-
# Send email with the chart image attached
|
649 |
-
"""send_email_with_attachment(
|
650 |
-
recipient_email=user_email,
|
651 |
-
subject="Warehouse Inventory Report",
|
652 |
-
body=response.get("output", "").split(":")[0],
|
653 |
-
# attachment_path=chart_path
|
654 |
-
attachment_path=os.getenv("IMAGE_PATH")
|
655 |
-
)"""
|
656 |
-
#return user_question, img
|
657 |
|
658 |
if "send email to" in user_question:
|
659 |
try:
|
@@ -663,22 +661,15 @@ def answer_question_thread(user_question, chatbot, audio=None):
|
|
663 |
except Exception as e:
|
664 |
print(f"Error loading image file: {e}")
|
665 |
response_text = "Chart generation failed. Please try again."
|
666 |
-
#chatbot.append((user_question, "Chart generation failed. Please try again."))
|
667 |
-
#return gr.update(value=chatbot)
|
668 |
-
return user_question, response_text
|
669 |
-
|
670 |
|
671 |
-
|
672 |
-
# return "/home/user/app/exports/charts/temp_chart.png"
|
673 |
else:
|
674 |
-
#chatbot.append((user_question, response_text))
|
675 |
-
#return gr.update(value=chatbot)
|
676 |
return user_question, response_text
|
677 |
# response_text = response_text.replace('\n', ' ').replace(' ', ' ').strip()
|
678 |
# return response_text
|
679 |
|
680 |
|
681 |
-
|
682 |
def answer_question(user_question, chatbot, audio=None):
|
683 |
|
684 |
global iterations
|
@@ -803,6 +794,7 @@ def handle_dislike(data: gr.LikeData):
|
|
803 |
print("upvote")
|
804 |
return gr.update(visible=False), gr.update(visible=False)
|
805 |
|
|
|
806 |
def update_message(request: gr.Request):
|
807 |
return f"<h2 style=' font-family: Calibri;'>Welcome, {request.username}</h4>"
|
808 |
|
@@ -902,4 +894,5 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
|
902 |
sample_button3.click(handle_query, [sample_button3, chatbot], [chatbot])
|
903 |
sample_button4.click(handle_query, [sample_button4, chatbot], [chatbot])
|
904 |
|
|
|
905 |
demo.launch(auth=[("lakshmi", "redmind"), ("arun", "redmind")])
|
|
|
11 |
import shutil
|
12 |
import json
|
13 |
import nltk
|
14 |
+
#commenting audio related code based on Arun's input
|
15 |
# audio package
|
16 |
+
"""import speech_recognition as sr
|
17 |
from pydub import AudioSegment
|
18 |
+
from pydub.playback import play"""
|
19 |
+
# Commenting SMTP code since HFSpaces doesn't support it
|
20 |
# email library
|
21 |
+
"""import smtplib, ssl
|
22 |
from email.mime.multipart import MIMEMultipart
|
23 |
from email.mime.text import MIMEText
|
24 |
from email.mime.base import MIMEBase
|
25 |
+
from email import encoders"""
|
26 |
# langchain
|
27 |
from langchain_core.prompts import ChatPromptTemplate
|
28 |
from langchain_core.output_parsers import StrOutputParser
|
|
|
53 |
# langfuse analytics
|
54 |
from langfuse.callback import CallbackHandler
|
55 |
|
56 |
+
# Inventory API data table
|
57 |
+
from tabulate import tabulate
|
58 |
+
|
59 |
#forcefully stop the agent execution
|
60 |
import concurrent.futures
|
61 |
+
import threading
|
62 |
|
63 |
+
# mailjet_rest to send email
|
64 |
+
from mailjet_rest import Client
|
65 |
+
import base64
|
66 |
|
67 |
+
# Define global variables for managing the thread and current_event
|
68 |
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
|
69 |
+
current_event = None
|
|
|
70 |
stop_event = threading.Event()
|
71 |
|
72 |
# LangFuse API keys and host settings
|
|
|
88 |
# Database setup
|
89 |
|
90 |
db = SQLDatabase.from_uri(db_uri)
|
91 |
+
|
92 |
user_email = ""
|
93 |
warehouse_name = ""
|
94 |
warehouse_id = ""
|
95 |
+
# Today's date to be populated in inventory API
|
96 |
inventory_date = datetime.today().strftime('%Y-%m-%d')
|
97 |
apis = [
|
98 |
# fetch warehouse ID
|
|
|
202 |
except Exception as err:
|
203 |
print(f"An error occurred: {err}")
|
204 |
|
|
|
|
|
|
|
205 |
def load_and_split_pdf(pdf_path):
|
206 |
reader = PdfReader(pdf_path)
|
207 |
text = ''
|
|
|
254 |
# summarized_response = summarize_document(query_response)
|
255 |
return query_response
|
256 |
|
257 |
+
# mailjet API since SMTP is not supported HF spaces
|
258 |
+
def send_email_with_attachment_mailjet(recipient_email, subject, body, attach_img_base64):
|
259 |
+
|
260 |
+
|
261 |
+
api_key = os.getenv("MAILJET_API_KEY")
|
262 |
+
api_secret = os.getenv("MAILJET_API_SECRET")
|
|
|
|
|
|
|
263 |
|
264 |
# Initialize the Mailjet client
|
265 |
mailjet = Client(auth=(api_key, api_secret), version='v3.1')
|
|
|
286 |
{
|
287 |
"ContentType": "image/png", # Replace with the correct MIME type of your image
|
288 |
"Filename": "inventory_report.png", # Name of the image as it will appear in the email
|
289 |
+
"Base64Content": attach_img_base64 # Base64-encoded image content
|
290 |
}
|
291 |
]
|
292 |
|
|
|
296 |
|
297 |
# Send the email
|
298 |
result = mailjet.send.create(data=data)
|
299 |
+
|
300 |
# Check if the email was sent successfully
|
301 |
if result.status_code == 200:
|
302 |
print("Email sent successfully with attachment!")
|
|
|
384 |
if "warehouseId" in api["params"]:
|
385 |
api["params"]["warehouseId"] = warehouse_id
|
386 |
|
|
|
|
|
|
|
|
|
|
|
387 |
|
388 |
+
data1 = make_api_request(apis[1]["url"], apis[1]["params"])
|
389 |
+
if (data1):
|
390 |
+
headers = ["S.No", "Warehouse Code", "Warehouse Name", "Customer Code", "Customer Name", "Item Code", "Item Name",
|
391 |
+
"Currency", "EAN", "UOM", "Quantity", "Gross Weight", "Volume", "Total Value"]
|
392 |
+
table_data = []
|
393 |
+
|
394 |
+
for index, item in enumerate(data1['result'], start=1):
|
395 |
+
row = [
|
396 |
+
index, # Serial number
|
397 |
+
item['warehouse']['code'],
|
398 |
+
item['warehouse']['name'],
|
399 |
+
item['customer']['code'],
|
400 |
+
item['customer']['name'],
|
401 |
+
item['skuMaster']['code'],
|
402 |
+
item['skuMaster']['name'],
|
403 |
+
item['currency']['code'],
|
404 |
+
item['eanUpc'],
|
405 |
+
item['uom']['code'],
|
406 |
+
item['totalQty'],
|
407 |
+
item['grossWeight'],
|
408 |
+
item['volume'],
|
409 |
+
item['totalValue']
|
410 |
+
]
|
411 |
+
table_data.append(row)
|
412 |
|
413 |
+
# Convert to pandas DataFrame
|
414 |
+
df = pd.DataFrame(table_data, columns=headers)
|
415 |
|
416 |
+
sdf = SmartDataframe(df, config={"llm": llm_chart})
|
417 |
|
418 |
+
# chart = sdf.chat("Can you draw a bar chart with all avaialble item name and quantity.")
|
419 |
+
chart = sdf.chat(question)
|
420 |
|
421 |
+
return chart
|
422 |
+
else:
|
423 |
+
return "There are no inventory details for the warehouse you have given."
|
424 |
|
425 |
|
426 |
# inventory_report("WH:can you give me a bar chart with item name and quantity for the warehouse WH")
|
|
|
512 |
max_iterations = 5
|
513 |
iterations = 0
|
514 |
|
515 |
+
|
516 |
def handle_query(user_question, chatbot, audio=None):
|
517 |
|
518 |
"""
|
519 |
Function to handle the processing of user input with `AgentExecutor.invoke()`.
|
520 |
"""
|
521 |
+
global current_event, stop_event
|
522 |
|
523 |
+
# Clear previous stop event and current_event
|
524 |
stop_event.clear()
|
525 |
+
|
526 |
+
#if current_event and not current_event.done():
|
527 |
# chatbot.append(("","A query is already being processed. Please stop it before starting a new one."))
|
528 |
# return gr.update(value=chatbot)
|
529 |
|
|
|
530 |
# Start the processing in a new thread
|
531 |
+
current_event = executor.submit(answer_question_thread, user_question, chatbot)
|
532 |
|
533 |
+
# Periodically check if current_event is done
|
534 |
+
while not current_event.done():
|
535 |
if stop_event.is_set():
|
536 |
+
current_event.cancel()
|
537 |
chatbot.append((user_question, "Sorry, we encountered an error while processing your request. Please try after some time."))
|
538 |
return gr.update(value=chatbot)
|
539 |
|
540 |
time.sleep(1) # Wait for 1 second before checking again
|
541 |
|
542 |
+
if current_event.cancelled():
|
543 |
chatbot.append((user_question, "Sorry, we encountered an error while processing your request. Please try after some time."))
|
544 |
return gr.update(value=chatbot)
|
545 |
else:
|
546 |
try:
|
547 |
+
user_question1, response_text1 = current_event.result() # Get the result of the completed current_event
|
548 |
print("output")
|
549 |
print(user_question1)
|
550 |
print(response_text1)
|
|
|
560 |
"""
|
561 |
Stops the current processing if it's running.
|
562 |
"""
|
563 |
+
global current_event, stop_event
|
564 |
+
if current_event and not current_event.done():
|
565 |
stop_event.set() # Signal the process to stop
|
566 |
+
current_event.cancel() # Attempt to cancel the current_event
|
567 |
chatbot.append((user_question, "Sorry, we encountered an error while processing your request. Please try after some time."))
|
568 |
return gr.update(value=chatbot)
|
569 |
|
|
|
577 |
# Clean the /tmp/gradio/ directory
|
578 |
# clean_gradio_tmp_dir()
|
579 |
# Handle audio input if provided
|
580 |
+
"""
|
581 |
if audio is not None:
|
582 |
sample_rate, audio_data = audio
|
583 |
audio_segment = AudioSegment(
|
|
|
599 |
user_question = "Sorry, I could not understand the audio."
|
600 |
except sr.RequestError:
|
601 |
user_question = "Could not request results from Google Speech Recognition service."
|
602 |
+
"""
|
603 |
|
604 |
while iterations < max_iterations:
|
605 |
|
|
|
634 |
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
635 |
|
636 |
img = f'<img src="data:image/png;base64,{img_str}" style="width:450px; height:400px;">'
|
637 |
+
|
|
|
638 |
response_text = response.get("output", "").split(".")[0] + img
|
639 |
|
640 |
email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
|
|
|
650 |
subject="Warehouse Inventory Report",
|
651 |
body=response.get("output", "").split(".")[0],
|
652 |
# attachment_path=chart_path
|
653 |
+
attach_img_base64=img_str)
|
654 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
655 |
|
656 |
if "send email to" in user_question:
|
657 |
try:
|
|
|
661 |
except Exception as e:
|
662 |
print(f"Error loading image file: {e}")
|
663 |
response_text = "Chart generation failed. Please try again."
|
|
|
|
|
|
|
|
|
664 |
|
665 |
+
return user_question, response_text
|
|
|
666 |
else:
|
|
|
|
|
667 |
return user_question, response_text
|
668 |
# response_text = response_text.replace('\n', ' ').replace(' ', ' ').strip()
|
669 |
# return response_text
|
670 |
|
671 |
|
672 |
+
# without forceful stop option
|
673 |
def answer_question(user_question, chatbot, audio=None):
|
674 |
|
675 |
global iterations
|
|
|
794 |
print("upvote")
|
795 |
return gr.update(visible=False), gr.update(visible=False)
|
796 |
|
797 |
+
# greet with user name on successful login
|
798 |
def update_message(request: gr.Request):
|
799 |
return f"<h2 style=' font-family: Calibri;'>Welcome, {request.username}</h4>"
|
800 |
|
|
|
894 |
sample_button3.click(handle_query, [sample_button3, chatbot], [chatbot])
|
895 |
sample_button4.click(handle_query, [sample_button4, chatbot], [chatbot])
|
896 |
|
897 |
+
#user_details for login page
|
898 |
demo.launch(auth=[("lakshmi", "redmind"), ("arun", "redmind")])
|