Da-Rabbit-Hole commited on
Commit
687468d
·
verified ·
1 Parent(s): 8c5c24b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +205 -2
app.py CHANGED
@@ -1,9 +1,16 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
 
 
 
 
 
 
7
 
8
  from Gradio_UI import GradioUI
9
 
@@ -18,11 +25,52 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
18
  """
19
  return "What magic will you build ?"
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
  """A tool that fetches the current local time in a specified timezone.
24
  Args:
25
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
 
 
26
  """
27
  try:
28
  # Create timezone object
@@ -46,6 +94,161 @@ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may
46
  custom_role_conversions=None,
47
  )
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  # Import tool from Hub
51
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
1
+ from smolagents import CodeAgent,DuckDuckGoSearchTool, WebSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
+ from ip2geotools.databases.noncommercial import DbIpCity
8
+ from bs4 import BeautifulSoup
9
+ import urllib as ur
10
+ import tkinter as tk
11
+ from tkinter.messagebox import *
12
+ import json
13
+ from PIL import ImageTk
14
 
15
  from Gradio_UI import GradioUI
16
 
 
25
  """
26
  return "What magic will you build ?"
27
 
28
+ @tool
29
+ def get_location_tool(ip: str): -> str
30
+
31
+ """This is a tool that gets the exact location of a user including city, region, country, latitude, and longitude.
32
+ Args:
33
+ ip: A string representing a valid ip address (e.g., '192.168.10.1')
34
+
35
+ Args:
36
+ city: A string representing a valid city (e.g., 'Dubai, Singapore')
37
+
38
+ Args:
39
+ region: A string representing a valid region (e.g, 'Africa, Asia')
40
+
41
+ Args:
42
+ country: A string representing a valid country (e.g., 'Ethiopia, United Arab Emirates')
43
+
44
+ Args:
45
+ latitude: A string representing valid latitude coordinates (e.g., '40.741895')
46
+
47
+ Args:
48
+ longitude: A string representing valid longitude coordinates (e.g, '73.989308')
49
+
50
+ Returns: ip address, city, region, country, and geo coordinates
51
+ """
52
+
53
+ response = DbIpCity.get(ip, api_key='free')
54
+ return {
55
+ 'city': response.city,
56
+ 'region': response.region,
57
+ 'country': response.country,
58
+ 'latitude': response.latitude,
59
+ 'longitude': response.longitude
60
+ }
61
+
62
+ ip_address = 'YOUR_IP_ADDRESS' # Replace with the actual IP address
63
+ location = get_location(ip_address)
64
+ print(location)
65
+
66
+
67
  @tool
68
  def get_current_time_in_timezone(timezone: str) -> str:
69
  """A tool that fetches the current local time in a specified timezone.
70
  Args:
71
+ timezone: A string representing a valid timezone (e.g., 'America/New_York')
72
+
73
+ Returns: The current local time in the following format ("%Y-%m-%d %H:%M:%S")
74
  """
75
  try:
76
  # Create timezone object
 
94
  custom_role_conversions=None,
95
  )
96
 
