Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,66 @@
|
|
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 |
-
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
11 |
@tool
|
12 |
-
def my_custom_tool(arg1:str, arg2:int)-> str:
|
13 |
-
|
14 |
-
"""A tool that does nothing yet
|
15 |
Args:
|
16 |
-
arg1:
|
17 |
-
arg2:
|
18 |
"""
|
19 |
-
return "
|
20 |
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
23 |
-
"""
|
24 |
Args:
|
25 |
-
timezone:
|
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"
|
33 |
except Exception as e:
|
34 |
-
return f"
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
41 |
|
|
|
42 |
model = HfApiModel(
|
43 |
-
max_tokens=2096,
|
44 |
-
temperature=0.5,
|
45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
46 |
-
custom_role_conversions=None,
|
47 |
)
|
48 |
|
49 |
-
|
50 |
-
# Import tool from Hub
|
51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
52 |
|
53 |
with open("prompts.yaml", 'r') as stream:
|
@@ -55,7 +68,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
55 |
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[final_answer
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
@@ -65,5 +78,4 @@ agent = CodeAgent(
|
|
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 |
+
import random
|
7 |
from tools.final_answer import FinalAnswerTool
|
|
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
|
|
10 |
@tool
|
11 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
12 |
+
"""Простой инструмент, который возвращает строку с аргументами.
|
|
|
13 |
Args:
|
14 |
+
arg1: первый аргумент (строка)
|
15 |
+
arg2: второй аргумент (число)
|
16 |
"""
|
17 |
+
return f"Вы передали аргументы: {arg1} и {arg2}"
|
18 |
|
19 |
@tool
|
20 |
def get_current_time_in_timezone(timezone: str) -> str:
|
21 |
+
"""Получает текущее локальное время в указанном часовом поясе.
|
22 |
Args:
|
23 |
+
timezone: Название часового пояса (например, 'Europe/Moscow').
|
24 |
"""
|
25 |
try:
|
|
|
26 |
tz = pytz.timezone(timezone)
|
|
|
27 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
28 |
+
return f"Текущее время в {timezone}: {local_time}"
|
29 |
except Exception as e:
|
30 |
+
return f"Ошибка: {str(e)}"
|
31 |
|
32 |
+
@tool
|
33 |
+
def generate_random_number(min_value: int, max_value: int) -> int:
|
34 |
+
"""Генерирует случайное число в заданном диапазоне.
|
35 |
+
Args:
|
36 |
+
min_value: Минимальное значение.
|
37 |
+
max_value: Максимальное значение.
|
38 |
+
"""
|
39 |
+
return random.randint(min_value, max_value)
|
40 |
|
41 |
+
@tool
|
42 |
+
def convert_text_case(text: str, to_upper: bool) -> str:
|
43 |
+
"""Конвертирует текст в верхний или нижний регистр.
|
44 |
+
Args:
|
45 |
+
text: Исходный текст.
|
46 |
+
to_upper: Если True - в верхний регистр, иначе - в нижний.
|
47 |
+
"""
|
48 |
+
return text.upper() if to_upper else text.lower()
|
49 |
|
50 |
+
@tool
|
51 |
+
def get_current_date() -> str:
|
52 |
+
"""Возвращает текущую дату и день недели."""
|
53 |
+
now = datetime.datetime.now()
|
54 |
+
return now.strftime("Сегодня %Y-%m-%d, %A")
|
55 |
|
56 |
+
final_answer = FinalAnswerTool()
|
57 |
model = HfApiModel(
|
58 |
+
max_tokens=2096,
|
59 |
+
temperature=0.5,
|
60 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
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:
|
|
|
68 |
|
69 |
agent = CodeAgent(
|
70 |
model=model,
|
71 |
+
tools=[final_answer, my_custom_tool, get_current_time_in_timezone, generate_random_number, convert_text_case, get_current_date],
|
72 |
max_steps=6,
|
73 |
verbosity_level=1,
|
74 |
grammar=None,
|
|
|
78 |
prompt_templates=prompt_templates
|
79 |
)
|
80 |
|
81 |
+
GradioUI(agent).launch()
|
|