Wayne0102 commited on
Commit
8902182
·
verified ·
1 Parent(s): b159dbe

Update app.py

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