Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -137,7 +137,7 @@ def run_query(query):
|
|
| 137 |
def database_tool(question):
|
| 138 |
# print(question)
|
| 139 |
sql_query = generate_sql_query(question)
|
| 140 |
-
print(sql_query)
|
| 141 |
return run_query(sql_query)
|
| 142 |
|
| 143 |
|
|
@@ -145,8 +145,8 @@ def get_ASN_data(question):
|
|
| 145 |
# print(question)
|
| 146 |
base_url = os.getenv("ASN_API_URL")
|
| 147 |
complete_url = f"{base_url}branchMaster.id=343&transactionUid={question}&userId=164&transactionType=ASN"
|
| 148 |
-
print("complete url")
|
| 149 |
-
print(complete_url)
|
| 150 |
try:
|
| 151 |
response = requests.get(complete_url)
|
| 152 |
data = response.json()
|
|
@@ -239,8 +239,8 @@ def document_data_tool(question):
|
|
| 239 |
print(f"Document data tool enter: {question}")
|
| 240 |
# query_string = question['tags'][0] if 'tags' in question and question['tags'] else ""
|
| 241 |
query_response = query_vector_store(vector_store, question, config={"callbacks": [langfuse_handler]})
|
| 242 |
-
print("query****")
|
| 243 |
-
print(query_response)
|
| 244 |
# summarized_response = summarize_document(query_response)
|
| 245 |
# print("summary***")
|
| 246 |
# print(summarized_response)
|
|
@@ -249,10 +249,10 @@ def document_data_tool(question):
|
|
| 249 |
|
| 250 |
def send_email_with_attachment(recipient_email, subject, body, image_data):
|
| 251 |
try:
|
| 252 |
-
print(recipient_email)
|
| 253 |
-
print(subject)
|
| 254 |
-
print(body)
|
| 255 |
-
print(image_data)
|
| 256 |
sender_email = os.getenv("EMAIL_SENDER")
|
| 257 |
sender_password = os.getenv("EMAIL_PASSWORD")
|
| 258 |
|
|
@@ -267,7 +267,7 @@ def send_email_with_attachment(recipient_email, subject, body, image_data):
|
|
| 267 |
"""
|
| 268 |
# Open the file to be sent
|
| 269 |
attachment = open(attachment_path, "rb")
|
| 270 |
-
print("Attached the image")
|
| 271 |
# Instance of MIMEBase and named as p
|
| 272 |
part = MIMEBase('application', 'octet-stream')
|
| 273 |
|
|
@@ -304,7 +304,7 @@ def send_email_with_attachment(recipient_email, subject, body, image_data):
|
|
| 304 |
server.sendmail(sender_email, recipient_email, text)
|
| 305 |
server.quit()
|
| 306 |
|
| 307 |
-
except error:
|
| 308 |
print(error)
|
| 309 |
|
| 310 |
# return 1
|
|
@@ -327,7 +327,7 @@ def make_api_request(url, params):
|
|
| 327 |
|
| 328 |
|
| 329 |
def inventory_report(question):
|
| 330 |
-
print(question)
|
| 331 |
|
| 332 |
|
| 333 |
|
|
@@ -341,15 +341,15 @@ def inventory_report(question):
|
|
| 341 |
else:
|
| 342 |
return "warehouse name not found"
|
| 343 |
|
| 344 |
-
print(apis[0]["url"])
|
| 345 |
-
print(apis[0]["params"])
|
| 346 |
data = make_api_request(apis[0]["url"], apis[0]["params"])
|
| 347 |
-
print(warehouse_name)
|
| 348 |
if data:
|
| 349 |
-
print(data)
|
| 350 |
# Extracting the id for the warehouse with the name "WH"
|
| 351 |
warehouse_id = next((item['id'] for item in data['result'] if item['name'] == warehouse_name), None)
|
| 352 |
-
print(warehouse_id)
|
| 353 |
if warehouse_id is None:
|
| 354 |
return "Please provide a warehouse name available in the database."
|
| 355 |
# print(f"The id for the warehouse named {name} is: {warehouse_id}")
|
|
@@ -518,7 +518,7 @@ def answer_question(user_question, chatbot, audio=None):
|
|
| 518 |
user_question = "Could not request results from Google Speech Recognition service."
|
| 519 |
|
| 520 |
while iterations < max_iterations:
|
| 521 |
-
print(user_question)
|
| 522 |
"""if "send email to" in user_question:
|
| 523 |
email_match = re.search(r"send email to ([\w\.-]+@[\w\.-]+)", user_question)
|
| 524 |
if email_match:
|
|
@@ -528,8 +528,8 @@ def answer_question(user_question, chatbot, audio=None):
|
|
| 528 |
"""
|
| 529 |
|
| 530 |
response = agent_executor.invoke({"input": user_question}, config={"callbacks": [langfuse_handler]})
|
| 531 |
-
print("name")
|
| 532 |
-
print(warehouse_name)
|
| 533 |
if isinstance(response, dict):
|
| 534 |
response_text = response.get("output", "")
|
| 535 |
else:
|
|
@@ -537,10 +537,10 @@ def answer_question(user_question, chatbot, audio=None):
|
|
| 537 |
if "invalid" not in response_text.lower():
|
| 538 |
break
|
| 539 |
iterations += 1
|
| 540 |
-
print(os.getenv("IMAGE_PATH"))
|
| 541 |
-
print(response_text)
|
| 542 |
-
print("validating. ")
|
| 543 |
-
print(os.getenv("IMAGE_PATH") in response_text)
|
| 544 |
if iterations == max_iterations:
|
| 545 |
return "The agent could not generate a valid response within the iteration limit."
|
| 546 |
|
|
@@ -548,12 +548,12 @@ def answer_question(user_question, chatbot, audio=None):
|
|
| 548 |
if os.getenv("IMAGE_PATH") in response_text:
|
| 549 |
# Open the image file
|
| 550 |
img = Image.open(os.getenv("IMAGE_PATH"))
|
| 551 |
-
print(img)
|
| 552 |
# Convert the PIL Image to a base64 encoded string
|
| 553 |
buffered = BytesIO()
|
| 554 |
img.save(buffered, format="PNG")
|
| 555 |
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 556 |
-
print(img_str)
|
| 557 |
img = f'<img src="data:image/png;base64,{img_str}" style="width:500px; height:400px;">'
|
| 558 |
# image = gr.Image(value=img_str)
|
| 559 |
chatbot.append((user_question, img))
|
|
@@ -562,8 +562,8 @@ def answer_question(user_question, chatbot, audio=None):
|
|
| 562 |
match = re.search(email_pattern, user_question)
|
| 563 |
if match:
|
| 564 |
user_email = match.group() # Return the matched email
|
| 565 |
-
print("user_email")
|
| 566 |
-
print(user_email)
|
| 567 |
# email send
|
| 568 |
if user_email:
|
| 569 |
# Send email with the chart image attached
|
|
|
|
| 137 |
def database_tool(question):
|
| 138 |
# print(question)
|
| 139 |
sql_query = generate_sql_query(question)
|
| 140 |
+
#print(sql_query)
|
| 141 |
return run_query(sql_query)
|
| 142 |
|
| 143 |
|
|
|
|
| 145 |
# print(question)
|
| 146 |
base_url = os.getenv("ASN_API_URL")
|
| 147 |
complete_url = f"{base_url}branchMaster.id=343&transactionUid={question}&userId=164&transactionType=ASN"
|
| 148 |
+
#print("complete url")
|
| 149 |
+
#print(complete_url)
|
| 150 |
try:
|
| 151 |
response = requests.get(complete_url)
|
| 152 |
data = response.json()
|
|
|
|
| 239 |
print(f"Document data tool enter: {question}")
|
| 240 |
# query_string = question['tags'][0] if 'tags' in question and question['tags'] else ""
|
| 241 |
query_response = query_vector_store(vector_store, question, config={"callbacks": [langfuse_handler]})
|
| 242 |
+
#print("query****")
|
| 243 |
+
#print(query_response)
|
| 244 |
# summarized_response = summarize_document(query_response)
|
| 245 |
# print("summary***")
|
| 246 |
# print(summarized_response)
|
|
|
|
| 249 |
|
| 250 |
def send_email_with_attachment(recipient_email, subject, body, image_data):
|
| 251 |
try:
|
| 252 |
+
#print(recipient_email)
|
| 253 |
+
#print(subject)
|
| 254 |
+
#print(body)
|
| 255 |
+
#print(image_data)
|
| 256 |
sender_email = os.getenv("EMAIL_SENDER")
|
| 257 |
sender_password = os.getenv("EMAIL_PASSWORD")
|
| 258 |
|
|
|
|
| 267 |
"""
|
| 268 |
# Open the file to be sent
|
| 269 |
attachment = open(attachment_path, "rb")
|
| 270 |
+
#print("Attached the image")
|
| 271 |
# Instance of MIMEBase and named as p
|
| 272 |
part = MIMEBase('application', 'octet-stream')
|
| 273 |
|
|
|
|
| 304 |
server.sendmail(sender_email, recipient_email, text)
|
| 305 |
server.quit()
|
| 306 |
|
| 307 |
+
except Exception as error:
|
| 308 |
print(error)
|
| 309 |
|
| 310 |
# return 1
|
|
|
|
| 327 |
|
| 328 |
|
| 329 |
def inventory_report(question):
|
| 330 |
+
#print(question)
|
| 331 |
|
| 332 |
|
| 333 |
|
|
|
|
| 341 |
else:
|
| 342 |
return "warehouse name not found"
|
| 343 |
|
| 344 |
+
#print(apis[0]["url"])
|
| 345 |
+
#print(apis[0]["params"])
|
| 346 |
data = make_api_request(apis[0]["url"], apis[0]["params"])
|
| 347 |
+
#print(warehouse_name)
|
| 348 |
if data:
|
| 349 |
+
#print(data)
|
| 350 |
# Extracting the id for the warehouse with the name "WH"
|
| 351 |
warehouse_id = next((item['id'] for item in data['result'] if item['name'] == warehouse_name), None)
|
| 352 |
+
#print(warehouse_id)
|
| 353 |
if warehouse_id is None:
|
| 354 |
return "Please provide a warehouse name available in the database."
|
| 355 |
# print(f"The id for the warehouse named {name} is: {warehouse_id}")
|
|
|
|
| 518 |
user_question = "Could not request results from Google Speech Recognition service."
|
| 519 |
|
| 520 |
while iterations < max_iterations:
|
| 521 |
+
#print(user_question)
|
| 522 |
"""if "send email to" in user_question:
|
| 523 |
email_match = re.search(r"send email to ([\w\.-]+@[\w\.-]+)", user_question)
|
| 524 |
if email_match:
|
|
|
|
| 528 |
"""
|
| 529 |
|
| 530 |
response = agent_executor.invoke({"input": user_question}, config={"callbacks": [langfuse_handler]})
|
| 531 |
+
#print("name")
|
| 532 |
+
#print(warehouse_name)
|
| 533 |
if isinstance(response, dict):
|
| 534 |
response_text = response.get("output", "")
|
| 535 |
else:
|
|
|
|
| 537 |
if "invalid" not in response_text.lower():
|
| 538 |
break
|
| 539 |
iterations += 1
|
| 540 |
+
#print(os.getenv("IMAGE_PATH"))
|
| 541 |
+
#print(response_text)
|
| 542 |
+
#print("validating. ")
|
| 543 |
+
#print(os.getenv("IMAGE_PATH") in response_text)
|
| 544 |
if iterations == max_iterations:
|
| 545 |
return "The agent could not generate a valid response within the iteration limit."
|
| 546 |
|
|
|
|
| 548 |
if os.getenv("IMAGE_PATH") in response_text:
|
| 549 |
# Open the image file
|
| 550 |
img = Image.open(os.getenv("IMAGE_PATH"))
|
| 551 |
+
#print(img)
|
| 552 |
# Convert the PIL Image to a base64 encoded string
|
| 553 |
buffered = BytesIO()
|
| 554 |
img.save(buffered, format="PNG")
|
| 555 |
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 556 |
+
#print(img_str)
|
| 557 |
img = f'<img src="data:image/png;base64,{img_str}" style="width:500px; height:400px;">'
|
| 558 |
# image = gr.Image(value=img_str)
|
| 559 |
chatbot.append((user_question, img))
|
|
|
|
| 562 |
match = re.search(email_pattern, user_question)
|
| 563 |
if match:
|
| 564 |
user_email = match.group() # Return the matched email
|
| 565 |
+
#print("user_email")
|
| 566 |
+
#print(user_email)
|
| 567 |
# email send
|
| 568 |
if user_email:
|
| 569 |
# Send email with the chart image attached
|