Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,105 @@
|
|
| 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 |
-
@tool
|
| 12 |
-
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
| 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: A string representing a valid timezone (e.g., 'America/New_York').
|
| 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='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
# Import tool from Hub
|
| 51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
|
| 53 |
with open("prompts.yaml", 'r') as stream:
|
| 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 |
grammar=None,
|
|
@@ -65,5 +109,5 @@ agent = CodeAgent(
|
|
| 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 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
+
# Tool: Fetch current time in a given timezone
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
@tool
|
| 11 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 12 |
+
"""Fetches the current local time in a specified timezone."""
|
|
|
|
|
|
|
|
|
|
| 13 |
try:
|
|
|
|
| 14 |
tz = pytz.timezone(timezone)
|
|
|
|
| 15 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 16 |
return f"The current local time in {timezone} is: {local_time}"
|
| 17 |
except Exception as e:
|
| 18 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 19 |
|
| 20 |
+
# Tool: Fetch real-time weather for a given location
|
| 21 |
+
@tool
|
| 22 |
+
def get_weather(city: str) -> str:
|
| 23 |
+
"""Fetches the current weather for a given city."""
|
| 24 |
+
api_url = f"https://wttr.in/{city}?format=%C+%t"
|
| 25 |
+
response = requests.get(api_url)
|
| 26 |
+
if response.status_code == 200:
|
| 27 |
+
return response.text
|
| 28 |
+
return "Unable to retrieve weather information."
|
| 29 |
|
| 30 |
+
# Tool: Fetch currency exchange rate
|
| 31 |
+
@tool
|
| 32 |
+
def get_exchange_rate(base_currency: str, target_currency: str) -> str:
|
| 33 |
+
"""Fetches the exchange rate between two currencies."""
|
| 34 |
+
url = f"https://api.exchangerate-api.com/v4/latest/{base_currency}"
|
| 35 |
+
response = requests.get(url)
|
| 36 |
+
if response.status_code == 200:
|
| 37 |
+
data = response.json()
|
| 38 |
+
rate = data['rates'].get(target_currency, "N/A")
|
| 39 |
+
return f"1 {base_currency} = {rate} {target_currency}"
|
| 40 |
+
return "Exchange rate information not available."
|
| 41 |
|
| 42 |
+
# Tool: Fetch Wikipedia summary
|
| 43 |
+
@tool
|
| 44 |
+
def get_wikipedia_summary(query: str) -> str:
|
| 45 |
+
"""Fetches a short summary from Wikipedia."""
|
| 46 |
+
url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{query}"
|
| 47 |
+
response = requests.get(url)
|
| 48 |
+
if response.status_code == 200:
|
| 49 |
+
return response.json().get("extract", "No summary available.")
|
| 50 |
+
return "Wikipedia summary not found."
|
| 51 |
|
| 52 |
+
# Tool: Fetch latest news headlines
|
| 53 |
+
@tool
|
| 54 |
+
def get_latest_news() -> str:
|
| 55 |
+
"""Fetches the latest news headlines."""
|
| 56 |
+
url = "https://newsapi.org/v2/top-headlines?country=us&apiKey=YOUR_NEWSAPI_KEY"
|
| 57 |
+
response = requests.get(url)
|
| 58 |
+
if response.status_code == 200:
|
| 59 |
+
articles = response.json().get("articles", [])[:5]
|
| 60 |
+
return "\n".join([f"{i+1}. {article['title']}" for i, article in enumerate(articles)])
|
| 61 |
+
return "Unable to retrieve news."
|
| 62 |
+
|
| 63 |
+
# Tool: Fetch stock price for a given company symbol
|
| 64 |
+
@tool
|
| 65 |
+
def get_stock_price(symbol: str) -> str:
|
| 66 |
+
"""Fetches the latest stock price for a given company symbol."""
|
| 67 |
+
url = f"https://finnhub.io/api/v1/quote?symbol={symbol}&token=YOUR_FINNHUB_API_KEY"
|
| 68 |
+
response = requests.get(url)
|
| 69 |
+
if response.status_code == 200:
|
| 70 |
+
data = response.json()
|
| 71 |
+
return f"Stock price of {symbol}: ${data.get('c', 'N/A')}"
|
| 72 |
+
return "Stock information not available."
|
| 73 |
+
|
| 74 |
+
final_answer = FinalAnswerTool()
|
| 75 |
+
|
| 76 |
+
# Hugging Face model setup
|
| 77 |
model = HfApiModel(
|
| 78 |
+
max_tokens=2096,
|
| 79 |
+
temperature=0.5,
|
| 80 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 81 |
+
custom_role_conversions=None,
|
| 82 |
)
|
| 83 |
|
| 84 |
+
# Load image generation tool from Hugging Face
|
|
|
|
| 85 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 86 |
|
| 87 |
with open("prompts.yaml", 'r') as stream:
|
| 88 |
prompt_templates = yaml.safe_load(stream)
|
| 89 |
|
| 90 |
+
# Creating the AI agent
|
| 91 |
agent = CodeAgent(
|
| 92 |
model=model,
|
| 93 |
+
tools=[
|
| 94 |
+
final_answer,
|
| 95 |
+
get_current_time_in_timezone,
|
| 96 |
+
get_weather,
|
| 97 |
+
get_exchange_rate,
|
| 98 |
+
get_wikipedia_summary,
|
| 99 |
+
get_latest_news,
|
| 100 |
+
get_stock_price,
|
| 101 |
+
image_generation_tool,
|
| 102 |
+
],
|
| 103 |
max_steps=6,
|
| 104 |
verbosity_level=1,
|
| 105 |
grammar=None,
|
|
|
|
| 109 |
prompt_templates=prompt_templates
|
| 110 |
)
|
| 111 |
|
| 112 |
+
# Launch Gradio UI for interaction
|
| 113 |
+
GradioUI(agent).launch()
|