Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
@@ -7,7 +7,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 execute_python_code(code: str) -> str:
|
13 |
"""A tool that safely executes a snippet of Python code and returns its output.
|
@@ -41,6 +40,7 @@ def execute_python_code(code: str) -> str:
|
|
41 |
@tool
|
42 |
def get_current_time_in_timezone(timezone: str) -> str:
|
43 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
44 |
Args:
|
45 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
46 |
"""
|
@@ -53,6 +53,47 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
53 |
except Exception as e:
|
54 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
final_answer = FinalAnswerTool()
|
58 |
|
@@ -60,22 +101,22 @@ final_answer = FinalAnswerTool()
|
|
60 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
61 |
|
62 |
model = HfApiModel(
|
63 |
-
max_tokens=2096,
|
64 |
-
temperature=0.5,
|
65 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
66 |
-
custom_role_conversions=None,
|
67 |
)
|
68 |
|
69 |
-
|
70 |
# Import tool from Hub
|
71 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
72 |
|
73 |
with open("prompts.yaml", 'r') as stream:
|
74 |
prompt_templates = yaml.safe_load(stream)
|
75 |
-
|
|
|
76 |
agent = CodeAgent(
|
77 |
model=model,
|
78 |
-
tools=[final_answer,get_current_time_in_timezone,execute_python_code],
|
79 |
max_steps=6,
|
80 |
verbosity_level=1,
|
81 |
grammar=None,
|
@@ -85,5 +126,4 @@ agent = CodeAgent(
|
|
85 |
prompt_templates=prompt_templates
|
86 |
)
|
87 |
|
88 |
-
|
89 |
-
GradioUI(agent).launch()
|
|
|
1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
|
|
10 |
@tool
|
11 |
def execute_python_code(code: str) -> str:
|
12 |
"""A tool that safely executes a snippet of Python code and returns its output.
|
|
|
40 |
@tool
|
41 |
def get_current_time_in_timezone(timezone: str) -> str:
|
42 |
"""A tool that fetches the current local time in a specified timezone.
|
43 |
+
|
44 |
Args:
|
45 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
46 |
"""
|
|
|
53 |
except Exception as e:
|
54 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
55 |
|
56 |
+
@tool
|
57 |
+
def calculate_bmi(weight: float, height: float) -> str:
|
58 |
+
"""A tool that calculates the Body Mass Index (BMI) given weight in kilograms and height in meters,
|
59 |
+
and provides health recommendations based on the BMI value.
|
60 |
+
|
61 |
+
Args:
|
62 |
+
weight: Your weight in kilograms.
|
63 |
+
height: Your height in meters.
|
64 |
+
"""
|
65 |
+
try:
|
66 |
+
bmi = weight / (height ** 2)
|
67 |
+
bmi = round(bmi, 2)
|
68 |
+
|
69 |
+
if bmi < 18.5:
|
70 |
+
category = "Underweight"
|
71 |
+
recommendation = (
|
72 |
+
"Consider increasing your calorie intake with nutrient-dense foods, "
|
73 |
+
"and possibly consult a healthcare provider for a tailored plan."
|
74 |
+
)
|
75 |
+
elif bmi < 25:
|
76 |
+
category = "Normal weight"
|
77 |
+
recommendation = (
|
78 |
+
"Great job maintaining a healthy weight! Keep up with your balanced diet "
|
79 |
+
"and regular physical activity."
|
80 |
+
)
|
81 |
+
elif bmi < 30:
|
82 |
+
category = "Overweight"
|
83 |
+
recommendation = (
|
84 |
+
"A balanced diet and regular exercise might help in managing your weight. "
|
85 |
+
"It could be helpful to consult with a nutritionist for personalized advice."
|
86 |
+
)
|
87 |
+
else:
|
88 |
+
category = "Obese"
|
89 |
+
recommendation = (
|
90 |
+
"It's advisable to consult a healthcare professional for personalized recommendations "
|
91 |
+
"regarding nutrition and exercise."
|
92 |
+
)
|
93 |
+
|
94 |
+
return f"Your BMI is {bmi} ({category}). {recommendation}"
|
95 |
+
except Exception as e:
|
96 |
+
return f"Error calculating BMI: {str(e)}"
|
97 |
|
98 |
final_answer = FinalAnswerTool()
|
99 |
|
|
|
101 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
102 |
|
103 |
model = HfApiModel(
|
104 |
+
max_tokens=2096,
|
105 |
+
temperature=0.5,
|
106 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
|
107 |
+
custom_role_conversions=None,
|
108 |
)
|
109 |
|
|
|
110 |
# Import tool from Hub
|
111 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
112 |
|
113 |
with open("prompts.yaml", 'r') as stream:
|
114 |
prompt_templates = yaml.safe_load(stream)
|
115 |
+
|
116 |
+
# Add all your tools here (ensure final_answer remains included)
|
117 |
agent = CodeAgent(
|
118 |
model=model,
|
119 |
+
tools=[final_answer, get_current_time_in_timezone, execute_python_code, calculate_bmi],
|
120 |
max_steps=6,
|
121 |
verbosity_level=1,
|
122 |
grammar=None,
|
|
|
126 |
prompt_templates=prompt_templates
|
127 |
)
|
128 |
|
129 |
+
GradioUI(agent).launch()
|
|