Update Gradio_UI.py
Browse files- Gradio_UI.py +31 -36
Gradio_UI.py
CHANGED
@@ -134,7 +134,6 @@ def stream_to_gradio(
|
|
134 |
actual_content_for_handling.save(tmp_file, format="PNG")
|
135 |
image_path_for_gradio = tmp_file.name
|
136 |
print(f"DEBUG Gradio: Saved PIL image to temp path: {image_path_for_gradio}")
|
137 |
-
# MODIFIED: Yield tuple (filepath, alt_text)
|
138 |
yield {"role": "assistant", "content": (image_path_for_gradio, "Generated Image")}
|
139 |
return
|
140 |
except Exception as e:
|
@@ -151,7 +150,6 @@ def stream_to_gradio(
|
|
151 |
image_path = final_answer_processed.to_string()
|
152 |
print(f"DEBUG Gradio (stream_to_gradio): final_answer_processed is AgentImage. Path: {image_path}")
|
153 |
if image_path and os.path.exists(image_path):
|
154 |
-
# MODIFIED: Yield tuple (filepath, alt_text)
|
155 |
yield {"role": "assistant", "content": (image_path, "Generated Image (from AgentImage)")}
|
156 |
else:
|
157 |
err_msg = f"Error: Image path from AgentImage ('{image_path}') not found or invalid."
|
@@ -161,7 +159,6 @@ def stream_to_gradio(
|
|
161 |
audio_path = final_answer_processed.to_string()
|
162 |
print(f"DEBUG Gradio (stream_to_gradio): AgentAudio path: {audio_path}")
|
163 |
if audio_path and os.path.exists(audio_path):
|
164 |
-
# MODIFIED: Yield tuple (filepath, alt_text) for consistency, though Gradio might just use path for audio
|
165 |
yield {"role": "assistant", "content": (audio_path, "Generated Audio")}
|
166 |
else:
|
167 |
err_msg = f"Error: Audio path from AgentAudio ('{audio_path}') not found"
|
@@ -182,40 +179,38 @@ class GradioUI:
|
|
182 |
os.makedirs(self.file_upload_folder, exist_ok=True)
|
183 |
self._latest_file_path_for_download = None
|
184 |
|
185 |
-
def _check_for_created_file(self):
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
if observations and isinstance(observations, str):
|
194 |
-
print(f"DEBUG Gradio UI: Log Entry Observations (for file check): {observations[:300]}") # Log more
|
195 |
-
|
196 |
-
# Regex to find paths specifically printed by our create_document tool
|
197 |
-
# It prints "Document created (docx): /path/to/file"
|
198 |
-
# or "Document created (txt): /path/to/file"
|
199 |
-
# or "Document converted to PDF: /path/to/file"
|
200 |
-
match = re.search(
|
201 |
-
r"(?:Document created \((?:docx|pdf|txt)\):|Document converted to PDF:)\s*(/tmp/[a-zA-Z0-9_]+/generated_document\.(?:docx|pdf|txt))",
|
202 |
-
observations
|
203 |
-
)
|
204 |
|
205 |
-
if
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
def interact_with_agent(self, prompt_text: str, current_chat_history: list):
|
220 |
print(f"DEBUG Gradio: interact_with_agent called with prompt: '{prompt_text}'")
|
221 |
print(f"DEBUG Gradio: Current chat history (input type {type(current_chat_history)}): {current_chat_history}")
|
|
|
134 |
actual_content_for_handling.save(tmp_file, format="PNG")
|
135 |
image_path_for_gradio = tmp_file.name
|
136 |
print(f"DEBUG Gradio: Saved PIL image to temp path: {image_path_for_gradio}")
|
|
|
137 |
yield {"role": "assistant", "content": (image_path_for_gradio, "Generated Image")}
|
138 |
return
|
139 |
except Exception as e:
|
|
|
150 |
image_path = final_answer_processed.to_string()
|
151 |
print(f"DEBUG Gradio (stream_to_gradio): final_answer_processed is AgentImage. Path: {image_path}")
|
152 |
if image_path and os.path.exists(image_path):
|
|
|
153 |
yield {"role": "assistant", "content": (image_path, "Generated Image (from AgentImage)")}
|
154 |
else:
|
155 |
err_msg = f"Error: Image path from AgentImage ('{image_path}') not found or invalid."
|
|
|
159 |
audio_path = final_answer_processed.to_string()
|
160 |
print(f"DEBUG Gradio (stream_to_gradio): AgentAudio path: {audio_path}")
|
161 |
if audio_path and os.path.exists(audio_path):
|
|
|
162 |
yield {"role": "assistant", "content": (audio_path, "Generated Audio")}
|
163 |
else:
|
164 |
err_msg = f"Error: Audio path from AgentAudio ('{audio_path}') not found"
|
|
|
179 |
os.makedirs(self.file_upload_folder, exist_ok=True)
|
180 |
self._latest_file_path_for_download = None
|
181 |
|
182 |
+
def _check_for_created_file(self): # Line 185 (ensure correct indentation below this line)
|
183 |
+
self._latest_file_path_for_download = None # Line 186 - START OF INDENTED BLOCK
|
184 |
+
if hasattr(self.agent, 'interaction_logs') and self.agent.interaction_logs:
|
185 |
+
print(f"DEBUG Gradio UI: Checking {len(self.agent.interaction_logs)} interaction log entries for created files.")
|
186 |
+
for log_entry in reversed(self.agent.interaction_logs):
|
187 |
+
if isinstance(log_entry, ActionStep):
|
188 |
+
observations = getattr(log_entry, 'observations', None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
|
190 |
+
if observations and isinstance(observations, str):
|
191 |
+
# This print statement is crucial for debugging this method
|
192 |
+
print(f"DEBUG Gradio UI (check_for_file): Log Entry Observations: '{observations[:300]}'")
|
193 |
+
|
194 |
+
# Refined regex to look for the specific output of your create_document tool
|
195 |
+
match = re.search(
|
196 |
+
r"(?:Document created \((?:docx|pdf|txt)\):|Document converted to PDF:)\s*(/tmp/[a-zA-Z0-9_]+/generated_document\.(?:docx|pdf|txt))",
|
197 |
+
observations
|
198 |
+
)
|
199 |
+
|
200 |
+
if match:
|
201 |
+
extracted_path = match.group(1)
|
202 |
+
normalized_path = os.path.normpath(extracted_path)
|
203 |
+
if os.path.exists(normalized_path):
|
204 |
+
self._latest_file_path_for_download = normalized_path
|
205 |
+
print(f"DEBUG Gradio UI: File path for download set (from create_document output): {self._latest_file_path_for_download}")
|
206 |
+
return True
|
207 |
+
else:
|
208 |
+
print(f"DEBUG Gradio UI: Path from create_document output ('{normalized_path}') does not exist.")
|
209 |
+
# else: # Optional: log if the specific pattern isn't found in this observation
|
210 |
+
# print(f"DEBUG Gradio UI: 'create_document' output pattern not found in this observation.")
|
211 |
+
print("DEBUG Gradio UI: No valid generated file path (from create_document) found in agent logs for download.")
|
212 |
+
return False # END OF INDENTED BLOCK for _check_for_created_file
|
213 |
+
|
214 |
def interact_with_agent(self, prompt_text: str, current_chat_history: list):
|
215 |
print(f"DEBUG Gradio: interact_with_agent called with prompt: '{prompt_text}'")
|
216 |
print(f"DEBUG Gradio: Current chat history (input type {type(current_chat_history)}): {current_chat_history}")
|