Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,10 @@
|
|
| 1 |
-
from smolagents import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
@@ -7,67 +13,167 @@ from tools.final_answer import FinalAnswerTool
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
Args:
|
| 16 |
-
arg1: the first argument
|
| 17 |
-
arg2: the second argument
|
| 18 |
-
"""
|
| 19 |
-
return "What magic will you build ?"
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
-
"""
|
| 24 |
Args:
|
| 25 |
-
timezone:
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
-
# Create timezone object
|
| 29 |
tz = pytz.timezone(timezone)
|
| 30 |
-
# Get current time in that timezone
|
| 31 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
return f"The current local time in {timezone} is: {local_time}"
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id=
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
tools=[
|
| 59 |
final_answer,
|
| 60 |
get_current_time_in_timezone,
|
|
|
|
|
|
|
|
|
|
| 61 |
image_generation_tool,
|
|
|
|
| 62 |
],
|
| 63 |
max_steps=6,
|
| 64 |
verbosity_level=1,
|
| 65 |
grammar=None,
|
| 66 |
planning_interval=None,
|
| 67 |
-
name=
|
| 68 |
-
description=
|
| 69 |
-
prompt_templates=prompt_templates
|
| 70 |
)
|
| 71 |
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import (
|
| 2 |
+
CodeAgent,
|
| 3 |
+
DuckDuckGoSearchTool,
|
| 4 |
+
HfApiModel,
|
| 5 |
+
load_tool,
|
| 6 |
+
tool,
|
| 7 |
+
)
|
| 8 |
import datetime
|
| 9 |
import requests
|
| 10 |
import pytz
|
|
|
|
| 13 |
|
| 14 |
from Gradio_UI import GradioUI
|
| 15 |
|
| 16 |
+
|
| 17 |
+
# ======================================================
|
| 18 |
+
# 🛠️ TOOLS
|
| 19 |
+
# ======================================================
|
| 20 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
@tool
|
| 23 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 24 |
+
"""Fetch the current local time in a specified timezone.
|
| 25 |
Args:
|
| 26 |
+
timezone: Valid timezone like 'Asia/Tokyo', 'Europe/Paris'
|
| 27 |
"""
|
| 28 |
try:
|
|
|
|
| 29 |
tz = pytz.timezone(timezone)
|
|
|
|
| 30 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 31 |
return f"The current local time in {timezone} is: {local_time}"
|
| 32 |
except Exception as e:
|
| 33 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 34 |
|
| 35 |
|
| 36 |
+
# ------------------------------------------------------
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@tool
|
| 40 |
+
def get_bitcoin_price() -> str:
|
| 41 |
+
"""Fetches the current Bitcoin price in USD using CoinGecko public API."""
|
| 42 |
+
try:
|
| 43 |
+
url = "https://api.coingecko.com/api/v3/simple/price"
|
| 44 |
+
params = {"ids": "bitcoin", "vs_currencies": "usd"}
|
| 45 |
+
r = requests.get(url, params=params, timeout=10)
|
| 46 |
+
data = r.json()
|
| 47 |
+
price = data["bitcoin"]["usd"]
|
| 48 |
+
return f"Bitcoin price right now: ${price:,} USD"
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"Error fetching Bitcoin price: {e}"
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# ------------------------------------------------------
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
@tool
|
| 57 |
+
def get_weather_city(city: str) -> str:
|
| 58 |
+
"""Gets current weather for a city using Open-Meteo public API.
|
| 59 |
+
Args:
|
| 60 |
+
city: Name of the city like Tokyo, London, Paris
|
| 61 |
+
"""
|
| 62 |
+
try:
|
| 63 |
+
geo_url = "https://geocoding-api.open-meteo.com/v1/search"
|
| 64 |
+
geo = requests.get(geo_url, params={"name": city, "count": 1}).json()
|
| 65 |
+
|
| 66 |
+
if "results" not in geo:
|
| 67 |
+
return f"Could not find coordinates for {city}"
|
| 68 |
+
|
| 69 |
+
lat = geo["results"][0]["latitude"]
|
| 70 |
+
lon = geo["results"][0]["longitude"]
|
| 71 |
+
|
| 72 |
+
weather_url = "https://api.open-meteo.com/v1/forecast"
|
| 73 |
+
weather = requests.get(
|
| 74 |
+
weather_url,
|
| 75 |
+
params={
|
| 76 |
+
"latitude": lat,
|
| 77 |
+
"longitude": lon,
|
| 78 |
+
"current_weather": True,
|
| 79 |
+
},
|
| 80 |
+
).json()
|
| 81 |
+
|
| 82 |
+
current = weather["current_weather"]
|
| 83 |
+
|
| 84 |
+
return (
|
| 85 |
+
f"Weather in {city}: {current['temperature']}°C, "
|
| 86 |
+
f"windspeed {current['windspeed']} km/h"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
return f"Weather lookup failed: {e}"
|
| 91 |
|
| 92 |
+
|
| 93 |
+
# ------------------------------------------------------
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
@tool
|
| 97 |
+
def summarize_neural_networks() -> str:
|
| 98 |
+
"""Fetches and summarizes the history of neural networks from Wikipedia."""
|
| 99 |
+
try:
|
| 100 |
+
url = "https://en.wikipedia.org/api/rest_v1/page/summary/Artificial_neural_network"
|
| 101 |
+
r = requests.get(url, timeout=10)
|
| 102 |
+
data = r.json()
|
| 103 |
+
|
| 104 |
+
extract = data.get("extract", "")
|
| 105 |
+
return extract[:1500]
|
| 106 |
+
|
| 107 |
+
except Exception as e:
|
| 108 |
+
return f"Failed to fetch history: {e}"
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# ======================================================
|
| 112 |
+
# 🧠 MODEL + FINAL ANSWER TOOL
|
| 113 |
+
# ======================================================
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
final_answer = FinalAnswerTool()
|
| 117 |
|
| 118 |
model = HfApiModel(
|
| 119 |
+
max_tokens=2096,
|
| 120 |
+
temperature=0.5,
|
| 121 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 122 |
+
custom_role_conversions=None,
|
| 123 |
)
|
| 124 |
|
| 125 |
|
| 126 |
+
# ======================================================
|
| 127 |
+
# 🌍 LOAD HUB TOOLS
|
| 128 |
+
# ======================================================
|
| 129 |
|
| 130 |
+
|
| 131 |
+
image_generation_tool = load_tool(
|
| 132 |
+
"agents-course/text-to-image",
|
| 133 |
+
trust_remote_code=True,
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
search_tool = DuckDuckGoSearchTool()
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
# ======================================================
|
| 140 |
+
# 📜 LOAD SYSTEM PROMPT
|
| 141 |
+
# ======================================================
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
with open("prompts.yaml", "r") as stream:
|
| 145 |
prompt_templates = yaml.safe_load(stream)
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
# ======================================================
|
| 149 |
+
# 🤖 AGENT
|
| 150 |
+
# ======================================================
|
| 151 |
+
|
| 152 |
+
|
| 153 |
agent = CodeAgent(
|
| 154 |
model=model,
|
| 155 |
tools=[
|
| 156 |
final_answer,
|
| 157 |
get_current_time_in_timezone,
|
| 158 |
+
get_bitcoin_price,
|
| 159 |
+
get_weather_city,
|
| 160 |
+
summarize_neural_networks,
|
| 161 |
image_generation_tool,
|
| 162 |
+
search_tool,
|
| 163 |
],
|
| 164 |
max_steps=6,
|
| 165 |
verbosity_level=1,
|
| 166 |
grammar=None,
|
| 167 |
planning_interval=None,
|
| 168 |
+
name="MultiSkill-Agent",
|
| 169 |
+
description="News, crypto, weather, history and timezone assistant",
|
| 170 |
+
prompt_templates=prompt_templates,
|
| 171 |
)
|
| 172 |
|
| 173 |
|
| 174 |
+
# ======================================================
|
| 175 |
+
# 🖥️ UI
|
| 176 |
+
# ======================================================
|
| 177 |
+
|
| 178 |
+
|
| 179 |
GradioUI(agent).launch()
|