Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,5 @@
|
|
| 1 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
-
import datetime
|
| 3 |
import requests
|
| 4 |
-
import
|
| 5 |
-
import yaml
|
| 6 |
-
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
from bs4 import BeautifulSoup
|
| 8 |
-
import urllib as ur
|
| 9 |
-
import tkinter as tk
|
| 10 |
-
from tkinter.messagebox import *
|
| 11 |
-
import json
|
| 12 |
-
from PIL import ImageTk
|
| 13 |
-
|
| 14 |
-
from Gradio_UI import GradioUI
|
| 15 |
-
|
| 16 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 17 |
# @tool
|
| 18 |
# def my_custom_tool(arg1:str, arg2:int): str #it's important to specify the return type
|
|
@@ -25,196 +12,31 @@ from Gradio_UI import GradioUI
|
|
| 25 |
# # return
|
| 26 |
|
| 27 |
@tool
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
"""
|
| 31 |
-
Args1: A tool that fetches the current local time in a specified timezone.
|
| 32 |
-
Args2: timezone: A string representing a valid timezone (e.g., 'America/New_York')
|
| 33 |
-
|
| 34 |
-
Output: The current local time in the string format %Y-%m-%d %H:%M:%S (e.g., '2015-09-14 04:20:00')
|
| 35 |
-
"""
|
| 36 |
-
try:
|
| 37 |
-
# Create timezone object
|
| 38 |
-
tz = pytz.timezone(timezone)
|
| 39 |
-
|
| 40 |
-
# Get current time in that timezone
|
| 41 |
-
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 42 |
-
return f"The current local time in {timezone} is: {local_time}"
|
| 43 |
-
|
| 44 |
-
except Exception as e:
|
| 45 |
-
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
final_answer = FinalAnswerTool()
|
| 49 |
-
|
| 50 |
-
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 51 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 52 |
-
|
| 53 |
-
model = HfApiModel(
|
| 54 |
-
max_tokens=2096,
|
| 55 |
-
temperature=0.5,
|
| 56 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
|
| 57 |
-
custom_role_conversions=None,
|
| 58 |
-
)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
def get_weather_data(city: str):
|
| 62 |
-
|
| 63 |
-
"""
|
| 64 |
-
Returns weather data for a given city.
|
| 65 |
Args:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
"""
|
| 70 |
-
# Get weather for multiple cities
|
| 71 |
-
cities = ["Tokyo", "New York", "London"]
|
| 72 |
-
data = {}
|
| 73 |
-
|
| 74 |
-
for city in cities:
|
| 75 |
-
data[city] = get_weather_data(city)
|
| 76 |
-
|
| 77 |
-
# Calculate average temperatures
|
| 78 |
-
avg_temps = {city: sum(info["temperatures"])/len(info["temperatures"])
|
| 79 |
-
for city, info in data.items()}
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
print(f"The warmest city is {warmest_city} with an average of {avg_temps[warmest_city]:.1f}°C")
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
@tool
|
| 87 |
-
def hotel_recommendation_tool(city: str, price: str) -> str:
|
| 88 |
-
"""
|
| 89 |
-
This is a hotel recommendation tool that web scrapes foursquare.com and yelp.com for hotel ratings, user comments, and hotel information.
|
| 90 |
-
Args:
|
| 91 |
-
city: A string representing a valid city (e.g., 'Dubai, Singapore')
|
| 92 |
-
price: A string representing a valid price for the hotel rooms (e.g., '$160/night, $100/day')
|
| 93 |
-
|
| 94 |
-
Returns:
|
| 95 |
-
A hotel name in a particular city with its pricing and rating information
|
| 96 |
-
"""
|
| 97 |
|
| 98 |
-
if
|
| 99 |
-
|
|
|
|
| 100 |
else:
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
html = response.read()
|
| 104 |
-
soup = BeautifulSoup(html, 'html.parser')
|
| 105 |
-
name = []
|
| 106 |
-
rate = []
|
| 107 |
-
add = []
|
| 108 |
-
y = """<li class="card singleRecommendation hasPhoto\""""
|
| 109 |
-
|
| 110 |
-
for link in soup.find_all('li'):
|
| 111 |
-
if y in str(link):
|
| 112 |
-
name.append(link.find('div', {"class":"venueName"}))
|
| 113 |
-
if link.find('div', {"class":"venueScore positive"}) != None:
|
| 114 |
-
rate.append(link.find('div', {"class":"venueScorepositive"}))
|
| 115 |
-
elif link.find('div', {"class":"venueScore neutral"}) != None:
|
| 116 |
-
rate.append(link.find('div', {"class":"venueScoreneutral"}))
|
| 117 |
-
else:
|
| 118 |
-
rate.append(link.find('div', {"class":"venueScoreunknown"}))
|
| 119 |
-
|
| 120 |
-
rate1 = []
|
| 121 |
-
|
| 122 |
-
for x in rate:
|
| 123 |
-
if x != None:
|
| 124 |
-
rate1.append(x.string.encode("utf-8"))
|
| 125 |
-
else:
|
| 126 |
-
rate1.append("?")
|
| 127 |
-
|
| 128 |
-
name1 = []
|
| 129 |
-
for x in name:
|
| 130 |
-
name1.append(x.a.string.encode("utf-8"))
|
| 131 |
-
|
| 132 |
-
name_rate = []
|
| 133 |
-
for i in range(0, len(name1)):
|
| 134 |
-
name_rate.append((name1[i], rate1[i]))
|
| 135 |
-
|
| 136 |
-
return name_rate
|
| 137 |
-
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
count = 0
|
| 142 |
-
while (count < 30):
|
| 143 |
-
if price != "-":
|
| 144 |
-
response = ur.request.urlopen('https://www.yelp.com/search?find_desc=Restaurants&find_loc=' + city + '&start=' + str(count) + '&ed_attrs=RestaurantsPriceRange2.' + price)
|
| 145 |
-
else:
|
| 146 |
-
response = ur.request.urlopen('https://www.yelp.com/search?find_desc=Restaurants&find_loc=' + city + '&start=' + str(count))
|
| 147 |
-
|
| 148 |
-
html = response.read()
|
| 149 |
-
count += 10
|
| 150 |
-
soup = BeautifulSoup(html, 'html.parser')
|
| 151 |
-
|
| 152 |
-
y = """<li class="regular-search-result">"""
|
| 153 |
-
name = []
|
| 154 |
-
rate = []
|
| 155 |
-
name1 = []
|
| 156 |
-
rate1 = []
|
| 157 |
-
|
| 158 |
-
for link in soup.find_all('li'):
|
| 159 |
-
if (y in str(link)):
|
| 160 |
-
name.append(link.find('span', {"class":"indexed-bizname"}))
|
| 161 |
-
rate.append(link.find('img', {"class":"offscreen"}))
|
| 162 |
-
|
| 163 |
-
for i in name:
|
| 164 |
-
name1.append(i.span.string.encode("utf-8"))
|
| 165 |
-
|
| 166 |
-
for i in rate:
|
| 167 |
-
try:
|
| 168 |
-
s = str(i['alt'])
|
| 169 |
-
rate1.append(float(s[0:3]))
|
| 170 |
-
except:
|
| 171 |
-
rate1.append("?")
|
| 172 |
-
|
| 173 |
-
for i in range(0, len(name1)):
|
| 174 |
-
name_rate.append((name1[i], rate1[i]))
|
| 175 |
-
|
| 176 |
-
return name_rate
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
root=tk.Tk()
|
| 180 |
-
root.geometry("645x660")
|
| 181 |
-
root.configure(bg='#211C5F')
|
| 182 |
-
root.title("Scraping Recommender")
|
| 183 |
-
|
| 184 |
-
def locate():
|
| 185 |
-
send_url = 'http://freegeoip.net/json'
|
| 186 |
-
r = requests.get(send_url)
|
| 187 |
-
j = json.loads(r.text)
|
| 188 |
-
lat = j['latitude']
|
| 189 |
-
lon = j['longitude']
|
| 190 |
-
e1.delete(0,tk.END)
|
| 191 |
-
city=str(lat)+" "+str(lon)
|
| 192 |
-
e1.insert(tk.END,"Current Location")
|
| 193 |
-
return city
|
| 194 |
-
|
| 195 |
-
def get():
|
| 196 |
-
city=e1.get()
|
| 197 |
-
if(city=="Current Location"):
|
| 198 |
-
city=locate()
|
| 199 |
-
price=e2.get()
|
| 200 |
-
if city=="":
|
| 201 |
-
showerror(title="Empty Field",message="Please Enter a City!")
|
| 202 |
-
return
|
| 203 |
-
l=["1","2","3","4","-"]
|
| 204 |
-
if price not in l:
|
| 205 |
-
showerror(title="Please Wait",message="Please Enter Correct Price Range!")
|
| 206 |
-
return
|
| 207 |
-
|
| 208 |
-
@tool
|
| 209 |
-
def SearchTool(DuckDuckGoSearchTool): query
|
| 210 |
-
"""
|
| 211 |
-
name = "web_search"
|
| 212 |
-
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."
|
| 213 |
-
inputs = {"query": {"type": "string", "description": "The search query to perform."}}
|
| 214 |
-
output_type = "string"
|
| 215 |
-
"""
|
| 216 |
-
|
| 217 |
-
search_tool = DuckDuckGoSearchTool()
|
| 218 |
-
agent = CodeAgent(tools=[search_tool], model=model, add_base_tools=False, verbosity_level=2)
|
| 219 |
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
+
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 4 |
# @tool
|
| 5 |
# def my_custom_tool(arg1:str, arg2:int): str #it's important to specify the return type
|
|
|
|
| 12 |
# # return
|
| 13 |
|
| 14 |
@tool
|
| 15 |
+
# Function to get current prices of specified cryptocurrencies
|
| 16 |
+
def get_crypto_prices(crypto_ids, currency='usd'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
"""This is a tool to get current crypto token market prices in usd currency using the coingecko api.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
Args:
|
| 20 |
+
args1: crypto_ids= str
|
| 21 |
+
args2: currency= int
|
| 22 |
+
|
| 23 |
+
Responses:
|
| 24 |
+
You should return all valid requests with the crypto id, and its current price in usd denominations.
|
| 25 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
url = f"https://api.coingecko.com/api/v3/simple/price?ids={','.join(crypto_ids)}&vs_currencies={currency}"
|
| 28 |
+
response = requests.get(url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
if response.status_code == 200:
|
| 31 |
+
data = response.json()
|
| 32 |
+
return pd.DataFrame(data).T # Transpose for better readability
|
| 33 |
else:
|
| 34 |
+
print("Failed to retrieve data")
|
| 35 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# Specify the cryptocurrencies you want to check
|
| 38 |
+
cryptos = ['bitcoin', 'ethereum', 'ripple']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Get current prices
|
| 41 |
+
prices = get_crypto_prices(cryptos)
|
| 42 |
+
print(prices)
|