Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -51,8 +51,10 @@ load_dotenv()
|
|
51 |
# langfuse analytics
|
52 |
from langfuse.callback import CallbackHandler
|
53 |
|
|
|
54 |
import concurrent.futures
|
55 |
|
|
|
56 |
|
57 |
# Define global variables for managing the thread and future
|
58 |
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
|
@@ -270,7 +272,7 @@ def send_email_with_attachment_mailjet(recipient_email, subject, body, attachmen
|
|
270 |
"To": [
|
271 |
{
|
272 |
"Email": recipient_email,
|
273 |
-
"Name": "
|
274 |
}
|
275 |
],
|
276 |
"Subject": subject,
|
@@ -280,7 +282,7 @@ def send_email_with_attachment_mailjet(recipient_email, subject, body, attachmen
|
|
280 |
"Attachments": [
|
281 |
{
|
282 |
"ContentType": "image/png", # Replace with the correct MIME type of your image
|
283 |
-
"Filename": "
|
284 |
"Base64Content": attachment_path # Base64-encoded image content
|
285 |
}
|
286 |
]
|
@@ -303,41 +305,6 @@ def send_email_with_attachment_mailjet(recipient_email, subject, body, attachmen
|
|
303 |
#smtp lib
|
304 |
def send_email_with_attachment(recipient_email, subject, body, attachment_path):
|
305 |
try:
|
306 |
-
from fastapi import FastAPI
|
307 |
-
|
308 |
-
from fastapi_mail import FastMail, MessageSchema, ConnectionConfig, MessageType
|
309 |
-
from pydantic import EmailStr, BaseModel
|
310 |
-
|
311 |
-
"""
|
312 |
-
|
313 |
-
conf = ConnectionConfig(
|
314 |
-
MAIL_USERNAME = "redmind",
|
315 |
-
MAIL_PASSWORD = "jymzapycraiheubg",
|
316 |
-
MAIL_FROM = "[email protected]",
|
317 |
-
MAIL_PORT = 2525,
|
318 |
-
MAIL_SERVER = "smtp.gmail.com",
|
319 |
-
MAIL_FROM_NAME="redmind",
|
320 |
-
MAIL_STARTTLS = True,
|
321 |
-
MAIL_SSL_TLS = False,
|
322 |
-
USE_CREDENTIALS = True,
|
323 |
-
VALIDATE_CERTS = True
|
324 |
-
)
|
325 |
-
|
326 |
-
html = <p>Hi this test mail, thanks for using Fastapi-mail</p>
|
327 |
-
|
328 |
-
message = MessageSchema(
|
329 |
-
subject="Fastapi-Mail module",
|
330 |
-
recipients=[recipient_email],
|
331 |
-
body=html,
|
332 |
-
subtype=MessageType.html)
|
333 |
-
print(message)
|
334 |
-
fm = FastMail(conf)
|
335 |
-
await fm.send_message(message)
|
336 |
-
print("message sent")
|
337 |
-
|
338 |
-
"""
|
339 |
-
|
340 |
-
|
341 |
sender_email = os.getenv("EMAIL_SENDER")
|
342 |
sender_password = os.getenv("EMAIL_PASSWORD")
|
343 |
# Create a multipart message
|
@@ -349,40 +316,31 @@ def send_email_with_attachment(recipient_email, subject, body, attachment_path):
|
|
349 |
msg.attach(MIMEText(body, 'plain'))
|
350 |
# Open the file to be sent
|
351 |
attachment = open(attachment_path, "rb")
|
352 |
-
#print("Attached the image")
|
353 |
# Instance of MIMEBase and named as p
|
354 |
part = MIMEBase('application', 'octet-stream')
|
355 |
-
|
356 |
# To change the payload into encoded form
|
357 |
part.set_payload((attachment).read())
|
358 |
-
|
359 |
# Encode into base64
|
360 |
encoders.encode_base64(part)
|
361 |
-
|
362 |
part.add_header('Content-Disposition', f"attachment; filename= {attachment_path}")
|
363 |
-
|
364 |
# Attach the instance 'part' to instance 'msg'
|
365 |
msg.attach(part)
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
# Send the email
|
370 |
-
try:
|
371 |
-
with smtplib.SMTP_SSL("smtp.gmail.com", 2525, context=context) as server:
|
372 |
-
server.login(sender_email, sender_password)
|
373 |
-
server.sendmail(sender_email, recipient_email, msg)
|
374 |
-
print("Email sent successfully")
|
375 |
-
except Exception as e:
|
376 |
-
print(f"Error occurred: {str(e)}")
|
377 |
-
server = smtplib.SMTP('smtp.gmail.com', 2525)
|
378 |
server.starttls()
|
379 |
server.login(sender_email, sender_password)
|
380 |
text = msg.as_string()
|
381 |
server.sendmail(sender_email, recipient_email, text)
|
382 |
server.quit()
|
|
|
383 |
except Exception as error:
|
384 |
print(f"An error occurred: {error}")
|
385 |
-
|
386 |
# return 1
|
387 |
|
388 |
|
@@ -399,14 +357,7 @@ def make_api_request(url, params):
|
|
399 |
print(f"An error occurred: {err}")
|
400 |
|
401 |
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
def inventory_report(question):
|
406 |
-
#print(question)
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
# Split the question to extract warehouse name, user question, and optional email
|
411 |
if question.count(":") > 0:
|
412 |
parts = question.split(":", 2)
|
@@ -432,8 +383,6 @@ def inventory_report(question):
|
|
432 |
|
433 |
data1 = make_api_request(apis[1]["url"], apis[1]["params"])
|
434 |
|
435 |
-
from tabulate import tabulate
|
436 |
-
|
437 |
headers = ["S.No", "Warehouse Code", "Warehouse Name", "Customer Code", "Customer Name", "Item Code", "Item Name",
|
438 |
"Currency", "EAN", "UOM", "Quantity", "Gross Weight", "Volume", "Total Value"]
|
439 |
table_data = []
|
@@ -558,6 +507,7 @@ max_iterations = 5
|
|
558 |
iterations = 0
|
559 |
|
560 |
def handle_query(user_question, chatbot, audio=None):
|
|
|
561 |
"""
|
562 |
Function to handle the processing of user input with `AgentExecutor.invoke()`.
|
563 |
"""
|
@@ -570,7 +520,7 @@ def handle_query(user_question, chatbot, audio=None):
|
|
570 |
return "A query is already being processed. Please stop it before starting a new one."
|
571 |
print(user_question)
|
572 |
# Start the processing in a new thread
|
573 |
-
future = executor.submit(
|
574 |
|
575 |
# Periodically check if future is done
|
576 |
while not future.done():
|
@@ -604,7 +554,8 @@ def stop_processing(chatbot):
|
|
604 |
chatbot.append((user_question, "Sorry, we encountered an error while processing your request. Please try after some time."))
|
605 |
return gr.update(value=chatbot)
|
606 |
|
607 |
-
|
|
|
608 |
|
609 |
global iterations
|
610 |
iterations = 0
|
@@ -846,16 +797,6 @@ def handle_dislike(data: gr.LikeData):
|
|
846 |
def update_message(request: gr.Request):
|
847 |
return f"<h2 style=' font-family: Calibri;'>Welcome, {request.username}</h4>"
|
848 |
|
849 |
-
def stop_agent():
|
850 |
-
"""
|
851 |
-
Stops the agent process if it is still running.
|
852 |
-
"""
|
853 |
-
global process
|
854 |
-
if process and process.is_alive():
|
855 |
-
process.terminate() # Terminate the process forcefully
|
856 |
-
process.join() # Ensure the process is properly terminated
|
857 |
-
return "Agent execution stopped."
|
858 |
-
return "No active process to stop."
|
859 |
|
860 |
# CSS for styling the buttons and other elements
|
861 |
css = """
|
@@ -931,11 +872,12 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
|
931 |
with gr.Row():
|
932 |
button = gr.Button("Submit", elem_id="submit",elem_classes="gr-button")
|
933 |
# Button to stop the current processing
|
934 |
-
stop_button = gr.Button("Stop
|
935 |
-
|
|
|
936 |
|
937 |
-
button.click(
|
938 |
-
message.submit(
|
939 |
message.submit(lambda x: gr.update(value=""), None, [message], queue=False)
|
940 |
button.click(lambda x: gr.update(value=''), [], [message])
|
941 |
|
@@ -945,9 +887,9 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
|
945 |
|
946 |
#sample_button.click(answer_question, [sample_button, chatbot], [chatbot])
|
947 |
sample_button.click(handle_query, [sample_button, chatbot], [chatbot])
|
948 |
-
sample_button1.click(
|
949 |
-
sample_button2.click(
|
950 |
-
sample_button3.click(
|
951 |
-
sample_button4.click(
|
952 |
|
953 |
demo.launch(auth=[("lakshmi", "redmind"), ("arun", "redmind")])
|
|
|
51 |
# langfuse analytics
|
52 |
from langfuse.callback import CallbackHandler
|
53 |
|
54 |
+
#forcefully stop the agent execution
|
55 |
import concurrent.futures
|
56 |
|
57 |
+
from tabulate import tabulate
|
58 |
|
59 |
# Define global variables for managing the thread and future
|
60 |
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
|
|
|
272 |
"To": [
|
273 |
{
|
274 |
"Email": recipient_email,
|
275 |
+
"Name": ""
|
276 |
}
|
277 |
],
|
278 |
"Subject": subject,
|
|
|
282 |
"Attachments": [
|
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": attachment_path # Base64-encoded image content
|
287 |
}
|
288 |
]
|
|
|
305 |
#smtp lib
|
306 |
def send_email_with_attachment(recipient_email, subject, body, attachment_path):
|
307 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
308 |
sender_email = os.getenv("EMAIL_SENDER")
|
309 |
sender_password = os.getenv("EMAIL_PASSWORD")
|
310 |
# Create a multipart message
|
|
|
316 |
msg.attach(MIMEText(body, 'plain'))
|
317 |
# Open the file to be sent
|
318 |
attachment = open(attachment_path, "rb")
|
319 |
+
# print("Attached the image")
|
320 |
# Instance of MIMEBase and named as p
|
321 |
part = MIMEBase('application', 'octet-stream')
|
322 |
+
|
323 |
# To change the payload into encoded form
|
324 |
part.set_payload((attachment).read())
|
325 |
+
|
326 |
# Encode into base64
|
327 |
encoders.encode_base64(part)
|
328 |
+
|
329 |
part.add_header('Content-Disposition', f"attachment; filename= {attachment_path}")
|
330 |
+
|
331 |
# Attach the instance 'part' to instance 'msg'
|
332 |
msg.attach(part)
|
333 |
+
|
334 |
+
server = smtplib.SMTP('smtp.gmail.com', 587)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
335 |
server.starttls()
|
336 |
server.login(sender_email, sender_password)
|
337 |
text = msg.as_string()
|
338 |
server.sendmail(sender_email, recipient_email, text)
|
339 |
server.quit()
|
340 |
+
|
341 |
except Exception as error:
|
342 |
print(f"An error occurred: {error}")
|
343 |
+
|
344 |
# return 1
|
345 |
|
346 |
|
|
|
357 |
print(f"An error occurred: {err}")
|
358 |
|
359 |
|
|
|
|
|
|
|
360 |
def inventory_report(question):
|
|
|
|
|
|
|
|
|
361 |
# Split the question to extract warehouse name, user question, and optional email
|
362 |
if question.count(":") > 0:
|
363 |
parts = question.split(":", 2)
|
|
|
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 = []
|
|
|
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 |
"""
|
|
|
520 |
return "A query is already being processed. Please stop it before starting a new one."
|
521 |
print(user_question)
|
522 |
# Start the processing in a new thread
|
523 |
+
future = executor.submit(answer_question_thread, user_question, chatbot)
|
524 |
|
525 |
# Periodically check if future is done
|
526 |
while not future.done():
|
|
|
554 |
chatbot.append((user_question, "Sorry, we encountered an error while processing your request. Please try after some time."))
|
555 |
return gr.update(value=chatbot)
|
556 |
|
557 |
+
# This function is for agent executor invoke with the option of stop
|
558 |
+
def answer_question_thread(user_question, chatbot, audio=None):
|
559 |
|
560 |
global iterations
|
561 |
iterations = 0
|
|
|
797 |
def update_message(request: gr.Request):
|
798 |
return f"<h2 style=' font-family: Calibri;'>Welcome, {request.username}</h4>"
|
799 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
800 |
|
801 |
# CSS for styling the buttons and other elements
|
802 |
css = """
|
|
|
872 |
with gr.Row():
|
873 |
button = gr.Button("Submit", elem_id="submit",elem_classes="gr-button")
|
874 |
# Button to stop the current processing
|
875 |
+
stop_button = gr.Button("Stop", elem_classes="gr-button")
|
876 |
+
|
877 |
+
stop_button.click(stop_processing, [chatbot],[chatbot])
|
878 |
|
879 |
+
button.click(handle_query, [message, chatbot], [chatbot])
|
880 |
+
message.submit(handle_query, [message, chatbot], [chatbot])
|
881 |
message.submit(lambda x: gr.update(value=""), None, [message], queue=False)
|
882 |
button.click(lambda x: gr.update(value=''), [], [message])
|
883 |
|
|
|
887 |
|
888 |
#sample_button.click(answer_question, [sample_button, chatbot], [chatbot])
|
889 |
sample_button.click(handle_query, [sample_button, chatbot], [chatbot])
|
890 |
+
sample_button1.click(handle_query, [sample_button1, chatbot], [chatbot])
|
891 |
+
sample_button2.click(handle_query, [sample_button2, chatbot], [chatbot])
|
892 |
+
sample_button3.click(handle_query, [sample_button3, chatbot], [chatbot])
|
893 |
+
sample_button4.click(handle_query, [sample_button4, chatbot], [chatbot])
|
894 |
|
895 |
demo.launch(auth=[("lakshmi", "redmind"), ("arun", "redmind")])
|