Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,22 @@
|
|
1 |
-
|
|
|
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 |
-
# Инструмент-заглушка, который пока ничего не делает
|
10 |
@tool
|
11 |
-
def
|
12 |
-
"""A tool that does nothing yet.
|
13 |
-
Args:
|
14 |
-
arg1: первый аргумент
|
15 |
-
arg2: второй аргумент (проверка)
|
16 |
-
"""
|
17 |
return "What magic will you build?"
|
18 |
|
19 |
-
# Инструмент для получения текущего времени в заданном часовом поясе
|
20 |
@tool
|
21 |
def get_current_time_in_timezone(timezone: str) -> str:
|
22 |
-
"""
|
23 |
-
Args:
|
24 |
-
timezone: строка с названием часового пояса (например, 'America/New_York').
|
25 |
-
"""
|
26 |
try:
|
27 |
tz = pytz.timezone(timezone)
|
28 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
@@ -30,13 +24,9 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
30 |
except Exception as e:
|
31 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
32 |
|
33 |
-
# Новый инструмент: получение текущей погоды по указанному местоположению
|
34 |
@tool
|
35 |
def get_weather(location: str) -> str:
|
36 |
-
"""
|
37 |
-
Args:
|
38 |
-
location: название местоположения (например, 'London').
|
39 |
-
"""
|
40 |
try:
|
41 |
response = requests.get(f"http://wttr.in/{location}?format=3")
|
42 |
if response.status_code == 200:
|
@@ -46,10 +36,9 @@ def get_weather(location: str) -> str:
|
|
46 |
except Exception as e:
|
47 |
return f"Error fetching weather: {str(e)}"
|
48 |
|
49 |
-
# Новый инструмент: получение случайной шутки
|
50 |
@tool
|
51 |
def get_random_joke() -> str:
|
52 |
-
"""
|
53 |
try:
|
54 |
response = requests.get("https://official-joke-api.appspot.com/random_joke")
|
55 |
if response.status_code == 200:
|
@@ -62,10 +51,9 @@ def get_random_joke() -> str:
|
|
62 |
except Exception as e:
|
63 |
return f"Error fetching joke: {str(e)}"
|
64 |
|
65 |
-
# Инструмент для вывода финального ответа
|
66 |
final_answer = FinalAnswerTool()
|
67 |
|
68 |
-
#
|
69 |
model = HfApiModel(
|
70 |
max_tokens=2096,
|
71 |
temperature=0.5,
|
@@ -73,19 +61,16 @@ model = HfApiModel(
|
|
73 |
custom_role_conversions=None,
|
74 |
)
|
75 |
|
76 |
-
# Загрузка инструмента генерации изображений с удалённого репозитория
|
77 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
78 |
|
79 |
-
# Загрузка шаблонов промтов из YAML-файла
|
80 |
with open("prompts.yaml", 'r') as stream:
|
81 |
prompt_templates = yaml.safe_load(stream)
|
82 |
-
|
83 |
-
# Создание агента с расширенным списком инструментов
|
84 |
agent = CodeAgent(
|
85 |
model=model,
|
86 |
tools=[
|
87 |
final_answer,
|
88 |
-
|
89 |
get_current_time_in_timezone,
|
90 |
get_weather,
|
91 |
get_random_joke,
|
@@ -101,5 +86,4 @@ agent = CodeAgent(
|
|
101 |
prompt_templates=prompt_templates
|
102 |
)
|
103 |
|
104 |
-
# Запуск пользовательского интерфейса через Gradio
|
105 |
GradioUI(agent).launch()
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
import datetime
|
4 |
import requests
|
5 |
import pytz
|
6 |
import yaml
|
7 |
+
|
8 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
9 |
from tools.final_answer import FinalAnswerTool
|
10 |
from Gradio_UI import GradioUI
|
11 |
|
|
|
12 |
@tool
|
13 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
14 |
+
"""A tool that does nothing yet."""
|
|
|
|
|
|
|
|
|
15 |
return "What magic will you build?"
|
16 |
|
|
|
17 |
@tool
|
18 |
def get_current_time_in_timezone(timezone: str) -> str:
|
19 |
+
"""Get the current local time in a specified timezone."""
|
|
|
|
|
|
|
20 |
try:
|
21 |
tz = pytz.timezone(timezone)
|
22 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
|
24 |
except Exception as e:
|
25 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
26 |
|
|
|
27 |
@tool
|
28 |
def get_weather(location: str) -> str:
|
29 |
+
"""Fetch the current weather for a given location."""
|
|
|
|
|
|
|
30 |
try:
|
31 |
response = requests.get(f"http://wttr.in/{location}?format=3")
|
32 |
if response.status_code == 200:
|
|
|
36 |
except Exception as e:
|
37 |
return f"Error fetching weather: {str(e)}"
|
38 |
|
|
|
39 |
@tool
|
40 |
def get_random_joke() -> str:
|
41 |
+
"""Fetch a random joke."""
|
42 |
try:
|
43 |
response = requests.get("https://official-joke-api.appspot.com/random_joke")
|
44 |
if response.status_code == 200:
|
|
|
51 |
except Exception as e:
|
52 |
return f"Error fetching joke: {str(e)}"
|
53 |
|
|
|
54 |
final_answer = FinalAnswerTool()
|
55 |
|
56 |
+
# Настройка модели (убедитесь, что модель доступна)
|
57 |
model = HfApiModel(
|
58 |
max_tokens=2096,
|
59 |
temperature=0.5,
|
|
|
61 |
custom_role_conversions=None,
|
62 |
)
|
63 |
|
|
|
64 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
65 |
|
|
|
66 |
with open("prompts.yaml", 'r') as stream:
|
67 |
prompt_templates = yaml.safe_load(stream)
|
68 |
+
|
|
|
69 |
agent = CodeAgent(
|
70 |
model=model,
|
71 |
tools=[
|
72 |
final_answer,
|
73 |
+
my_custom_tool,
|
74 |
get_current_time_in_timezone,
|
75 |
get_weather,
|
76 |
get_random_joke,
|
|
|
86 |
prompt_templates=prompt_templates
|
87 |
)
|
88 |
|
|
|
89 |
GradioUI(agent).launch()
|