Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,117 @@
|
|
| 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 |
-
def my_cutom_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 |
final_answer = FinalAnswerTool()
|
| 38 |
model = HfApiModel(
|
| 39 |
-
max_tokens=2096,
|
| 40 |
-
temperature=0.5,
|
| 41 |
-
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud'
|
| 42 |
-
custom_role_conversions=None,
|
| 43 |
)
|
| 44 |
|
| 45 |
-
|
| 46 |
# Import tool from Hub
|
| 47 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 48 |
|
|
@@ -51,7 +120,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 51 |
|
| 52 |
agent = CodeAgent(
|
| 53 |
model=model,
|
| 54 |
-
tools=[final_answer
|
| 55 |
max_steps=6,
|
| 56 |
verbosity_level=1,
|
| 57 |
grammar=None,
|
|
@@ -61,5 +130,4 @@ agent = CodeAgent(
|
|
| 61 |
prompt_templates=prompt_templates
|
| 62 |
)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
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 random
|
| 7 |
+
import math
|
| 8 |
+
import yfinance as yf
|
| 9 |
+
from forex_python.converter import CurrencyRates
|
| 10 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 11 |
from Gradio_UI import GradioUI
|
| 12 |
|
| 13 |
+
# Example of a custom tool
|
| 14 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
|
|
|
|
|
| 15 |
"""A tool that does nothing yet
|
| 16 |
Args:
|
| 17 |
arg1: the first argument
|
| 18 |
arg2: the second argument
|
| 19 |
"""
|
| 20 |
+
return "What magic will you build?"
|
| 21 |
|
| 22 |
@tool
|
| 23 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 24 |
+
"""Fetches the current local time in a specified timezone.
|
| 25 |
Args:
|
| 26 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 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 |
+
@tool
|
| 36 |
+
def calculator(expression: str) -> str:
|
| 37 |
+
"""Evaluates a mathematical expression and returns the result.
|
| 38 |
+
Args:
|
| 39 |
+
expression: A string representing a valid mathematical expression (e.g., '2 + 3 * 4').
|
| 40 |
+
"""
|
| 41 |
+
try:
|
| 42 |
+
result = eval(expression, {"__builtins__": None}, {"math": math})
|
| 43 |
+
return f"The result of {expression} is: {result}"
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"Error evaluating expression '{expression}': {str(e)}"
|
| 46 |
+
|
| 47 |
+
@tool
|
| 48 |
+
def python_repl(command: str) -> str:
|
| 49 |
+
"""Executes a Python command in a safe restricted environment.
|
| 50 |
+
Args:
|
| 51 |
+
command: A string representing a Python command (e.g., '2 ** 10').
|
| 52 |
+
"""
|
| 53 |
+
try:
|
| 54 |
+
result = eval(command, {"__builtins__": None}, {"math": math})
|
| 55 |
+
return f"Execution result: {result}"
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"Error executing command '{command}': {str(e)}"
|
| 58 |
+
|
| 59 |
+
@tool
|
| 60 |
+
def unit_converter(value: float, from_unit: str, to_unit: str) -> str:
|
| 61 |
+
"""Converts a given value from one unit to another (supports length and weight).
|
| 62 |
+
Args:
|
| 63 |
+
value: The numerical value to be converted.
|
| 64 |
+
from_unit: The unit to convert from (e.g., 'm', 'km', 'lb', 'kg').
|
| 65 |
+
to_unit: The unit to convert to.
|
| 66 |
+
"""
|
| 67 |
+
conversions = {
|
| 68 |
+
('m', 'km'): lambda x: x / 1000,
|
| 69 |
+
('km', 'm'): lambda x: x * 1000,
|
| 70 |
+
('lb', 'kg'): lambda x: x * 0.453592,
|
| 71 |
+
('kg', 'lb'): lambda x: x / 0.453592,
|
| 72 |
+
}
|
| 73 |
+
try:
|
| 74 |
+
result = conversions[(from_unit, to_unit)](value)
|
| 75 |
+
return f"{value} {from_unit} is equal to {result} {to_unit}"
|
| 76 |
+
except KeyError:
|
| 77 |
+
return "Conversion not supported."
|
| 78 |
+
|
| 79 |
+
@tool
|
| 80 |
+
def stock_price_lookup(ticker: str) -> str:
|
| 81 |
+
"""Fetches the latest stock price for a given ticker symbol.
|
| 82 |
+
Args:
|
| 83 |
+
ticker: The stock ticker symbol (e.g., 'AAPL' for Apple Inc.).
|
| 84 |
+
"""
|
| 85 |
+
try:
|
| 86 |
+
stock = yf.Ticker(ticker)
|
| 87 |
+
price = stock.history(period='1d')['Close'].iloc[-1]
|
| 88 |
+
return f"The latest stock price of {ticker} is ${price:.2f}"
|
| 89 |
+
except Exception as e:
|
| 90 |
+
return f"Error fetching stock price for {ticker}: {str(e)}"
|
| 91 |
+
|
| 92 |
+
@tool
|
| 93 |
+
def currency_converter(amount: float, from_currency: str, to_currency: str) -> str:
|
| 94 |
+
"""Converts currency based on the latest exchange rates.
|
| 95 |
+
Args:
|
| 96 |
+
amount: The amount of money to be converted.
|
| 97 |
+
from_currency: The currency to convert from (e.g., 'USD').
|
| 98 |
+
to_currency: The currency to convert to (e.g., 'EUR').
|
| 99 |
+
"""
|
| 100 |
+
try:
|
| 101 |
+
c = CurrencyRates()
|
| 102 |
+
converted_amount = c.convert(from_currency, to_currency, amount)
|
| 103 |
+
return f"{amount} {from_currency} is equal to {converted_amount:.2f} {to_currency}"
|
| 104 |
+
except Exception as e:
|
| 105 |
+
return f"Error converting currency from {from_currency} to {to_currency}: {str(e)}"
|
| 106 |
|
| 107 |
final_answer = FinalAnswerTool()
|
| 108 |
model = HfApiModel(
|
| 109 |
+
max_tokens=2096,
|
| 110 |
+
temperature=0.5,
|
| 111 |
+
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud',
|
| 112 |
+
custom_role_conversions=None,
|
| 113 |
)
|
| 114 |
|
|
|
|
| 115 |
# Import tool from Hub
|
| 116 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 117 |
|
|
|
|
| 120 |
|
| 121 |
agent = CodeAgent(
|
| 122 |
model=model,
|
| 123 |
+
tools=[final_answer, calculator, python_repl, unit_converter, stock_price_lookup, currency_converter],
|
| 124 |
max_steps=6,
|
| 125 |
verbosity_level=1,
|
| 126 |
grammar=None,
|
|
|
|
| 130 |
prompt_templates=prompt_templates
|
| 131 |
)
|
| 132 |
|
| 133 |
+
GradioUI(agent).launch()
|
|
|