Currency convertor
Browse files
app.py
CHANGED
|
@@ -1,69 +1,76 @@
|
|
| 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_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 |
-
|
| 43 |
-
max_tokens=2096,
|
| 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,
|
| 47 |
-
)
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
with
|
| 54 |
-
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
| 56 |
agent = CodeAgent(
|
| 57 |
-
model=model
|
| 58 |
-
tools=[final_answer
|
| 59 |
-
max_steps=
|
| 60 |
verbosity_level=1,
|
| 61 |
-
grammar=None,
|
| 62 |
-
planning_interval=None,
|
| 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 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
+
# API Key for Currency Conversion (Use your own key if needed)
|
| 10 |
+
CURRENCY_API_KEY = "https://api.exchangerate-api.com/v4/latest/"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
@tool
|
| 13 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 14 |
+
"""Fetches the current local time in a specified timezone.
|
| 15 |
Args:
|
| 16 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 17 |
"""
|
| 18 |
try:
|
|
|
|
| 19 |
tz = pytz.timezone(timezone)
|
|
|
|
| 20 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 21 |
return f"The current local time in {timezone} is: {local_time}"
|
| 22 |
except Exception as e:
|
| 23 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 24 |
|
| 25 |
+
@tool
|
| 26 |
+
def convert_currency(amount: float, from_currency: str, to_currency: str) -> str:
|
| 27 |
+
"""Converts an amount from one currency to another using real-time exchange rates.
|
| 28 |
+
Args:
|
| 29 |
+
amount: The amount of money to convert.
|
| 30 |
+
from_currency: The 3-letter currency code to convert from (e.g., USD).
|
| 31 |
+
to_currency: The 3-letter currency code to convert to (e.g., EUR).
|
| 32 |
+
"""
|
| 33 |
+
try:
|
| 34 |
+
response = requests.get(f"{CURRENCY_API_KEY}{from_currency}")
|
| 35 |
+
data = response.json()
|
| 36 |
|
| 37 |
+
if "rates" not in data or to_currency not in data["rates"]:
|
| 38 |
+
return f"Invalid currency code: {from_currency} or {to_currency}. Please check and try again."
|
| 39 |
|
| 40 |
+
rate = data["rates"][to_currency]
|
| 41 |
+
converted_amount = round(amount * rate, 2)
|
| 42 |
|
| 43 |
+
return f"{amount} {from_currency} is equal to {converted_amount} {to_currency} at the rate of {rate}."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"Error fetching exchange rates: {str(e)}"
|
| 47 |
+
|
| 48 |
+
final_answer = FinalAnswerTool()
|
| 49 |
|
| 50 |
+
# Model Configuration
|
| 51 |
+
# Uncomment the model if needed
|
| 52 |
+
# model = HfApiModel(
|
| 53 |
+
# max_tokens=2096,
|
| 54 |
+
# temperature=0.5,
|
| 55 |
+
# model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # Alternative model
|
| 56 |
+
# custom_role_conversions=None,
|
| 57 |
+
# )
|
| 58 |
|
| 59 |
+
# Define Agent with Currency Converter Tool
|
|
|
|
|
|
|
| 60 |
agent = CodeAgent(
|
| 61 |
+
model=None, # Add your model if required
|
| 62 |
+
tools=[final_answer, get_current_time_in_timezone, convert_currency],
|
| 63 |
+
max_steps=4,
|
| 64 |
verbosity_level=1,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
+
# Launch UI
|
| 68 |
+
GradioUI(agent).launch()
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
|
| 76 |
GradioUI(agent).launch()
|