Spaces:
Build error
Build error
Update app.py
Browse files136 unmodified lines1371381391401411421431 unmodified line14514614714814915015115286 unmodified lines2392402412422432442452462 unmodified lines2492502512522532542552562572588 unmodified lines26726826927027127227330 unmodified lines30430530630730830931016 unmodified lines3273283293303313323337 unmodified lines341342343344345346347348349350351352353354355162 unmodified lines5185195205215225235243 unmodified lines5285295305315325335345351 unmodified line5375385395405415425435445455461 unmodified line5485495505515525535545555565575585592 unmodified lines562563564565566567568569136 unmodified linesdef database_tool(question):# print(question)sql_query = generate_sql_query(question)print(sql_query)return run_query(sql_query)1 unmodified line# print(question)base_url = os.getenv("ASN_API_URL")complete_url = f"{base_url}branchMaster.id=343&transactionUid={question}&userId=164&transactionType=ASN"print("complete url")print(complete_url)try:response = requests.get(complete_url)data = response.json()86 unmodified linesprint(f"Document data tool enter: {question}")# query_string = question['tags'][0] if 'tags' in question and question['tags'] else ""query_response = query_vector_store(vector_store, question, config={"callbacks": [langfuse_handler]})print("query****")print(query_response)# summarized_response = summarize_document(query_response)# print("summary***")# print(summarized_response)2 unmodified linesdef send_email_with_attachment(recipient_email, subject, body, image_data):try:print(recipient_email)print(subject)print(body)print(image_data)sender_email = os.getenv("EMAIL_SENDER")sender_password = os.getenv("EMAIL_PASSWORD")8 unmodified lines"""# Open the file to be sentattachment = open(attachment_path, "rb")print("Attached the image")# Instance of MIMEBase and named as ppart = MIMEBase('application', 'octet-stream')30 unmodified linesserver.sendmail(sender_email, recipient_email, text)server.quit()except error:print(error)# return 116 unmodified linesdef inventory_report(question):print(question)7 unmodified lineselse:return "warehouse name not found"print(apis[0]["url"])print(apis[0]["params"])data = make_api_request(apis[0]["url"], apis[0]["params"])print(warehouse_name)if data:print(data)# Extracting the id for the warehouse with the name "WH"warehouse_id = next((item['id'] for item in data['result'] if item['name'] == warehouse_name), None)print(warehouse_id)if warehouse_id is None:return "Please provide a warehouse name available in the database."# print(f"The id for the warehouse named {name} is: {warehouse_id}")162 unmodified linesuser_question = "Could not request results from Google Speech Recognition service."while iterations < max_iterations:print(user_question)"""if "send email to" in user_question:email_match = re.search(r"send email to ([\w\.-]+@[\w\.-]+)", user_question)if email_match:3 unmodified lines"""response = agent_executor.invoke({"input": user_question}, config={"callbacks": [langfuse_handler]})print("name")print(warehouse_name)if isinstance(response, dict):response_text = response.get("output", "")else:1 unmodified lineif "invalid" not in response_text.lower():breakiterations += 1print(os.getenv("IMAGE_PATH"))print(response_text)print("validating. ")print(os.getenv("IMAGE_PATH") in response_text)if iterations == max_iterations:return "The agent could not generate a valid response within the iteration limit."1 unmodified lineif os.getenv("IMAGE_PATH") in response_text:# Open the image fileimg = Image.open(os.getenv("IMAGE_PATH"))print(img)# Convert the PIL Image to a base64 encoded stringbuffered = BytesIO()img.save(buffered, format="PNG")img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")print(img_str)img = f'<img src="data:image/png;base64,{img_str}" style="width:500px; height:400px;">'# image = gr.Image(value=img_str)chatbot.append((user_question, img))2 unmodified linesmatch = re.search(email_pattern, user_question)if match:user_email = match.group() # Return the matched emailprint("user_email")print(user_email)# email sendif user_email:# Send email with the chart image attached136 unmodified lines1371381391401411421431 unmodified line14514614714814915015115286 unmodified lines2392402412422432442452462 unmodified lines2492502512522532542552562572588 unmodified lines26726826927027127227330 unmodified lines30430530630730830931016 unmodified lines3273283293303313323337 unmodified lines341342343344345346347348349350351352353354355162 unmodified lines5185195205215225235243 unmodified lines5285295305315325335345351 unmodified line5375385395405415425435445455461 unmodified line5485495505515525535545555565575585592 unmodified lines562563564565566567568569136 unmodified linesdef database_tool(question):# print(question)sql_query = generate_sql_query(question)#print(sql_query)return run_query(sql_query)1 unmodified line# print(question)base_url = os.getenv("ASN_API_URL")complete_url = f"{base_url}branchMaster.id=343&transactionUid={question}&userId=164&transactionType=ASN"#print("complete url")#print(complete_url)try:response = requests.get(complete_url)data = response.json()86 unmodified linesprint(f"Document data tool enter: {question}")# query_string = question['tags'][0] if 'tags' in question and question['tags'] else ""query_response = query_vector_store(vector_store, question, config={"callbacks": [langfuse_handler]})#print("query****")#print(query_response)# summarized_response = summarize_document(query_response)# print("summary***")# print(summarized_response)2 unmodified linesdef send_email_with_attachment(recipient_email, subject, body, image_data):try:#print(recipient_email)#print(subject)#print(body)#print(image_data)sender_email = os.getenv("EMAIL_SENDER")sender_password = os.getenv("EMAIL_PASSWORD")8 unmodified lines"""# Open the file to be sentattachment = open(attachment_path, "rb")#print("Attached the image")# Instance of MIMEBase and named as ppart = MIMEBase('application', 'octet-stream')30 unmodified linesserver.sendmail(sender_email, recipient_email, text)server.quit()except Exception as error:print(error)# return 116 unmodified linesdef inventory_report(question):#print(question)7 unmodified lineselse:return "warehouse name not found"#print(apis[0]["url"])#print(apis[0]["params"])data = make_api_request(apis[0]["url"], apis[0]["params"])#print(warehouse_name)if data:#print(data)# Extracting the id for the warehouse with the name "WH"warehouse_id = next((item['id'] for item in data['result'] if item['name'] == warehouse_name), None)#print(warehouse_id)if warehouse_id is None:return "Please provide a warehouse name available in the database."# print(f"The id for the warehouse named {name} is: {warehouse_id}")162 unmodified linesuser_question = "Could not request results from Google Speech Recognition service."while iterations < max_iterations:#print(user_question)"""if "send email to" in user_question:email_match = re.search(r"send email to ([\w\.-]+@[\w\.-]+)", user_question)if email_match:3 unmodified lines"""response = agent_executor.invoke({"input": user_question}, config={"callbacks": [langfuse_handler]})#print("name")#print(warehouse_name)if isinstance(response, dict):response_text = response.get("output", "")else:1 unmodified lineif "invalid" not in response_text.lower():breakiterations += 1#print(os.getenv("IMAGE_PATH"))#print(response_text)#print("validating. ")#print(os.getenv("IMAGE_PATH") in response_text)if iterations == max_iterations:return "The agent could not generate a valid response within the iteration limit."1 unmodified lineif os.getenv("IMAGE_PATH") in response_text:# Open the image fileimg = Image.open(os.getenv("IMAGE_PATH"))#print(img)# Convert the PIL Image to a base64 encoded stringbuffered = BytesIO()img.save(buffered, format="PNG")img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")#print(img_str)img = f'<img src="data:image/png;base64,{img_str}" style="width:500px; height:400px;">'# image = gr.Image(value=img_str)chatbot.append((user_question, img))2 unmodified linesmatch = re.search(email_pattern, user_question)if match:user_email = match.group() # Return the matched email#print("user_email")#print(user_email)# email sendif user_email:# Send email with the chart image attached
app.py+28-28