|
|
import streamlit as st |
|
|
import pandas as pd |
|
|
import joblib |
|
|
import matplotlib.pyplot as plt |
|
|
from datetime import datetime, timedelta |
|
|
from langchain_google_genai import GoogleGenerativeAI |
|
|
from langchain.prompts import PromptTemplate |
|
|
from langchain.chains import LLMChain |
|
|
|
|
|
|
|
|
st.set_page_config(page_title="Interactive Sleep Predictor", layout="wide") |
|
|
|
|
|
|
|
|
st.title("β° Interactive Sleep & Health Predictor") |
|
|
st.markdown("Track your sleep, activity & get personalized health + fitness advice with Gemini π§ πͺ") |
|
|
|
|
|
|
|
|
@st.cache_resource |
|
|
def load_model(): |
|
|
return joblib.load("log_reg_model.pkl") |
|
|
|
|
|
model = load_model() |
|
|
|
|
|
|
|
|
api_key = st.secrets.get('genai_key') |
|
|
llm = GoogleGenerativeAI(model="gemini-1.5-pro", google_api_key=api_key) |
|
|
|
|
|
|
|
|
prompt_template = """ |
|
|
You are a certified health and fitness advisor. |
|
|
|
|
|
A user has recorded: |
|
|
- Sleep Duration: {sleep_duration} hours |
|
|
- Step Count: {step_count} steps |
|
|
- Current State: {state} (awake or asleep) |
|
|
|
|
|
Based on these values: |
|
|
1. Give a personalized health and wellness suggestion (max 5 lines). |
|
|
2. Give specific exercise tips suitable for their state and activity level (step count). |
|
|
3. Mention if their step count is low/average/high and whether they should increase activity. |
|
|
|
|
|
Start with "π€ Summary for the User:" and then provide your insights. |
|
|
""" |
|
|
|
|
|
|
|
|
def generate_personalized_insights(sleep_duration, step_count, state): |
|
|
prompt = PromptTemplate( |
|
|
input_variables=["sleep_duration", "step_count", "state"], |
|
|
template=prompt_template |
|
|
) |
|
|
chain = LLMChain(llm=llm, prompt=prompt) |
|
|
return chain.run({ |
|
|
"sleep_duration": sleep_duration, |
|
|
"step_count": step_count, |
|
|
"state": state |
|
|
}) |
|
|
|
|
|
|
|
|
with st.form("predictor_form"): |
|
|
step = st.number_input("πΆ Step Count (today)", min_value=0, step=10) |
|
|
hour = st.slider("β° Hour of the Day", min_value=0, max_value=23) |
|
|
|
|
|
col1, col2 = st.columns(2) |
|
|
with col1: |
|
|
sleep_time = st.time_input("π Sleep Onset Time") |
|
|
with col2: |
|
|
wake_time = st.time_input("π Wake-Up Time") |
|
|
|
|
|
submit_button = st.form_submit_button("Predict & Get Gemini Tips") |
|
|
|
|
|
|
|
|
if submit_button: |
|
|
|
|
|
input_df = pd.DataFrame([[step, hour]], columns=["step", "hour"]) |
|
|
prediction = model.predict(input_df)[0] |
|
|
state = "asleep" if prediction == 1 else "awake" |
|
|
emoji = "π΄" if state == "asleep" else "π" |
|
|
|
|
|
|
|
|
today = datetime.today() |
|
|
sleep_dt = datetime.combine(today, sleep_time) |
|
|
wake_dt = datetime.combine(today, wake_time) |
|
|
if wake_dt < sleep_dt: |
|
|
wake_dt += timedelta(days=1) |
|
|
|
|
|
sleep_duration = round((wake_dt - sleep_dt).seconds / 3600, 2) |
|
|
|
|
|
|
|
|
st.success(f"{emoji} **You're likely {state}**. You've logged **{sleep_duration} hours** of sleep and taken **{step} steps** today.") |
|
|
|
|
|
|
|
|
insights = generate_personalized_insights(sleep_duration, step, state) |
|
|
st.markdown("### π§ Gemini-Generated Tips:") |
|
|
st.markdown(insights) |
|
|
|
|
|
|
|
|
fig, ax = plt.subplots(figsize=(8, 4)) |
|
|
ax.barh(["Your Sleep Duration"], sleep_duration, color="skyblue") |
|
|
ax.set_xlim(0, 10) |
|
|
ax.set_xlabel("Hours") |
|
|
ax.set_title("Logged Sleep Duration") |
|
|
st.pyplot(fig) |
|
|
|