import streamlit as st
import numpy as np
import pandas as pd
import pickle
# Load model
try:
with open("final_model (3).pkl", "rb") as f:
model = pickle.load(f)
st.success("โ
Model loaded successfully!")
except FileNotFoundError:
st.error("โ Model file not found! Please upload `final_model (3).pkl`.")
model = None
# Custom CSS for styling
st.markdown(
"""
""",
unsafe_allow_html=True,
)
st.markdown("
๐ฉบ PCOS Risk Predictor
", unsafe_allow_html=True)
# Collect user inputs
with st.expander("๐ Personal Information", expanded=True):
Age = st.slider("๐ Age", 18, 44, 30)
with st.expander("๐ฅ Medical Information", expanded=True):
BMI = st.number_input("โ๏ธ BMI", min_value=8, max_value=50, value=23)
Menstrual_Irregularity = st.selectbox("๐ฉธ Menstrual Irregularity (0 = No, 1 = Yes)", [0, 1])
Testosterone_Level = st.number_input("๐งฌ Testosterone Level (ng/dL)", min_value=20, max_value=135, value=53)
Antral_Follicle_Count = st.number_input("๐งช Antral Follicle Count", min_value=3, max_value=39, value=8)
# Predict
if st.button("๐ Predict Risk"):
if model is None:
st.error("โ ๏ธ Model is not loaded. Please ensure the model file exists.")
else:
try:
input_df = pd.DataFrame([[
Age,
BMI,
Menstrual_Irregularity,
Testosterone_Level,
Antral_Follicle_Count
]], columns=[
'Age',
'BMI',
'Menstrual_Irregularity',
'Testosterone_Level(ng/dL)',
'Antral_Follicle_Count'
])
prediction = model.predict(input_df)[0]
probability = model.predict_proba(input_df)[0][1] * 100
if prediction == 1:
result_text = "โ ๏ธโ High Risk of PCOS Detected!"
result_class = "high-risk"
else:
result_text = "โ
Low Risk of PCOS โ You're Good!"
result_class = "low-risk"
st.markdown(
f"{result_text}
",
unsafe_allow_html=True
)
st.markdown(
f"๐ Estimated Probability: {probability:.2f}%
",
unsafe_allow_html=True
)
except Exception as e:
st.error(f"๐ซ Prediction failed: {e}")