Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,17 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
from langchain_core.messages import HumanMessage
|
3 |
from config import *
|
4 |
from tools import tools
|
5 |
from agents import *
|
6 |
from workflow import create_workflow
|
7 |
|
|
|
8 |
graph = create_workflow()
|
9 |
|
|
|
10 |
def run_graph(input_message, history, user_details):
|
11 |
try:
|
12 |
# Relevant fitness-related keywords to handle irrelevant user prompts
|
@@ -51,7 +56,6 @@ def run_graph(input_message, history, user_details):
|
|
51 |
return f"An error occurred while processing your request: {e}"
|
52 |
|
53 |
|
54 |
-
# Advanced Functionalities
|
55 |
def calculate_bmi(height, weight):
|
56 |
try:
|
57 |
height_m = height / 100
|
@@ -66,24 +70,45 @@ def calculate_bmi(height, weight):
|
|
66 |
status = "obese"
|
67 |
return f"Your BMI is {bmi:.2f}, which is considered {status}."
|
68 |
except:
|
69 |
-
return "
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
# Interface Components
|
73 |
with gr.Blocks() as demo:
|
74 |
-
gr.Markdown("<strong>FIT.AI - Your
|
75 |
|
76 |
-
# User
|
77 |
with gr.Row():
|
78 |
user_name = gr.Textbox(placeholder="Enter your name", label="Name")
|
79 |
-
user_age = gr.Number(label="Age", value=25, precision=0)
|
80 |
user_weight = gr.Number(label="Weight (kg)", value=70, precision=1)
|
81 |
user_height = gr.Number(label="Height (cm)", value=170, precision=1)
|
|
|
|
|
|
|
|
|
|
|
82 |
|
|
|
83 |
chatbot = gr.Chatbot(label="Chat with FIT.AI")
|
84 |
text_input = gr.Textbox(placeholder="Type your question here...", label="Your Question")
|
85 |
submit_button = gr.Button("Submit")
|
86 |
-
clear_button = gr.Button("Clear")
|
87 |
|
88 |
# Examples
|
89 |
examples = gr.Examples(
|
@@ -92,49 +117,25 @@ with gr.Blocks() as demo:
|
|
92 |
"What should I eat to lose 5 kg in two months?",
|
93 |
"Suggest a yoga routine for beginners",
|
94 |
"How much water should I drink daily based on my weight?",
|
95 |
-
"What are the best recovery exercises after a leg injury?",
|
96 |
-
"Can you help me calculate my BMI?",
|
97 |
-
"What are some quick tips to manage work stress?",
|
98 |
-
"Explain the benefits of intermittent fasting",
|
99 |
-
"Which exercises can help improve posture?",
|
100 |
-
"What should I include in my diet to build muscle?",
|
101 |
-
"How can I stay active while working from home?",
|
102 |
-
"What are the signs of overtraining?",
|
103 |
-
"What are some healthy snacks for weight loss?",
|
104 |
-
"How can I improve my sleep quality?",
|
105 |
-
"Suggest a daily routine to stay fit and mentally healthy",
|
106 |
],
|
107 |
inputs=text_input,
|
108 |
)
|
109 |
|
110 |
-
#
|
111 |
-
def submit_message(message, history=[]
|
112 |
user_details = {
|
113 |
"name": user_name.value,
|
114 |
"age": user_age.value,
|
115 |
"weight": user_weight.value,
|
116 |
-
"height": user_height.value
|
|
|
117 |
}
|
118 |
response = run_graph(message, history, user_details)
|
119 |
history.append(("User", message))
|
120 |
history.append(("FIT.AI", response))
|
121 |
return history, ""
|
122 |
|
123 |
-
submit_button.click(
|
124 |
-
submit_message,
|
125 |
-
inputs=[text_input, chatbot],
|
126 |
-
outputs=[chatbot, text_input]
|
127 |
-
)
|
128 |
clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, text_input])
|
129 |
|
130 |
-
# BMI Calculator Button
|
131 |
-
with gr.Row():
|
132 |
-
calculate_bmi_button = gr.Button("Calculate BMI")
|
133 |
-
bmi_result = gr.Textbox(label="BMI Result", interactive=False)
|
134 |
-
calculate_bmi_button.click(
|
135 |
-
calculate_bmi,
|
136 |
-
inputs=[user_height, user_weight],
|
137 |
-
outputs=[bmi_result]
|
138 |
-
)
|
139 |
-
|
140 |
demo.launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import pandas as pd
|
4 |
+
from datetime import datetime
|
5 |
from langchain_core.messages import HumanMessage
|
6 |
from config import *
|
7 |
from tools import tools
|
8 |
from agents import *
|
9 |
from workflow import create_workflow
|
10 |
|
11 |
+
# Initialize workflow
|
12 |
graph = create_workflow()
|
13 |
|
14 |
+
# Helper Functions
|
15 |
def run_graph(input_message, history, user_details):
|
16 |
try:
|
17 |
# Relevant fitness-related keywords to handle irrelevant user prompts
|
|
|
56 |
return f"An error occurred while processing your request: {e}"
|
57 |
|
58 |
|
|
|
59 |
def calculate_bmi(height, weight):
|
60 |
try:
|
61 |
height_m = height / 100
|
|
|
70 |
status = "obese"
|
71 |
return f"Your BMI is {bmi:.2f}, which is considered {status}."
|
72 |
except:
|
73 |
+
return "Invalid height or weight provided."
|
74 |
|
75 |
+
def calculate_calories(age, weight, height, activity_level):
|
76 |
+
try:
|
77 |
+
# Harris-Benedict Equation for calorie calculation
|
78 |
+
bmr = 10 * weight + 6.25 * height - 5 * age + 5 # Male (adjust for female by subtracting 161)
|
79 |
+
activity_multipliers = {
|
80 |
+
"Sedentary": 1.2,
|
81 |
+
"Lightly active": 1.375,
|
82 |
+
"Moderately active": 1.55,
|
83 |
+
"Very active": 1.725,
|
84 |
+
"Extra active": 1.9,
|
85 |
+
}
|
86 |
+
calories = bmr * activity_multipliers[activity_level]
|
87 |
+
return f"Your estimated daily calorie needs are {calories:.2f} kcal."
|
88 |
+
except Exception:
|
89 |
+
return "Invalid inputs for calorie calculation."
|
90 |
|
91 |
# Interface Components
|
92 |
with gr.Blocks() as demo:
|
93 |
+
gr.Markdown("<strong>FIT.AI - Your Advanced Fitness and Wellbeing Coach</strong>")
|
94 |
|
95 |
+
# User Info
|
96 |
with gr.Row():
|
97 |
user_name = gr.Textbox(placeholder="Enter your name", label="Name")
|
98 |
+
user_age = gr.Number(label="Age (years)", value=25, precision=0)
|
99 |
user_weight = gr.Number(label="Weight (kg)", value=70, precision=1)
|
100 |
user_height = gr.Number(label="Height (cm)", value=170, precision=1)
|
101 |
+
activity_level = gr.Dropdown(
|
102 |
+
choices=["Sedentary", "Lightly active", "Moderately active", "Very active", "Extra active"],
|
103 |
+
label="Activity Level",
|
104 |
+
value="Moderately active"
|
105 |
+
)
|
106 |
|
107 |
+
# Chatbot Section
|
108 |
chatbot = gr.Chatbot(label="Chat with FIT.AI")
|
109 |
text_input = gr.Textbox(placeholder="Type your question here...", label="Your Question")
|
110 |
submit_button = gr.Button("Submit")
|
111 |
+
clear_button = gr.Button("Clear Chat")
|
112 |
|
113 |
# Examples
|
114 |
examples = gr.Examples(
|
|
|
117 |
"What should I eat to lose 5 kg in two months?",
|
118 |
"Suggest a yoga routine for beginners",
|
119 |
"How much water should I drink daily based on my weight?",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
],
|
121 |
inputs=text_input,
|
122 |
)
|
123 |
|
124 |
+
# Chat Logic
|
125 |
+
def submit_message(message, history=[]):
|
126 |
user_details = {
|
127 |
"name": user_name.value,
|
128 |
"age": user_age.value,
|
129 |
"weight": user_weight.value,
|
130 |
+
"height": user_height.value,
|
131 |
+
"activity_level": activity_level.value
|
132 |
}
|
133 |
response = run_graph(message, history, user_details)
|
134 |
history.append(("User", message))
|
135 |
history.append(("FIT.AI", response))
|
136 |
return history, ""
|
137 |
|
138 |
+
submit_button.click(submit_message, inputs=[text_input, chatbot], outputs=[chatbot, text_input])
|
|
|
|
|
|
|
|
|
139 |
clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, text_input])
|
140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
demo.launch(share=True)
|