Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,95 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 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
|
| 13 |
-
|
| 14 |
-
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
-
def
|
| 23 |
-
"""
|
|
|
|
| 24 |
Args:
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
-
return f"
|
| 35 |
-
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
-
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 40 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 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:
|
| 54 |
prompt_templates = yaml.safe_load(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 +99,4 @@ agent = CodeAgent(
|
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool, DuckDuckGoSearchTool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
import base64
|
| 7 |
+
import os
|
| 8 |
+
import io
|
| 9 |
+
from PIL import Image
|
| 10 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 11 |
from Gradio_UI import GradioUI
|
| 12 |
|
| 13 |
+
# 1. функции-инструменты
|
| 14 |
+
|
| 15 |
@tool
|
| 16 |
+
def web_search(query: str) -> str:
|
| 17 |
+
"""Search the web for information using DuckDuckGo.
|
| 18 |
+
|
| 19 |
Args:
|
| 20 |
+
query: The search query to look up
|
| 21 |
+
|
| 22 |
+
Returns:
|
| 23 |
+
Search results from DuckDuckGo
|
| 24 |
"""
|
| 25 |
+
try:
|
| 26 |
+
search_tool = DuckDuckGoSearchTool()
|
| 27 |
+
results = search_tool(query)
|
| 28 |
+
return f"**Результаты поиска по '{query}':**\n\n{results}"
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"Ошибка поиска: {str(e)}"
|
| 31 |
|
| 32 |
@tool
|
| 33 |
+
def get_weather(city: str) -> str:
|
| 34 |
+
"""Get current weather for a city using wttr.in service.
|
| 35 |
+
|
| 36 |
Args:
|
| 37 |
+
city: The name of the city to get weather for (e.g., 'Moscow', 'London')
|
| 38 |
+
|
| 39 |
+
Returns:
|
| 40 |
+
Current weather information
|
| 41 |
"""
|
| 42 |
try:
|
| 43 |
+
url = f"http://wttr.in/{city}?format=%C+%t+%h+%w+%P&lang=ru&m"
|
| 44 |
+
response = requests.get(url, timeout=10)
|
| 45 |
+
|
| 46 |
+
if response.status_code == 200:
|
| 47 |
+
data = response.text.strip()
|
| 48 |
+
|
| 49 |
+
parts = data.split(' ')
|
| 50 |
+
if len(parts) >= 4:
|
| 51 |
+
condition = parts[0] # Погодные условия
|
| 52 |
+
temperature = parts[1] # Температура
|
| 53 |
+
humidity = parts[2] # Влажность
|
| 54 |
+
wind = parts[3] # Ветер
|
| 55 |
+
|
| 56 |
+
return (f"Погода в {city}:\n"
|
| 57 |
+
f"{temperature}\n"
|
| 58 |
+
f"{condition}\n"
|
| 59 |
+
f"Влажность: {humidity}\n"
|
| 60 |
+
f"Ветер: {wind}")
|
| 61 |
+
else:
|
| 62 |
+
return f"Погода в {city}: {data}"
|
| 63 |
+
else:
|
| 64 |
+
return f"Не удалось получить погоду для {city}"
|
| 65 |
+
|
| 66 |
except Exception as e:
|
| 67 |
+
return f"Ошибка: {str(e)}"
|
| 68 |
+
# 2. остальные объекты
|
| 69 |
|
| 70 |
final_answer = FinalAnswerTool()
|
| 71 |
|
|
|
|
|
|
|
|
|
|
| 72 |
model = HfApiModel(
|
| 73 |
+
max_tokens=2096,
|
| 74 |
+
temperature=0.5,
|
| 75 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 76 |
+
custom_role_conversions=None,
|
| 77 |
)
|
| 78 |
|
|
|
|
|
|
|
| 79 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 80 |
|
| 81 |
with open("prompts.yaml", 'r') as stream:
|
| 82 |
prompt_templates = yaml.safe_load(stream)
|
| 83 |
+
|
| 84 |
+
if "final_answer" not in prompt_templates:
|
| 85 |
+
prompt_templates["final_answer"] = {
|
| 86 |
+
"pre_messages": "Based on my research: ",
|
| 87 |
+
"post_messages": ""
|
| 88 |
+
}
|
| 89 |
|
| 90 |
agent = CodeAgent(
|
| 91 |
model=model,
|
| 92 |
+
tools=[final_answer, web_search,get_weather],
|
| 93 |
max_steps=6,
|
| 94 |
verbosity_level=1,
|
| 95 |
grammar=None,
|
|
|
|
| 99 |
prompt_templates=prompt_templates
|
| 100 |
)
|
| 101 |
|
|
|
|
| 102 |
GradioUI(agent).launch()
|