Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,31 @@
|
|
| 1 |
-
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 2 |
-
from tools.final_answer import FinalAnswerTool
|
| 3 |
from duckduckgo_search import DDGS
|
| 4 |
-
import
|
| 5 |
-
import pytz
|
| 6 |
-
import yaml
|
| 7 |
-
from Gradio_UI import GradioUI
|
| 8 |
-
|
| 9 |
|
| 10 |
@tool
|
| 11 |
def duckduckgo_search(query: str, max_results: int = 5) -> str:
|
| 12 |
-
"""
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
model = HfApiModel(
|
| 36 |
-
max_tokens=2096,
|
| 37 |
-
temperature=0.5,
|
| 38 |
-
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
with open("prompts.yaml", "r") as stream:
|
| 42 |
-
prompt_templates = yaml.safe_load(stream)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
agent = CodeAgent(
|
| 46 |
-
model=model,
|
| 47 |
-
tools=[duckduckgo_search, get_current_time_in_timezone, final_answer],
|
| 48 |
-
max_steps=6,
|
| 49 |
-
verbosity_level=1,
|
| 50 |
-
prompt_templates=prompt_templates
|
| 51 |
-
)
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from duckduckgo_search import DDGS
|
| 2 |
+
from smolagents import tool
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
@tool
|
| 5 |
def duckduckgo_search(query: str, max_results: int = 5) -> str:
|
| 6 |
+
"""
|
| 7 |
+
Perform a web search using DuckDuckGo.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
query (str): The search query string.
|
| 11 |
+
max_results (int): The maximum number of search results to return.
|
| 12 |
+
|
| 13 |
+
Returns:
|
| 14 |
+
str: A formatted string containing search results.
|
| 15 |
+
"""
|
| 16 |
+
try:
|
| 17 |
+
with DDGS() as ddgs:
|
| 18 |
+
results = ddgs.text(query, max_results=max_results)
|
| 19 |
+
|
| 20 |
+
output = []
|
| 21 |
+
for i, r in enumerate(results, 1):
|
| 22 |
+
output.append(
|
| 23 |
+
f"{i}. {r.get('title')}\n"
|
| 24 |
+
f"{r.get('href')}\n"
|
| 25 |
+
f"{r.get('body')}\n"
|
| 26 |
+
)
|
| 27 |
|
| 28 |
+
return "\n".join(output) if output else "No results found."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"Search failed due to error: {str(e)}"
|