Update app.py
Browse files
app.py
CHANGED
|
@@ -10,34 +10,26 @@ import gradio as gr
|
|
| 10 |
from PIL import Image
|
| 11 |
from io import BytesIO
|
| 12 |
import base64
|
| 13 |
-
from smolagents import load_tool
|
| 14 |
from typing import Any
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# def __init__(self, max_results=10, **kwargs):
|
| 23 |
-
# super().__init__()
|
| 24 |
-
# self.max_results = max_results
|
| 25 |
-
# try:
|
| 26 |
-
# from duckduckgo_search import DDGS
|
| 27 |
-
# except ImportError as e:
|
| 28 |
-
# raise ImportError(
|
| 29 |
-
# "You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`."
|
| 30 |
-
# ) from e
|
| 31 |
-
# self.ddgs = DDGS(**kwargs)
|
| 32 |
-
|
| 33 |
-
# def forward(self, query: str) -> str:
|
| 34 |
-
# results = self.ddgs.text(query, max_results=self.max_results)
|
| 35 |
-
# if len(results) == 0:
|
| 36 |
-
# raise Exception("No results found! Try a less restrictive/shorter query.")
|
| 37 |
-
# postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
|
| 38 |
-
# return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
|
| 39 |
-
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
model = InferenceClientModel("qwen/Qwen2.5-0.5B-Instruct",
|
| 42 |
max_tokens=512,
|
| 43 |
system_message="""
|
|
@@ -112,7 +104,7 @@ tools = [
|
|
| 112 |
name="image-captioning",
|
| 113 |
description="Generates a caption for an input image."
|
| 114 |
),
|
| 115 |
-
DuckDuckGoSearchTool(),
|
| 116 |
WikipediaSearchTool(),
|
| 117 |
PythonInterpreterTool(),
|
| 118 |
UserInputTool(),
|
|
@@ -155,7 +147,7 @@ class BasicAgent:
|
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
-
def run_agent_on_image(image):
|
| 159 |
try:
|
| 160 |
# Wrap image as expected by the agent tool
|
| 161 |
response = agent("Describe this image", inputs={"image": image})
|
|
@@ -189,7 +181,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 189 |
|
| 190 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 191 |
try:
|
| 192 |
-
agent = BasicAgent()
|
| 193 |
except Exception as e:
|
| 194 |
print(f"Error instantiating agent: {e}")
|
| 195 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 10 |
from PIL import Image
|
| 11 |
from io import BytesIO
|
| 12 |
import base64
|
|
|
|
| 13 |
from typing import Any
|
| 14 |
|
| 15 |
+
class DuckDuckGoSearchTool(Tool):
|
| 16 |
+
name = "web_search"
|
| 17 |
+
description = "Performs a DuckDuckGo web search."
|
| 18 |
+
inputs = {'query': {'type': 'string', 'description': 'Search query'}}
|
| 19 |
+
output_type = "string"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
def __init__(self, max_results=10, **kwargs):
|
| 22 |
+
super().__init__()
|
| 23 |
+
self.max_results = max_results
|
| 24 |
+
self.ddgs = DDGS(**kwargs)
|
| 25 |
+
|
| 26 |
+
def forward(self, query: str) -> str:
|
| 27 |
+
results = self.ddgs.text(query, max_results=self.max_results)
|
| 28 |
+
if not results:
|
| 29 |
+
return "No results found."
|
| 30 |
+
return "\n\n".join(
|
| 31 |
+
f"[{r['title']}]({r['href']})\n{r['body']}" for r in results
|
| 32 |
+
)
|
| 33 |
model = InferenceClientModel("qwen/Qwen2.5-0.5B-Instruct",
|
| 34 |
max_tokens=512,
|
| 35 |
system_message="""
|
|
|
|
| 104 |
name="image-captioning",
|
| 105 |
description="Generates a caption for an input image."
|
| 106 |
),
|
| 107 |
+
DuckDuckGoSearchTool(max_results=5),
|
| 108 |
WikipediaSearchTool(),
|
| 109 |
PythonInterpreterTool(),
|
| 110 |
UserInputTool(),
|
|
|
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
+
def run_agent_on_image(image, agent):
|
| 151 |
try:
|
| 152 |
# Wrap image as expected by the agent tool
|
| 153 |
response = agent("Describe this image", inputs={"image": image})
|
|
|
|
| 181 |
|
| 182 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 183 |
try:
|
| 184 |
+
agent = BasicAgent(model=model, tools=tools)
|
| 185 |
except Exception as e:
|
| 186 |
print(f"Error instantiating agent: {e}")
|
| 187 |
return f"Error initializing agent: {e}", None
|