Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ from workflow import create_workflow
|
|
7 |
|
8 |
graph = create_workflow()
|
9 |
|
10 |
-
def run_graph(input_message, history):
|
11 |
try:
|
12 |
# Relevant fitness-related keywords to handle irrelevant user prompts
|
13 |
relevant_keywords = [
|
@@ -34,29 +34,58 @@ def run_graph(input_message, history):
|
|
34 |
]
|
35 |
|
36 |
greetings = ["hello", "hi", "how are you doing"]
|
37 |
-
|
38 |
-
# Check if the input message contains any relevant keywords
|
39 |
if any(keyword in input_message.lower() for keyword in relevant_keywords):
|
40 |
response = graph.invoke({
|
41 |
-
"messages": [HumanMessage(content=input_message)]
|
|
|
42 |
})
|
43 |
return response['messages'][1].content
|
44 |
-
|
45 |
elif any(keyword in input_message.lower() for keyword in greetings):
|
46 |
-
return "Hi there, I am FIT bot, your personal wellbeing coach!"
|
47 |
-
|
48 |
else:
|
49 |
return "I'm here to assist with fitness, nutrition, mental health, and related topics. Please ask questions related to these areas."
|
50 |
except Exception as e:
|
51 |
return f"An error occurred while processing your request: {e}"
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
# Interface Components
|
54 |
with gr.Blocks() as demo:
|
55 |
gr.Markdown("<strong>FIT.AI - Your Personal Wellbeing Coach</strong>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
chatbot = gr.Chatbot(label="Chat with FIT.AI")
|
57 |
text_input = gr.Textbox(placeholder="Type your question here...", label="Your Question")
|
58 |
submit_button = gr.Button("Submit")
|
59 |
clear_button = gr.Button("Clear")
|
|
|
|
|
60 |
examples = gr.Examples(
|
61 |
examples=[
|
62 |
"Create a personalized workout plan for me",
|
@@ -77,15 +106,35 @@ with gr.Blocks() as demo:
|
|
77 |
],
|
78 |
inputs=text_input,
|
79 |
)
|
80 |
-
|
81 |
-
# Chatbot
|
82 |
-
def submit_message(message, history=[]):
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
history.append(("User", message))
|
85 |
history.append(("FIT.AI", response))
|
86 |
return history, ""
|
87 |
|
88 |
-
submit_button.click(
|
|
|
|
|
|
|
|
|
89 |
clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, text_input])
|
90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
demo.launch(share=True)
|
|
|
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
|
13 |
relevant_keywords = [
|
|
|
34 |
]
|
35 |
|
36 |
greetings = ["hello", "hi", "how are you doing"]
|
37 |
+
|
|
|
38 |
if any(keyword in input_message.lower() for keyword in relevant_keywords):
|
39 |
response = graph.invoke({
|
40 |
+
"messages": [HumanMessage(content=input_message)],
|
41 |
+
"user_details": user_details # Pass user-specific data for customization
|
42 |
})
|
43 |
return response['messages'][1].content
|
44 |
+
|
45 |
elif any(keyword in input_message.lower() for keyword in greetings):
|
46 |
+
return "Hi there, I am FIT bot, your personal wellbeing coach! Let me know your fitness goals or questions."
|
47 |
+
|
48 |
else:
|
49 |
return "I'm here to assist with fitness, nutrition, mental health, and related topics. Please ask questions related to these areas."
|
50 |
except Exception as e:
|
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
|
58 |
+
bmi = weight / (height_m ** 2)
|
59 |
+
if bmi < 18.5:
|
60 |
+
status = "underweight"
|
61 |
+
elif 18.5 <= bmi < 24.9:
|
62 |
+
status = "normal weight"
|
63 |
+
elif 25 <= bmi < 29.9:
|
64 |
+
status = "overweight"
|
65 |
+
else:
|
66 |
+
status = "obese"
|
67 |
+
return f"Your BMI is {bmi:.2f}, which is considered {status}."
|
68 |
+
except:
|
69 |
+
return "Please provide valid height and weight values."
|
70 |
+
|
71 |
+
|
72 |
# Interface Components
|
73 |
with gr.Blocks() as demo:
|
74 |
gr.Markdown("<strong>FIT.AI - Your Personal Wellbeing Coach</strong>")
|
75 |
+
|
76 |
+
# User Details Form
|
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(
|
90 |
examples=[
|
91 |
"Create a personalized workout plan for me",
|
|
|
106 |
],
|
107 |
inputs=text_input,
|
108 |
)
|
109 |
+
|
110 |
+
# Chatbot Logic
|
111 |
+
def submit_message(message, history=[], user_details=None):
|
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)
|