|
import streamlit as st |
|
import requests |
|
import os |
|
from dotenv import load_dotenv |
|
from transformers import pipeline |
|
|
|
|
|
pipe = pipeline("fill-mask", model="microsoft/deberta-v3-base") |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
st.set_page_config(page_title="AI Healthcare Status Checker", page_icon="π§ ") |
|
st.title("π§ AI Healthcare Status Checker") |
|
|
|
|
|
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") |
|
|
|
|
|
def calculate_bmi(height, weight): |
|
height_m = height / 100 |
|
return weight / (height_m ** 2) |
|
|
|
|
|
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() |
|
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}") |
|
|
|
|
|
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})") |
|
|
|
|
|
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)}") |
|
|