Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -740,40 +740,52 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"),
|
|
740 |
logging.info(f"Agent response: text='{str(agent_text_response)[:100]}...', image_path='{agent_image_path}'")
|
741 |
|
742 |
if agent_image_path and os.path.exists(agent_image_path):
|
743 |
-
|
744 |
-
|
745 |
-
text_for_display
|
746 |
-
|
747 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
748 |
|
749 |
-
|
750 |
-
|
751 |
-
|
752 |
-
bot_message_for_display = agent_text_response
|
753 |
-
if agent_image_path: # Log if image path was given but not valid
|
754 |
logging.warning(f"Agent provided image_path '{agent_image_path}' but it does not exist or was not used.")
|
|
|
755 |
else:
|
756 |
-
|
757 |
logging.warning(f"AI response dict issue. Text: {agent_text_response}, Image Path: {agent_image_path}")
|
|
|
758 |
|
759 |
elif isinstance(ai_response_dict, str):
|
760 |
-
|
761 |
logging.warning(f"AI response was a plain string: {ai_response_dict}")
|
|
|
762 |
else:
|
763 |
-
|
764 |
logging.error(f"Unexpected AI response type: {type(ai_response_dict)}, content: {ai_response_dict}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
765 |
|
766 |
-
|
767 |
-
updated_history = pending_history[:-1] + [[user_message, bot_message_for_display]]
|
768 |
-
|
769 |
-
status_update_msg = "Stato Agente: Risposta ricevuta."
|
770 |
-
yield updated_history, updated_history, gr.update(value=""), gr.update(value=status_update_msg), gr.update(value=schemas_text_for_display)
|
771 |
return
|
772 |
-
|
773 |
except Exception as e:
|
774 |
logging.error(f"Error during EB Agent processing: {e}", exc_info=True)
|
775 |
error_msg_for_chat = f"# Errore dell'Agente AI:\n{type(e).__name__}: {str(e)}"
|
776 |
-
# Use pending_history which already has the user message
|
777 |
updated_history = pending_history[:-1] + [[user_message, error_msg_for_chat]]
|
778 |
status_update_msg = f"Stato Agente: Errore - {type(e).__name__}"
|
779 |
yield updated_history, updated_history, gr.update(value=""), gr.update(value=status_update_msg), gr.update(value=schemas_text_for_display)
|
|
|
740 |
logging.info(f"Agent response: text='{str(agent_text_response)[:100]}...', image_path='{agent_image_path}'")
|
741 |
|
742 |
if agent_image_path and os.path.exists(agent_image_path):
|
743 |
+
text_for_display = str(agent_text_response) if agent_text_response is not None else ""
|
744 |
+
|
745 |
+
if text_for_display.strip(): # If there's actual text, add it as the first bot message
|
746 |
+
bot_response_list_for_history.append(text_for_display)
|
747 |
+
logging.info(f"Adding text message to chat: '{text_for_display[:100]}...'")
|
748 |
+
|
749 |
+
# Add the image as a second bot message (or first if no text)
|
750 |
+
# Using a simple caption for the image tuple
|
751 |
+
bot_response_list_for_history.append(( "📊 Generated Chart", agent_image_path))
|
752 |
+
logging.info(f"Adding image message to chat: {agent_image_path}")
|
753 |
+
status_update_msg = "Stato Agente: Risposta con grafico ricevuta."
|
754 |
|
755 |
+
elif agent_text_response is not None:
|
756 |
+
bot_response_list_for_history.append(str(agent_text_response))
|
757 |
+
if agent_image_path:
|
|
|
|
|
758 |
logging.warning(f"Agent provided image_path '{agent_image_path}' but it does not exist or was not used.")
|
759 |
+
status_update_msg = "Stato Agente: Risposta testuale ricevuta."
|
760 |
else:
|
761 |
+
bot_response_list_for_history.append("L'agente ha risposto, ma il contenuto non è visualizzabile.")
|
762 |
logging.warning(f"AI response dict issue. Text: {agent_text_response}, Image Path: {agent_image_path}")
|
763 |
+
status_update_msg = "Stato Agente: Risposta con problemi."
|
764 |
|
765 |
elif isinstance(ai_response_dict, str):
|
766 |
+
bot_response_list_for_history.append(ai_response_dict)
|
767 |
logging.warning(f"AI response was a plain string: {ai_response_dict}")
|
768 |
+
status_update_msg = "Stato Agente: Risposta testuale ricevuta."
|
769 |
else:
|
770 |
+
bot_response_list_for_history.append(f"Error: Agent returned an unexpected data type: {type(ai_response_dict)}.")
|
771 |
logging.error(f"Unexpected AI response type: {type(ai_response_dict)}, content: {ai_response_dict}")
|
772 |
+
status_update_msg = "Stato Agente: Errore nella risposta."
|
773 |
+
|
774 |
+
# Construct the final history
|
775 |
+
# pending_history already has [user_message, None]
|
776 |
+
# We replace the None with the first bot response, then append any subsequent bot responses
|
777 |
+
final_updated_history = pending_history[:-1] # Remove the [user_message, None]
|
778 |
+
final_updated_history.append([user_message, bot_response_list_for_history[0] if bot_response_list_for_history else "No response content."])
|
779 |
+
if len(bot_response_list_for_history) > 1:
|
780 |
+
for bot_msg_item in bot_response_list_for_history[1:]:
|
781 |
+
final_updated_history.append([None, bot_msg_item]) # Subsequent messages from bot have None for user part
|
782 |
|
783 |
+
yield final_updated_history, final_updated_history, gr.update(value=""), gr.update(value=status_update_msg), gr.update(value=schemas_text_for_display)
|
|
|
|
|
|
|
|
|
784 |
return
|
785 |
+
|
786 |
except Exception as e:
|
787 |
logging.error(f"Error during EB Agent processing: {e}", exc_info=True)
|
788 |
error_msg_for_chat = f"# Errore dell'Agente AI:\n{type(e).__name__}: {str(e)}"
|
|
|
789 |
updated_history = pending_history[:-1] + [[user_message, error_msg_for_chat]]
|
790 |
status_update_msg = f"Stato Agente: Errore - {type(e).__name__}"
|
791 |
yield updated_history, updated_history, gr.update(value=""), gr.update(value=status_update_msg), gr.update(value=schemas_text_for_display)
|