Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,124 +1,80 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
from Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
-
|
| 10 |
-
def get_user_location():
|
| 11 |
-
"""Fetch the user's approximate location based on their IP address."""
|
| 12 |
-
try:
|
| 13 |
-
response = requests.get("http://ip-api.com/json/")
|
| 14 |
-
data = response.json()
|
| 15 |
-
|
| 16 |
-
if response.status_code == 200 and data.get("status") == "success":
|
| 17 |
-
return {
|
| 18 |
-
"latitude": data.get("lat"),
|
| 19 |
-
"longitude": data.get("lon"),
|
| 20 |
-
"city": data.get("city"),
|
| 21 |
-
"region": data.get("regionName"),
|
| 22 |
-
"country": data.get("country"),
|
| 23 |
-
"timezone": data.get("timezone"),
|
| 24 |
-
}
|
| 25 |
-
else:
|
| 26 |
-
return {"error": "Could not fetch location data"}
|
| 27 |
-
|
| 28 |
-
except Exception as e:
|
| 29 |
-
return {"error": str(e)}
|
| 30 |
-
|
| 31 |
-
# Function to fetch restaurants using Google Places API
|
| 32 |
-
def get_nearby_restaurants(lat, lon, api_key):
|
| 33 |
-
"""Fetch nearby restaurants using Google Places API."""
|
| 34 |
-
url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json"
|
| 35 |
-
params = {
|
| 36 |
-
"location": f"{lat},{lon}",
|
| 37 |
-
"radius": 5000, # 5 km radius
|
| 38 |
-
"type": "restaurant",
|
| 39 |
-
"key": api_key
|
| 40 |
-
}
|
| 41 |
|
| 42 |
-
|
| 43 |
-
response = requests.get(url, params=params)
|
| 44 |
-
data = response.json()
|
| 45 |
-
|
| 46 |
-
if "results" in data:
|
| 47 |
-
return [
|
| 48 |
-
{"name": place["name"], "address": place.get("vicinity", "No address")}
|
| 49 |
-
for place in data["results"][:5] # Top 5 results
|
| 50 |
-
]
|
| 51 |
-
|
| 52 |
-
return {"error": "No restaurants found"}
|
| 53 |
-
|
| 54 |
-
except Exception as e:
|
| 55 |
-
return {"error": str(e)}
|
| 56 |
|
| 57 |
@tool
|
| 58 |
-
def ddg_tool(query:
|
| 59 |
-
|
| 60 |
-
|
| 61 |
Args:
|
| 62 |
-
query: The type of
|
| 63 |
-
location:
|
| 64 |
"""
|
| 65 |
try:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
user_location = get_user_location()
|
| 69 |
-
if "error" in user_location:
|
| 70 |
-
return user_location["error"]
|
| 71 |
-
location = user_location["city"]
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
search_query = f"{query} restaurants in {location}"
|
| 76 |
-
results = search_tool(search_query)
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
food_links = [result['href'] for result in results if isinstance(
|
| 80 |
|
| 81 |
if food_links:
|
| 82 |
-
return f"Here are some
|
|
|
|
| 83 |
else:
|
| 84 |
-
return "No
|
| 85 |
|
| 86 |
except Exception as e:
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
@tool
|
| 90 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 91 |
"""A tool that fetches the current local time in a specified timezone.
|
| 92 |
-
|
| 93 |
Args:
|
| 94 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 95 |
"""
|
| 96 |
try:
|
|
|
|
| 97 |
tz = pytz.timezone(timezone)
|
|
|
|
| 98 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 99 |
return f"The current local time in {timezone} is: {local_time}"
|
| 100 |
except Exception as e:
|
| 101 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 102 |
|
|
|
|
| 103 |
final_answer = FinalAnswerTool()
|
| 104 |
|
| 105 |
-
#
|
|
|
|
|
|
|
| 106 |
model = HfApiModel(
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
)
|
| 112 |
|
|
|
|
| 113 |
# Import tool from Hub
|
| 114 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 115 |
|
| 116 |
with open("prompts.yaml", 'r') as stream:
|
| 117 |
prompt_templates = yaml.safe_load(stream)
|
| 118 |
-
|
| 119 |
agent = CodeAgent(
|
| 120 |
model=model,
|
| 121 |
-
tools=[final_answer
|
| 122 |
max_steps=6,
|
| 123 |
verbosity_level=1,
|
| 124 |
grammar=None,
|
|
@@ -128,4 +84,5 @@ agent = CodeAgent(
|
|
| 128 |
prompt_templates=prompt_templates
|
| 129 |
)
|
| 130 |
|
| 131 |
-
|
|
|
|
|
|
| 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 |
|
| 10 |
+
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
@tool
|
| 13 |
+
def ddg_tool(query:str, location:str)-> str: # it's important to specify the return type
|
| 14 |
+
# Keep this format for the tool description / args description but feel free to modify the tool
|
| 15 |
+
"""A tool that searches for restaraunts ina particular location using DuckDuckGo search.
|
| 16 |
Args:
|
| 17 |
+
query: The type of restaraunt or keyword (e.g. "italian").
|
| 18 |
+
location: The location the restaraunts are being searched based on timezone (e.g. "America/Phoenix")
|
| 19 |
"""
|
| 20 |
try:
|
| 21 |
+
search_tool = ddg_tool()
|
| 22 |
+
search_query = f"{query} restaraunts in {location}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Perform the search
|
| 25 |
+
results = search_tool(search_query)
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# extract top links (if available)
|
| 28 |
+
food_links = [result['href'] for result in results if isinstance(results, dict) and 'href' in result][:5] #return top 5 links
|
| 29 |
|
| 30 |
if food_links:
|
| 31 |
+
return f"Here are some restaraunts ofr '{query}' in {location}:\n" +"\n".join(food_links)
|
| 32 |
+
|
| 33 |
else:
|
| 34 |
+
return "No restaraunts found near you."
|
| 35 |
|
| 36 |
except Exception as e:
|
| 37 |
+
return f"Error searching for food links: {str(e)}"
|
| 38 |
+
|
| 39 |
|
| 40 |
@tool
|
| 41 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 42 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 43 |
Args:
|
| 44 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 45 |
"""
|
| 46 |
try:
|
| 47 |
+
# Create timezone object
|
| 48 |
tz = pytz.timezone(timezone)
|
| 49 |
+
# Get current time in that timezone
|
| 50 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 51 |
return f"The current local time in {timezone} is: {local_time}"
|
| 52 |
except Exception as e:
|
| 53 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 54 |
|
| 55 |
+
|
| 56 |
final_answer = FinalAnswerTool()
|
| 57 |
|
| 58 |
+
# 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:
|
| 59 |
+
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 60 |
+
|
| 61 |
model = HfApiModel(
|
| 62 |
+
max_tokens=2096,
|
| 63 |
+
temperature=0.5,
|
| 64 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 65 |
+
custom_role_conversions=None,
|
| 66 |
)
|
| 67 |
|
| 68 |
+
|
| 69 |
# Import tool from Hub
|
| 70 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 71 |
|
| 72 |
with open("prompts.yaml", 'r') as stream:
|
| 73 |
prompt_templates = yaml.safe_load(stream)
|
| 74 |
+
|
| 75 |
agent = CodeAgent(
|
| 76 |
model=model,
|
| 77 |
+
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 78 |
max_steps=6,
|
| 79 |
verbosity_level=1,
|
| 80 |
grammar=None,
|
|
|
|
| 84 |
prompt_templates=prompt_templates
|
| 85 |
)
|
| 86 |
|
| 87 |
+
|
| 88 |
+
GradioUI(agent).launch()
|