Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import requests
|
4 |
+
from transformers import pipeline
|
5 |
+
import datetime
|
6 |
+
|
7 |
+
# Load the dataset
|
8 |
+
@st.cache
|
9 |
+
def load_data(file):
|
10 |
+
return pd.read_csv(file)
|
11 |
+
|
12 |
+
# Fetch health advice from the dataset
|
13 |
+
def get_health_advice(df, age, heart_rate, systolic_bp, diastolic_bp):
|
14 |
+
filtered_df = df[
|
15 |
+
(df['Age'] == age) &
|
16 |
+
(df['Heart_Rate'] == heart_rate) &
|
17 |
+
(df['Blood_Pressure_Systolic'] == systolic_bp) &
|
18 |
+
(df['Blood_Pressure_Diastolic'] == diastolic_bp)
|
19 |
+
]
|
20 |
+
if not filtered_df.empty:
|
21 |
+
return filtered_df.iloc[0]['Health_Risk_Level']
|
22 |
+
return "No matching health data found."
|
23 |
+
|
24 |
+
# Fetch related articles using the GROC API
|
25 |
+
def get_health_documents_from_groc(query):
|
26 |
+
api_key = "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" # Replace with your GROC API key
|
27 |
+
url = f"https://api.groc.com/v1/search"
|
28 |
+
params = {
|
29 |
+
"query": query,
|
30 |
+
"api_key": api_key,
|
31 |
+
"type": "article"
|
32 |
+
}
|
33 |
+
response = requests.get(url, params=params)
|
34 |
+
if response.status_code == 200:
|
35 |
+
data = response.json()
|
36 |
+
return data.get("results", [])
|
37 |
+
else:
|
38 |
+
return [{"title": f"Error: {response.status_code}", "url": ""}]
|
39 |
+
|
40 |
+
# GPT-2 Model for generating advice
|
41 |
+
@st.cache(allow_output_mutation=True)
|
42 |
+
def load_gpt2_model():
|
43 |
+
return pipeline("text-generation", model="gpt2")
|
44 |
+
|
45 |
+
# Main Streamlit App
|
46 |
+
def main():
|
47 |
+
st.title("Health Advisory Assistant")
|
48 |
+
st.write("A personalized health advisor based on student health data.")
|
49 |
+
|
50 |
+
# Sidebar for dataset upload
|
51 |
+
uploaded_file = st.sidebar.file_uploader("Upload your dataset (CSV)", type=["csv"])
|
52 |
+
if uploaded_file is not None:
|
53 |
+
df = load_data(uploaded_file)
|
54 |
+
st.sidebar.success("Dataset loaded successfully!")
|
55 |
+
st.write("### Dataset Preview")
|
56 |
+
st.dataframe(df.head())
|
57 |
+
|
58 |
+
# User input for health parameters
|
59 |
+
st.write("### Input Health Parameters")
|
60 |
+
age = st.number_input("Age", min_value=0, max_value=100, value=25)
|
61 |
+
heart_rate = st.number_input("Heart Rate (bpm)", min_value=0, max_value=200, value=72)
|
62 |
+
systolic_bp = st.number_input("Systolic Blood Pressure", min_value=0, max_value=200, value=120)
|
63 |
+
diastolic_bp = st.number_input("Diastolic Blood Pressure", min_value=0, max_value=200, value=80)
|
64 |
+
|
65 |
+
# Severity slider
|
66 |
+
severity = st.slider("Severity (1-10)", min_value=1, max_value=10, value=5)
|
67 |
+
|
68 |
+
# Fetch health advice
|
69 |
+
if st.button("Get Health Advice"):
|
70 |
+
risk_level = get_health_advice(df, age, heart_rate, systolic_bp, diastolic_bp)
|
71 |
+
st.write(f"**Health Risk Level**: {risk_level}")
|
72 |
+
|
73 |
+
# Fetch related health articles
|
74 |
+
st.write("### Related Health Articles")
|
75 |
+
articles = get_health_documents_from_groc("Blood Pressure and Heart Rate")
|
76 |
+
if articles:
|
77 |
+
for article in articles:
|
78 |
+
st.write(f"- [{article['title']}]({article['url']})")
|
79 |
+
else:
|
80 |
+
st.write("No articles found.")
|
81 |
+
|
82 |
+
# Generate GPT-2 response
|
83 |
+
gpt2_model = load_gpt2_model()
|
84 |
+
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}."
|
85 |
+
response = gpt2_model(advice_prompt, max_length=100)[0]['generated_text']
|
86 |
+
st.write("### AI-Generated Advice")
|
87 |
+
st.write(response)
|
88 |
+
|
89 |
+
# Run the app
|
90 |
+
if __name__ == "__main__":
|
91 |
+
main()
|