Spaces:
Build error
Build error
Update app.py
Browse files496 unmodified lines49749849950050150250323 unmodified lines5275285295305315325335345351 unmodified line5375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785792 unmodified lines58258358458558658758858959013 unmodified lines604605606607608609496 unmodified linesdef answer_question(user_question, chatbot, audio=None):global iterationsiterations = 0# Ensure the temporary chart directory exists23 unmodified linesexcept sr.RequestError:user_question = "Could not request results from Google Speech Recognition service."while iterations < max_iterations:"""if "send email to" in user_question:email_match = re.search(r"send email to ([\w\.-]+@[\w\.-]+)", user_question)if email_match:user_email = email_match.group(1).strip()1 unmodified lineuser_question = f"{user_question}:{user_email}""""response = agent_executor.invoke({"input": user_question}, config={"callbacks": [langfuse_handler]})if isinstance(response, dict):response_text = response.get("output", "")else:response_text = responseif "invalid" not in response_text.lower():breakiterations += 1if iterations == max_iterations:return "The agent could not generate a valid response within the iteration limit."if os.getenv("IMAGE_PATH") in response_text:# Open the image fileimg = Image.open(os.getenv("IMAGE_PATH"))# Convert the PIL Image to a base64 encoded stringbuffered = BytesIO()img.save(buffered, format="PNG")img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")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))email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'match = re.search(email_pattern, user_question)if match:user_email = match.group() # Return the matched email# email sendif len(user_email)>0:import asyncio# Send email with the chart image attached#coro=send_email_with_attachment(recipient_email=user_email,subject="Warehouse Inventory Report",2 unmodified linesattachment_path=os.getenv("IMAGE_PATH"))#asyncio.run(coro)if "send email to" in user_question:try:os.remove(img) # Clean up the temporary image file13 unmodified lines# response_text = response_text.replace('\n', ' ').replace(' ', ' ').strip()# return response_textcss = """.gradio-container {width: 90%; /* Set the width as a percentage of the viewport */496 unmodified lines49749849950050150223 unmodified lines5265275285295305315325335341 unmodified line5365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745752 unmodified lines57857958058158258313 unmodified lines597598599600601602603496 unmodified linesdef answer_question(user_question, chatbot, audio=None):global iterationsiterations = 0# Ensure the temporary chart directory exists23 unmodified linesexcept sr.RequestError:user_question = "Could not request results from Google Speech Recognition service."while iterations < max_iterations:"""if "send email to" in user_question:email_match = re.search(r"send email to ([\w\.-]+@[\w\.-]+)", user_question)if email_match:user_email = email_match.group(1).strip()1 unmodified lineuser_question = f"{user_question}:{user_email}""""response = agent_executor.invoke({"input": user_question}, config={"callbacks": [langfuse_handler]})if isinstance(response, dict):response_text = response.get("output", "")else:response_text = responseif "invalid" not in response_text.lower():breakiterations += 1if iterations == max_iterations:return "The agent could not generate a valid response within the iteration limit."if os.getenv("IMAGE_PATH") in response_text:# Open the image fileimg = Image.open(os.getenv("IMAGE_PATH"))# Convert the PIL Image to a base64 encoded stringbuffered = BytesIO()img.save(buffered, format="PNG")img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")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))email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'match = re.search(email_pattern, user_question)if match:user_email = match.group() # Return the matched email# email sendif len(user_email) > 0:# Send email with the chart image attachedsend_email_with_attachment(recipient_email=user_email,subject="Warehouse Inventory Report",2 unmodified linesattachment_path=os.getenv("IMAGE_PATH"))if "send email to" in user_question:try:os.remove(img) # Clean up the temporary image file13 unmodified lines# response_text = response_text.replace('\n', ' ').replace(' ', ' ').strip()# return response_textcss = """.gradio-container {width: 90%; /* Set the width as a percentage of the viewport */
app.py+20-26