Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,97 @@
|
|
| 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 |
@tool
|
| 12 |
-
def my_cutom_tool(arg1:str, arg2:int)-> str:
|
| 13 |
-
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
-
arg1:
|
| 17 |
-
arg2:
|
| 18 |
"""
|
| 19 |
-
return "What magic will you build
|
| 20 |
|
|
|
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
| 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"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 |
final_answer = FinalAnswerTool()
|
|
|
|
|
|
|
| 38 |
model = HfApiModel(
|
| 39 |
-
max_tokens=2096,
|
| 40 |
-
temperature=0.5,
|
| 41 |
-
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud'
|
| 42 |
-
custom_role_conversions=None,
|
| 43 |
)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
# Import tool from Hub
|
| 47 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 48 |
|
|
|
|
| 49 |
with open("prompts.yaml", 'r') as stream:
|
| 50 |
prompt_templates = yaml.safe_load(stream)
|
| 51 |
|
|
|
|
| 52 |
agent = CodeAgent(
|
| 53 |
model=model,
|
| 54 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
max_steps=6,
|
| 56 |
verbosity_level=1,
|
| 57 |
grammar=None,
|
|
@@ -61,5 +101,5 @@ agent = CodeAgent(
|
|
| 61 |
prompt_templates=prompt_templates
|
| 62 |
)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
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 |
+
# Инструмент-заглушка, который пока ничего не делает
|
| 10 |
@tool
|
| 11 |
+
def my_cutom_tool(arg1: str, arg2: int) -> str:
|
| 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 |
"""A tool that fetches the current local time in a specified timezone.
|
| 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")
|
| 29 |
return f"The current local time in {timezone} is: {local_time}"
|
| 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 |
+
"""A tool that fetches the current weather for a given location.
|
| 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:
|
| 43 |
+
return response.text
|
| 44 |
+
else:
|
| 45 |
+
return f"Error: Received status code {response.status_code}"
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"Error fetching weather: {str(e)}"
|
| 48 |
|
| 49 |
+
# Новый инструмент: получение случайной шутки
|
| 50 |
+
@tool
|
| 51 |
+
def get_random_joke() -> str:
|
| 52 |
+
"""A tool that fetches a random joke."""
|
| 53 |
+
try:
|
| 54 |
+
response = requests.get("https://official-joke-api.appspot.com/random_joke")
|
| 55 |
+
if response.status_code == 200:
|
| 56 |
+
joke_data = response.json()
|
| 57 |
+
setup = joke_data.get("setup", "")
|
| 58 |
+
punchline = joke_data.get("punchline", "")
|
| 59 |
+
return f"{setup} - {punchline}"
|
| 60 |
+
else:
|
| 61 |
+
return f"Error: Received status code {response.status_code}"
|
| 62 |
+
except Exception as e:
|
| 63 |
+
return f"Error fetching joke: {str(e)}"
|
| 64 |
+
|
| 65 |
+
# Инструмент для вывода финального ответа
|
| 66 |
final_answer = FinalAnswerTool()
|
| 67 |
+
|
| 68 |
+
# Инициализация модели для агента через API Hugging Face
|
| 69 |
model = HfApiModel(
|
| 70 |
+
max_tokens=2096,
|
| 71 |
+
temperature=0.5,
|
| 72 |
+
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud',
|
| 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 |
+
my_cutom_tool,
|
| 89 |
+
get_current_time_in_timezone,
|
| 90 |
+
get_weather,
|
| 91 |
+
get_random_joke,
|
| 92 |
+
image_generation_tool,
|
| 93 |
+
DuckDuckGoSearchTool()
|
| 94 |
+
],
|
| 95 |
max_steps=6,
|
| 96 |
verbosity_level=1,
|
| 97 |
grammar=None,
|
|
|
|
| 101 |
prompt_templates=prompt_templates
|
| 102 |
)
|
| 103 |
|
| 104 |
+
# Запуск пользовательского интерфейса через Gradio
|
| 105 |
+
GradioUI(agent).launch()
|