GuglielmoTor commited on
Commit
409c5be
·
verified ·
1 Parent(s): b3904a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -20
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
- # If there's an image, display it. Text can accompany it or be None.
744
- # Ensure text_response is a string or None for the tuple.
745
- text_for_display = agent_text_response if isinstance(agent_text_response, str) else None
746
- if not text_for_display and agent_text_response is not None: # e.g. if text_response was empty string
747
- text_for_display = str(agent_text_response)
 
 
 
 
 
 
748
 
749
- bot_message_for_display = (text_for_display, agent_image_path)
750
- logging.info(f"Displaying image: {agent_image_path} with text: {text_for_display}")
751
- elif agent_text_response is not None: # Text only, or image path was invalid
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
- bot_message_for_display = "L'agente ha risposto, ma il contenuto non è visualizzabile."
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
- bot_message_for_display = ai_response_dict
761
  logging.warning(f"AI response was a plain string: {ai_response_dict}")
 
762
  else:
763
- bot_message_for_display = f"Error: Agent returned an unexpected data type: {type(ai_response_dict)}."
764
  logging.error(f"Unexpected AI response type: {type(ai_response_dict)}, content: {ai_response_dict}")
 
 
 
 
 
 
 
 
 
 
765
 
766
- # Use pending_history which already has the user message with None for bot response
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)