Update Gradio_UI.py
Browse files- Gradio_UI.py +38 -27
Gradio_UI.py
CHANGED
|
@@ -179,8 +179,8 @@ class GradioUI:
|
|
| 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):
|
| 183 |
-
self._latest_file_path_for_download = None
|
| 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):
|
|
@@ -188,32 +188,43 @@ class GradioUI:
|
|
| 188 |
observations = getattr(log_entry, 'observations', None)
|
| 189 |
|
| 190 |
if observations and isinstance(observations, str):
|
| 191 |
-
#
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
observations
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
print("DEBUG Gradio UI: No valid generated file path (from create_document) found in agent logs for download.")
|
| 212 |
-
return False
|
| 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}")
|
| 217 |
|
| 218 |
updated_chat_history = current_chat_history + [{"role": "user", "content": prompt_text}]
|
| 219 |
|
|
@@ -224,11 +235,11 @@ class GradioUI:
|
|
| 224 |
agent_responses_for_history.append(msg_dict)
|
| 225 |
yield updated_chat_history + agent_responses_for_history, gr.update(visible=False), gr.update(value=None, visible=False)
|
| 226 |
|
| 227 |
-
file_found = self._check_for_created_file()
|
| 228 |
|
| 229 |
final_chat_display = updated_chat_history + agent_responses_for_history
|
| 230 |
-
print(f"DEBUG Gradio: Final chat history for display: {len(final_chat_display)} messages.")
|
| 231 |
-
yield final_chat_display, gr.update(visible=file_found), gr.update(value=None, visible=False)
|
| 232 |
|
| 233 |
def upload_file(self, file, file_uploads_log_state):
|
| 234 |
if file is None:
|
|
|
|
| 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):
|
| 183 |
+
self._latest_file_path_for_download = None
|
| 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):
|
|
|
|
| 188 |
observations = getattr(log_entry, 'observations', None)
|
| 189 |
|
| 190 |
if observations and isinstance(observations, str):
|
| 191 |
+
# Log the full observations for the relevant step to help debug the regex
|
| 192 |
+
# We are looking for the output of python_interpreter which contains create_document's print
|
| 193 |
+
tool_calls = getattr(log_entry, 'tool_calls', [])
|
| 194 |
+
is_python_interpreter_step = any(tc.name == "python_interpreter" for tc in tool_calls)
|
| 195 |
+
|
| 196 |
+
if is_python_interpreter_step: # Only parse observations from python_interpreter
|
| 197 |
+
print(f"DEBUG Gradio UI (_check_for_file): Python Interpreter Observations: '''{observations}'''")
|
| 198 |
+
|
| 199 |
+
# Regex to find paths specifically printed by our create_document tool
|
| 200 |
+
# This pattern should match:
|
| 201 |
+
# "Document created (docx): /tmp/random/generated_document.docx"
|
| 202 |
+
# "Document created (txt): /tmp/random/generated_document.txt"
|
| 203 |
+
# "Document converted to PDF: /tmp/random/generated_document.pdf"
|
| 204 |
+
# It allows for optional "Execution logs:" prefix and surrounding newlines/whitespace.
|
| 205 |
+
match = re.search(
|
| 206 |
+
r"(?:Document created \((?:docx|pdf|txt)\):|Document converted to PDF:)\s*(/tmp/[a-zA-Z0-9_]+/generated_document\.(?:docx|pdf|txt))",
|
| 207 |
+
observations,
|
| 208 |
+
re.MULTILINE # Important if observations string has multiple lines
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
if match:
|
| 212 |
+
extracted_path = match.group(1)
|
| 213 |
+
normalized_path = os.path.normpath(extracted_path)
|
| 214 |
+
if os.path.exists(normalized_path):
|
| 215 |
+
self._latest_file_path_for_download = normalized_path
|
| 216 |
+
print(f"DEBUG Gradio UI: File path for download SET: {self._latest_file_path_for_download}")
|
| 217 |
+
return True # File found and path set
|
| 218 |
+
else:
|
| 219 |
+
print(f"DEBUG Gradio UI: Path from create_document output ('{normalized_path}') does not exist.")
|
| 220 |
+
# else: # Optional: log if the specific pattern isn't found in this observation
|
| 221 |
+
# print(f"DEBUG Gradio UI: 'create_document' output pattern not found in this observation block.")
|
| 222 |
print("DEBUG Gradio UI: No valid generated file path (from create_document) found in agent logs for download.")
|
| 223 |
+
return False
|
| 224 |
|
| 225 |
def interact_with_agent(self, prompt_text: str, current_chat_history: list):
|
| 226 |
print(f"DEBUG Gradio: interact_with_agent called with prompt: '{prompt_text}'")
|
| 227 |
+
# print(f"DEBUG Gradio: Current chat history (input type {type(current_chat_history)}): {current_chat_history}")
|
| 228 |
|
| 229 |
updated_chat_history = current_chat_history + [{"role": "user", "content": prompt_text}]
|
| 230 |
|
|
|
|
| 235 |
agent_responses_for_history.append(msg_dict)
|
| 236 |
yield updated_chat_history + agent_responses_for_history, gr.update(visible=False), gr.update(value=None, visible=False)
|
| 237 |
|
| 238 |
+
file_found = self._check_for_created_file() # This call is crucial
|
| 239 |
|
| 240 |
final_chat_display = updated_chat_history + agent_responses_for_history
|
| 241 |
+
print(f"DEBUG Gradio: Final chat history for display: {len(final_chat_display)} messages. File found for download: {file_found}")
|
| 242 |
+
yield final_chat_display, gr.update(visible=file_found), gr.update(value=None, visible=False) # Update download button visibility
|
| 243 |
|
| 244 |
def upload_file(self, file, file_uploads_log_state):
|
| 245 |
if file is None:
|