Wayne0102 commited on
Commit
e31d1f8
·
verified ·
1 Parent(s): 8fba84e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -36
app.py CHANGED
@@ -6,7 +6,6 @@ from tools.final_answer import FinalAnswerTool
6
  from Gradio_UI import GradioUI
7
 
8
  # ==================== YOUR TOOLS ====================
9
- # Existing tool from Unit 1
10
  @tool
11
  def get_current_time_in_timezone(timezone: str) -> str:
12
  """A tool that fetches the current local time in a specified timezone.
@@ -20,7 +19,6 @@ def get_current_time_in_timezone(timezone: str) -> str:
20
  except Exception as e:
21
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
22
 
23
- # NEW TOOL 1: Tip Calculator
24
  @tool
25
  def calculate_tip(bill_amount: float, tip_percent: float) -> str:
26
  """Calculate tip amount for a restaurant bill.
@@ -35,7 +33,6 @@ def calculate_tip(bill_amount: float, tip_percent: float) -> str:
35
  except Exception as e:
36
  return f"Error: {str(e)}"
37
 
38
- # NEW TOOL 2: Word Counter
39
  @tool
40
  def word_counter(text: str) -> str:
41
  """Count words and characters in text.
@@ -49,52 +46,54 @@ def word_counter(text: str) -> str:
49
  except Exception as e:
50
  return f"Error: {str(e)}"
51
 
52
- # NEW TOOL 3: BMI Calculator
53
- @tool
54
- def calculate_bmi(weight_kg: float, height_m: float) -> str:
55
- """Calculate Body Mass Index (BMI).
56
- Args:
57
- weight_kg: Weight in kilograms
58
- height_m: Height in meters
59
- """
60
- try:
61
- bmi = weight_kg / (height_m ** 2)
62
- if bmi < 18.5:
63
- cat = "Underweight"
64
- elif 18.5 <= bmi < 25:
65
- cat = "Normal"
66
- elif 25 <= bmi < 30:
67
- cat = "Overweight"
68
- else:
69
- cat = "Obese"
70
- return f"⚖️ BMI: {bmi:.1f} ({cat})"
71
- except Exception as e:
72
- return f"Error: {str(e)}"
73
-
74
  # ==================== AGENT SETUP ====================
75
  final_answer = FinalAnswerTool()
76
 
77
- # Load image generation tool from Hub
78
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
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
- # SIMPLIFIED: Use default model instead of InferenceClientModel
86
  agent = CodeAgent(
 
87
  tools=[
88
- final_answer, # Required
89
- DuckDuckGoSearchTool(), # Search
90
- image_generation_tool, # Images
91
- get_current_time_in_timezone, # Time
92
- calculate_tip, # NEW
93
- word_counter, # NEW
94
- calculate_bmi # NEW
95
  ],
96
- max_steps=8, # Increased
97
- verbosity_level=1, # Show thinking
98
  prompt_templates=prompt_templates
99
  )
100
 
 
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.
 
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.
 
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.
 
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