Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,30 +4,26 @@ import requests
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
|
| 7 |
-
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 8 |
from tools.final_answer import FinalAnswerTool
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
# Custom tools
|
| 13 |
-
# ---------------------------
|
| 14 |
|
| 15 |
@tool
|
| 16 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 17 |
-
"""
|
| 18 |
-
A tool that demonstrates how to define custom functions.
|
| 19 |
Args:
|
| 20 |
-
arg1:
|
| 21 |
-
arg2:
|
| 22 |
"""
|
| 23 |
-
return f"
|
| 24 |
|
| 25 |
@tool
|
| 26 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 27 |
-
"""
|
| 28 |
-
A tool that fetches the current local time in a specified timezone.
|
| 29 |
Args:
|
| 30 |
-
timezone:
|
| 31 |
"""
|
| 32 |
try:
|
| 33 |
tz = pytz.timezone(timezone)
|
|
@@ -38,10 +34,9 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 38 |
|
| 39 |
@tool
|
| 40 |
def get_eur_exchange_rate(currency_code: str) -> str:
|
| 41 |
-
"""
|
| 42 |
-
A tool that retrieves the current exchange rate from EUR to a given currency.
|
| 43 |
Args:
|
| 44 |
-
currency_code:
|
| 45 |
"""
|
| 46 |
try:
|
| 47 |
api_url = "https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/eur.json"
|
|
@@ -57,13 +52,11 @@ def get_eur_exchange_rate(currency_code: str) -> str:
|
|
| 57 |
else:
|
| 58 |
return f"Currency code '{currency_code}' not found."
|
| 59 |
except Exception as e:
|
| 60 |
-
return f"Error fetching exchange rate
|
| 61 |
|
| 62 |
@tool
|
| 63 |
def get_simple_joke() -> str:
|
| 64 |
-
"""
|
| 65 |
-
A tool that fetches a random joke from JokeAPI with minimal filtering.
|
| 66 |
-
"""
|
| 67 |
try:
|
| 68 |
api_url = "https://v2.jokeapi.dev/joke/any"
|
| 69 |
response = requests.get(api_url, timeout=10)
|
|
@@ -78,51 +71,45 @@ def get_simple_joke() -> str:
|
|
| 78 |
elif joke_data.get("type") == "twopart":
|
| 79 |
return f"Here's a joke:\n\n{joke_data.get('setup', '')}\n{joke_data.get('delivery', '')}"
|
| 80 |
|
| 81 |
-
return "Could not parse joke from API response"
|
| 82 |
except Exception as e:
|
| 83 |
-
return f"Error: {str(e)}"
|
| 84 |
-
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
# Setup model and agent
|
| 88 |
-
# ---------------------------
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
|
| 93 |
model = HfApiModel(
|
| 94 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 95 |
-
max_tokens=
|
| 96 |
temperature=0.5,
|
| 97 |
-
|
| 98 |
)
|
| 99 |
|
| 100 |
-
#
|
| 101 |
-
|
|
|
|
| 102 |
|
| 103 |
-
#
|
| 104 |
with open("prompts.yaml", "r") as stream:
|
| 105 |
prompt_templates = yaml.safe_load(stream)
|
| 106 |
|
| 107 |
-
#
|
|
|
|
|
|
|
| 108 |
agent = CodeAgent(
|
| 109 |
model=model,
|
| 110 |
-
tools=[
|
| 111 |
-
|
| 112 |
-
my_custom_tool,
|
| 113 |
-
get_current_time_in_timezone,
|
| 114 |
-
get_eur_exchange_rate,
|
| 115 |
-
get_simple_joke,
|
| 116 |
-
image_generation_tool,
|
| 117 |
-
],
|
| 118 |
max_steps=6,
|
| 119 |
verbosity_level=1,
|
| 120 |
-
grammar=None,
|
| 121 |
-
planning_interval=None,
|
| 122 |
prompt_templates=prompt_templates,
|
| 123 |
)
|
| 124 |
|
| 125 |
-
#
|
| 126 |
-
|
| 127 |
-
#
|
| 128 |
-
GradioUI(agent).launch()
|
|
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
|
| 7 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 8 |
from tools.final_answer import FinalAnswerTool
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
+
# ---------------------- Custom Tools ----------------------
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@tool
|
| 14 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 15 |
+
"""A simple example tool that just echoes its inputs.
|
|
|
|
| 16 |
Args:
|
| 17 |
+
arg1: the first argument
|
| 18 |
+
arg2: the second argument
|
| 19 |
"""
|
| 20 |
+
return f"Tool received arg1={arg1}, arg2={arg2}"
|
| 21 |
|
| 22 |
@tool
|
| 23 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 24 |
+
"""Fetches the current local time in a specified timezone.
|
|
|
|
| 25 |
Args:
|
| 26 |
+
timezone: e.g., 'America/New_York'
|
| 27 |
"""
|
| 28 |
try:
|
| 29 |
tz = pytz.timezone(timezone)
|
|
|
|
| 34 |
|
| 35 |
@tool
|
| 36 |
def get_eur_exchange_rate(currency_code: str) -> str:
|
| 37 |
+
"""Retrieves the current exchange rate from EUR to a given currency.
|
|
|
|
| 38 |
Args:
|
| 39 |
+
currency_code: 3-letter code, e.g. "usd", "gbp"
|
| 40 |
"""
|
| 41 |
try:
|
| 42 |
api_url = "https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/eur.json"
|
|
|
|
| 52 |
else:
|
| 53 |
return f"Currency code '{currency_code}' not found."
|
| 54 |
except Exception as e:
|
| 55 |
+
return f"Error fetching exchange rate: {str(e)}"
|
| 56 |
|
| 57 |
@tool
|
| 58 |
def get_simple_joke() -> str:
|
| 59 |
+
"""Fetches a random joke from JokeAPI."""
|
|
|
|
|
|
|
| 60 |
try:
|
| 61 |
api_url = "https://v2.jokeapi.dev/joke/any"
|
| 62 |
response = requests.get(api_url, timeout=10)
|
|
|
|
| 71 |
elif joke_data.get("type") == "twopart":
|
| 72 |
return f"Here's a joke:\n\n{joke_data.get('setup', '')}\n{joke_data.get('delivery', '')}"
|
| 73 |
|
| 74 |
+
return "Could not parse joke from API response."
|
| 75 |
except Exception as e:
|
| 76 |
+
return f"Error fetching joke: {str(e)}"
|
|
|
|
| 77 |
|
| 78 |
+
# ---------------------- Model ----------------------
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
# ВАЖНО: токен нужно добавить в Secrets как HF_TOKEN
|
| 81 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 82 |
+
if not hf_token:
|
| 83 |
+
raise RuntimeError("❌ HF_TOKEN is not set in repository secrets!")
|
| 84 |
|
| 85 |
model = HfApiModel(
|
| 86 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 87 |
+
max_tokens=2048,
|
| 88 |
temperature=0.5,
|
| 89 |
+
api_key=hf_token, # <-- токен берется из Secrets
|
| 90 |
)
|
| 91 |
|
| 92 |
+
# ---------------------- Agent ----------------------
|
| 93 |
+
|
| 94 |
+
final_answer = FinalAnswerTool()
|
| 95 |
|
| 96 |
+
# Подгружаем промпты
|
| 97 |
with open("prompts.yaml", "r") as stream:
|
| 98 |
prompt_templates = yaml.safe_load(stream)
|
| 99 |
|
| 100 |
+
# Импорт примера инструмента с Hugging Face Hub
|
| 101 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 102 |
+
|
| 103 |
agent = CodeAgent(
|
| 104 |
model=model,
|
| 105 |
+
tools=[final_answer, my_custom_tool, get_current_time_in_timezone,
|
| 106 |
+
get_eur_exchange_rate, get_simple_joke, image_generation_tool],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
max_steps=6,
|
| 108 |
verbosity_level=1,
|
|
|
|
|
|
|
| 109 |
prompt_templates=prompt_templates,
|
| 110 |
)
|
| 111 |
|
| 112 |
+
# ---------------------- UI ----------------------
|
| 113 |
+
|
| 114 |
+
# Исправлено: убран подсчет last_input_token_count (он может быть None)
|
| 115 |
+
GradioUI(agent).launch(share=True)
|