MallikarjunSonna commited on
Commit
07f84c5
·
verified ·
1 Parent(s): 0c4af1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -19
app.py CHANGED
@@ -6,12 +6,16 @@ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, t
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
 
9
- # 1. Tool: Get Current Time in Any Timezone
 
 
10
  @tool
11
  def get_current_time_in_timezone(timezone: str) -> str:
12
  """Fetches the current local time in a specified timezone.
13
  Args:
14
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
 
 
15
  """
16
  try:
17
  tz = pytz.timezone(timezone)
@@ -20,14 +24,16 @@ def get_current_time_in_timezone(timezone: str) -> str:
20
  except Exception as e:
21
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
22
 
23
- # 2. Tool: Get Current Weather Data (Requires OpenWeatherMap API)
24
  @tool
25
  def get_weather(city: str) -> str:
26
  """Fetches current weather information for a specified city.
27
  Args:
28
  city: The name of the city (e.g., 'New York').
 
 
29
  """
30
- api_key = "ac662b5c2463cfa522e1acd2d4cf5a81" # Replace with your OpenWeather API key
31
  url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
32
 
33
  try:
@@ -41,32 +47,36 @@ def get_weather(city: str) -> str:
41
  except Exception as e:
42
  return f"Error fetching weather data: {str(e)}"
43
 
44
- # 3. Tool: Currency Exchange Converter
45
  @tool
46
  def convert_currency(amount: float, from_currency: str, to_currency: str) -> str:
47
  """Converts currency from one type to another using real-time exchange rates.
48
  Args:
49
  amount: The amount to convert.
50
- from_currency: The source currency code (e.g., 'USD').
51
- to_currency: The target currency code (e.g., 'INR').
 
 
52
  """
53
- api_key = "90171124ae953d74d1515263" # Replace with a valid API key from ExchangeRate API
54
  url = f"https://v6.exchangerate-api.com/v6/{api_key}/latest/{from_currency}"
55
 
56
  try:
57
  response = requests.get(url).json()
58
  if "conversion_rates" not in response:
59
- return f"Error: Unable to fetch exchange rates."
60
 
61
- rate = response["conversion_rates"].get(to_currency, None)
62
  if rate is None:
63
- return f"Error: Unsupported currency code."
64
 
65
  converted_amount = round(amount * rate, 2)
66
  return f"{amount} {from_currency} is equal to {converted_amount} {to_currency}."
67
  except Exception as e:
68
  return f"Error fetching exchange rates: {str(e)}"
69
-
 
 
70
  # Load Hugging Face Text-to-Image Tool
71
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
72
 
@@ -78,14 +88,13 @@ model = HfApiModel(
78
  max_tokens=2096,
79
  temperature=0.7,
80
  model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
81
- custom_role_conversions=None,
82
  )
83
 
84
  # Load Prompt Templates
85
  with open("prompts.yaml", "r") as stream:
86
  prompt_templates = yaml.safe_load(stream)
87
 
88
- # Create Agent with Additional Tools
89
  agent = CodeAgent(
90
  model=model,
91
  tools=[
@@ -98,12 +107,10 @@ agent = CodeAgent(
98
  ],
99
  max_steps=6,
100
  verbosity_level=1,
101
- grammar=None,
102
- planning_interval=None,
103
- name=None,
104
- description=None,
105
  prompt_templates=prompt_templates,
106
  )
107
 
 
 
108
  # Launch Gradio UI
109
- GradioUI(agent).launch()
 
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
 
9
+ # ---------------------------- TOOLS ----------------------------
10
+
11
+ # 1. Get Current Time in Any Timezone
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 valid timezone string (e.g., 'America/New_York').
17
+ Returns:
18
+ A string with the current local time in the requested timezone.
19
  """
20
  try:
21
  tz = pytz.timezone(timezone)
 
24
  except Exception as e:
25
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
26
 
27
+ # 2. Get Current Weather Data
28
  @tool
29
  def get_weather(city: str) -> str:
30
  """Fetches current weather information for a specified city.
31
  Args:
32
  city: The name of the city (e.g., 'New York').
33
+ Returns:
34
+ A string with the weather description and temperature.
35
  """
36
+ api_key = "YOUR_OPENWEATHER_API_KEY" # Replace with your actual API key
37
  url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
38
 
39
  try:
 
47
  except Exception as e:
48
  return f"Error fetching weather data: {str(e)}"
49
 
50
+ # 3. Currency Exchange Converter
51
  @tool
52
  def convert_currency(amount: float, from_currency: str, to_currency: str) -> str:
53
  """Converts currency from one type to another using real-time exchange rates.
54
  Args:
55
  amount: The amount to convert.
56
+ from_currency: Source currency code (e.g., 'USD').
57
+ to_currency: Target currency code (e.g., 'INR').
58
+ Returns:
59
+ A string with the converted currency amount.
60
  """
61
+ api_key = "YOUR_EXCHANGE_RATE_API_KEY" # Replace with your actual API key
62
  url = f"https://v6.exchangerate-api.com/v6/{api_key}/latest/{from_currency}"
63
 
64
  try:
65
  response = requests.get(url).json()
66
  if "conversion_rates" not in response:
67
+ return "Error: Unable to fetch exchange rates."
68
 
69
+ rate = response["conversion_rates"].get(to_currency)
70
  if rate is None:
71
+ return "Error: Unsupported currency code."
72
 
73
  converted_amount = round(amount * rate, 2)
74
  return f"{amount} {from_currency} is equal to {converted_amount} {to_currency}."
75
  except Exception as e:
76
  return f"Error fetching exchange rates: {str(e)}"
77
+
78
+ # ---------------------------- MODEL & AGENT SETUP ----------------------------
79
+
80
  # Load Hugging Face Text-to-Image Tool
81
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
82
 
 
88
  max_tokens=2096,
89
  temperature=0.7,
90
  model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
 
91
  )
92
 
93
  # Load Prompt Templates
94
  with open("prompts.yaml", "r") as stream:
95
  prompt_templates = yaml.safe_load(stream)
96
 
97
+ # Create AI Agent with Defined Tools
98
  agent = CodeAgent(
99
  model=model,
100
  tools=[
 
107
  ],
108
  max_steps=6,
109
  verbosity_level=1,
 
 
 
 
110
  prompt_templates=prompt_templates,
111
  )
112
 
113
+ # ---------------------------- LAUNCH APPLICATION ----------------------------
114
+
115
  # Launch Gradio UI
116
+ GradioUI(agent).launch()