amjad21sw18 commited on
Commit
fe7cce5
Β·
verified Β·
1 Parent(s): c2a0d1d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import os
4
+ from dotenv import load_dotenv
5
+ from transformers import pipeline
6
+
7
+ # Load model directly using the pipeline
8
+ pipe = pipeline("fill-mask", model="microsoft/deberta-v3-base")
9
+
10
+ # Load environment variables
11
+ load_dotenv()
12
+
13
+ # Streamlit app configuration
14
+ st.set_page_config(page_title="AI Healthcare Status Checker", page_icon="🧠")
15
+ st.title("🧠 AI Healthcare Status Checker")
16
+
17
+ # User input section
18
+ with st.form("health_inputs"):
19
+ age = st.slider("Age", 1, 100, 25)
20
+ weight = st.number_input("Weight (kg)", min_value=1.0, value=70.0)
21
+ height = st.number_input("Height (cm)", min_value=50.0, value=170.0)
22
+ systolic = st.slider("Systolic BP", 80, 200, 120)
23
+ diastolic = st.slider("Diastolic BP", 50, 130, 80)
24
+ heart_rate = st.slider("Heart Rate (bpm)", 40, 200, 72)
25
+ submitted = st.form_submit_button("Check with AI")
26
+
27
+ # Calculate BMI
28
+ def calculate_bmi(height, weight):
29
+ height_m = height / 100
30
+ return weight / (height_m ** 2)
31
+
32
+ # API configuration
33
+ HF_TOKEN = os.getenv("HF_TOKEN")
34
+ API_URL = "https://api-inference.huggingface.co/models/cardioai/risk-prediction-v1"
35
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
36
+
37
+ def query_api(payload):
38
+ try:
39
+ response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
40
+ response.raise_for_status() # Raise an error for bad HTTP response codes
41
+ return response.json()
42
+ except requests.exceptions.RequestException as e:
43
+ st.error(f"API request failed: {str(e)}")
44
+ return None
45
+
46
+ if submitted:
47
+ bmi = calculate_bmi(height, weight)
48
+ st.write(f"πŸ“Š Calculated BMI: {bmi:.2f}")
49
+
50
+ # Prepare input data for the AI model
51
+ input_data = {
52
+ "inputs": {
53
+ "age": age,
54
+ "bmi": round(bmi, 2),
55
+ "systolic": systolic,
56
+ "diastolic": diastolic,
57
+ "heart_rate": heart_rate
58
+ }
59
+ }
60
+
61
+ with st.spinner("Analyzing your health data..."):
62
+ output = query_api(input_data)
63
+
64
+ if output:
65
+ try:
66
+ label = output[0].get('label', '')
67
+ score = output[0].get('score', 0)
68
+
69
+ if label == 'LABEL_0':
70
+ st.success(f"βœ… You are Healthy! (Confidence: {score:.2f})")
71
+ else:
72
+ st.warning(f"⚠️ Health Needs Attention! (Confidence: {score:.2f})")
73
+
74
+ # Additional health tips
75
+ if bmi > 25:
76
+ st.info("πŸ’‘ Your BMI suggests you might be overweight. Consider consulting a nutritionist.")
77
+ if systolic > 140 or diastolic > 90:
78
+ st.info("πŸ’‘ Your blood pressure is elevated. Regular monitoring is recommended.")
79
+
80
+ except (KeyError, IndexError) as e:
81
+ st.error(f"Error processing API response: {str(e)}")