LamiaYT commited on
Commit
c2dcfa0
·
1 Parent(s): 9cb982a

Deploy GAIA agent

Browse files
Files changed (1) hide show
  1. app.py +17 -54
app.py CHANGED
@@ -25,63 +25,26 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
25
  @tool
26
  def web_search_tool(query: str) -> str:
27
  """
28
- Search the web for information using DuckDuckGo.
29
- Args:
30
- query: The search query string
31
- Returns:
32
- String containing search results
33
  """
34
  try:
35
- # Try multiple approaches for DDGS
36
- search_results = []
37
-
38
- # Approach 1: Basic DDGS
39
- try:
40
- ddgs = DDGS()
41
- search_results = list(ddgs.text(query, max_results=5))
42
- except Exception as e1:
43
- print(f"DDGS Approach 1 failed: {e1}")
44
-
45
- # Approach 2: Try with timeout parameter only
46
- try:
47
- ddgs = DDGS(timeout=10)
48
- search_results = list(ddgs.text(query, max_results=5))
49
- except Exception as e2:
50
- print(f"DDGS Approach 2 failed: {e2}")
51
-
52
- # Approach 3: Minimal call without max_results
53
- try:
54
- ddgs = DDGS()
55
- search_results = list(ddgs.text(query))[:5] # Limit results manually
56
- except Exception as e3:
57
- print(f"DDGS Approach 3 failed: {e3}")
58
-
59
- # Approach 4: Try older style initialization
60
- try:
61
- from duckduckgo_search import ddg
62
- search_results = ddg(query, max_results=5)
63
- except Exception as e4:
64
- print(f"DDGS Approach 4 failed: {e4}")
65
- return f"All DDGS approaches failed. Last error: {e4}"
66
-
67
- if not search_results:
68
- return "No search results found."
69
-
70
- results = []
71
- for r in search_results:
72
- try:
73
- title = r.get('title', 'No title')
74
- url = r.get('href', 'No URL')
75
- snippet = r.get('body', 'No snippet')
76
- results.append(f"Title: {title}\nURL: {url}\nSnippet: {snippet}")
77
- except Exception as parse_error:
78
- print(f"Error parsing result: {parse_error}")
79
- continue
80
-
81
- return "\n\n".join(results) if results else "No valid results could be parsed."
82
-
83
  except Exception as e:
84
- return f"Search tool error: {str(e)}"
 
85
 
86
  @tool
87
  def calculator_tool(expression: str) -> str:
 
25
  @tool
26
  def web_search_tool(query: str) -> str:
27
  """
28
+ Perform a DuckDuckGo search and return formatted results.
 
 
 
 
29
  """
30
  try:
31
+ from duckduckgo_search import DDGS
32
+ with DDGS() as ddgs:
33
+ results = ddgs.text(query, max_results=5)
34
+ if not results:
35
+ return "No search results found."
36
+
37
+ output = []
38
+ for result in results:
39
+ output.append(
40
+ f"Title: {result.get('title', 'No title')}\n"
41
+ f"URL: {result.get('href', 'No URL')}\n"
42
+ f"Snippet: {result.get('body', 'No snippet')}\n"
43
+ )
44
+ return "\n\n".join(output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  except Exception as e:
46
+ return f"Web search failed: {str(e)}"
47
+
48
 
49
  @tool
50
  def calculator_tool(expression: str) -> str: