Spaces:
Sleeping
Sleeping
Update Gradio_UI.py
Browse files- Gradio_UI.py +213 -75
Gradio_UI.py
CHANGED
|
@@ -25,19 +25,159 @@ from smolagents.memory import MemoryStep
|
|
| 25 |
from smolagents.utils import _is_package_available
|
| 26 |
|
| 27 |
|
| 28 |
-
def pull_messages_from_step(
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
class GradioUI:
|
|
|
|
|
|
|
| 41 |
def __init__(self, agent: MultiStepAgent, file_upload_folder: str | None = None):
|
| 42 |
if not _is_package_available("gradio"):
|
| 43 |
raise ModuleNotFoundError(
|
|
@@ -45,28 +185,13 @@ class GradioUI:
|
|
| 45 |
)
|
| 46 |
self.agent = agent
|
| 47 |
self.file_upload_folder = file_upload_folder
|
| 48 |
-
if self.file_upload_folder is not None
|
| 49 |
-
os.
|
|
|
|
| 50 |
|
| 51 |
-
def interact_with_agent(self, prompt, messages
|
| 52 |
import gradio as gr
|
| 53 |
|
| 54 |
-
# If PDF file and prompt/question are provided, use document_qna_tool
|
| 55 |
-
if pdf_file_path and prompt:
|
| 56 |
-
# Run document QnA tool directly
|
| 57 |
-
for tool in self.agent.tools:
|
| 58 |
-
# Assuming your document QnA tool is named "document_qna_tool"
|
| 59 |
-
if hasattr(tool, "name") and tool.name == "document_qna_tool":
|
| 60 |
-
try:
|
| 61 |
-
answer = tool.run(pdf_path=pdf_file_path, question=prompt)
|
| 62 |
-
except Exception as e:
|
| 63 |
-
answer = f"Error running Document QnA tool: {str(e)}"
|
| 64 |
-
messages.append(gr.ChatMessage(role="user", content=prompt))
|
| 65 |
-
messages.append(gr.ChatMessage(role="assistant", content=answer))
|
| 66 |
-
yield messages
|
| 67 |
-
return
|
| 68 |
-
|
| 69 |
-
# Otherwise fallback to normal chat interaction
|
| 70 |
messages.append(gr.ChatMessage(role="user", content=prompt))
|
| 71 |
yield messages
|
| 72 |
for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):
|
|
@@ -84,6 +209,9 @@ class GradioUI:
|
|
| 84 |
"text/plain",
|
| 85 |
],
|
| 86 |
):
|
|
|
|
|
|
|
|
|
|
| 87 |
import gradio as gr
|
| 88 |
|
| 89 |
if file is None:
|
|
@@ -97,43 +225,46 @@ class GradioUI:
|
|
| 97 |
if mime_type not in allowed_file_types:
|
| 98 |
return gr.Textbox("File type disallowed", visible=True), file_uploads_log, None
|
| 99 |
|
|
|
|
| 100 |
original_name = os.path.basename(file.name)
|
| 101 |
-
sanitized_name = re.sub(
|
|
|
|
|
|
|
| 102 |
|
| 103 |
type_to_ext = {}
|
| 104 |
for ext, t in mimetypes.types_map.items():
|
| 105 |
if t not in type_to_ext:
|
| 106 |
type_to_ext[t] = ext
|
| 107 |
|
|
|
|
| 108 |
sanitized_name = sanitized_name.split(".")[:-1]
|
| 109 |
sanitized_name.append("" + type_to_ext[mime_type])
|
| 110 |
sanitized_name = "".join(sanitized_name)
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
def launch(self, **kwargs):
|
| 130 |
import gradio as gr
|
| 131 |
-
|
| 132 |
with gr.Blocks(fill_height=True) as demo:
|
| 133 |
stored_messages = gr.State([])
|
| 134 |
file_uploads_log = gr.State([])
|
| 135 |
-
|
| 136 |
-
|
| 137 |
chatbot = gr.Chatbot(
|
| 138 |
label="Agent",
|
| 139 |
type="messages",
|
|
@@ -144,37 +275,44 @@ class GradioUI:
|
|
| 144 |
resizeable=True,
|
| 145 |
scale=1,
|
| 146 |
)
|
| 147 |
-
|
| 148 |
if self.file_upload_folder is not None:
|
| 149 |
-
upload_file = gr.File(label="Upload a
|
| 150 |
upload_status = gr.Textbox(label="Upload Status", interactive=False, visible=False)
|
| 151 |
-
|
| 152 |
upload_file.change(
|
| 153 |
self.upload_file,
|
| 154 |
-
|
| 155 |
-
|
| 156 |
)
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
text_input.submit(
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
)
|
| 179 |
-
|
| 180 |
demo.launch(debug=True, share=True, **kwargs)
|
|
|
|
| 25 |
from smolagents.utils import _is_package_available
|
| 26 |
|
| 27 |
|
| 28 |
+
def pull_messages_from_step(
|
| 29 |
+
step_log: MemoryStep,
|
| 30 |
+
):
|
| 31 |
+
"""Extract ChatMessage objects from agent steps with proper nesting"""
|
| 32 |
+
import gradio as gr
|
| 33 |
|
| 34 |
+
if isinstance(step_log, ActionStep):
|
| 35 |
+
# Output the step number
|
| 36 |
+
step_number = f"Step {step_log.step_number}" if step_log.step_number is not None else ""
|
| 37 |
+
yield gr.ChatMessage(role="assistant", content=f"**{step_number}**")
|
| 38 |
|
| 39 |
+
# First yield the thought/reasoning from the LLM
|
| 40 |
+
if hasattr(step_log, "model_output") and step_log.model_output is not None:
|
| 41 |
+
# Clean up the LLM output
|
| 42 |
+
model_output = step_log.model_output.strip()
|
| 43 |
+
# Remove any trailing <end_code> and extra backticks, handling multiple possible formats
|
| 44 |
+
model_output = re.sub(r"```\s*<end_code>", "```", model_output) # handles ```<end_code>
|
| 45 |
+
model_output = re.sub(r"<end_code>\s*```", "```", model_output) # handles <end_code>```
|
| 46 |
+
model_output = re.sub(r"```\s*\n\s*<end_code>", "```", model_output) # handles ```\n<end_code>
|
| 47 |
+
model_output = model_output.strip()
|
| 48 |
+
yield gr.ChatMessage(role="assistant", content=model_output)
|
| 49 |
+
|
| 50 |
+
# For tool calls, create a parent message
|
| 51 |
+
if hasattr(step_log, "tool_calls") and step_log.tool_calls is not None:
|
| 52 |
+
first_tool_call = step_log.tool_calls[0]
|
| 53 |
+
used_code = first_tool_call.name == "python_interpreter"
|
| 54 |
+
parent_id = f"call_{len(step_log.tool_calls)}"
|
| 55 |
+
|
| 56 |
+
# Tool call becomes the parent message with timing info
|
| 57 |
+
# First we will handle arguments based on type
|
| 58 |
+
args = first_tool_call.arguments
|
| 59 |
+
if isinstance(args, dict):
|
| 60 |
+
content = str(args.get("answer", str(args)))
|
| 61 |
+
else:
|
| 62 |
+
content = str(args).strip()
|
| 63 |
+
|
| 64 |
+
if used_code:
|
| 65 |
+
# Clean up the content by removing any end code tags
|
| 66 |
+
content = re.sub(r"```.*?\n", "", content) # Remove existing code blocks
|
| 67 |
+
content = re.sub(r"\s*<end_code>\s*", "", content) # Remove end_code tags
|
| 68 |
+
content = content.strip()
|
| 69 |
+
if not content.startswith("```python"):
|
| 70 |
+
content = f"```python\n{content}\n```"
|
| 71 |
+
|
| 72 |
+
parent_message_tool = gr.ChatMessage(
|
| 73 |
+
role="assistant",
|
| 74 |
+
content=content,
|
| 75 |
+
metadata={
|
| 76 |
+
"title": f"🛠️ Used tool {first_tool_call.name}",
|
| 77 |
+
"id": parent_id,
|
| 78 |
+
"status": "pending",
|
| 79 |
+
},
|
| 80 |
+
)
|
| 81 |
+
yield parent_message_tool
|
| 82 |
+
|
| 83 |
+
# Nesting execution logs under the tool call if they exist
|
| 84 |
+
if hasattr(step_log, "observations") and (
|
| 85 |
+
step_log.observations is not None and step_log.observations.strip()
|
| 86 |
+
): # Only yield execution logs if there's actual content
|
| 87 |
+
log_content = step_log.observations.strip()
|
| 88 |
+
if log_content:
|
| 89 |
+
log_content = re.sub(r"^Execution logs:\s*", "", log_content)
|
| 90 |
+
yield gr.ChatMessage(
|
| 91 |
+
role="assistant",
|
| 92 |
+
content=f"{log_content}",
|
| 93 |
+
metadata={"title": "📝 Execution Logs", "parent_id": parent_id, "status": "done"},
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# Nesting any errors under the tool call
|
| 97 |
+
if hasattr(step_log, "error") and step_log.error is not None:
|
| 98 |
+
yield gr.ChatMessage(
|
| 99 |
+
role="assistant",
|
| 100 |
+
content=str(step_log.error),
|
| 101 |
+
metadata={"title": "💥 Error", "parent_id": parent_id, "status": "done"},
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
# Update parent message metadata to done status without yielding a new message
|
| 105 |
+
parent_message_tool.metadata["status"] = "done"
|
| 106 |
+
|
| 107 |
+
# Handle standalone errors but not from tool calls
|
| 108 |
+
elif hasattr(step_log, "error") and step_log.error is not None:
|
| 109 |
+
yield gr.ChatMessage(role="assistant", content=str(step_log.error), metadata={"title": "💥 Error"})
|
| 110 |
+
|
| 111 |
+
# Calculate duration and token information
|
| 112 |
+
step_footnote = f"{step_number}"
|
| 113 |
+
if hasattr(step_log, "input_token_count") and hasattr(step_log, "output_token_count"):
|
| 114 |
+
token_str = (
|
| 115 |
+
f" | Input-tokens:{step_log.input_token_count:,} | Output-tokens:{step_log.output_token_count:,}"
|
| 116 |
+
)
|
| 117 |
+
step_footnote += token_str
|
| 118 |
+
if hasattr(step_log, "duration"):
|
| 119 |
+
step_duration = f" | Duration: {round(float(step_log.duration), 2)}" if step_log.duration else None
|
| 120 |
+
step_footnote += step_duration
|
| 121 |
+
step_footnote = f"""<span style="color: #bbbbc2; font-size: 12px;">{step_footnote}</span> """
|
| 122 |
+
yield gr.ChatMessage(role="assistant", content=f"{step_footnote}")
|
| 123 |
+
yield gr.ChatMessage(role="assistant", content="-----")
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def stream_to_gradio(
|
| 127 |
+
agent,
|
| 128 |
+
task: str,
|
| 129 |
+
reset_agent_memory: bool = False,
|
| 130 |
+
additional_args: Optional[dict] = None,
|
| 131 |
+
):
|
| 132 |
+
"""Runs an agent with the given task and streams the messages from the agent as gradio ChatMessages."""
|
| 133 |
+
if not _is_package_available("gradio"):
|
| 134 |
+
raise ModuleNotFoundError(
|
| 135 |
+
"Please install 'gradio' extra to use the GradioUI: `pip install 'smolagents[gradio]'`"
|
| 136 |
+
)
|
| 137 |
+
import gradio as gr
|
| 138 |
+
|
| 139 |
+
total_input_tokens = 0
|
| 140 |
+
total_output_tokens = 0
|
| 141 |
+
|
| 142 |
+
for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
|
| 143 |
+
# Track tokens if model provides them
|
| 144 |
+
if hasattr(agent.model, "last_input_token_count"):
|
| 145 |
+
total_input_tokens += agent.model.last_input_token_count
|
| 146 |
+
total_output_tokens += agent.model.last_output_token_count
|
| 147 |
+
if isinstance(step_log, ActionStep):
|
| 148 |
+
step_log.input_token_count = agent.model.last_input_token_count
|
| 149 |
+
step_log.output_token_count = agent.model.last_output_token_count
|
| 150 |
+
|
| 151 |
+
for message in pull_messages_from_step(
|
| 152 |
+
step_log,
|
| 153 |
+
):
|
| 154 |
+
yield message
|
| 155 |
+
|
| 156 |
+
final_answer = step_log # Last log is the run's final_answer
|
| 157 |
+
final_answer = handle_agent_output_types(final_answer)
|
| 158 |
+
|
| 159 |
+
if isinstance(final_answer, AgentText):
|
| 160 |
+
yield gr.ChatMessage(
|
| 161 |
+
role="assistant",
|
| 162 |
+
content=f"**Final answer:**\n{final_answer.to_string()}\n",
|
| 163 |
+
)
|
| 164 |
+
elif isinstance(final_answer, AgentImage):
|
| 165 |
+
yield gr.ChatMessage(
|
| 166 |
+
role="assistant",
|
| 167 |
+
content={"path": final_answer.to_string(), "mime_type": "image/png"},
|
| 168 |
+
)
|
| 169 |
+
elif isinstance(final_answer, AgentAudio):
|
| 170 |
+
yield gr.ChatMessage(
|
| 171 |
+
role="assistant",
|
| 172 |
+
content={"path": final_answer.to_string(), "mime_type": "audio/wav"},
|
| 173 |
+
)
|
| 174 |
+
else:
|
| 175 |
+
yield gr.ChatMessage(role="assistant", content=f"**Final answer:** {str(final_answer)}")
|
| 176 |
|
| 177 |
|
| 178 |
class GradioUI:
|
| 179 |
+
"""A one-line interface to launch your agent in Gradio"""
|
| 180 |
+
|
| 181 |
def __init__(self, agent: MultiStepAgent, file_upload_folder: str | None = None):
|
| 182 |
if not _is_package_available("gradio"):
|
| 183 |
raise ModuleNotFoundError(
|
|
|
|
| 185 |
)
|
| 186 |
self.agent = agent
|
| 187 |
self.file_upload_folder = file_upload_folder
|
| 188 |
+
if self.file_upload_folder is not None:
|
| 189 |
+
if not os.path.exists(file_upload_folder):
|
| 190 |
+
os.mkdir(file_upload_folder)
|
| 191 |
|
| 192 |
+
def interact_with_agent(self, prompt, messages):
|
| 193 |
import gradio as gr
|
| 194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
messages.append(gr.ChatMessage(role="user", content=prompt))
|
| 196 |
yield messages
|
| 197 |
for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):
|
|
|
|
| 209 |
"text/plain",
|
| 210 |
],
|
| 211 |
):
|
| 212 |
+
"""
|
| 213 |
+
Handle file uploads, default allowed types are .pdf, .docx, and .txt
|
| 214 |
+
"""
|
| 215 |
import gradio as gr
|
| 216 |
|
| 217 |
if file is None:
|
|
|
|
| 225 |
if mime_type not in allowed_file_types:
|
| 226 |
return gr.Textbox("File type disallowed", visible=True), file_uploads_log, None
|
| 227 |
|
| 228 |
+
# Sanitize file name
|
| 229 |
original_name = os.path.basename(file.name)
|
| 230 |
+
sanitized_name = re.sub(
|
| 231 |
+
r"[^\w\-.]", "_", original_name
|
| 232 |
+
) # Replace any non-alphanumeric, non-dash, or non-dot characters with underscores
|
| 233 |
|
| 234 |
type_to_ext = {}
|
| 235 |
for ext, t in mimetypes.types_map.items():
|
| 236 |
if t not in type_to_ext:
|
| 237 |
type_to_ext[t] = ext
|
| 238 |
|
| 239 |
+
# Ensure the extension correlates to the mime type
|
| 240 |
sanitized_name = sanitized_name.split(".")[:-1]
|
| 241 |
sanitized_name.append("" + type_to_ext[mime_type])
|
| 242 |
sanitized_name = "".join(sanitized_name)
|
| 243 |
|
| 244 |
+
# Save the uploaded file to the specified folder
|
| 245 |
+
output_path = os.path.join(self.file_upload_folder, sanitized_name)
|
| 246 |
+
shutil.copyfile(file.name, output_path)
|
| 247 |
+
file_uploads_log.append(output_path)
|
| 248 |
+
|
| 249 |
+
return gr.Textbox(f"Uploaded: {sanitized_name}", visible=True), file_uploads_log, output_path
|
| 250 |
+
|
| 251 |
+
def log_user_message(self, message, file_uploads_log, messages):
|
| 252 |
+
import gradio as gr
|
| 253 |
+
|
| 254 |
+
if not messages:
|
| 255 |
+
messages = []
|
| 256 |
+
|
| 257 |
+
messages.append(gr.ChatMessage(role="user", content=message))
|
| 258 |
+
return messages, ""
|
| 259 |
+
|
|
|
|
| 260 |
def launch(self, **kwargs):
|
| 261 |
import gradio as gr
|
| 262 |
+
|
| 263 |
with gr.Blocks(fill_height=True) as demo:
|
| 264 |
stored_messages = gr.State([])
|
| 265 |
file_uploads_log = gr.State([])
|
| 266 |
+
current_file_path = gr.State(None)
|
| 267 |
+
|
| 268 |
chatbot = gr.Chatbot(
|
| 269 |
label="Agent",
|
| 270 |
type="messages",
|
|
|
|
| 275 |
resizeable=True,
|
| 276 |
scale=1,
|
| 277 |
)
|
| 278 |
+
|
| 279 |
if self.file_upload_folder is not None:
|
| 280 |
+
upload_file = gr.File(label="Upload a file")
|
| 281 |
upload_status = gr.Textbox(label="Upload Status", interactive=False, visible=False)
|
| 282 |
+
|
| 283 |
upload_file.change(
|
| 284 |
self.upload_file,
|
| 285 |
+
[upload_file, file_uploads_log],
|
| 286 |
+
[upload_status, file_uploads_log, current_file_path],
|
| 287 |
)
|
| 288 |
+
|
| 289 |
+
# New button to send the uploaded document to the agent
|
| 290 |
+
send_doc_btn = gr.Button("Send Document")
|
| 291 |
+
|
| 292 |
+
# Handler function for document sending
|
| 293 |
+
def send_document(file_path, messages):
|
| 294 |
+
if not file_path:
|
| 295 |
+
messages.append(gr.ChatMessage(role="assistant", content="No document uploaded."))
|
| 296 |
+
return messages
|
| 297 |
+
# Construct prompt with file path
|
| 298 |
+
prompt = f"Please process this document: {file_path}"
|
| 299 |
+
messages.append(gr.ChatMessage(role="user", content=prompt))
|
| 300 |
+
for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):
|
| 301 |
+
messages.append(msg)
|
| 302 |
+
yield messages
|
| 303 |
+
yield messages
|
| 304 |
+
|
| 305 |
+
send_doc_btn.click(
|
| 306 |
+
send_document,
|
| 307 |
+
inputs=[current_file_path, stored_messages],
|
| 308 |
+
outputs=chatbot,
|
| 309 |
+
)
|
| 310 |
+
|
| 311 |
+
text_input = gr.Textbox(lines=1, label="Chat Message")
|
| 312 |
text_input.submit(
|
| 313 |
+
self.log_user_message,
|
| 314 |
+
[text_input, file_uploads_log, stored_messages],
|
| 315 |
+
[stored_messages, text_input],
|
| 316 |
+
).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
|
| 317 |
+
|
| 318 |
demo.launch(debug=True, share=True, **kwargs)
|