File size: 3,536 Bytes
aeb7752
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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()