Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -43,6 +43,50 @@ from smolagents.gradio_ui import pull_messages_from_step, handle_agent_output_ty
|
|
43 |
|
44 |
from smolagents import Tool
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
class GoogleSearchTool(Tool):
|
48 |
name = "web_search"
|
@@ -126,10 +170,7 @@ class GoogleSearchTool(Tool):
|
|
126 |
|
127 |
return "## Search Results\n" + "\n\n".join(web_snippets)
|
128 |
|
129 |
-
# web_search = GoogleSearchTool()
|
130 |
|
131 |
-
# print(web_search(query="Donald Trump news"))
|
132 |
-
# quit()
|
133 |
AUTHORIZED_IMPORTS = [
|
134 |
"requests",
|
135 |
"zipfile",
|
@@ -185,6 +226,7 @@ ti_tool = TextInspectorTool(model, text_limit)
|
|
185 |
browser = SimpleTextBrowser(**BROWSER_CONFIG)
|
186 |
|
187 |
WEB_TOOLS = [
|
|
|
188 |
GoogleSearchTool(),
|
189 |
VisitTool(browser),
|
190 |
PageUpTool(browser),
|
@@ -200,7 +242,7 @@ def create_agent():
|
|
200 |
"""Creates a fresh agent instance for each session"""
|
201 |
return CodeAgent(
|
202 |
model=model,
|
203 |
-
tools=[visualizer] + WEB_TOOLS,
|
204 |
max_steps=10,
|
205 |
verbosity_level=1,
|
206 |
additional_authorized_imports=AUTHORIZED_IMPORTS,
|
|
|
43 |
|
44 |
from smolagents import Tool
|
45 |
|
46 |
+
from urllib import parse
|
47 |
+
|
48 |
+
WOLFRAM_RESPONSE_KEYS = [
|
49 |
+
"Result",
|
50 |
+
"Solution",
|
51 |
+
"RealSolution",
|
52 |
+
]
|
53 |
+
|
54 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
55 |
+
|
56 |
+
@tool
|
57 |
+
def wolfram_alpha(query: str)-> str:
|
58 |
+
"""
|
59 |
+
A wrapper arounf Wolfram Alpha, nn intelligent tool that answers questions about Math, Geography,
|
60 |
+
Demographics, Sports, Music, Science, Technology, Culture, Society
|
61 |
+
and Everyday Life. Input should be a search query."
|
62 |
+
Args:
|
63 |
+
query: The search query.
|
64 |
+
Returns:
|
65 |
+
A string containing the answer for the query.
|
66 |
+
"""
|
67 |
+
api_key = os.environ["WOLFRAM_ALPHA_APPID"]
|
68 |
+
formatted_query = parse.quote_plus(query)
|
69 |
+
url = f"http://api.wolframalpha.com/v2/query?appid={api_key}&input={formatted_query}&output=json&format=plaintext"
|
70 |
+
for key in WOLFRAM_RESPONSE_KEYS:
|
71 |
+
url += f"&includepodid={key}"
|
72 |
+
print(f"\nURL:\n{url}")
|
73 |
+
|
74 |
+
try:
|
75 |
+
response = requests.get(url)
|
76 |
+
response.raise_for_status() # Raise an exception for HTTP errors
|
77 |
+
|
78 |
+
data = response.json()
|
79 |
+
|
80 |
+
if data.get("queryresult").get("error"): # Check if there's an error in the response
|
81 |
+
return f"Error: {data['queryresult']['error'].get('msg', 'Unable to fetch Wolfram response.')}"
|
82 |
+
|
83 |
+
response = ""
|
84 |
+
for result in data.get("queryresult").get("pods")[0].get("subpods"):
|
85 |
+
response += f"{result.get('plaintext')}; "
|
86 |
+
return response
|
87 |
+
|
88 |
+
except requests.exceptions.RequestException as e:
|
89 |
+
return f"Error fetching Wolfram response: {str(e)}"
|
90 |
|
91 |
class GoogleSearchTool(Tool):
|
92 |
name = "web_search"
|
|
|
170 |
|
171 |
return "## Search Results\n" + "\n\n".join(web_snippets)
|
172 |
|
|
|
173 |
|
|
|
|
|
174 |
AUTHORIZED_IMPORTS = [
|
175 |
"requests",
|
176 |
"zipfile",
|
|
|
226 |
browser = SimpleTextBrowser(**BROWSER_CONFIG)
|
227 |
|
228 |
WEB_TOOLS = [
|
229 |
+
wolfram_alpha,
|
230 |
GoogleSearchTool(),
|
231 |
VisitTool(browser),
|
232 |
PageUpTool(browser),
|
|
|
242 |
"""Creates a fresh agent instance for each session"""
|
243 |
return CodeAgent(
|
244 |
model=model,
|
245 |
+
tools=[visualizer, image_generation_tool] + WEB_TOOLS,
|
246 |
max_steps=10,
|
247 |
verbosity_level=1,
|
248 |
additional_authorized_imports=AUTHORIZED_IMPORTS,
|