File size: 2,872 Bytes
fe7cce5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests
import os
from dotenv import load_dotenv
from transformers import pipeline

# Load model directly using the pipeline
pipe = pipeline("fill-mask", model="microsoft/deberta-v3-base")

# Load environment variables
load_dotenv()

# Streamlit app configuration
st.set_page_config(page_title="AI Healthcare Status Checker", page_icon="🧠")
st.title("🧠 AI Healthcare Status Checker")

# User input section
with st.form("health_inputs"):
    age = st.slider("Age", 1, 100, 25)
    weight = st.number_input("Weight (kg)", min_value=1.0, value=70.0)
    height = st.number_input("Height (cm)", min_value=50.0, value=170.0)
    systolic = st.slider("Systolic BP", 80, 200, 120)
    diastolic = st.slider("Diastolic BP", 50, 130, 80)
    heart_rate = st.slider("Heart Rate (bpm)", 40, 200, 72)
    submitted = st.form_submit_button("Check with AI")

# Calculate BMI
def calculate_bmi(height, weight):
    height_m = height / 100
    return weight / (height_m ** 2)

# API configuration
HF_TOKEN = os.getenv("HF_TOKEN")
API_URL = "https://api-inference.huggingface.co/models/cardioai/risk-prediction-v1"
headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}

def query_api(payload):
    try:
        response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
        response.raise_for_status()  # Raise an error for bad HTTP response codes
        return response.json()
    except requests.exceptions.RequestException as e:
        st.error(f"API request failed: {str(e)}")
        return None

if submitted:
    bmi = calculate_bmi(height, weight)
    st.write(f"πŸ“Š Calculated BMI: {bmi:.2f}")
    
    # Prepare input data for the AI model
    input_data = {
        "inputs": {
            "age": age,
            "bmi": round(bmi, 2),
            "systolic": systolic,
            "diastolic": diastolic,
            "heart_rate": heart_rate
        }
    }

    with st.spinner("Analyzing your health data..."):
        output = query_api(input_data)
    
    if output:
        try:
            label = output[0].get('label', '')
            score = output[0].get('score', 0)
            
            if label == 'LABEL_0':
                st.success(f"βœ… You are Healthy! (Confidence: {score:.2f})")
            else:
                st.warning(f"⚠️ Health Needs Attention! (Confidence: {score:.2f})")
                
                # Additional health tips
                if bmi > 25:
                    st.info("πŸ’‘ Your BMI suggests you might be overweight. Consider consulting a nutritionist.")
                if systolic > 140 or diastolic > 90:
                    st.info("πŸ’‘ Your blood pressure is elevated. Regular monitoring is recommended.")
                    
        except (KeyError, IndexError) as e:
            st.error(f"Error processing API response: {str(e)}")