Wayne0102 commited on
Commit
8db7b73
·
verified ·
1 Parent(s): 3aa2129

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, load_tool, tool
2
+ import datetime
3
+ import pytz
4
+ import yaml
5
+ from tools.final_answer import FinalAnswerTool
6
+ from Gradio_UI import GradioUI
7
+
8
+ # ==================== YOUR TOOLS ====================
9
+ @tool
10
+ def get_current_time_in_timezone(timezone: str) -> str:
11
+ """A tool that fetches the current local time in a specified timezone.
12
+ Args:
13
+ timezone: A string representing a 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"The current local time in {timezone} is: {local_time}"
19
+ except Exception as e:
20
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
21
+
22
+ @tool
23
+ def calculate_tip(bill_amount: float, tip_percent: float) -> str:
24
+ """Calculate tip amount for a restaurant bill.
25
+ Args:
26
+ bill_amount: Total bill amount in dollars
27
+ tip_percent: Tip percentage (e.g., 15 for 15%)
28
+ """
29
+ try:
30
+ tip = bill_amount * (tip_percent / 100)
31
+ total = bill_amount + tip
32
+ return f"💰 Tip: ${tip:.2f}, Total: ${total:.2f} ({tip_percent}%)"
33
+ except Exception as e:
34
+ return f"Error: {str(e)}"
35
+
36
+ @tool
37
+ def word_counter(text: str) -> str:
38
+ """Count words and characters in text.
39
+ Args:
40
+ text: The text to analyze
41
+ """
42
+ try:
43
+ words = len(text.split())
44
+ chars = len(text)
45
+ return f"📝 Words: {words}, Characters: {chars}"
46
+ except Exception as e:
47
+ return f"Error: {str(e)}"
48
+
49
+ # ==================== AGENT SETUP ====================
50
+ final_answer = FinalAnswerTool()
51
+
52
+ # Load image generation tool
53
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
54
+
55
+ # ⚠️ FIX: ADD THE MISSING MODEL SETUP
56
+ # Try different model import approaches:
57
+
58
+ # OPTION 1: Use HfApiModel (common in spaces)
59
+ try:
60
+ from smolagents import HfApiModel
61
+ model = HfApiModel(
62
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
63
+ max_tokens=512,
64
+ temperature=0.5
65
+ )
66
+ except ImportError:
67
+ # OPTION 2: Use InferenceClientModel with correct import path
68
+ try:
69
+ from smolagents.models import InferenceClientModel
70
+ model = InferenceClientModel(
71
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
72
+ max_tokens=512,
73
+ temperature=0.5
74
+ )
75
+ except ImportError:
76
+ # OPTION 3: Use default model
77
+ from smolagents.models import DefaultModel
78
+ model = DefaultModel()
79
+
80
+ # Load system prompt
81
+ with open("prompts.yaml", 'r') as stream:
82
+ prompt_templates = yaml.safe_load(stream)
83
+
84
+ # ==================== CREATE AGENT ====================
85
+ agent = CodeAgent(
86
+ model=model, # ⚠️ THIS WAS MISSING!
87
+ tools=[
88
+ final_answer,
89
+ DuckDuckGoSearchTool(),
90
+ image_generation_tool,
91
+ get_current_time_in_timezone,
92
+ calculate_tip,
93
+ word_counter
94
+ ],
95
+ max_steps=8,
96
+ verbosity_level=1,
97
+ prompt_templates=prompt_templates
98
+ )
99
+
100
+ # ==================== LAUNCH ====================
101
+ GradioUI(agent).launch()