ajitgupta commited on
Commit
c1a2e92
·
verified ·
1 Parent(s): e7057c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -27
app.py CHANGED
@@ -4,8 +4,8 @@ import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
- from typing import List, Dict, Any, Optional
8
- from duckduckgo_search import DDGS
9
 
10
  from Gradio_UI import GradioUI
11
 
@@ -36,37 +36,57 @@ def get_current_time_in_timezone(timezone: str) -> str:
36
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
37
 
38
 
39
- # Simple Germany bounding box
40
- GERMANY_BBOX = (5.8663, 47.2701, 15.0419, 55.0581)
41
 
42
  @tool
43
- def DuckDuckGoSearchTool(query: str, city: Optional[str] = None, limit: int = 10) -> List[Dict[str, Any]]:
44
- """A tool that searches DuckDuckGo Maps for cafes in Germany.
 
 
45
  Args:
46
- query: A string to refine the cafe search (e.g., 'vegan', 'wifi').
47
- city: Optional German city name (e.g., 'Berlin', 'Munich').
48
- limit: Maximum number of cafes to return.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  """
 
 
 
50
  try:
51
- maps_query = f"cafe {query}".strip()
52
- results = []
53
-
54
- with DDGS() as ddgs:
55
- for item in ddgs.maps(maps_query, region="de-de", bbox=GERMANY_BBOX, max_results=limit):
56
- results.append({
57
- "name": item.get("title"),
58
- "address": item.get("address"),
59
- "website": item.get("url"),
60
- "phone": item.get("phone"),
61
- "rating": item.get("rating"),
62
- "lat": (item.get("coordinates") or {}).get("latitude"),
63
- "lon": (item.get("coordinates") or {}).get("longitude"),
64
- })
65
-
66
- return results
67
 
68
  except Exception as e:
69
- return [{"error": f"Failed to fetch cafes: {str(e)}"}]
 
70
 
71
 
72
 
@@ -91,7 +111,7 @@ with open("prompts.yaml", 'r') as stream:
91
 
92
  agent = CodeAgent(
93
  model=model,
94
- tools=[final_answer, image_generation_tool, DuckDuckGoSearchTool], ## add your tools here (don't remove final answer)
95
  max_steps=6,
96
  verbosity_level=1,
97
  grammar=None,
 
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
+ from typing import List, Dict
8
+ from smolagents import DuckDuckGoSearchTool, tool
9
 
10
  from Gradio_UI import GradioUI
11
 
 
36
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
37
 
38
 
39
+
 
40
 
41
  @tool
42
+ def search_cafes_in_germany(query: str, city: str = None, limit: int = 10) -> List[Dict]:
43
+ """
44
+ A refined tool that searches for cafes or restaurants in Germany using DuckDuckGo.
45
+
46
  Args:
47
+ query: Search keywords (e.g., 'vegan cafe', 'Indian restaurant').
48
+ city: Optional German city to narrow results (e.g., 'Berlin', 'Cologne').
49
+ limit: Maximum number of results to return (default 10).
50
+
51
+ Returns:
52
+ A list of dictionaries containing structured information about each place, e.g.,
53
+ [
54
+ {
55
+ "name": "Cafe ABC",
56
+ "address": "Some street, Berlin",
57
+ "website": "https://cafeabc.de",
58
+ "phone": "+49 123 456789",
59
+ "rating": 4.5,
60
+ "lat": 52.5200,
61
+ "lon": 13.4050
62
+ },
63
+ ...
64
+ ]
65
  """
66
+ # Build the final query string
67
+ final_query = f"{query} in {city}" if city else query
68
+
69
  try:
70
+ # Use the built-in DuckDuckGoSearchTool from smolagents
71
+ results = DuckDuckGoSearchTool(query=final_query, limit=limit)
72
+
73
+ # Optional: normalize fields for consistency
74
+ normalized = []
75
+ for r in results:
76
+ normalized.append({
77
+ "name": r.get("name"),
78
+ "address": r.get("address"),
79
+ "website": r.get("website"),
80
+ "phone": r.get("phone"),
81
+ "rating": r.get("rating"),
82
+ "lat": r.get("lat"),
83
+ "lon": r.get("lon"),
84
+ })
85
+ return normalized
86
 
87
  except Exception as e:
88
+ return [{"error": f"Search failed: {str(e)}"}]
89
+
90
 
91
 
92
 
 
111
 
112
  agent = CodeAgent(
113
  model=model,
114
+ tools=[final_answer, image_generation_tool, search_cafes_in_germany], ## add your tools here (don't remove final answer)
115
  max_steps=6,
116
  verbosity_level=1,
117
  grammar=None,