saherPervaiz commited on
Commit
a8ba087
·
verified ·
1 Parent(s): e2e8eb2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -55
app.py CHANGED
@@ -1,29 +1,22 @@
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,
@@ -35,57 +28,37 @@ def get_health_documents_from_groc(query):
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()
 
1
  import streamlit as st
2
  import pandas as pd
3
  import requests
 
 
4
 
5
+ # Function to get health advice based on inputs
 
 
 
 
 
6
  def get_health_advice(df, age, heart_rate, systolic_bp, diastolic_bp):
7
  filtered_df = df[
8
+ (df['Age'].between(age - 2, age + 2)) & # Allow ±2 years
9
+ (df['Heart_Rate'].between(heart_rate - 5, heart_rate + 5)) & # Allow ±5 bpm
10
+ (df['Blood_Pressure_Systolic'].between(systolic_bp - 10, systolic_bp + 10)) & # Allow ±10
11
+ (df['Blood_Pressure_Diastolic'].between(diastolic_bp - 10, diastolic_bp + 10)) # Allow ±10
12
  ]
13
  if not filtered_df.empty:
14
  return filtered_df.iloc[0]['Health_Risk_Level']
15
  return "No matching health data found."
16
 
17
+ # Function to get health articles from GROC API
18
  def get_health_documents_from_groc(query):
19
+ api_key = "YOUR_GROC_API_KEY" # Replace with your actual GROC API key
20
  url = f"https://api.groc.com/v1/search"
21
  params = {
22
  "query": query,
 
28
  data = response.json()
29
  return data.get("results", [])
30
  else:
31
+ st.error(f"Error {response.status_code}: {response.text}")
32
  return [{"title": f"Error: {response.status_code}", "url": ""}]
33
 
34
+ # Main Streamlit app
 
 
 
 
 
35
  def main():
36
+ st.title("Health Risk Level and Advisory Assistant")
 
37
 
38
+ # File upload
39
+ uploaded_file = st.file_uploader("Upload your dataset (CSV)", type="csv")
40
  if uploaded_file is not None:
41
+ df = pd.read_csv(uploaded_file)
42
+ st.write("Dataset Preview:")
 
43
  st.dataframe(df.head())
44
 
45
+ # User input
46
+ age = st.number_input("Enter Age", min_value=1, max_value=100, step=1)
47
+ heart_rate = st.number_input("Enter Heart Rate (bpm)", min_value=30, max_value=200, step=1)
48
+ systolic_bp = st.number_input("Enter Systolic Blood Pressure", min_value=80, max_value=200, step=1)
49
+ diastolic_bp = st.number_input("Enter Diastolic Blood Pressure", min_value=40, max_value=120, step=1)
 
50
 
51
+ # Predict health risk level
52
+ if st.button("Get Health Risk Level"):
 
 
 
53
  risk_level = get_health_advice(df, age, heart_rate, systolic_bp, diastolic_bp)
54
+ st.write(f"Health Risk Level: {risk_level}")
 
 
 
 
 
 
 
 
 
55
 
56
+ # Retrieve related health articles
57
+ query = f"Health risk for age {age}, heart rate {heart_rate}, BP {systolic_bp}/{diastolic_bp}"
58
+ st.write("Related Health Articles:")
59
+ articles = get_health_documents_from_groc(query)
60
+ for article in articles:
61
+ st.markdown(f"- [{article['title']}]({article['url']})")
62
 
 
63
  if __name__ == "__main__":
64
  main()