Health_advisor / app.py
saherPervaiz's picture
Create app.py
aeb7752 verified
raw
history blame
3.54 kB
import streamlit as st
import pandas as pd
import requests
from transformers import pipeline
import datetime
# Load the dataset
@st.cache
def load_data(file):
return pd.read_csv(file)
# Fetch health advice from the dataset
def get_health_advice(df, age, heart_rate, systolic_bp, diastolic_bp):
filtered_df = df[
(df['Age'] == age) &
(df['Heart_Rate'] == heart_rate) &
(df['Blood_Pressure_Systolic'] == systolic_bp) &
(df['Blood_Pressure_Diastolic'] == diastolic_bp)
]
if not filtered_df.empty:
return filtered_df.iloc[0]['Health_Risk_Level']
return "No matching health data found."
# Fetch related articles using the GROC API
def get_health_documents_from_groc(query):
api_key = "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" # Replace with your GROC API key
url = f"https://api.groc.com/v1/search"
params = {
"query": query,
"api_key": api_key,
"type": "article"
}
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
return data.get("results", [])
else:
return [{"title": f"Error: {response.status_code}", "url": ""}]
# GPT-2 Model for generating advice
@st.cache(allow_output_mutation=True)
def load_gpt2_model():
return pipeline("text-generation", model="gpt2")
# Main Streamlit App
def main():
st.title("Health Advisory Assistant")
st.write("A personalized health advisor based on student health data.")
# Sidebar for dataset upload
uploaded_file = st.sidebar.file_uploader("Upload your dataset (CSV)", type=["csv"])
if uploaded_file is not None:
df = load_data(uploaded_file)
st.sidebar.success("Dataset loaded successfully!")
st.write("### Dataset Preview")
st.dataframe(df.head())
# User input for health parameters
st.write("### Input Health Parameters")
age = st.number_input("Age", min_value=0, max_value=100, value=25)
heart_rate = st.number_input("Heart Rate (bpm)", min_value=0, max_value=200, value=72)
systolic_bp = st.number_input("Systolic Blood Pressure", min_value=0, max_value=200, value=120)
diastolic_bp = st.number_input("Diastolic Blood Pressure", min_value=0, max_value=200, value=80)
# Severity slider
severity = st.slider("Severity (1-10)", min_value=1, max_value=10, value=5)
# Fetch health advice
if st.button("Get Health Advice"):
risk_level = get_health_advice(df, age, heart_rate, systolic_bp, diastolic_bp)
st.write(f"**Health Risk Level**: {risk_level}")
# Fetch related health articles
st.write("### Related Health Articles")
articles = get_health_documents_from_groc("Blood Pressure and Heart Rate")
if articles:
for article in articles:
st.write(f"- [{article['title']}]({article['url']})")
else:
st.write("No articles found.")
# Generate GPT-2 response
gpt2_model = load_gpt2_model()
advice_prompt = f"Provide health advice for a person with Age: {age}, Heart Rate: {heart_rate}, Systolic BP: {systolic_bp}, Diastolic BP: {diastolic_bp}, and Severity: {severity}."
response = gpt2_model(advice_prompt, max_length=100)[0]['generated_text']
st.write("### AI-Generated Advice")
st.write(response)
# Run the app
if __name__ == "__main__":
main()