Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,27 @@
|
|
| 1 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, load_tool, tool, HfApiModel
|
| 2 |
-
|
| 3 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 4 |
from Gradio_UI import GradioUI
|
| 5 |
|
| 6 |
import datetime
|
| 7 |
import pytz
|
| 8 |
import yaml
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# --- Custom Tool Example (You can edit this) ---
|
| 12 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
@@ -34,15 +49,18 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
|
| 37 |
-
# --- Load
|
| 38 |
final_answer = FinalAnswerTool()
|
| 39 |
-
|
| 40 |
model = HfApiModel(
|
| 41 |
model_id='Qwen/Qwen3-Coder-30B-A3B-Instruct',
|
| 42 |
max_tokens=2096,
|
| 43 |
temperature=0.5,
|
| 44 |
)
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# --- Load System Prompts ---
|
| 48 |
with open("prompts.yaml", 'r') as stream:
|
|
@@ -51,25 +69,18 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 51 |
# --- Load External Tools ---
|
| 52 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 53 |
|
| 54 |
-
# You can load more tools here if needed:
|
| 55 |
-
# search_tool = DuckDuckGoSearchTool() # Optional
|
| 56 |
-
|
| 57 |
# --- Create the Agent ---
|
| 58 |
agent = CodeAgent(
|
| 59 |
model=model,
|
| 60 |
tools=[
|
| 61 |
final_answer,
|
| 62 |
image_generation_tool,
|
| 63 |
-
#
|
| 64 |
-
# my_custom_tool,
|
| 65 |
-
# get_current_time_in_timezone
|
| 66 |
],
|
| 67 |
max_steps=6,
|
| 68 |
verbosity_level=1,
|
| 69 |
-
grammar=None,
|
| 70 |
-
planning_interval=None,
|
| 71 |
-
name=None,
|
| 72 |
-
description=None,
|
| 73 |
prompt_templates=prompt_templates
|
| 74 |
)
|
| 75 |
|
|
|
|
| 1 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, load_tool, tool, HfApiModel
|
|
|
|
| 2 |
from tools.final_answer import FinalAnswerTool
|
| 3 |
+
import Gradio_UI # ← we’ll patch its stream_to_gradio
|
| 4 |
from Gradio_UI import GradioUI
|
| 5 |
|
| 6 |
import datetime
|
| 7 |
import pytz
|
| 8 |
import yaml
|
| 9 |
|
| 10 |
+
# --- Monkey-patch to guard against None token counts ─────────────
|
| 11 |
+
# Wrap the original stream_to_gradio so that None → 0 for token counting
|
| 12 |
+
_original_stream = Gradio_UI.stream_to_gradio
|
| 13 |
+
def _patched_stream_to_gradio(agent, task, reset_agent_memory=False):
|
| 14 |
+
for msg in _original_stream(agent, task, reset_agent_memory):
|
| 15 |
+
# ensure these are ints
|
| 16 |
+
if agent.model.last_input_token_count is None:
|
| 17 |
+
agent.model.last_input_token_count = 0
|
| 18 |
+
if agent.model.last_output_token_count is None:
|
| 19 |
+
agent.model.last_output_token_count = 0
|
| 20 |
+
yield msg
|
| 21 |
+
|
| 22 |
+
Gradio_UI.stream_to_gradio = _patched_stream_to_gradio
|
| 23 |
+
# ────────────────────────────────────────────────────────────────────
|
| 24 |
+
|
| 25 |
|
| 26 |
# --- Custom Tool Example (You can edit this) ---
|
| 27 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
|
|
| 49 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 50 |
|
| 51 |
|
| 52 |
+
# --- Load and configure model ---
|
| 53 |
final_answer = FinalAnswerTool()
|
|
|
|
| 54 |
model = HfApiModel(
|
| 55 |
model_id='Qwen/Qwen3-Coder-30B-A3B-Instruct',
|
| 56 |
max_tokens=2096,
|
| 57 |
temperature=0.5,
|
| 58 |
)
|
| 59 |
|
| 60 |
+
# initialize counters so they're never None
|
| 61 |
+
model.last_input_token_count = 0
|
| 62 |
+
model.last_output_token_count = 0
|
| 63 |
+
|
| 64 |
|
| 65 |
# --- Load System Prompts ---
|
| 66 |
with open("prompts.yaml", 'r') as stream:
|
|
|
|
| 69 |
# --- Load External Tools ---
|
| 70 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 71 |
|
|
|
|
|
|
|
|
|
|
| 72 |
# --- Create the Agent ---
|
| 73 |
agent = CodeAgent(
|
| 74 |
model=model,
|
| 75 |
tools=[
|
| 76 |
final_answer,
|
| 77 |
image_generation_tool,
|
| 78 |
+
# DuckDuckGoSearchTool(), # optional
|
| 79 |
+
# tool(my_custom_tool), # if you decorate it
|
| 80 |
+
# tool(get_current_time_in_timezone), # likewise
|
| 81 |
],
|
| 82 |
max_steps=6,
|
| 83 |
verbosity_level=1,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
prompt_templates=prompt_templates
|
| 85 |
)
|
| 86 |
|