Create interim.py
Browse files- interim.py +142 -0
interim.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 tools import tools
|
7 |
+
from agents import *
|
8 |
+
from config 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
|
18 |
+
relevant_keywords = [
|
19 |
+
"workout", "training", "exercise", "cardio", "strength training", "hiit (high-intensity interval training)",
|
20 |
+
"flexibility", "yoga", "pilates", "aerobics", "crossfit", "bodybuilding", "endurance", "running",
|
21 |
+
"cycling", "swimming", "martial arts", "stretching", "warm-up", "cool-down",
|
22 |
+
"diet plan", "meal plan", "macronutrients", "micronutrients", "vitamins", "minerals", "protein",
|
23 |
+
"carbohydrates", "fats", "calories", "calorie", "daily", "nutrition", "supplements", "hydration", "weightloss",
|
24 |
+
"weight gain", "healthy eating", "health", "fitness", "intermittent fasting", "keto diet", "vegan diet", "paleo diet",
|
25 |
+
"mediterranean diet", "gluten-free", "low-carb", "high-protein", "bmi", "calculate", "body mass index", "calculator",
|
26 |
+
"mental health", "mindfulness", "meditation", "stress management", "anxiety relief", "depression",
|
27 |
+
"positive thinking", "motivation", "self-care", "relaxation", "sleep hygiene", "therapy",
|
28 |
+
"counseling", "cognitive-behavioral therapy (cbt)", "mood tracking", "mental", "emotional well-being",
|
29 |
+
"healthy lifestyle", "fitness goals", "health routines", "daily habits", "ergonomics",
|
30 |
+
"posture", "work-life balance", "workplace", "habit tracking", "goal setting", "personal growth",
|
31 |
+
"injury prevention", "recovery", "rehabilitation", "physical therapy", "sports injuries",
|
32 |
+
"pain management", "recovery techniques", "foam rolling", "stretching exercises",
|
33 |
+
"injury management", "injuries", "apps", "health tracking", "wearable technology", "equipment",
|
34 |
+
"home workouts", "gym routines", "outdoor activities", "sports", "wellness tips", "water", "adult", "adults",
|
35 |
+
"child", "children", "infant", "sleep", "habit", "habits", "routine", "weight", "fruits", "vegetables", "lose", "lost weight", "weight-loss",
|
36 |
+
"chicken", "veg", "vegetarian", "non-veg", "non-vegetarian", "plant", "plant-based", "plant based", "fat", "resources",
|
37 |
+
"help", "cutting", "bulking", "link", "links", "website", "online", "websites", "peace", "mind", "equipments", "equipment",
|
38 |
+
"watch", "tracker", "watch", "band", "height", "injured", "quick", "remedy", "solution", "solutions", "pain", "male", "female", "kilograms", "kg", "Pounds",
|
39 |
+
"lbs"
|
40 |
+
]
|
41 |
+
|
42 |
+
greetings = ["hello", "hi", "how are you doing"]
|
43 |
+
|
44 |
+
if any(keyword in input_message.lower() for keyword in relevant_keywords):
|
45 |
+
response = graph.invoke({
|
46 |
+
"messages": [HumanMessage(content=input_message)],
|
47 |
+
"user_details": user_details # Pass user-specific data for customization
|
48 |
+
})
|
49 |
+
return response['messages'][1].content
|
50 |
+
|
51 |
+
elif any(keyword in input_message.lower() for keyword in greetings):
|
52 |
+
return "Hi there, I am FIT bot, your personal wellbeing coach! Let me know your fitness goals or questions."
|
53 |
+
|
54 |
+
else:
|
55 |
+
return "I'm here to assist with fitness, nutrition, mental health, and related topics. Please ask questions related to these areas."
|
56 |
+
except Exception as e:
|
57 |
+
return f"An error occurred while processing your request: {e}"
|
58 |
+
|
59 |
+
|
60 |
+
def calculate_bmi(height, weight):
|
61 |
+
try:
|
62 |
+
height_m = height / 100
|
63 |
+
bmi = weight / (height_m ** 2)
|
64 |
+
if bmi < 18.5:
|
65 |
+
status = "underweight"
|
66 |
+
elif 18.5 <= bmi < 24.9:
|
67 |
+
status = "normal weight"
|
68 |
+
elif 25 <= bmi < 29.9:
|
69 |
+
status = "overweight"
|
70 |
+
else:
|
71 |
+
status = "obese"
|
72 |
+
return f"Your BMI is {bmi:.2f}, which is considered {status}."
|
73 |
+
except:
|
74 |
+
return "Invalid height or weight provided."
|
75 |
+
|
76 |
+
def calculate_calories(age, weight, height, activity_level):
|
77 |
+
try:
|
78 |
+
# Harris-Benedict Equation for calorie calculation
|
79 |
+
bmr = 10 * weight + 6.25 * height - 5 * age + 5 # Male (adjust for female by subtracting 161)
|
80 |
+
activity_multipliers = {
|
81 |
+
"Sedentary": 1.2,
|
82 |
+
"Lightly active": 1.375,
|
83 |
+
"Moderately active": 1.55,
|
84 |
+
"Very active": 1.725,
|
85 |
+
"Extra active": 1.9,
|
86 |
+
}
|
87 |
+
calories = bmr * activity_multipliers[activity_level]
|
88 |
+
return f"Your estimated daily calorie needs are {calories:.2f} kcal."
|
89 |
+
except Exception:
|
90 |
+
return "Invalid inputs for calorie calculation."
|
91 |
+
|
92 |
+
# Interface Components
|
93 |
+
with gr.Blocks() as demo:
|
94 |
+
gr.Markdown("<strong>FIT.AI - Your Fitness and Wellbeing Coach</strong>")
|
95 |
+
|
96 |
+
# User Info
|
97 |
+
with gr.Row():
|
98 |
+
user_name = gr.Textbox(placeholder="Enter your name", label="Name")
|
99 |
+
user_age = gr.Number(label="Age (years)", value=25, precision=0)
|
100 |
+
user_weight = gr.Number(label="Weight (kg)", value=70, precision=1)
|
101 |
+
user_height = gr.Number(label="Height (cm)", value=170, precision=1)
|
102 |
+
activity_level = gr.Dropdown(
|
103 |
+
choices=["Sedentary", "Lightly active", "Moderately active", "Very active", "Extra active"],
|
104 |
+
label="Activity Level",
|
105 |
+
value="Moderately active"
|
106 |
+
)
|
107 |
+
|
108 |
+
# Chatbot Section
|
109 |
+
chatbot = gr.Chatbot(label="Chat with FIT.AI")
|
110 |
+
text_input = gr.Textbox(placeholder="Type your question here...", label="Your Question")
|
111 |
+
submit_button = gr.Button("Submit")
|
112 |
+
clear_button = gr.Button("Clear Chat")
|
113 |
+
|
114 |
+
# Examples
|
115 |
+
examples = gr.Examples(
|
116 |
+
examples=[
|
117 |
+
"Create a personalized workout plan for me",
|
118 |
+
"What should I eat to lose 5 kg in two months?",
|
119 |
+
"Suggest a yoga routine for beginners",
|
120 |
+
"How much water should I drink daily based on my weight?",
|
121 |
+
],
|
122 |
+
inputs=text_input,
|
123 |
+
)
|
124 |
+
|
125 |
+
# Chat Logic
|
126 |
+
def submit_message(message, history=[]):
|
127 |
+
user_details = {
|
128 |
+
"name": user_name.value,
|
129 |
+
"age": user_age.value,
|
130 |
+
"weight": user_weight.value,
|
131 |
+
"height": user_height.value,
|
132 |
+
"activity_level": activity_level.value
|
133 |
+
}
|
134 |
+
response = run_graph(message, history, user_details)
|
135 |
+
history.append(("User", message))
|
136 |
+
history.append(("FIT.AI", response))
|
137 |
+
return history, ""
|
138 |
+
|
139 |
+
submit_button.click(submit_message, inputs=[text_input, chatbot], outputs=[chatbot, text_input])
|
140 |
+
clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, text_input])
|
141 |
+
|
142 |
+
demo.launch(share=True)
|