Update app.py
Browse files
app.py
CHANGED
@@ -8,10 +8,15 @@ from tools import tools
|
|
8 |
from agents import *
|
9 |
from config import *
|
10 |
from workflow import create_workflow
|
|
|
11 |
|
12 |
# Initialize workflow
|
13 |
graph = create_workflow()
|
14 |
|
|
|
|
|
|
|
|
|
15 |
# Helper Functions
|
16 |
def run_graph(input_message, history, user_details):
|
17 |
try:
|
@@ -41,10 +46,13 @@ def run_graph(input_message, history, user_details):
|
|
41 |
]
|
42 |
|
43 |
# Generate the response
|
44 |
-
|
|
|
|
|
45 |
return response["messages"][1]["content"]
|
46 |
|
47 |
except Exception as e:
|
|
|
48 |
return f"An error occurred while processing your request: {e}"
|
49 |
|
50 |
|
@@ -141,50 +149,58 @@ with gr.Blocks() as demo:
|
|
141 |
clear_button = gr.Button("Clear Chat")
|
142 |
|
143 |
def submit_message(message, history=[]):
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
|
189 |
submit_button.click(submit_message, inputs=[text_input, chatbot], outputs=[chatbot, bmi_chart])
|
190 |
clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, bmi_chart])
|
@@ -209,10 +225,14 @@ with gr.Blocks() as demo:
|
|
209 |
calculate_button = gr.Button("Calculate")
|
210 |
|
211 |
def calculate_metrics(age, weight, height, gender, activity_level):
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
|
|
|
|
|
|
|
|
216 |
|
217 |
calculate_button.click(
|
218 |
calculate_metrics,
|
|
|
8 |
from agents import *
|
9 |
from config import *
|
10 |
from workflow import create_workflow
|
11 |
+
import logging
|
12 |
|
13 |
# Initialize workflow
|
14 |
graph = create_workflow()
|
15 |
|
16 |
+
# Configure logging
|
17 |
+
logging.basicConfig(level=logging.DEBUG)
|
18 |
+
logger = logging.getLogger(__name__)
|
19 |
+
|
20 |
# Helper Functions
|
21 |
def run_graph(input_message, history, user_details):
|
22 |
try:
|
|
|
46 |
]
|
47 |
|
48 |
# Generate the response
|
49 |
+
logger.debug("Invoking workflow with messages: %s", messages)
|
50 |
+
response = graph.invoke({"messages": messages}, timeout=10) # Add a timeout for safety
|
51 |
+
logger.debug("Workflow response: %s", response)
|
52 |
return response["messages"][1]["content"]
|
53 |
|
54 |
except Exception as e:
|
55 |
+
logger.error("Error in run_graph: %s", str(e))
|
56 |
return f"An error occurred while processing your request: {e}"
|
57 |
|
58 |
|
|
|
149 |
clear_button = gr.Button("Clear Chat")
|
150 |
|
151 |
def submit_message(message, history=[]):
|
152 |
+
try:
|
153 |
+
user_details = {
|
154 |
+
"name": user_name.value,
|
155 |
+
"age": user_age.value,
|
156 |
+
"weight": user_weight.value,
|
157 |
+
"height": user_height.value,
|
158 |
+
"activity_level": activity_level.value,
|
159 |
+
"gender": user_gender.value
|
160 |
+
}
|
161 |
+
|
162 |
+
bmi, status = calculate_bmi(user_details['height'], user_details['weight'])
|
163 |
+
calories = calculate_calories(
|
164 |
+
user_details['age'], user_details['weight'], user_details['height'], user_details['activity_level'], user_details['gender']
|
165 |
+
)
|
166 |
+
chart_path = visualize_bmi_and_calories(bmi, calories)
|
167 |
+
|
168 |
+
# Compose input for the LLM dynamically
|
169 |
+
system_prompt = (
|
170 |
+
"You are a fitness and health assistant. Provide detailed advice based on the user's metrics, including BMI and caloric needs."
|
171 |
+
)
|
172 |
+
|
173 |
+
user_prompt = (
|
174 |
+
f"User Details:\n"
|
175 |
+
f"- Name: {user_details['name']}\n"
|
176 |
+
f"- Age: {user_details['age']}\n"
|
177 |
+
f"- Gender: {user_details['gender']}\n"
|
178 |
+
f"- Weight: {user_details['weight']} kg\n"
|
179 |
+
f"- Height: {user_details['height']} cm\n"
|
180 |
+
f"- Activity Level: {user_details['activity_level']}\n"
|
181 |
+
f"- BMI: {bmi:.2f} ({status})\n"
|
182 |
+
f"- Daily Caloric Needs: {calories:.2f} kcal\n"
|
183 |
+
f"\nProvide tailored advice based on these metrics."
|
184 |
+
)
|
185 |
+
|
186 |
+
messages = [
|
187 |
+
{"role": "system", "content": system_prompt},
|
188 |
+
{"role": "user", "content": user_prompt}
|
189 |
+
]
|
190 |
+
|
191 |
+
logger.debug("Submitting messages to LLM: %s", messages)
|
192 |
+
response = graph.invoke({"messages": messages}, timeout=10) # Add a timeout
|
193 |
+
logger.debug("Received LLM response: %s", response)
|
194 |
+
|
195 |
+
personalized_response = response["messages"][1]["content"]
|
196 |
+
|
197 |
+
history.append((f"User", message))
|
198 |
+
history.append(("FIT.AI", personalized_response))
|
199 |
+
return history, chart_path
|
200 |
+
|
201 |
+
except Exception as e:
|
202 |
+
logger.error("Error in submit_message: %s", str(e))
|
203 |
+
return history + [("FIT.AI", "An error occurred while processing your request." + str(e))], None
|
204 |
|
205 |
submit_button.click(submit_message, inputs=[text_input, chatbot], outputs=[chatbot, bmi_chart])
|
206 |
clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, bmi_chart])
|
|
|
225 |
calculate_button = gr.Button("Calculate")
|
226 |
|
227 |
def calculate_metrics(age, weight, height, gender, activity_level):
|
228 |
+
try:
|
229 |
+
bmi, status = calculate_bmi(height, weight)
|
230 |
+
bmi_path = visualize_bmi_and_calories(bmi, calculate_calories(age, weight, height, activity_level, gender))
|
231 |
+
calories = calculate_calories(age, weight, height, activity_level, gender)
|
232 |
+
return f"Your BMI is {bmi:.2f}, considered {status}.", f"Daily calorie needs: {calories:.2f} kcal", bmi_path
|
233 |
+
except Exception as e:
|
234 |
+
logger.error("Error in calculate_metrics: %s", str(e))
|
235 |
+
return "An error occurred.", "", None
|
236 |
|
237 |
calculate_button.click(
|
238 |
calculate_metrics,
|