amjad21sw18's picture
Create app.py
fe7cce5 verified
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)}")