ArslanFOX commited on
Commit
6d2e6ba
·
verified ·
1 Parent(s): f549a40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -1,4 +1,7 @@
1
  import os
 
 
 
2
  from smolagents import CodeAgent, HfApiModel, tool
3
  import datetime
4
  import pytz
@@ -7,9 +10,11 @@ from geopy.geocoders import Nominatim
7
  from timezonefinder import TimezoneFinder
8
  import requests
9
 
10
- # Инструмент: Получение таймзоны по названию города
11
  @tool
12
  def get_timezone_by_city(city: str) -> str:
 
 
 
13
  if not isinstance(city, str):
14
  return f"Error: Expected a string for city, got: {type(city)}"
15
  try:
@@ -26,9 +31,11 @@ def get_timezone_by_city(city: str) -> str:
26
  except Exception as e:
27
  return f"Error fetching timezone: {str(e)}"
28
 
29
- # Инструмент: Время по таймзоне
30
  @tool
31
  def get_current_time_in_timezone(timezone: str) -> str:
 
 
 
32
  try:
33
  tz = pytz.timezone(timezone)
34
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
@@ -36,9 +43,11 @@ def get_current_time_in_timezone(timezone: str) -> str:
36
  except Exception as e:
37
  return f"Error fetching time: {str(e)}"
38
 
39
- # Инструмент: Качество воздуха
40
  @tool
41
  def get_air_quality(city: str, lang: str = "en") -> str:
 
 
 
42
  try:
43
  geolocator = Nominatim(user_agent="smolagents_bot")
44
  location = geolocator.geocode(city, language="en")
@@ -66,9 +75,11 @@ def get_air_quality(city: str, lang: str = "en") -> str:
66
  except Exception as e:
67
  return f"Error fetching air quality: {str(e)}"
68
 
69
- # Инструмент: Погода
70
  @tool
71
  def get_weather(city: str, lang: str = "en") -> str:
 
 
 
72
  try:
73
  geolocator = Nominatim(user_agent="smolagents_bot")
74
  location = geolocator.geocode(city, language="en")
@@ -89,19 +100,19 @@ def get_weather(city: str, lang: str = "en") -> str:
89
  except Exception as e:
90
  return f"Error fetching weather: {str(e)}"
91
 
92
- # Инструмент: Финальный ответ
93
  @tool
94
  def final_answer(answer: str) -> str:
 
 
 
95
  return answer
96
 
97
- # Модель
98
  model = HfApiModel(
99
  max_tokens=1000,
100
  temperature=0.5,
101
  model_id='mistralai/Mixtral-8x7B-Instruct-v0.1',
102
  )
103
 
104
- # System prompt — что может делать агент
105
  system_prompt = (
106
  "You are a helpful assistant. Given a user question, use ONLY the available tools: "
107
  "`get_timezone_by_city`, `get_current_time_in_timezone`, `get_air_quality`, `get_weather`, `final_answer`. "
@@ -111,7 +122,6 @@ system_prompt = (
111
  "```py\nfinal_answer('...')\n```"
112
  )
113
 
114
- # Агент
115
  agent = CodeAgent(
116
  model=model,
117
  tools=[get_timezone_by_city, get_current_time_in_timezone, get_air_quality, get_weather, final_answer],
@@ -123,7 +133,6 @@ agent = CodeAgent(
123
  verbosity_level=2,
124
  )
125
 
126
- # Gradio интерфейс
127
  def process_input(user_input):
128
  try:
129
  response = agent.run(user_input)
 
1
  import os
2
+ from dotenv import load_dotenv
3
+ load_dotenv()
4
+
5
  from smolagents import CodeAgent, HfApiModel, tool
6
  import datetime
7
  import pytz
 
10
  from timezonefinder import TimezoneFinder
11
  import requests
12
 
 
13
  @tool
14
  def get_timezone_by_city(city: str) -> str:
15
+ """
16
+ Get the timezone string for a given city name.
17
+ """
18
  if not isinstance(city, str):
19
  return f"Error: Expected a string for city, got: {type(city)}"
20
  try:
 
31
  except Exception as e:
32
  return f"Error fetching timezone: {str(e)}"
33
 
 
34
  @tool
35
  def get_current_time_in_timezone(timezone: str) -> str:
36
+ """
37
+ Return the current local time in the specified timezone.
38
+ """
39
  try:
40
  tz = pytz.timezone(timezone)
41
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
 
43
  except Exception as e:
44
  return f"Error fetching time: {str(e)}"
45
 
 
46
  @tool
47
  def get_air_quality(city: str, lang: str = "en") -> str:
48
+ """
49
+ Return formatted air quality information for a given city.
50
+ """
51
  try:
52
  geolocator = Nominatim(user_agent="smolagents_bot")
53
  location = geolocator.geocode(city, language="en")
 
75
  except Exception as e:
76
  return f"Error fetching air quality: {str(e)}"
77
 
 
78
  @tool
79
  def get_weather(city: str, lang: str = "en") -> str:
80
+ """
81
+ Return formatted weather information for a given city.
82
+ """
83
  try:
84
  geolocator = Nominatim(user_agent="smolagents_bot")
85
  location = geolocator.geocode(city, language="en")
 
100
  except Exception as e:
101
  return f"Error fetching weather: {str(e)}"
102
 
 
103
  @tool
104
  def final_answer(answer: str) -> str:
105
+ """
106
+ Return the final answer string to be displayed to the user.
107
+ """
108
  return answer
109
 
 
110
  model = HfApiModel(
111
  max_tokens=1000,
112
  temperature=0.5,
113
  model_id='mistralai/Mixtral-8x7B-Instruct-v0.1',
114
  )
115
 
 
116
  system_prompt = (
117
  "You are a helpful assistant. Given a user question, use ONLY the available tools: "
118
  "`get_timezone_by_city`, `get_current_time_in_timezone`, `get_air_quality`, `get_weather`, `final_answer`. "
 
122
  "```py\nfinal_answer('...')\n```"
123
  )
124
 
 
125
  agent = CodeAgent(
126
  model=model,
127
  tools=[get_timezone_by_city, get_current_time_in_timezone, get_air_quality, get_weather, final_answer],
 
133
  verbosity_level=2,
134
  )
135
 
 
136
  def process_input(user_input):
137
  try:
138
  response = agent.run(user_input)