Spaces:
Sleeping
Sleeping
oo
Browse files- .gradio/certificate.pem +31 -0
- Gradio_UI.py +26 -9
- __pycache__/Gradio_UI.cpython-313.pyc +0 -0
- app.py +6 -3
- requirements.txt +4 -1
- tools/__pycache__/final_answer.cpython-313.pyc +0 -0
- tools/__pycache__/visit_webpage.cpython-313.pyc +0 -0
- tools/__pycache__/web_search.cpython-313.pyc +0 -0
- tools/visit_webpage.py +1 -3
- tools/web_search.py +10 -6
.gradio/certificate.pem
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-----BEGIN CERTIFICATE-----
|
| 2 |
+
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
|
| 3 |
+
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
|
| 4 |
+
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
|
| 5 |
+
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
|
| 6 |
+
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
|
| 7 |
+
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
|
| 8 |
+
h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
|
| 9 |
+
0TM8ukj13Xnfs7j/EvEh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DyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5
|
| 24 |
+
ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur
|
| 25 |
+
TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC
|
| 26 |
+
jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc
|
| 27 |
+
oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq
|
| 28 |
+
4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA
|
| 29 |
+
mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
|
| 30 |
+
emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
|
| 31 |
+
-----END CERTIFICATE-----
|
Gradio_UI.py
CHANGED
|
@@ -17,6 +17,7 @@ import mimetypes
|
|
| 17 |
import os
|
| 18 |
import re
|
| 19 |
import shutil
|
|
|
|
| 20 |
from typing import Optional
|
| 21 |
|
| 22 |
from smolagents.agent_types import AgentAudio, AgentImage, AgentText, handle_agent_output_types
|
|
@@ -153,8 +154,9 @@ def stream_to_gradio(
|
|
| 153 |
):
|
| 154 |
yield message
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
|
|
|
| 158 |
|
| 159 |
if isinstance(final_answer, AgentText):
|
| 160 |
yield gr.ChatMessage(
|
|
@@ -171,6 +173,15 @@ def stream_to_gradio(
|
|
| 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 |
|
|
@@ -260,20 +271,26 @@ class GradioUI:
|
|
| 260 |
|
| 261 |
def launch(self, **kwargs):
|
| 262 |
import gradio as gr
|
|
|
|
| 263 |
|
| 264 |
with gr.Blocks(fill_height=True) as demo:
|
| 265 |
stored_messages = gr.State([])
|
| 266 |
file_uploads_log = gr.State([])
|
| 267 |
-
|
| 268 |
-
label
|
| 269 |
-
type
|
| 270 |
-
avatar_images
|
| 271 |
None,
|
| 272 |
"https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/Alfred.png",
|
| 273 |
),
|
| 274 |
-
resizeable
|
| 275 |
-
scale
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
# If an upload folder is provided, enable the upload feature
|
| 278 |
if self.file_upload_folder is not None:
|
| 279 |
upload_file = gr.File(label="Upload a file")
|
|
|
|
| 17 |
import os
|
| 18 |
import re
|
| 19 |
import shutil
|
| 20 |
+
import tempfile
|
| 21 |
from typing import Optional
|
| 22 |
|
| 23 |
from smolagents.agent_types import AgentAudio, AgentImage, AgentText, handle_agent_output_types
|
|
|
|
| 154 |
):
|
| 155 |
yield message
|
| 156 |
|
| 157 |
+
# The last streamed item is typically a FinalAnswerStep, so unwrap its payload first.
|
| 158 |
+
raw_final_answer = getattr(step_log, "final_answer", step_log)
|
| 159 |
+
final_answer = handle_agent_output_types(raw_final_answer)
|
| 160 |
|
| 161 |
if isinstance(final_answer, AgentText):
|
| 162 |
yield gr.ChatMessage(
|
|
|
|
| 173 |
role="assistant",
|
| 174 |
content={"path": final_answer.to_string(), "mime_type": "audio/wav"},
|
| 175 |
)
|
| 176 |
+
elif hasattr(final_answer, "save"):
|
| 177 |
+
# Support PIL-style image objects by persisting them to a temp file for chat rendering.
|
| 178 |
+
fd, image_path = tempfile.mkstemp(prefix="smolagents_final_", suffix=".png")
|
| 179 |
+
os.close(fd)
|
| 180 |
+
final_answer.save(image_path)
|
| 181 |
+
yield gr.ChatMessage(
|
| 182 |
+
role="assistant",
|
| 183 |
+
content={"path": image_path, "mime_type": "image/png"},
|
| 184 |
+
)
|
| 185 |
else:
|
| 186 |
yield gr.ChatMessage(role="assistant", content=f"**Final answer:** {str(final_answer)}")
|
| 187 |
|
|
|
|
| 271 |
|
| 272 |
def launch(self, **kwargs):
|
| 273 |
import gradio as gr
|
| 274 |
+
import inspect
|
| 275 |
|
| 276 |
with gr.Blocks(fill_height=True) as demo:
|
| 277 |
stored_messages = gr.State([])
|
| 278 |
file_uploads_log = gr.State([])
|
| 279 |
+
chatbot_kwargs = {
|
| 280 |
+
"label": "Agent",
|
| 281 |
+
"type": "messages",
|
| 282 |
+
"avatar_images": (
|
| 283 |
None,
|
| 284 |
"https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/Alfred.png",
|
| 285 |
),
|
| 286 |
+
"resizeable": True,
|
| 287 |
+
"scale": 1,
|
| 288 |
+
}
|
| 289 |
+
supported_chatbot_args = set(inspect.signature(gr.Chatbot.__init__).parameters.keys())
|
| 290 |
+
filtered_chatbot_kwargs = {
|
| 291 |
+
key: value for key, value in chatbot_kwargs.items() if key in supported_chatbot_args
|
| 292 |
+
}
|
| 293 |
+
chatbot = gr.Chatbot(**filtered_chatbot_kwargs)
|
| 294 |
# If an upload folder is provided, enable the upload feature
|
| 295 |
if self.file_upload_folder is not None:
|
| 296 |
upload_file = gr.File(label="Upload a file")
|
__pycache__/Gradio_UI.cpython-313.pyc
ADDED
|
Binary file (13.3 kB). View file
|
|
|
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 2 |
import datetime
|
| 3 |
-
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
@@ -49,13 +50,15 @@ custom_role_conversions=None,
|
|
| 49 |
|
| 50 |
# Import tool from Hub
|
| 51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
|
|
|
|
|
|
| 52 |
|
| 53 |
with open("prompts.yaml", 'r') as stream:
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
|
|
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
from tools.final_answer import FinalAnswerTool
|
| 6 |
+
from tools.web_search import DuckDuckGoSearchTool
|
| 7 |
+
from tools.visit_webpage import VisitWebpageTool
|
| 8 |
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
|
|
|
| 50 |
|
| 51 |
# Import tool from Hub
|
| 52 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 53 |
+
web_search_tool = DuckDuckGoSearchTool()
|
| 54 |
+
visit_webpage_tool = VisitWebpageTool()
|
| 55 |
|
| 56 |
with open("prompts.yaml", 'r') as stream:
|
| 57 |
prompt_templates = yaml.safe_load(stream)
|
| 58 |
|
| 59 |
agent = CodeAgent(
|
| 60 |
model=model,
|
| 61 |
+
tools=[final_answer, web_search_tool, visit_webpage_tool, image_generation_tool], ## add your tools here (don't remove final answer)
|
| 62 |
max_steps=6,
|
| 63 |
verbosity_level=1,
|
| 64 |
grammar=None,
|
requirements.txt
CHANGED
|
@@ -1,5 +1,8 @@
|
|
| 1 |
markdownify
|
| 2 |
-
smolagents==1.13.0
|
| 3 |
requests
|
| 4 |
duckduckgo_search
|
|
|
|
| 5 |
pandas
|
|
|
|
|
|
|
|
|
| 1 |
markdownify
|
| 2 |
+
smolagents[gradio]==1.13.0
|
| 3 |
requests
|
| 4 |
duckduckgo_search
|
| 5 |
+
ddgs
|
| 6 |
pandas
|
| 7 |
+
pytz
|
| 8 |
+
PyYAML
|
tools/__pycache__/final_answer.cpython-313.pyc
ADDED
|
Binary file (1.08 kB). View file
|
|
|
tools/__pycache__/visit_webpage.cpython-313.pyc
ADDED
|
Binary file (2.52 kB). View file
|
|
|
tools/__pycache__/web_search.cpython-313.pyc
ADDED
|
Binary file (2.38 kB). View file
|
|
|
tools/visit_webpage.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
-
import
|
| 4 |
-
import markdownify
|
| 5 |
-
import smolagents
|
| 6 |
|
| 7 |
class VisitWebpageTool(Tool):
|
| 8 |
name = "visit_webpage"
|
|
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
+
import re
|
|
|
|
|
|
|
| 4 |
|
| 5 |
class VisitWebpageTool(Tool):
|
| 6 |
name = "visit_webpage"
|
tools/web_search.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
-
import duckduckgo_search
|
| 4 |
|
| 5 |
class DuckDuckGoSearchTool(Tool):
|
| 6 |
name = "web_search"
|
|
@@ -12,15 +11,20 @@ class DuckDuckGoSearchTool(Tool):
|
|
| 12 |
super().__init__()
|
| 13 |
self.max_results = max_results
|
| 14 |
try:
|
| 15 |
-
|
|
|
|
| 16 |
except ImportError as e:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
self.ddgs = DDGS(**kwargs)
|
| 21 |
|
| 22 |
def forward(self, query: str) -> str:
|
| 23 |
-
results = self.ddgs.text(query, max_results=self.max_results)
|
| 24 |
if len(results) == 0:
|
| 25 |
raise Exception("No results found! Try a less restrictive/shorter query.")
|
| 26 |
postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
|
|
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
|
|
|
| 3 |
|
| 4 |
class DuckDuckGoSearchTool(Tool):
|
| 5 |
name = "web_search"
|
|
|
|
| 11 |
super().__init__()
|
| 12 |
self.max_results = max_results
|
| 13 |
try:
|
| 14 |
+
# Preferred package name (new)
|
| 15 |
+
from ddgs import DDGS
|
| 16 |
except ImportError as e:
|
| 17 |
+
try:
|
| 18 |
+
# Backward-compatible fallback (old package name)
|
| 19 |
+
from duckduckgo_search import DDGS
|
| 20 |
+
except ImportError as inner_e:
|
| 21 |
+
raise ImportError(
|
| 22 |
+
"You must install package `ddgs` (or legacy `duckduckgo_search`) to run this tool: for instance run `pip install ddgs`."
|
| 23 |
+
) from inner_e
|
| 24 |
self.ddgs = DDGS(**kwargs)
|
| 25 |
|
| 26 |
def forward(self, query: str) -> str:
|
| 27 |
+
results = list(self.ddgs.text(query, max_results=self.max_results))
|
| 28 |
if len(results) == 0:
|
| 29 |
raise Exception("No results found! Try a less restrictive/shorter query.")
|
| 30 |
postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
|