Spaces:
Running
Running
Update Gradio_UI.py
Browse files- Gradio_UI.py +53 -5
Gradio_UI.py
CHANGED
@@ -1,3 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
class GradioUI:
|
2 |
def __init__(self, agent, file_upload_folder: str = "./uploads"):
|
3 |
self.agent = agent
|
@@ -6,7 +34,6 @@ class GradioUI:
|
|
6 |
os.makedirs(self.file_upload_folder)
|
7 |
|
8 |
def interact_with_agent(self, prompt, messages):
|
9 |
-
import gradio as gr
|
10 |
messages.append(gr.ChatMessage(role="user", content=prompt))
|
11 |
yield messages
|
12 |
for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):
|
@@ -24,8 +51,6 @@ class GradioUI:
|
|
24 |
"text/plain",
|
25 |
],
|
26 |
):
|
27 |
-
import gradio as gr
|
28 |
-
|
29 |
if file is None:
|
30 |
return gr.Textbox("No file uploaded", visible=True), file_uploads_log
|
31 |
|
@@ -59,8 +84,6 @@ class GradioUI:
|
|
59 |
return gr.Textbox(f"File uploaded: {sanitized_name}", visible=True), file_uploads_log
|
60 |
|
61 |
def build_ui(self):
|
62 |
-
import gradio as gr
|
63 |
-
|
64 |
chatbot = gr.Chatbot()
|
65 |
msg = gr.Textbox(placeholder="Ask something...", label="Your message")
|
66 |
clear = gr.Button("Clear")
|
@@ -78,3 +101,28 @@ class GradioUI:
|
|
78 |
|
79 |
return demo
|
80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding=utf-8
|
3 |
+
# Copyright 2024 The HuggingFace Inc. team. All rights reserved.
|
4 |
+
#
|
5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6 |
+
# you may not use this file except in compliance with the License.
|
7 |
+
# You may obtain a copy of the License at
|
8 |
+
#
|
9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
#
|
11 |
+
# Unless required by applicable law or agreed to in writing, software
|
12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 |
+
# See the License for the specific language governing permissions and
|
15 |
+
# limitations under the License.
|
16 |
+
|
17 |
+
import mimetypes
|
18 |
+
import os
|
19 |
+
import re
|
20 |
+
from typing import Optional
|
21 |
+
|
22 |
+
import gradio as gr
|
23 |
+
from smolagents.agent_types import AgentAudio, AgentImage, AgentText, handle_agent_output_types
|
24 |
+
from smolagents.agents import ActionStep
|
25 |
+
from smolagents.memory import MemoryStep
|
26 |
+
from smolagents.utils import _is_package_available
|
27 |
+
|
28 |
+
|
29 |
class GradioUI:
|
30 |
def __init__(self, agent, file_upload_folder: str = "./uploads"):
|
31 |
self.agent = agent
|
|
|
34 |
os.makedirs(self.file_upload_folder)
|
35 |
|
36 |
def interact_with_agent(self, prompt, messages):
|
|
|
37 |
messages.append(gr.ChatMessage(role="user", content=prompt))
|
38 |
yield messages
|
39 |
for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):
|
|
|
51 |
"text/plain",
|
52 |
],
|
53 |
):
|
|
|
|
|
54 |
if file is None:
|
55 |
return gr.Textbox("No file uploaded", visible=True), file_uploads_log
|
56 |
|
|
|
84 |
return gr.Textbox(f"File uploaded: {sanitized_name}", visible=True), file_uploads_log
|
85 |
|
86 |
def build_ui(self):
|
|
|
|
|
87 |
chatbot = gr.Chatbot()
|
88 |
msg = gr.Textbox(placeholder="Ask something...", label="Your message")
|
89 |
clear = gr.Button("Clear")
|
|
|
101 |
|
102 |
return demo
|
103 |
|
104 |
+
|
105 |
+
def pull_messages_from_step(step_log: MemoryStep):
|
106 |
+
if isinstance(step_log, ActionStep):
|
107 |
+
step_number = f"Step {step_log.step_number}" if step_log.step_number is not None else ""
|
108 |
+
yield gr.ChatMessage(role="assistant", content=f"**{step_number}**")
|
109 |
+
|
110 |
+
if hasattr(step_log, "model_output") and step_log.model_output is not None:
|
111 |
+
model_output = step_log.model_output.strip()
|
112 |
+
model_output = re.sub(r"```\s*<end_code>", "```", model_output)
|
113 |
+
model_output = re.sub(r"<end_code>\s*```", "```", model_output)
|
114 |
+
model_output = re.sub(r"```\s*\n\s*<end_code>", "```", model_output)
|
115 |
+
model_output = model_output.strip()
|
116 |
+
yield gr.ChatMessage(role="assistant", content=model_output)
|
117 |
+
|
118 |
+
if hasattr(step_log, "tool_calls") and step_log.tool_calls is not None:
|
119 |
+
first_tool_call = step_log.tool_calls[0]
|
120 |
+
used_code = first_tool_call.name == "python_interpreter"
|
121 |
+
parent_id = f"call_{len(step_log.tool_calls)}"
|
122 |
+
args = first_tool_call.arguments
|
123 |
+
content = str(args.get("answer", str(args))) if isinstance(args, dict) else str(args).strip()
|
124 |
+
|
125 |
+
if used_code:
|
126 |
+
content = re.sub(r"```.*?\n", "", content)
|
127 |
+
content = re.sub(r"\s*<end_code>\s*", "", content).strip()
|
128 |
+
if not content.startswith("```python"):
|