First_agent_template / Gradio_UI.py
mohammedelfeky-ai's picture
Update Gradio_UI.py
9b8d24a verified
raw
history blame
18.6 kB
#!/usr/bin/env python
# coding=utf-8
# Copyright 2024 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# import mimetypes # No longer needed if file upload is removed
import os
import re
# import shutil # No longer needed if file upload is removed
from typing import Optional
import tempfile
from PIL import Image as PILImage
from smolagents.agent_types import AgentAudio, AgentImage, AgentText, handle_agent_output_types
from smolagents.agents import ActionStep, MultiStepAgent
from smolagents.memory import MemoryStep
from smolagents.utils import _is_package_available
import gradio as gr
def pull_messages_from_step_dict(step_log: MemoryStep):
"""Extract messages as dicts for Gradio type='messages' Chatbot"""
if isinstance(step_log, ActionStep):
step_number_str = f"Step {step_log.step_number}" if step_log.step_number is not None else "Processing"
yield {"role": "assistant", "content": f"**{step_number_str}**"}
if hasattr(step_log, "model_output") and step_log.model_output is not None:
model_output = step_log.model_output.strip()
model_output = re.sub(r"```\s*<end_code>[\s\S]*|[\s\S]*<end_code>\s*```", "```", model_output, flags=re.DOTALL)
model_output = re.sub(r"<end_code>", "", model_output)
model_output = model_output.strip()
yield {"role": "assistant", "content": model_output}
if hasattr(step_log, "tool_calls") and step_log.tool_calls:
tc = step_log.tool_calls[0]
tool_info_md = f"🛠️ **Tool Used: {tc.name}**\n"
args = tc.arguments
if isinstance(args, dict):
args_str = str(args.get("answer", str(args)))
else:
args_str = str(args).strip()
if tc.name == "python_interpreter":
code_content = args_str
code_content = re.sub(r"^```python\s*\n?", "", code_content)
code_content = re.sub(r"\n?```\s*$", "", code_content)
code_content = re.sub(r"^\s*<end_code>\s*", "", code_content)
code_content = re.sub(r"\s*<end_code>\s*$", "", code_content)
code_content = code_content.strip()
tool_info_md += f"Executing Code:\n```python\n{code_content}\n```\n"
else:
tool_info_md += f"Arguments: `{args_str}`\n"
if hasattr(step_log, "observations") and step_log.observations and step_log.observations.strip():
obs_content = step_log.observations.strip()
obs_content = re.sub(r"^Execution logs:\s*", "", obs_content).strip()
if obs_content:
tool_info_md += f"📝 **Tool Output/Logs:**\n```text\n{obs_content}\n```\n"
if hasattr(step_log, "error") and step_log.error:
tool_info_md += f"💥 **Error:** {str(step_log.error)}\n"
yield {"role": "assistant", "content": tool_info_md.strip()}
elif hasattr(step_log, "error") and step_log.error:
yield {"role": "assistant", "content": f"💥 **Error:** {str(step_log.error)}"}
footnote_parts = []
if step_log.step_number is not None:
footnote_parts.append(f"Step {step_log.step_number}")
if hasattr(step_log, "duration") and step_log.duration is not None:
footnote_parts.append(f"Duration: {round(float(step_log.duration), 2)}s")
if hasattr(step_log, "input_token_count") and step_log.input_token_count is not None:
footnote_parts.append(f"InTokens: {step_log.input_token_count:,}")
if hasattr(step_log, "output_token_count") and step_log.output_token_count is not None:
footnote_parts.append(f"OutTokens: {step_log.output_token_count:,}")
if footnote_parts:
footnote_text = " | ".join(footnote_parts)
yield {"role": "assistant", "content": f"""<p style="color: #999; font-size: 0.8em; margin-top:0; margin-bottom:0;">{footnote_text}</p>"""}
yield {"role": "assistant", "content": "---"}
def stream_to_gradio(
agent,
task: str,
reset_agent_memory: bool = False,
additional_args: Optional[dict] = None,
):
if not _is_package_available("gradio"):
raise ModuleNotFoundError("Install 'gradio': `pip install 'smolagents[gradio]'`")
if hasattr(agent, 'interaction_logs'):
agent.interaction_logs.clear()
print("DEBUG Gradio: Cleared agent interaction_logs for new run.")
all_step_logs = []
for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
all_step_logs.append(step_log)
if hasattr(agent.model, "last_input_token_count") and agent.model.last_input_token_count is not None:
if isinstance(step_log, ActionStep):
step_log.input_token_count = agent.model.last_input_token_count
step_log.output_token_count = agent.model.last_output_token_count
for msg_dict in pull_messages_from_step_dict(step_log):
yield msg_dict
if not all_step_logs:
yield {"role": "assistant", "content": "Agent did not produce any output."}
return
final_answer_content = all_step_logs[-1]
actual_content_for_handling = final_answer_content
# Check if final_answer_content is a wrapper like FinalAnswerStep and extract the core content
if hasattr(final_answer_content, 'final_answer') and not isinstance(final_answer_content, (str, PILImage.Image, tuple)): # Added tuple to avoid unwrapping already formatted image content
actual_content_for_handling = final_answer_content.final_answer
print(f"DEBUG Gradio: Extracted actual_content_for_handling from FinalAnswerStep: {type(actual_content_for_handling)}")
# Priority 1: Handle raw PIL Image object for direct display
if isinstance(actual_content_for_handling, PILImage.Image):
print("DEBUG Gradio (stream_to_gradio): Actual content IS a raw PIL Image.")
try:
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file: # delete=False is important
actual_content_for_handling.save(tmp_file, format="PNG")
image_path_for_gradio = tmp_file.name
print(f"DEBUG Gradio: Saved PIL image to temp path: {image_path_for_gradio}")
yield {"role": "assistant", "content": (image_path_for_gradio, "Generated Image")}
return
except Exception as e:
print(f"DEBUG Gradio: Error saving extracted PIL image: {e}")
yield {"role": "assistant", "content": f"**Final Answer (Error displaying image):** {e}"}
return
# Priority 2: Use smolagents' type handling if not a raw PIL image
final_answer_processed = handle_agent_output_types(actual_content_for_handling)
print(f"DEBUG Gradio: final_answer_processed type after handle_agent_output_types: {type(final_answer_processed)}")
if isinstance(final_answer_processed, AgentText):
yield {"role": "assistant", "content": f"**Final Answer:**\n{final_answer_processed.to_string()}"}
elif isinstance(final_answer_processed, AgentImage):
image_path = final_answer_processed.to_string()
print(f"DEBUG Gradio (stream_to_gradio): final_answer_processed is AgentImage. Path: {image_path}")
if image_path and os.path.exists(image_path):
yield {"role": "assistant", "content": (image_path, "Generated Image (from AgentImage)")}
else:
err_msg = f"Error: Image path from AgentImage ('{image_path}') not found or invalid."
print(f"DEBUG Gradio: {err_msg}")
yield {"role": "assistant", "content": f"**Final Answer ({err_msg})**"}
elif isinstance(final_answer_processed, AgentAudio):
audio_path = final_answer_processed.to_string()
print(f"DEBUG Gradio (stream_to_gradio): AgentAudio path: {audio_path}")
if audio_path and os.path.exists(audio_path):
yield {"role": "assistant", "content": (audio_path, "Generated Audio")}
else:
err_msg = f"Error: Audio path from AgentAudio ('{audio_path}') not found"
print(f"DEBUG Gradio: {err_msg}")
yield {"role": "assistant", "content": f"**Final Answer ({err_msg})**"}
else:
# This will display the string representation of the object if not specifically handled
yield {"role": "assistant", "content": f"**Final Answer:**\n{str(final_answer_processed)}"}
class GradioUI:
def __init__(self, agent: MultiStepAgent, file_upload_folder: str | None = None): # file_upload_folder kept for potential future use
if not _is_package_available("gradio"):
raise ModuleNotFoundError("Install 'gradio': `pip install 'smolagents[gradio]'`")
self.agent = agent
# self.file_upload_folder = file_upload_folder # Commented out as per request to simplify
# if self.file_upload_folder is not None:
# if not os.path.exists(self.file_upload_folder):
# os.makedirs(self.file_upload_folder, exist_ok=True)
self.file_upload_folder = None # Explicitly disable for now
self._latest_file_path_for_download = None
def _check_for_created_file(self):
self._latest_file_path_for_download = None
if hasattr(self.agent, 'interaction_logs') and self.agent.interaction_logs:
print(f"DEBUG Gradio UI: Checking {len(self.agent.interaction_logs)} interaction log entries for created files.")
for log_entry in reversed(self.agent.interaction_logs):
if isinstance(log_entry, ActionStep):
observations = getattr(log_entry, 'observations', None)
tool_calls = getattr(log_entry, 'tool_calls', []) # Get tool calls if any
# Check if this step involved python_interpreter
is_python_interpreter_step = any(tc.name == "python_interpreter" for tc in tool_calls)
if is_python_interpreter_step and observations and isinstance(observations, str):
print(f"DEBUG Gradio UI (_check_for_file): Python Interpreter Observations: '''{observations[:500]}...'''") # Log snippet
# Regex to find paths specifically printed by our create_document tool
# This pattern expects: "Document created (docx): /tmp/random/generated_document.docx"
match = re.search(
# Capture group 1 is the prefix, group 2 is the path
r"(Document created \((?:docx|pdf|txt)\):|Document converted to PDF:)\s*(/tmp/[a-zA-Z0-9_]+/generated_document\.(?:docx|pdf|txt))",
observations,
re.MULTILINE
)
if match:
# extracted_prefix = match.group(1) # e.g., "Document created (docx):"
extracted_path = match.group(2) # The actual path
print(f"DEBUG Gradio UI: Regex matched. Extracted path: '{extracted_path}'")
normalized_path = os.path.normpath(extracted_path)
if os.path.exists(normalized_path):
self._latest_file_path_for_download = normalized_path
print(f"DEBUG Gradio UI: File path for download SET: {self._latest_file_path_for_download}")
return True
else:
print(f"DEBUG Gradio UI: Path from create_document output ('{normalized_path}') does not exist.")
# else:
# print(f"DEBUG Gradio UI: 'create_document' output pattern not found in this observation block.")
print("DEBUG Gradio UI: No valid generated file path (from create_document) found for download button.")
return False
def interact_with_agent(self, prompt_text: str, current_chat_history: list):
print(f"DEBUG Gradio: interact_with_agent called with prompt: '{prompt_text}'")
updated_chat_history = current_chat_history + [{"role": "user", "content": prompt_text}]
# Initial yield: show user message, hide download components until agent run is complete
yield updated_chat_history, gr.update(visible=False), gr.update(value=None, visible=False)
agent_responses_for_history = []
for msg_dict in stream_to_gradio(self.agent, task=prompt_text, reset_agent_memory=False):
agent_responses_for_history.append(msg_dict)
# Yield progressively to update chat, keep download components hidden during streaming
yield updated_chat_history + agent_responses_for_history, gr.update(visible=False), gr.update(value=None, visible=False)
# After all agent messages are processed and added to history
final_chat_display_content = updated_chat_history + agent_responses_for_history
# Now check for created files to decide visibility of download button
file_found_for_download = self._check_for_created_file()
print(f"DEBUG Gradio: Final chat history for display: {len(final_chat_display_content)} messages. File found for download button: {file_found_for_download}")
# Final yield: update chat, set visibility of download button, keep file display component hidden (it's shown on button click)
yield final_chat_display_content, gr.update(visible=file_found_for_download), gr.update(value=None, visible=False)
def log_user_message(self, text_input_value: str): # Removed current_file_uploads as upload is disabled
full_prompt = text_input_value
# if current_file_uploads: # This part is now disabled
# files_str = ", ".join([os.path.basename(f) for f in current_file_uploads])
# full_prompt += f"\n\n[Uploaded files for context: {files_str}]"
print(f"DEBUG Gradio: Prepared prompt for agent: {full_prompt[:300]}...")
return full_prompt, ""
def prepare_and_show_download_file(self):
if self._latest_file_path_for_download and os.path.exists(self._latest_file_path_for_download):
print(f"DEBUG Gradio UI: Preparing download for UI component: {self._latest_file_path_for_download}")
return gr.File.update(value=self._latest_file_path_for_download,
label=os.path.basename(self._latest_file_path_for_download),
visible=True)
else:
print("DEBUG Gradio UI: No valid file path to prepare for download component.")
return gr.File.update(visible=False, value=None)
def launch(self, **kwargs):
with gr.Blocks(fill_height=True, theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue)) as demo:
# file_uploads_log_state = gr.State([]) # No longer needed as upload is disabled
prepared_prompt_for_agent = gr.State("")
gr.Markdown("## Smol Talk with your Agent")
with gr.Row(equal_height=False):
with gr.Column(scale=3): # Main chat column
chatbot_display = gr.Chatbot(
type="messages",
avatar_images=(None, "https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo-round.png"),
height=700,
show_copy_button=True,
bubble_full_width=False,
show_label=False
)
text_message_input = gr.Textbox(
lines=1,
placeholder="Type your message and press Enter, or Shift+Enter for new line...",
show_label=False
)
with gr.Column(scale=1): # Sidebar column
# --- File Upload Section Removed/Commented Out ---
# if self.file_upload_folder is not None:
# with gr.Accordion("File Upload", open=False):
# file_uploader = gr.File(label="Upload a supporting file (PDF, DOCX, TXT, JPG, PNG)")
# upload_status_text = gr.Textbox(label="Upload Status", interactive=False, lines=1)
# file_uploader.upload(
# self.upload_file,
# [file_uploader, file_uploads_log_state],
# [upload_status_text, file_uploads_log_state],
# )
with gr.Accordion("Generated File", open=True): # Keep this section
download_action_button = gr.Button("Download Generated File", visible=False)
file_download_display_component = gr.File(label="Downloadable Document", visible=False, interactive=False)
# Event Handling Chain for Text Submission
text_message_input.submit(
self.log_user_message,
[text_message_input], # Removed file_uploads_log_state from inputs
[prepared_prompt_for_agent, text_message_input]
).then(
self.interact_with_agent,
[prepared_prompt_for_agent, chatbot_display],
[chatbot_display, download_action_button, file_download_display_component]
)
download_action_button.click(
self.prepare_and_show_download_file,
[],
[file_download_display_component]
)
demo.launch(debug=True, share=kwargs.get("share", False), **kwargs)
__all__ = ["stream_to_gradio", "GradioUI"]