97
+ @tool
98
+ def get_weather_data(city: str):
99
+ """
100
+ Returns weather data for a given city.
101
+ Args:
102
+ city: The name of the city (e.g., New York, London, Tokyo)
103
+ Returns:
104
+ A dictionary containing weather data
105
+ """
106
+ # Get weather for multiple cities
107
+ cities = ["Tokyo", "New York", "London"]
108
+ data = {}
109
+
110
+ for city in cities:
111
+ data[city] = get_weather_data(city)
112
+ # Calculate average temperatures
113
+ avg_temps = {city: sum(info["temperatures"])/len(info["temperatures"])
114
+ for city, info in data.items()}
115
+ # Find the warmest city
116
+ warmest_city = max(avg_temps, key=avg_temps.get)
117
+ print(f"The warmest city is {warmest_city} with an average of {avg_temps[warmest_city]:.1f}°C")
118
+
119
+ @tool
120
+ def hotel_recommendation_tool(city: str,price: str): -> str
121
+ """This is a hotel recommendation tool that web scrapes foursquare.com, and yelp.com for hotel ratings, user comments, and hotel information.
122
+ Args:
123
+ city: A string representing a valid city (e.g., 'Dubai, Singapore')
124
+ Args:
125
+ price: A string representing a valid price for the hotel rooms (e.g., '$160/night, $100/day')
126
+
127
+ Returns: A hotel name in a particular city with its pricing, and rating information
128
+ """
129
+
130
+ if(price!="-"):
131
+ response=ur.request.urlopen('https://foursquare.com/explore?mode=url&near='+city+'&price='+price+'&q=Resturant')
132
+ else:
133
+ response=ur.request.urlopen('https://foursquare.com/explore?mode=url&near='+city+'&q=Resturant')
134
+ html=response.read()
135
+ soup=BeautifulSoup(html,'html.parser')
136
+ name=[]
137
+ rate=[]
138
+ add=[]
139
+ y="""<li class="card singleRecommendation hasPhoto"""
140
+ for link in soup.find_all('li'):
141
+ if (y in str(link)):
142
+ name.append(link.find('div',{"class":"venueName"}))
143
+ if(link.find('div',{"class":"venueScore positive"})!=None):
144
+ rate.append(link.find('div',{"class":"venueScorepositive"}))
145
+ elif(link.find('div',{"class":"venueScoreneutral"})!=None):
146
+ rate.append(link.find('div',{"class":"venueScoreneutral"}))
147
+ else:
148
+ rate.append(link.find('div',{"class":"venueScoreunknown"}))
149
+
150
+ rate1=[]
151
+ for x in rate:
152
+ if(x!=None):
153
+ rate1.append(x.string.encode("utf-8"))
154
+ else:
155
+ rate1.append("?")
156
+
157
+ ##print(rate1)
158
+
159
+ name1=[]
160
+ for x in name:
161
+ name1.append(x.a.string.encode("utf-8"))
162
+
163
+ name_rate=[]
164
+ for i in range(0,len(name1)):
165
+ name_rate.append((name1[i],rate1[i]))
166
+
167
+ return name_rate
168
+
169
+
170
+
171
+ def yelp(city,price):
172
+ name_rate=[]
173
+ count=0
174
+ while(count<30):
175
+ if(price!="-"):
176
+ response=ur.request.urlopen('https://www.yelp.com/search?find_desc=Restaurants&find_loc='+city+'&start='+str(count)+'&ed_attrs=RestaurantsPriceRange2.'+price)
177
+ else:
178
+ response=ur.request.urlopen('https://www.yelp.com/search?find_desc=Restaurants&find_loc='+city+'&start='+str(count))
179
+
180
+ html=response.read()
181
+ count+=10
182
+ soup=BeautifulSoup(html,'html.parser')
183
+
184
+ y="""<li class="regular-search-result">"""
185
+ name=[]
186
+ rate=[]
187
+ name1=[]
188
+ rate1=[]
189
+ for link in soup.find_all('li'):
190
+ if (y in str(link)):
191
+ name.append(link.find('span',{"class":"indexed-bizname"}))
192
+ rate.append(link.find('img',{"class":"offscreen"}))
193
+ for i in name:
194
+ name1.append(i.span.string.encode("utf-8"))
195
+
196
+ for i in rate:
197
+ try:
198
+ s=str(i['alt'])
199
+ rate1.append(float(s[0:3]))
200
+ except:
201
+ rate1.append("?")
202
+ for i in range(0,len(name1)):
203
+ name_rate.append((name1[i],rate1[i]))
204
+
205
+ return name_rate
206
+
207
+
208
+
209
+ root=tk.Tk()
210
+ root.geometry("645x660")
211
+ root.configure(bg='#211C5F')
212
+ root.title("Scraping Recommender")
213
+ def locate():
214
+ send_url = 'http://freegeoip.net/json'
215
+ r = requests.get(send_url)
216
+ j = json.loads(r.text)
217
+ lat = j['latitude']
218
+ lon = j['longitude']
219
+ e1.delete(0,tk.END)
220
+ city=str(lat)+" "+str(lon)
221
+ e1.insert(tk.END,"Current Location")
222
+ return city
223
+
224
+
225
+ def get():
226
+ city=e1.get()
227
+ if(city=="Current Location"):
228
+ city=locate()
229
+ price=e2.get()
230
+ if city=="":
231
+ showerror(title="Empty Field",message="Please Enter a City!")
232
+ return
233
+ l=["1","2","3","4","-"]
234
+ if price not in l:
235
+ showerror(title="Please Wait",message="Please Enter Correct Price Range!")
236
+ return
237
+
238
+ @tool
239
+ CustomDuckDuckGoSearchTool(DuckDuckGoSearchTool: query):
240
+ """
241
+ name = "web_search"
242
+ description = "Performs a web search for a query and returns a list of the top search results formatted as markdown with page titles and urls."
243
+ inputs = {"query": {"type": "string", "description": "The search query to perform."}}
244
+ output_type = "string"
245
+ """
246
+
247
+ tool = CustomDuckDuckGoSearchTool(
248
+ max_results=3,
249
+ rate_limit=1.0,)
250
+ result = tool(query='()')
251
+ print(result)
252
 
253
  # Import tool from Hub
254
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)