Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,304 @@
|
|
| 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 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 14 |
-
"""A tool that does nothing yet
|
| 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"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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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 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
name=None,
|
| 64 |
-
description=None,
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
import wikipedia
|
| 7 |
+
import io
|
| 8 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
+
# ---------------------------------------------------
|
| 12 |
+
# TIME TOOL
|
| 13 |
+
# ---------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
@tool
|
| 16 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 17 |
+
"""
|
| 18 |
+
Fetch current time in a given timezone.
|
| 19 |
+
|
| 20 |
Args:
|
| 21 |
+
timezone: Timezone like 'Asia/Kolkata', 'America/New_York'
|
| 22 |
"""
|
| 23 |
try:
|
|
|
|
| 24 |
tz = pytz.timezone(timezone)
|
|
|
|
| 25 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 26 |
+
return f"Current time in {timezone}: {local_time}"
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error: {str(e)}"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# ---------------------------------------------------
|
| 32 |
+
# WEATHER TOOL
|
| 33 |
+
# ---------------------------------------------------
|
| 34 |
+
|
| 35 |
+
@tool
|
| 36 |
+
def get_weather(city: str) -> str:
|
| 37 |
+
"""
|
| 38 |
+
Get current weather of a city.
|
| 39 |
+
|
| 40 |
+
Args:
|
| 41 |
+
city: City name
|
| 42 |
+
"""
|
| 43 |
+
try:
|
| 44 |
+
geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1"
|
| 45 |
+
geo = requests.get(geo_url).json()
|
| 46 |
+
|
| 47 |
+
if "results" not in geo:
|
| 48 |
+
return f"City {city} not found."
|
| 49 |
+
|
| 50 |
+
lat = geo["results"][0]["latitude"]
|
| 51 |
+
lon = geo["results"][0]["longitude"]
|
| 52 |
+
|
| 53 |
+
weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t_weather=true"
|
| 54 |
+
weather = requests.get(weather_url).json()
|
| 55 |
+
|
| 56 |
+
temp = weather["current_weather"]["temperature"]
|
| 57 |
+
wind = weather["current_weather"]["windspeed"]
|
| 58 |
+
|
| 59 |
+
return f"Weather in {city}: {temp}°C, wind speed {wind} km/h"
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
return f"Weather lookup failed: {str(e)}"
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# ---------------------------------------------------
|
| 66 |
+
# CALCULATOR TOOL
|
| 67 |
+
# ---------------------------------------------------
|
| 68 |
+
|
| 69 |
+
@tool
|
| 70 |
+
def calculator(expression: str) -> str:
|
| 71 |
+
"""
|
| 72 |
+
Evaluate a mathematical expression.
|
| 73 |
+
|
| 74 |
+
Args:
|
| 75 |
+
expression: Example "25*4+10"
|
| 76 |
+
"""
|
| 77 |
+
try:
|
| 78 |
+
result = eval(expression, {"__builtins__": {}})
|
| 79 |
+
return str(result)
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return f"Calculation error: {str(e)}"
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# ---------------------------------------------------
|
| 85 |
+
# FETCH WEBPAGE TOOL
|
| 86 |
+
# ---------------------------------------------------
|
| 87 |
+
|
| 88 |
+
@tool
|
| 89 |
+
def fetch_webpage(url: str) -> str:
|
| 90 |
+
"""
|
| 91 |
+
Fetch webpage content.
|
| 92 |
+
|
| 93 |
+
Args:
|
| 94 |
+
url: Website URL
|
| 95 |
+
"""
|
| 96 |
+
try:
|
| 97 |
+
response = requests.get(url, timeout=10)
|
| 98 |
+
return response.text[:2000]
|
| 99 |
+
except Exception as e:
|
| 100 |
+
return f"Failed fetching webpage: {str(e)}"
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
# ---------------------------------------------------
|
| 104 |
+
# CRYPTO PRICE TOOL
|
| 105 |
+
# ---------------------------------------------------
|
| 106 |
+
|
| 107 |
+
@tool
|
| 108 |
+
def get_crypto_price(symbol: str) -> str:
|
| 109 |
+
"""
|
| 110 |
+
Get cryptocurrency price.
|
| 111 |
+
|
| 112 |
+
Args:
|
| 113 |
+
symbol: Crypto symbol like BTC, ETH
|
| 114 |
+
"""
|
| 115 |
+
try:
|
| 116 |
+
url = f"https://api.coingecko.com/api/v3/simple/price?ids={symbol.lower()}&vs_currencies=usd"
|
| 117 |
+
data = requests.get(url).json()
|
| 118 |
+
|
| 119 |
+
if symbol.lower() not in data:
|
| 120 |
+
return "Crypto not found."
|
| 121 |
+
|
| 122 |
+
price = data[symbol.lower()]["usd"]
|
| 123 |
+
return f"{symbol.upper()} price: ${price}"
|
| 124 |
+
|
| 125 |
+
except Exception as e:
|
| 126 |
+
return f"Crypto lookup failed: {str(e)}"
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
# ---------------------------------------------------
|
| 130 |
+
# NEWS TOOL
|
| 131 |
+
# ---------------------------------------------------
|
| 132 |
+
|
| 133 |
+
@tool
|
| 134 |
+
def get_news(topic: str) -> str:
|
| 135 |
+
"""
|
| 136 |
+
Get latest news about a topic.
|
| 137 |
+
|
| 138 |
+
Args:
|
| 139 |
+
topic: News topic
|
| 140 |
+
"""
|
| 141 |
+
try:
|
| 142 |
+
url = f"https://newsapi.org/v2/everything?q={topic}&pageSize=5&apiKey=demo"
|
| 143 |
+
data = requests.get(url).json()
|
| 144 |
+
|
| 145 |
+
articles = data.get("articles", [])
|
| 146 |
+
if not articles:
|
| 147 |
+
return "No news found."
|
| 148 |
+
|
| 149 |
+
result = ""
|
| 150 |
+
for a in articles[:5]:
|
| 151 |
+
result += f"{a['title']} - {a['source']['name']}\n"
|
| 152 |
+
|
| 153 |
+
return result
|
| 154 |
+
|
| 155 |
+
except Exception as e:
|
| 156 |
+
return f"News lookup failed: {str(e)}"
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
# ---------------------------------------------------
|
| 160 |
+
# WIKIPEDIA TOOL
|
| 161 |
+
# ---------------------------------------------------
|
| 162 |
+
|
| 163 |
+
@tool
|
| 164 |
+
def wikipedia_search(query: str) -> str:
|
| 165 |
+
"""
|
| 166 |
+
Search Wikipedia and return summary.
|
| 167 |
+
|
| 168 |
+
Args:
|
| 169 |
+
query: Topic to search
|
| 170 |
+
"""
|
| 171 |
+
try:
|
| 172 |
+
summary = wikipedia.summary(query, sentences=5)
|
| 173 |
+
return summary
|
| 174 |
+
except Exception as e:
|
| 175 |
+
return f"Wikipedia search failed: {str(e)}"
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
# ---------------------------------------------------
|
| 179 |
+
# YOUTUBE SEARCH TOOL
|
| 180 |
+
# ---------------------------------------------------
|
| 181 |
+
|
| 182 |
+
@tool
|
| 183 |
+
def youtube_search(query: str) -> str:
|
| 184 |
+
"""
|
| 185 |
+
Search YouTube videos.
|
| 186 |
+
|
| 187 |
+
Args:
|
| 188 |
+
query: Video topic
|
| 189 |
+
"""
|
| 190 |
+
try:
|
| 191 |
+
url = f"https://ytsearch.vercel.app/api?q={query}"
|
| 192 |
+
data = requests.get(url).json()
|
| 193 |
+
|
| 194 |
+
videos = data.get("videos", [])[:5]
|
| 195 |
+
|
| 196 |
+
results = ""
|
| 197 |
+
for v in videos:
|
| 198 |
+
results += f"{v['title']} - {v['url']}\n"
|
| 199 |
+
|
| 200 |
+
return results
|
| 201 |
+
|
| 202 |
except Exception as e:
|
| 203 |
+
return f"YouTube search failed: {str(e)}"
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
# ---------------------------------------------------
|
| 207 |
+
# PDF READER TOOL
|
| 208 |
+
# ---------------------------------------------------
|
| 209 |
+
|
| 210 |
+
@tool
|
| 211 |
+
def read_pdf_from_url(url: str) -> str:
|
| 212 |
+
"""
|
| 213 |
+
Read text from a PDF file.
|
| 214 |
+
|
| 215 |
+
Args:
|
| 216 |
+
url: Direct PDF URL
|
| 217 |
+
"""
|
| 218 |
+
try:
|
| 219 |
+
import PyPDF2
|
| 220 |
|
| 221 |
+
response = requests.get(url)
|
| 222 |
+
file = io.BytesIO(response.content)
|
| 223 |
+
|
| 224 |
+
reader = PyPDF2.PdfReader(file)
|
| 225 |
+
|
| 226 |
+
text = ""
|
| 227 |
+
for page in reader.pages[:3]:
|
| 228 |
+
text += page.extract_text()
|
| 229 |
+
|
| 230 |
+
return text[:2000]
|
| 231 |
+
|
| 232 |
+
except Exception as e:
|
| 233 |
+
return f"PDF reading failed: {str(e)}"
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
# ---------------------------------------------------
|
| 237 |
+
# LOAD FINAL ANSWER TOOL
|
| 238 |
+
# ---------------------------------------------------
|
| 239 |
|
| 240 |
final_answer = FinalAnswerTool()
|
| 241 |
|
| 242 |
+
# ---------------------------------------------------
|
| 243 |
+
# MODEL
|
| 244 |
+
# ---------------------------------------------------
|
| 245 |
|
| 246 |
model = HfApiModel(
|
| 247 |
+
max_tokens=2096,
|
| 248 |
+
temperature=0.5,
|
| 249 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 250 |
+
custom_role_conversions=None,
|
| 251 |
)
|
| 252 |
|
| 253 |
+
# ---------------------------------------------------
|
| 254 |
+
# LOAD HUB TOOL
|
| 255 |
+
# ---------------------------------------------------
|
| 256 |
|
| 257 |
+
image_generation_tool = load_tool(
|
| 258 |
+
"agents-course/text-to-image",
|
| 259 |
+
trust_remote_code=True
|
| 260 |
+
)
|
| 261 |
+
|
| 262 |
+
search_tool = DuckDuckGoSearchTool()
|
| 263 |
|
| 264 |
+
# ---------------------------------------------------
|
| 265 |
+
# LOAD PROMPTS
|
| 266 |
+
# ---------------------------------------------------
|
| 267 |
+
|
| 268 |
+
with open("prompts.yaml", "r") as stream:
|
| 269 |
prompt_templates = yaml.safe_load(stream)
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
# ---------------------------------------------------
|
| 273 |
+
# CREATE AGENT
|
| 274 |
+
# ---------------------------------------------------
|
| 275 |
+
|
| 276 |
agent = CodeAgent(
|
| 277 |
model=model,
|
| 278 |
+
tools=[
|
| 279 |
+
final_answer,
|
| 280 |
+
search_tool,
|
| 281 |
+
image_generation_tool,
|
| 282 |
+
get_current_time_in_timezone,
|
| 283 |
+
get_weather,
|
| 284 |
+
calculator,
|
| 285 |
+
fetch_webpage,
|
| 286 |
+
get_crypto_price,
|
| 287 |
+
get_news,
|
| 288 |
+
wikipedia_search,
|
| 289 |
+
youtube_search,
|
| 290 |
+
read_pdf_from_url
|
| 291 |
+
],
|
| 292 |
max_steps=6,
|
| 293 |
verbosity_level=1,
|
| 294 |
+
name="SmartAI-Agent",
|
| 295 |
+
description="An AI agent capable of search, weather lookup, crypto prices, calculations, reading PDFs, generating images, and web browsing.",
|
|
|
|
|
|
|
| 296 |
prompt_templates=prompt_templates
|
| 297 |
)
|
| 298 |
|
| 299 |
|
| 300 |
+
# ---------------------------------------------------
|
| 301 |
+
# GRADIO UI
|
| 302 |
+
# ---------------------------------------------------
|
| 303 |
+
|
| 304 |
GradioUI(agent).launch()
|