rahulkarda commited on
Commit
e9dbe69
·
verified ·
1 Parent(s): 6b51f04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -47
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 datetime
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
- """Search the web using DuckDuckGo."""
13
- with DDGS() as ddgs:
14
- results = ddgs.text(query, max_results=max_results)
15
- output = []
16
- for i, r in enumerate(results, 1):
17
- output.append(
18
- f"{i}. {r.get('title')}\n"
19
- f"{r.get('href')}\n"
20
- f"{r.get('body')}\n"
21
- )
22
- return "\n".join(output) if output else "No results found."
23
-
24
-
25
- @tool
26
- def get_current_time_in_timezone(timezone: str) -> str:
27
- """Get current time in a timezone."""
28
- tz = pytz.timezone(timezone)
29
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
30
- return f"The current time in {timezone} is {local_time}"
31
-
 
32
 
33
- final_answer = FinalAnswerTool()
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
- GradioUI(agent).launch()
 
 
 
 
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)}"