Update app.py
#460
by
vasili01
- opened
app.py
CHANGED
@@ -1,69 +1,95 @@
|
|
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: #it's import to specify the return type
|
13 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
14 |
-
"""A tool that does nothing yet
|
15 |
-
Args:
|
16 |
-
arg1: the first argument
|
17 |
-
arg2: the second argument
|
18 |
-
"""
|
19 |
-
return "What magic will you build ?"
|
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"Error
|
35 |
-
|
36 |
|
|
|
37 |
final_answer = FinalAnswerTool()
|
38 |
|
39 |
-
#
|
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 |
-
#
|
51 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
52 |
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[final_answer],
|
59 |
-
max_steps=6
|
60 |
-
verbosity_level=1
|
61 |
grammar=None,
|
62 |
-
planning_interval=
|
63 |
-
name=
|
64 |
-
description=
|
65 |
prompt_templates=prompt_templates
|
66 |
)
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
|
|
|
|
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 |
@tool
|
10 |
def get_current_time_in_timezone(timezone: str) -> str:
|
11 |
+
"""Get current time in specified timezone.
|
12 |
Args:
|
13 |
+
timezone: Valid timezone (e.g., 'America/New_York')
|
14 |
"""
|
15 |
try:
|
|
|
16 |
tz = pytz.timezone(timezone)
|
|
|
17 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
18 |
+
return f"Current time in {timezone}: {local_time}"
|
19 |
except Exception as e:
|
20 |
+
return f"Error: {str(e)}"
|
|
|
21 |
|
22 |
+
# Performance optimizations
|
23 |
final_answer = FinalAnswerTool()
|
24 |
|
25 |
+
# Try alternative model endpoint if main is overloaded
|
|
|
|
|
26 |
model = HfApiModel(
|
27 |
+
max_tokens=1024, # Reduced from 2096 to limit output length
|
28 |
+
temperature=0.3, # Reduced from 0.5 for more focused responses
|
29 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
30 |
+
custom_role_conversions=None,
|
31 |
)
|
32 |
|
33 |
+
# Fallback model configuration
|
34 |
+
fallback_model = HfApiModel(
|
35 |
+
max_tokens=1024,
|
36 |
+
temperature=0.3,
|
37 |
+
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
|
38 |
+
custom_role_conversions=None,
|
39 |
+
)
|
40 |
|
41 |
+
# Load image generation tool (only if needed)
|
42 |
+
# image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
43 |
|
44 |
+
# Optimized prompt templates
|
45 |
+
optimized_prompts = {
|
46 |
+
"system": """You are a helpful coding assistant. Be concise and direct.
|
47 |
+
Focus on the specific task. Avoid unnecessary explanations unless requested.""",
|
48 |
+
"user": """Task: {task}
|
49 |
+
|
50 |
+
Available tools: {tools}
|
51 |
|
52 |
+
Provide a clear, step-by-step solution."""
|
53 |
+
}
|
54 |
+
|
55 |
+
# Try to load custom prompts, fallback to optimized ones
|
56 |
+
try:
|
57 |
+
with open("prompts.yaml", 'r') as stream:
|
58 |
+
prompt_templates = yaml.safe_load(stream)
|
59 |
+
# Add length limits to existing prompts
|
60 |
+
if 'system' in prompt_templates:
|
61 |
+
prompt_templates['system'] = prompt_templates['system'][:500] # Limit system prompt
|
62 |
+
except FileNotFoundError:
|
63 |
+
prompt_templates = optimized_prompts
|
64 |
+
|
65 |
+
# Create agent with performance optimizations
|
66 |
agent = CodeAgent(
|
67 |
model=model,
|
68 |
+
tools=[final_answer, get_current_time_in_timezone],
|
69 |
+
max_steps=3, # Reduced from 6 to limit processing time
|
70 |
+
verbosity_level=0, # Reduced from 1 to minimize output
|
71 |
grammar=None,
|
72 |
+
planning_interval=2, # Add planning interval for better step management
|
73 |
+
name="OptimizedAgent",
|
74 |
+
description="Fast, efficient coding assistant",
|
75 |
prompt_templates=prompt_templates
|
76 |
)
|
77 |
|
78 |
+
# Add error handling and timeout
|
79 |
+
def launch_with_optimizations():
|
80 |
+
try:
|
81 |
+
# Try main model first
|
82 |
+
GradioUI(agent).launch(
|
83 |
+
share=False, # Disable sharing for better performance
|
84 |
+
server_name="127.0.0.1",
|
85 |
+
server_port=7860,
|
86 |
+
max_threads=4 # Limit concurrent threads
|
87 |
+
)
|
88 |
+
except Exception as e:
|
89 |
+
print(f"Main model failed: {e}")
|
90 |
+
print("Switching to fallback model...")
|
91 |
+
agent.model = fallback_model
|
92 |
+
GradioUI(agent).launch()
|
93 |
|
94 |
+
if __name__ == "__main__":
|
95 |
+
launch_with_optimizations()
|