Update app.py
Browse files
app.py
CHANGED
|
@@ -3,20 +3,52 @@ 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 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
arg2: the second argument
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -40,7 +72,7 @@ final_answer = FinalAnswerTool()
|
|
| 40 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 41 |
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=
|
| 44 |
temperature=0.5,
|
| 45 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 46 |
custom_role_conversions=None,
|
|
@@ -55,7 +87,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
import os
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
+
from bs4 import BeautifulSoup
|
| 10 |
+
|
| 11 |
+
# Set HF Token from Secrets
|
| 12 |
+
os.environ["HF_TOKEN"] = os.getenv("HF_TOKEN", "default_token_if_missing")
|
| 13 |
|
| 14 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 15 |
@tool
|
| 16 |
+
def get_weather(city: str) -> str:
|
| 17 |
+
"""A tool that fetches the current weather for a given city.
|
| 18 |
+
Args:
|
| 19 |
+
city: A string representing the city name (e.g., 'New York').
|
| 20 |
+
"""
|
| 21 |
+
api_key = "8549c1b0e2c7d6cf8abe4407d739fdf2"
|
| 22 |
+
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
|
| 23 |
+
try:
|
| 24 |
+
response = requests.get(url)
|
| 25 |
+
data = response.json()
|
| 26 |
+
if data["cod"] != 200:
|
| 27 |
+
return f"Error: City '{city}' not found or API issue."
|
| 28 |
+
temp = data["main"]["temp"]
|
| 29 |
+
desc = data["weather"][0]["description"]
|
| 30 |
+
return f"Current weather in {city}: {temp}°C, {desc}."
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return f"Error fetching weather for '{city}': {str(e)}"
|
| 33 |
+
|
| 34 |
+
@tool
|
| 35 |
+
def get_trending_hashtags(location: str = "usa") -> str:
|
| 36 |
+
"""A tool that fetches trending hashtags from getdaytrends.com for a given location.
|
| 37 |
Args:
|
| 38 |
+
location: A string representing the location (e.g., 'usa', 'uk'). Defaults to 'usa'.
|
|
|
|
| 39 |
"""
|
| 40 |
+
try:
|
| 41 |
+
url = f"https://getdaytrends.com/{location.lower()}/"
|
| 42 |
+
response = requests.get(url)
|
| 43 |
+
if response.status_code != 200:
|
| 44 |
+
return f"Error: Couldn’t fetch trends for '{location}'."
|
| 45 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 46 |
+
trends = [tag.text.strip() for tag in soup.select("td a[href*='twitter.com']")[:5]]
|
| 47 |
+
if not trends:
|
| 48 |
+
return f"Error: No trends found for '{location}' - site might’ve changed."
|
| 49 |
+
return f"Top trending hashtags in {location}: {', '.join(trends)}"
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"Error fetching trends for '{location}': {str(e)}"
|
| 52 |
|
| 53 |
@tool
|
| 54 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 72 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 73 |
|
| 74 |
model = HfApiModel(
|
| 75 |
+
max_tokens=500,
|
| 76 |
temperature=0.5,
|
| 77 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 78 |
custom_role_conversions=None,
|
|
|
|
| 87 |
|
| 88 |
agent = CodeAgent(
|
| 89 |
model=model,
|
| 90 |
+
tools=[final_answer, get_weather, get_trending_hashtags, get_current_time_in_timezone], ## add your tools here (don't remove final answer)
|
| 91 |
max_steps=6,
|
| 92 |
verbosity_level=1,
|
| 93 |
grammar=None,
|