Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# enhanced_interactive_sleep_predictor.py
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import joblib
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
from langchain_google_genai import GoogleGenerativeAI
|
| 8 |
+
from langchain.prompts import PromptTemplate
|
| 9 |
+
from langchain.chains import LLMChain
|
| 10 |
+
|
| 11 |
+
# Load your trained model
|
| 12 |
+
@st.cache_resource
|
| 13 |
+
def load_model():
|
| 14 |
+
return joblib.load("sleep_model.pkl") # Update with your actual model path
|
| 15 |
+
|
| 16 |
+
model = load_model()
|
| 17 |
+
|
| 18 |
+
# LangChain Setup
|
| 19 |
+
api_key = st.secrets.get('genai_key')
|
| 20 |
+
llm= GoogleGenerativeAI(model="gemini-1.5-pro", google_api_key=api_key) # or "gpt-4", based on your requirement
|
| 21 |
+
|
| 22 |
+
# Health tips prompt template
|
| 23 |
+
prompt_template = """
|
| 24 |
+
You are a health advisor. Based on the sleep duration (in hours), give personalized sleep and health tips.
|
| 25 |
+
Sleep duration: {sleep_duration} hours.
|
| 26 |
+
Health Tip:
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
# LangChain chain for generating health tips
|
| 30 |
+
def generate_health_tips(sleep_duration):
|
| 31 |
+
prompt = PromptTemplate(input_variables=["sleep_duration"], template=prompt_template)
|
| 32 |
+
chain = LLMChain(llm=llm, prompt=prompt)
|
| 33 |
+
return chain.run({"sleep_duration": sleep_duration})
|
| 34 |
+
|
| 35 |
+
# Streamlit UI Enhancements
|
| 36 |
+
st.set_page_config(page_title="Interactive Sleep Predictor", layout="wide")
|
| 37 |
+
st.title("π **Interactive Sleep Predictor**")
|
| 38 |
+
st.markdown("""
|
| 39 |
+
## Get Personalized Sleep & Health Predictions
|
| 40 |
+
Enter your step count and current hour to predict your sleep/wake status and receive personalized health tips and exercise suggestions.
|
| 41 |
+
""")
|
| 42 |
+
|
| 43 |
+
# User input form
|
| 44 |
+
with st.form("predictor_form"):
|
| 45 |
+
step = st.number_input("πΆ **Step Count** (Enter your current steps)", min_value=0, step=10, label_visibility="visible")
|
| 46 |
+
hour = st.slider("β° **Hour of the Day** (24h format)", min_value=0, max_value=23, label_visibility="visible")
|
| 47 |
+
submit_button = st.form_submit_button("Predict Sleep/Wake State")
|
| 48 |
+
|
| 49 |
+
# When the user clicks the predict button
|
| 50 |
+
if submit_button:
|
| 51 |
+
# Prepare input for the model
|
| 52 |
+
input_df = pd.DataFrame([[step, hour]], columns=["step", "hour"])
|
| 53 |
+
|
| 54 |
+
# Predict sleep state (0 = awake, 1 = asleep)
|
| 55 |
+
prediction = model.predict(input_df)[0]
|
| 56 |
+
|
| 57 |
+
# Calculate sleep duration (use 8 hours if asleep, 0 if awake)
|
| 58 |
+
sleep_duration = 8 if prediction == 1 else 0
|
| 59 |
+
|
| 60 |
+
# Display prediction
|
| 61 |
+
if prediction == 1:
|
| 62 |
+
st.success(f"π΄ **You are likely asleep**. You might sleep for **{sleep_duration} hours**.")
|
| 63 |
+
else:
|
| 64 |
+
st.info(f"π **You are likely awake**. Stay active and hydrated today!")
|
| 65 |
+
|
| 66 |
+
# Generate and display health tips based on sleep duration
|
| 67 |
+
health_tips = generate_health_tips(sleep_duration)
|
| 68 |
+
st.markdown("### Health Tips:")
|
| 69 |
+
st.write(f"**{health_tips}**")
|
| 70 |
+
|
| 71 |
+
# Exercise Tips based on the user's state (asleep vs awake)
|
| 72 |
+
if prediction == 0: # Awake
|
| 73 |
+
exercise_tips = """
|
| 74 |
+
- πββοΈ **Go for a walk or jog**: 20-30 minutes of aerobic exercise can boost your energy.
|
| 75 |
+
- π§ββοΈ **Stretch or do yoga**: Focus on flexibility and relaxation to stay active.
|
| 76 |
+
- ποΈββοΈ **Strength Training**: Include bodyweight exercises like squats and push-ups.
|
| 77 |
+
"""
|
| 78 |
+
else: # Asleep
|
| 79 |
+
exercise_tips = "Rest is the best exercise. Ensure you are getting quality sleep for good health."
|
| 80 |
+
|
| 81 |
+
st.markdown("### Exercise Suggestions:")
|
| 82 |
+
st.write(f"**{exercise_tips}**")
|
| 83 |
+
|
| 84 |
+
# Visual representation: Sleep Prediction Bar Chart
|
| 85 |
+
fig, ax = plt.subplots(figsize=(8, 4))
|
| 86 |
+
ax.barh(["Predicted Sleep"], sleep_duration, color="lightblue")
|
| 87 |
+
ax.set_xlim(0, 10) # Limit max sleep time
|
| 88 |
+
ax.set_xlabel("Hours of Sleep")
|
| 89 |
+
ax.set_title("Predicted Sleep Duration")
|
| 90 |
+
st.pyplot(fig)
|
| 91 |
+
|
| 92 |
+
# # Footer for the app
|
| 93 |
+
# st.markdown("---")
|
| 94 |
+
# st.markdown("""
|
| 95 |
+
# Built with β€οΈ by **Pavan Manikanta Muthyala** | **Interactive Sleep Predictor** using LangChain and GenAI
|
| 96 |
+
# """)
|
| 97 |
+
|