import streamlit as st import pandas as pd import numpy as np from pathlib import Path import sys import os import time from datetime import datetime import plotly.express as px # Add project root to Python path project_root = Path(__file__).resolve().parent.parent sys.path.append(str(project_root)) from src.model import BreastCancerModel from src.models.diabetes import DiabetesModel from src.models.heart_disease import HeartDiseaseModel from src.models.parkinsons import ParkinsonsModel from src.config import ( BREAST_CANCER_MODEL_PATH, DIABETES_MODEL_PATH, HEART_DISEASE_MODEL_PATH, PARKINSONS_MODEL_PATH ) # Set page config st.set_page_config( page_title="Medical Prediction System", page_icon="🏥", layout="wide", initial_sidebar_state="expanded" ) # Add this updated CSS at the beginning of the file st.markdown(""" """, unsafe_allow_html=True) def check_model_exists(model_path): """Check if a model file exists""" return os.path.exists(model_path) def load_animation(): """Show a loading animation""" with st.spinner('Loading...'): time.sleep(0.5) def show_success_animation(): """Show success animation""" placeholder = st.empty() for i in range(5): placeholder.markdown(f"{'🎯' * (i+1)}") time.sleep(0.1) placeholder.empty() def add_home_button(): """Add a Back to Home button""" if st.button("🏠 Back to Home"): st.session_state.page = "Home" def show_loading_page(): """Show an animated loading screen""" placeholder = st.empty() with placeholder.container(): st.markdown("""

🏥 Medical AI Assistant

Loading advanced diagnostic tools...

""", unsafe_allow_html=True) time.sleep(1) placeholder.empty() def show_success_message(message): """Show animated success message""" st.markdown(f"""
{message}
""", unsafe_allow_html=True) def show_feature_cards(): """Show animated feature cards""" st.markdown("""
🎯

High Accuracy

Advanced ML algorithms with 96.5% accuracy

Real-time Analysis

Get instant predictions and risk assessments

🔒

Secure Analysis

Your data is processed securely and privately

""", unsafe_allow_html=True) def home_page(): show_loading_page() # Hero section with gradient background st.markdown("""

🏥 Medical AI Assistant

Advanced AI-powered diagnostics for healthcare professionals

""", unsafe_allow_html=True) # Quick stats cards st.markdown("""

96.5%

Accuracy Rate

5,200+

Assessments

0.5s

Response Time

1,200+

Active Users

""", unsafe_allow_html=True) # Available tools section st.markdown("""

Available Assessment Tools

""", unsafe_allow_html=True) col1, col2 = st.columns(2) with col1: st.markdown("""

🔬 Breast Cancer Assessment

Advanced cellular analysis using machine learning to assess cancer risk with high accuracy.

""", unsafe_allow_html=True) if st.button("Start Breast Cancer Assessment", key="breast"): st.session_state.page = "Breast Cancer" st.markdown("""

❤️ Heart Disease Assessment

Comprehensive cardiovascular risk analysis using multiple health indicators.

""", unsafe_allow_html=True) if st.button("Start Heart Disease Assessment", key="heart"): st.session_state.page = "Heart Disease" with col2: st.markdown("""

🩺 Diabetes Assessment

Predictive analysis of diabetes risk based on key health metrics and indicators.

""", unsafe_allow_html=True) if st.button("Start Diabetes Assessment", key="diabetes"): st.session_state.page = "Diabetes" st.markdown("""

🧠 Parkinson's Assessment

Advanced voice pattern analysis for early detection of Parkinson's disease.

""", unsafe_allow_html=True) if st.button("Start Parkinson's Assessment", key="parkinsons"): st.session_state.page = "Parkinson's Disease" # Technical Specifications Section st.markdown("""

Technical Specifications

🔬 Data Sources

  • • Breast Cancer Wisconsin Dataset
  • • Pima Indians Diabetes Database
  • • Heart Disease UCI Dataset
  • • Parkinson's Disease Dataset

⚙️ Technologies Used

  • • Machine Learning: scikit-learn
  • • Web Interface: Streamlit
  • • Data Processing: pandas, numpy
  • • Version Control: Git

📊 Model Performance

  • • Breast Cancer Detection: 96.5%
  • • Diabetes Prediction: 94.2%
  • • Heart Disease Assessment: 91.8%
  • • Parkinson's Detection: 93.5%
""", unsafe_allow_html=True) # Features Section st.markdown("""

Why Choose Our Platform?

🎯

High Accuracy

Advanced ML algorithms with 96.5% accuracy in predictions

Real-time Analysis

Get instant predictions and comprehensive risk assessments

🔒

Secure Analysis

Your data is processed securely and privately

""", unsafe_allow_html=True) # Research & Publications Section st.markdown("## Research & Publications") # Create three columns for the sections col1, col2, col3 = st.columns(3) with col1: st.markdown("### 📚 Recent Papers") with st.container(): st.markdown(""" **Machine Learning in Medical Diagnosis** (2023) *Impact on early disease detection and prevention* """) st.markdown(""" **AI Applications in Healthcare** (2022) *Transforming patient care through technology* """) st.markdown(""" **Early Disease Detection Using ML** (2023) *Predictive analytics in healthcare* """) with col2: st.markdown("### 🔍 Methodology") with st.container(): st.info(""" Our system employs advanced machine learning algorithms trained on extensive medical datasets, ensuring reliable and accurate predictions for various medical conditions. All models undergo rigorous testing and validation procedures, with continuous monitoring and updates to maintain high accuracy levels. """) with col3: st.markdown("### 🎯 Future Developments") # Future Development Cards with st.container(): st.success("**Integration**\n\nElectronic health records integration for seamless data flow") st.success("**Visualization**\n\nAdvanced visualization tools for better insight into predictions") st.success("**Mobile Access**\n\nDevelopment of mobile applications for on-the-go access") # Add some spacing st.markdown("
", unsafe_allow_html=True) # Footer Section st.markdown("---") # Add a divider # Header st.header("Ready to get started?") st.write("Choose any assessment tool above to begin your analysis") # Create three columns for contact, resources, and legal contact_col, resources_col, legal_col = st.columns(3) with contact_col: st.subheader("Contact") st.markdown(""" 📧 **Email:** support@medicalai.com 📞 **Phone:** +1 (555) 123-4567 """) with resources_col: st.subheader("Resources") st.markdown(""" 📚 [Documentation](https://docs.medicalai.com) 🔧 [API Reference](https://api.medicalai.com) """) with legal_col: st.subheader("Legal") st.markdown(""" 📜 [Privacy Policy](https://privacy.medicalai.com) ⚖️ [Terms of Service](https://terms.medicalai.com) """) # Copyright and version info st.markdown("---") col1, col2 = st.columns(2) with col1: st.markdown("© 2024 Medical AI Assistant | Version 1.0.0") with col2: st.markdown("Developed with ❤️ for healthcare professionals") # Add new sections st.markdown("## 📊 Additional Features") # Create tabs for different features tab1, tab2, tab3 = st.tabs(["📈 History", "🔍 Analysis", "💡 Recommendations"]) with tab1: show_patient_history() export_report() with tab2: show_risk_factors_analysis() show_trends_analysis() with tab3: show_recommendations() compare_assessments() def breast_cancer_prediction(): add_home_button() show_loading_page() st.markdown(""" """, unsafe_allow_html=True) if not check_model_exists(BREAST_CANCER_MODEL_PATH): st.error("⚠️ Breast Cancer model not found. Please train the model first.") return try: model = BreastCancerModel.load_model() except Exception as e: st.error(f"⚠️ Error loading model: {str(e)}") return # Create tabs for input methods tab1, tab2 = st.tabs(["📊 Standard Input", "🔬 Detailed Input"]) with tab1: col1, col2 = st.columns(2) with col1: mean_radius = st.slider("Mean Radius", 6.0, 28.0, 14.0, help="Average size of cell nuclei") mean_texture = st.slider("Mean Texture", 9.0, 40.0, 14.0, help="Average standard deviation of gray-scale values") mean_perimeter = st.slider("Mean Perimeter", 40.0, 190.0, 90.0, help="Average size of the core tumor") mean_area = st.slider("Mean Area", 140.0, 2500.0, 550.0, help="Average area of cell nuclei") with col2: mean_smoothness = st.slider("Mean Smoothness", 0.05, 0.16, 0.1, help="Average of local variation in radius lengths") mean_compactness = st.slider("Mean Compactness", 0.02, 0.35, 0.1, help="Average of perimeter^2 / area - 1.0") mean_concavity = st.slider("Mean Concavity", 0.0, 0.5, 0.1, help="Average severity of concave portions of the contour") mean_concave_points = st.slider("Mean Concave Points", 0.0, 0.2, 0.1, help="Average number of concave portions of the contour") with tab2: st.markdown("### Detailed Measurements") col1, col2, col3 = st.columns(3) with col1: radius_mean = st.number_input("Radius (mean)", 6.0, 28.0, 14.0, help="Mean of distances from center to points on the perimeter") texture_mean = st.number_input("Texture (mean)", 9.0, 40.0, 14.0, help="Standard deviation of gray-scale values") perimeter_mean = st.number_input("Perimeter (mean)", 40.0, 190.0, 90.0, help="Mean size of the core tumor") area_mean = st.number_input("Area (mean)", 140.0, 2500.0, 550.0, help="Mean area of the tumor") smoothness_mean = st.number_input("Smoothness (mean)", 0.05, 0.16, 0.1, help="Mean of local variation in radius lengths") compactness_mean = st.number_input("Compactness (mean)", 0.02, 0.35, 0.1, help="Mean of perimeter^2 / area - 1.0") concavity_mean = st.number_input("Concavity (mean)", 0.0, 0.5, 0.1, help="Mean of severity of concave portions") concave_points_mean = st.number_input("Concave points (mean)", 0.0, 0.2, 0.1, help="Mean number of concave portions") symmetry_mean = st.number_input("Symmetry (mean)", 0.1, 0.3, 0.2, help="Mean symmetry of the tumor") fractal_dimension_mean = st.number_input("Fractal dimension (mean)", 0.05, 0.1, 0.06, help="Mean fractal dimension") with col2: radius_se = st.number_input("Radius (SE)", 0.1, 2.0, 0.4, help="Standard error of distances from center to points") texture_se = st.number_input("Texture (SE)", 0.2, 4.0, 1.0, help="Standard error of gray-scale values") perimeter_se = st.number_input("Perimeter (SE)", 1.0, 20.0, 5.0, help="Standard error of perimeter") area_se = st.number_input("Area (SE)", 6.0, 540.0, 40.0, help="Standard error of area") smoothness_se = st.number_input("Smoothness (SE)", 0.001, 0.03, 0.007, help="Standard error of smoothness") compactness_se = st.number_input("Compactness (SE)", 0.002, 0.135, 0.025, help="Standard error of compactness") concavity_se = st.number_input("Concavity (SE)", 0.0, 0.396, 0.03, help="Standard error of concavity") concave_points_se = st.number_input("Concave points (SE)", 0.0, 0.05, 0.01, help="Standard error of concave points") symmetry_se = st.number_input("Symmetry (SE)", 0.008, 0.079, 0.02, help="Standard error of symmetry") fractal_dimension_se = st.number_input("Fractal dimension (SE)", 0.001, 0.029, 0.003, help="Standard error of fractal dimension") with col3: radius_worst = st.number_input("Radius (worst)", 7.0, 36.0, 16.0, help="Worst radius") texture_worst = st.number_input("Texture (worst)", 12.0, 50.0, 21.0, help="Worst texture") perimeter_worst = st.number_input("Perimeter (worst)", 50.0, 250.0, 107.0, help="Worst perimeter") area_worst = st.number_input("Area (worst)", 185.0, 4250.0, 750.0, help="Worst area") smoothness_worst = st.number_input("Smoothness (worst)", 0.07, 0.22, 0.13, help="Worst smoothness") compactness_worst = st.number_input("Compactness (worst)", 0.03, 1.06, 0.25, help="Worst compactness") concavity_worst = st.number_input("Concavity (worst)", 0.0, 1.25, 0.27, help="Worst concavity") concave_points_worst = st.number_input("Concave points (worst)", 0.0, 0.29, 0.11, help="Worst concave points") symmetry_worst = st.number_input("Symmetry (worst)", 0.15, 0.66, 0.29, help="Worst symmetry") fractal_dimension_worst = st.number_input("Fractal dimension (worst)", 0.055, 0.207, 0.083, help="Worst fractal dimension") # Add analyze button outside tabs to work for both if st.button("Analyze Risk", help="Click to analyze breast cancer risk"): with st.spinner('Analyzing samples...'): try: # Get input data based on active tab if tab1._active: input_data = np.array([ mean_radius, mean_texture, mean_perimeter, mean_area, mean_smoothness, mean_compactness, mean_concavity, mean_concave_points, 0.2, 0.06, 0.4, 0.4, 2.0, 20.0, 0.01, 0.02, 0.02, 0.01, 0.02, 0.003, 16.0, 16.0, 100.0, 700.0, 0.12, 0.15, 0.15, 0.1, 0.25, 0.08 ]).reshape(1, -1) else: input_data = np.array([ radius_mean, texture_mean, perimeter_mean, area_mean, smoothness_mean, compactness_mean, concavity_mean, concave_points_mean, symmetry_mean, fractal_dimension_mean, radius_se, texture_se, perimeter_se, area_se, smoothness_se, compactness_se, concavity_se, concave_points_se, symmetry_se, fractal_dimension_se, radius_worst, texture_worst, perimeter_worst, area_worst, smoothness_worst, compactness_worst, concavity_worst, concave_points_worst, symmetry_worst, fractal_dimension_worst ]).reshape(1, -1) prediction, similar_cases, similar_outcomes, distances = model.predict(input_data) # Show prediction results if prediction[0] == 0: st.error("⚠️ High Risk of Breast Cancer") st.warning( "The analysis indicates characteristics commonly associated with malignant breast masses." ) # Show risk factors based on active tab st.subheader("Risk Factors Identified") if tab1._active: if mean_radius > 15: st.warning(f"• Mean radius ({mean_radius:.2f}) is elevated") if mean_concave_points > 0.05: st.warning(f"• Mean concave points ({mean_concave_points:.3f}) are high") else: if radius_worst > 20: st.warning(f"• Worst radius ({radius_worst:.2f}) is significantly elevated") if concave_points_worst > 0.15: st.warning(f"• Worst concave points ({concave_points_worst:.3f}) are very high") else: st.success("✅ Low Risk of Breast Cancer") st.info( "The analysis indicates characteristics commonly associated with benign breast masses." ) # Show similar cases with st.expander("View Similar Cases"): st.markdown("### Reference Cases") st.markdown("These are similar cases from our database:") similar_df = pd.DataFrame({ 'Mean Radius': similar_cases['mean radius'].round(2), 'Mean Texture': similar_cases['mean texture'].round(2), 'Mean Area': similar_cases['mean area'].round(2), 'Diagnosis': ['Malignant' if o == 0 else 'Benign' for o in similar_outcomes], 'Similarity': [f"{(1 - d/d.max())*100:.1f}%" for d in distances] }) st.dataframe(similar_df) show_success_message("Analysis completed successfully!") except Exception as e: st.error(f"⚠️ Error during analysis: {str(e)}") def diabetes_prediction(): # Add home button at the top add_home_button() load_animation() st.header("Diabetes Prediction") st.write("Enter measurements to predict diabetes risk") try: model = DiabetesModel.load_model() except Exception as e: st.error(f"Error loading model: {str(e)}") return col1, col2 = st.columns(2) with col1: pregnancies = st.number_input("Number of Pregnancies", value=0, min_value=0) glucose = st.number_input("Glucose (mg/dL)", value=120, min_value=0) blood_pressure = st.number_input("Blood Pressure (mm Hg)", value=70, min_value=0) skin_thickness = st.number_input("Skin Thickness (mm)", value=20, min_value=0) with col2: insulin = st.number_input("Insulin (mu U/ml)", value=79, min_value=0) bmi = st.number_input("BMI", value=25.0, min_value=0.0) dpf = st.number_input("Diabetes Pedigree Function", value=0.5, min_value=0.0) age = st.number_input("Age", value=33, min_value=0) if st.button("Predict"): try: # Calculate derived features glucose_bmi = glucose * bmi / 1000 glucose_age = glucose * age / 100 input_data = np.array([ pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, dpf, age, glucose_bmi, glucose_age ]).reshape(1, -1) prediction, similar_cases, similar_outcomes, distances = model.predict(input_data) # Show prediction with risk factors if prediction[0] == 1: st.error("High risk of diabetes") if glucose > 140: st.warning("⚠️ High glucose level detected") if bmi > 30: st.warning("⚠️ High BMI detected") else: st.success("Low risk of diabetes") # Show similar cases st.write("### Similar Cases from Dataset") st.write("The prediction is based on these similar cases:") similar_df = pd.DataFrame({ 'Age': similar_cases['Age'].round(1), 'BMI': similar_cases['BMI'].round(1), 'Glucose': similar_cases['Glucose'].round(1), 'Blood Pressure': similar_cases['BloodPressure'].round(1), 'Outcome': ['Diabetic' if o == 1 else 'Non-diabetic' for o in similar_outcomes], 'Similarity': [f"{(1 - d/d.max())*100:.1f}%" for d in distances] }) st.dataframe(similar_df) # Show risk analysis st.write("### Risk Analysis") risk_factors = [] if glucose > 140: risk_factors.append(f"Glucose ({glucose} mg/dL) is above normal range") if bmi > 30: risk_factors.append(f"BMI ({bmi:.1f}) indicates obesity") if blood_pressure > 90: risk_factors.append(f"Blood pressure ({blood_pressure} mm Hg) is elevated") if dpf > 0.8: risk_factors.append(f"Diabetes pedigree function ({dpf:.2f}) indicates family history") if risk_factors: st.write("Risk factors identified:") for factor in risk_factors: st.write(f"• {factor}") else: st.write("No major risk factors identified") except Exception as e: st.error(f"Error making prediction: {str(e)}") def heart_disease_prediction(): # Add home button at the top add_home_button() load_animation() st.header("Heart Disease Prediction") st.write("Enter measurements to predict heart disease risk") try: model = HeartDiseaseModel.load_model() except Exception as e: st.error(f"Error loading model: {str(e)}") return col1, col2 = st.columns(2) with col1: age = st.number_input("Age", value=50, min_value=0) sex = st.selectbox("Sex", ["Male", "Female"]) cp = st.selectbox("Chest Pain Type", ["Typical Angina", "Atypical Angina", "Non-anginal Pain", "Asymptomatic"]) trestbps = st.number_input("Resting Blood Pressure (mm Hg)", value=120, min_value=0) chol = st.number_input("Serum Cholesterol (mg/dl)", value=200, min_value=0) fbs = st.selectbox("Fasting Blood Sugar > 120 mg/dl", ["No", "Yes"]) restecg = st.selectbox("Resting ECG Results", ["Normal", "ST-T Wave Abnormality", "Left Ventricular Hypertrophy"]) with col2: thalach = st.number_input("Maximum Heart Rate", value=150, min_value=0) exang = st.selectbox("Exercise Induced Angina", ["No", "Yes"]) oldpeak = st.number_input("ST Depression by Exercise", value=0.0) slope = st.selectbox("Slope of Peak Exercise ST", ["Upsloping", "Flat", "Downsloping"]) ca = st.number_input("Number of Major Vessels (0-3)", value=0, min_value=0, max_value=3) thal = st.selectbox("Thalassemia", ["Normal", "Fixed Defect", "Reversible Defect"]) if st.button("Predict"): try: # Convert categorical inputs to numerical sex_num = 1 if sex == "Male" else 0 cp_num = ["Typical Angina", "Atypical Angina", "Non-anginal Pain", "Asymptomatic"].index(cp) fbs_num = 1 if fbs == "Yes" else 0 restecg_num = ["Normal", "ST-T Wave Abnormality", "Left Ventricular Hypertrophy"].index(restecg) exang_num = 1 if exang == "Yes" else 0 slope_num = ["Upsloping", "Flat", "Downsloping"].index(slope) thal_num = ["Normal", "Fixed Defect", "Reversible Defect"].index(thal) + 3 input_data = np.array([ age, sex_num, cp_num, trestbps, chol, fbs_num, restecg_num, thalach, exang_num, oldpeak, slope_num, ca, thal_num ]).reshape(1, -1) prediction, similar_cases, similar_outcomes, distances = model.predict(input_data) # Show prediction and risk analysis if prediction[0] == 1: st.error("High risk of heart disease") # Show specific risk factors st.write("### Risk Factors Identified:") risk_factors = [] if age > 60: risk_factors.append(f"Age ({age} years) - Higher risk with increasing age") if cp_num >= 2: risk_factors.append("Chest Pain Type indicates potential issue") if trestbps > 140: risk_factors.append(f"High Blood Pressure ({trestbps} mm Hg)") if chol > 240: risk_factors.append(f"High Cholesterol ({chol} mg/dl)") if thalach < 120: risk_factors.append(f"Low Maximum Heart Rate ({thalach} bpm)") if oldpeak > 2: risk_factors.append(f"Significant ST Depression ({oldpeak})") if ca > 0: risk_factors.append(f"Number of Major Vessels: {ca}") for factor in risk_factors: st.warning(f"⚠️ {factor}") else: st.success("Low risk of heart disease") # Show protective factors good_factors = [] if age < 50: good_factors.append(f"Age ({age} years) is in a lower risk category") if trestbps < 120: good_factors.append(f"Normal Blood Pressure ({trestbps} mm Hg)") if chol < 200: good_factors.append(f"Healthy Cholesterol Level ({chol} mg/dl)") if good_factors: st.write("### Protective Factors:") for factor in good_factors: st.info(f"✓ {factor}") # Show similar cases st.write("### Similar Cases from Dataset") st.write("The prediction is based on these similar cases:") similar_df = pd.DataFrame({ 'Age': similar_cases['age'].round(0), 'Sex': ['Male' if s == 1 else 'Female' for s in similar_cases['sex']], 'Blood Pressure': similar_cases['trestbps'].round(0), 'Cholesterol': similar_cases['chol'].round(0), 'Max Heart Rate': similar_cases['thalach'].round(0), 'Outcome': ['High Risk' if o == 1 else 'Low Risk' for o in similar_outcomes], 'Similarity': [f"{(1 - d/d.max())*100:.1f}%" for d in distances] }) st.dataframe(similar_df) except Exception as e: st.error(f"Error making prediction: {str(e)}") def parkinsons_prediction(): # Add home button at the top add_home_button() load_animation() st.header("Parkinsons Disease Prediction") st.write("Enter the following measurements:") if not check_model_exists(PARKINSONS_MODEL_PATH): st.error("Parkinson's model not found. Please train the model first.") if st.button("Train Parkinson's Model"): try: from train_models import train_parkinsons train_parkinsons() st.success("Model trained successfully! Please refresh the page.") except Exception as e: st.error(f"Error training model: {str(e)}") return try: model = ParkinsonsModel.load_model() except Exception as e: st.error(f"Error loading model: {str(e)}") return col1, col2 = st.columns(2) with col1: mdvp_fo = st.number_input("MDVP:Fo(Hz)", min_value=88.333, max_value=260.105, value=120.000, format="%.6f") mdvp_fhi = st.number_input("MDVP:Fhi(Hz)", min_value=102.145, max_value=592.030, value=157.000, format="%.6f") mdvp_flo = st.number_input("MDVP:Flo(Hz)", min_value=65.476, max_value=239.170, value=75.000, format="%.6f") mdvp_jitter = st.number_input("MDVP:Jitter(%)", min_value=0.00168, max_value=0.03316, value=0.00784, format="%.6f") mdvp_jitter_abs = st.number_input("MDVP:Jitter(Abs)", min_value=0.000007, max_value=0.000260, value=0.000070, format="%.6f") mdvp_rap = st.number_input("MDVP:RAP", min_value=0.00068, max_value=0.02144, value=0.00370, format="%.6f") mdvp_ppq = st.number_input("MDVP:PPQ", min_value=0.00092, max_value=0.01958, value=0.00554, format="%.6f") jitter_ddp = st.number_input("Jitter:DDP", min_value=0.00204, max_value=0.06433, value=0.01109, format="%.6f") with col2: mdvp_shimmer = st.number_input("MDVP:Shimmer", min_value=0.00954, max_value=0.11908, value=0.04374, format="%.6f") mdvp_shimmer_db = st.number_input("MDVP:Shimmer(dB)", min_value=0.085, max_value=1.302, value=0.426, format="%.6f") shimmer_apq3 = st.number_input("Shimmer:APQ3", min_value=0.00455, max_value=0.05647, value=0.02182, format="%.6f") shimmer_apq5 = st.number_input("Shimmer:APQ5", min_value=0.0057, max_value=0.0794, value=0.03130, format="%.6f") mdvp_apq = st.number_input("MDVP:APQ", min_value=0.00719, max_value=0.13778, value=0.02971, format="%.6f") shimmer_dda = st.number_input("Shimmer:DDA", min_value=0.01364, max_value=0.16942, value=0.06545, format="%.6f") nhr = st.number_input("NHR", min_value=0.00065, max_value=0.31482, value=0.02211, format="%.6f") hnr = st.number_input("HNR", min_value=8.441, max_value=33.047, value=21.033, format="%.6f") rpde = st.number_input("RPDE", min_value=0.256570, max_value=0.685151, value=0.414783, format="%.6f") dfa = st.number_input("DFA", min_value=0.574282, max_value=0.825288, value=0.815285, format="%.6f") spread1 = st.number_input("Spread1", min_value= -7.964984, max_value= -2.434031, value= -4.813031, format="%.6f") spread2 = st.number_input("Spread2", min_value=0.006274, max_value=0.450493, value=0.266482, format="%.6f") d2 = st.number_input("D2", min_value=1.423287, max_value=3.671155, value=2.301442, format="%.6f") ppe = st.number_input("PPE", min_value=0.044539, max_value=0.527367, value=0.284654, format="%.6f") if st.button("Predict"): try: input_data = np.array([ mdvp_fo, mdvp_fhi, mdvp_flo, mdvp_jitter, mdvp_jitter_abs, mdvp_rap, mdvp_ppq, jitter_ddp, mdvp_shimmer, mdvp_shimmer_db, shimmer_apq3, shimmer_apq5, mdvp_apq, shimmer_dda, nhr, hnr, rpde, dfa, spread1, spread2, d2, ppe ]).reshape(1, -1) prediction, similar_cases, similar_outcomes, distances = model.predict(input_data) if prediction[0] == 1: st.error("⚠️ High risk of Parkinson's disease") st.write("### Risk Factors Identified:") risk_factors = [] if mdvp_jitter > 0.008: risk_factors.append(f"High Jitter ({mdvp_jitter:.5f}%) indicates vocal instability") if mdvp_jitter_abs > 0.0004: risk_factors.append(f"High Absolute Jitter ({mdvp_jitter_abs:.5f}) indicates frequency instability") if mdvp_shimmer > 0.04: risk_factors.append(f"High Shimmer ({mdvp_shimmer:.5f}) indicates amplitude variations") if mdvp_shimmer_db > 0.4: risk_factors.append(f"High Shimmer dB ({mdvp_shimmer_db:.5f}dB) indicates amplitude instability") if hnr < 20: risk_factors.append(f"Low HNR ({hnr:.3f}) indicates voice quality issues") if nhr > 0.03: risk_factors.append(f"High NHR ({nhr:.5f}) indicates increased noise") if rpde > 0.5: risk_factors.append(f"High RPDE ({rpde:.3f}) indicates increased vocal complexity") if dfa < 0.65: risk_factors.append(f"Low DFA ({dfa:.3f}) indicates changes in vocal pattern") for factor in risk_factors: st.warning(f"⚠️ {factor}") else: st.success("✅ Low risk of Parkinson's disease") good_factors = [] if mdvp_jitter < 0.006: good_factors.append(f"Normal Jitter ({mdvp_jitter:.5f}%)") if mdvp_shimmer < 0.03: good_factors.append(f"Normal Shimmer ({mdvp_shimmer:.5f})") if hnr > 22: good_factors.append(f"Good HNR ({hnr:.3f})") if nhr < 0.02: good_factors.append(f"Good NHR ({nhr:.5f})") if good_factors: st.write("### Protective Factors:") for factor in good_factors: st.info(f"✓ {factor}") # Show similar cases st.write("### Similar Cases from Dataset") similar_df = pd.DataFrame({ 'Jitter(%)': similar_cases['MDVP:Jitter(%)'].round(5), 'Shimmer': similar_cases['MDVP:Shimmer'].round(5), 'HNR': similar_cases['HNR'].round(2), 'RPDE': similar_cases['RPDE'].round(3), 'DFA': similar_cases['DFA'].round(3), 'Diagnosis': ['Parkinson\'s' if o == 1 else 'Healthy' for o in similar_outcomes], 'Similarity': [f"{(1 - d/d.max())*100:.1f}%" for d in distances] }) st.dataframe(similar_df) except Exception as e: st.error(f"Error making prediction: {str(e)}") def show_patient_history(): """Display patient history visualization with interactive elements""" st.markdown("### 📈 Patient History Tracker") # Add date range selector col1, col2 = st.columns(2) with col1: start_date = st.date_input("From Date", value=datetime(2024, 1, 1)) with col2: end_date = st.date_input("To Date", value=datetime.now()) # Add assessment type filter assessment_types = ["All", "Breast Cancer", "Diabetes", "Heart Disease", "Parkinson's"] selected_type = st.multiselect("Filter by Assessment Type", assessment_types, default=["All"]) # Mock data for demonstration - Fixed: Ensure all arrays have same length num_records = 14 # Define a fixed number of records history_data = { 'Date': pd.date_range(start='2024-01-01', periods=num_records, freq='W'), 'Risk Score': np.random.uniform(0.2, 0.8, size=num_records), 'Assessment Type': np.random.choice(assessment_types[1:], size=num_records), 'Status': np.random.choice(['Normal', 'Warning', 'Critical'], size=num_records), 'Doctor': np.random.choice(['Dr. Smith', 'Dr. Johnson', 'Dr. Williams'], size=num_records) } df = pd.DataFrame(history_data) # Create tabs for different views tab1, tab2 = st.tabs(["📊 Trend Analysis", "📋 Detailed Records"]) with tab1: # Plot interactive trend fig = px.line(df, x='Date', y='Risk Score', color='Assessment Type', title='Risk Score Trends Over Time') fig.update_layout(height=400) st.plotly_chart(fig, use_container_width=True) # Add summary metrics col1, col2, col3 = st.columns(3) with col1: st.metric("Average Risk Score", f"{df['Risk Score'].mean():.2f}", delta=f"{(df['Risk Score'].iloc[-1] - df['Risk Score'].iloc[0]):.2f}") with col2: st.metric("Assessments", len(df), delta="↑2 from last month") with col3: st.metric("Critical Alerts", len(df[df['Status'] == 'Critical']), delta="-1 from last month") with tab2: # Add search and filter options search = st.text_input("Search records...") filtered_df = df[df.astype(str).apply(lambda x: x.str.contains(search, case=False)).any(axis=1)] # Display detailed records with styling st.dataframe( filtered_df.style.apply(lambda x: ['background-color: #ffcccc' if v == 'Critical' else 'background-color: #ffffcc' if v == 'Warning' else '' for v in x], subset=['Status']) ) def export_report(): """Generate and export comprehensive assessment report""" st.markdown("### 📄 Export Assessment Report") # Report configuration col1, col2 = st.columns(2) with col1: report_format = st.selectbox( "Report Format", ["PDF", "CSV", "JSON", "Excel"] ) include_graphs = st.checkbox("Include Visualizations", value=True) with col2: report_type = st.selectbox( "Report Type", ["Summary", "Detailed", "Technical"] ) include_recommendations = st.checkbox("Include Recommendations", value=True) # Generate report if st.button("Generate Report", type="primary"): with st.spinner("Generating comprehensive report..."): # Simulate report generation progress_bar = st.progress(0) for i in range(100): time.sleep(0.01) progress_bar.progress(i + 1) # Show success message st.success(f"Report generated successfully in {report_format} format!") # Provide download option if report_format == "PDF": mime = "application/pdf" elif report_format == "CSV": mime = "text/csv" elif report_format == "JSON": mime = "application/json" else: mime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" st.download_button( label=f"📥 Download {report_format} Report", data=b"Sample report data", # Replace with actual report generation file_name=f"medical_report_{datetime.now().strftime('%Y%m%d')}_{report_type.lower()}.{report_format.lower()}", mime=mime, key="download_report" ) def show_risk_factors_analysis(): """Display comprehensive risk factors analysis with interactive elements""" st.markdown("### 🔍 Risk Factors Analysis") # Create tabs for different analyses tab1, tab2 = st.tabs(["📊 Risk Factor Impact", "🔄 Correlation Analysis"]) with tab1: # Mock data risk_factors = { 'Factor': ['Age', 'BMI', 'Blood Pressure', 'Glucose Level', 'Family History', 'Smoking', 'Physical Activity', 'Diet', 'Stress Level'], 'Impact': [0.8, 0.6, 0.7, 0.9, 0.5, 0.65, 0.55, 0.45, 0.75], 'Category': ['Demographics', 'Physical', 'Physical', 'Medical', 'Medical', 'Lifestyle', 'Lifestyle', 'Lifestyle', 'Lifestyle'] } df = pd.DataFrame(risk_factors) # Add category filter categories = ['All'] + list(df['Category'].unique()) selected_category = st.selectbox("Filter by Category", categories) if selected_category != 'All': df_filtered = df[df['Category'] == selected_category] else: df_filtered = df # Create interactive bar chart fig = px.bar(df_filtered, x='Factor', y='Impact', color='Category', title='Risk Factors Impact Analysis', labels={'Impact': 'Impact Score (0-1)'}) st.plotly_chart(fig, use_container_width=True) with tab2: # Create correlation matrix visualization correlation_data = np.random.rand(len(df), len(df)) fig = px.imshow(correlation_data, labels=dict(x="Risk Factors", y="Risk Factors"), x=df['Factor'], y=df['Factor'], title="Risk Factors Correlation Matrix") st.plotly_chart(fig, use_container_width=True) def show_recommendations(): """Display personalized health recommendations with interactive elements""" st.markdown("### 💡 Personalized Recommendations") # Add risk profile selector risk_profile = st.select_slider( "Risk Profile", options=["Low", "Moderate", "High"], value="Moderate" ) # Recommendations based on risk profile recommendations = { "Lifestyle Changes": { "Low": [ "Maintain regular exercise routine", "Continue balanced diet", "Regular sleep schedule" ], "Moderate": [ "Increase exercise to 45 minutes daily", "Reduce processed food intake", "Improve sleep quality" ], "High": [ "Structured exercise program with supervision", "Strict dietary guidelines", "Sleep monitoring and improvement" ] }, "Medical Follow-up": { "Low": [ "Annual check-ups", "Regular blood pressure monitoring", "Basic health screenings" ], "Moderate": [ "Semi-annual check-ups", "Monthly blood pressure monitoring", "Comprehensive screenings" ], "High": [ "Quarterly check-ups", "Weekly blood pressure monitoring", "Advanced health screenings" ] }, "Risk Management": { "Low": [ "Basic health monitoring", "Stress management awareness", "General health education" ], "Moderate": [ "Regular health monitoring", "Active stress management", "Specific health education" ], "High": [ "Intensive health monitoring", "Professional stress management", "Specialized health education" ] } } # Display recommendations with expandable sections for category, risk_levels in recommendations.items(): with st.expander(f"📌 {category}", expanded=True): for item in risk_levels[risk_profile]: st.markdown(f"• {item}") # Add progress tracking if st.checkbox(f"Track {category.lower()} progress", key=category): st.slider(f"{category} Adherence", 0, 100, 50, key=f"adherence_{category}") st.progress(50) def show_trends_analysis(): """Display comprehensive health trends analysis""" st.markdown("### 📊 Health Trends Analysis") # Date range selector col1, col2 = st.columns(2) with col1: start_date = st.date_input("Start Date", value=datetime(2024, 1, 1), key="trends_start") with col2: end_date = st.date_input("End Date", value=datetime.now(), key="trends_end") # Mock data dates = pd.date_range(start='2024-01-01', end='2024-04-01', freq='D') trends_data = { 'Date': dates, 'Blood Pressure': np.random.normal(120, 5, len(dates)), 'Glucose Level': np.random.normal(100, 3, len(dates)), 'BMI': np.random.normal(25, 0.5, len(dates)), 'Cholesterol': np.random.normal(180, 10, len(dates)), 'Heart Rate': np.random.normal(75, 3, len(dates)) } df = pd.DataFrame(trends_data) # Metric selector metrics = list(df.columns[1:]) selected_metrics = st.multiselect("Select metrics to analyze", metrics, default=[metrics[0]]) if selected_metrics: # Create interactive line chart fig = px.line(df, x='Date', y=selected_metrics, title='Health Metrics Trends Over Time') fig.update_layout(height=400) st.plotly_chart(fig, use_container_width=True) # Add statistical analysis st.markdown("#### Statistical Analysis") col1, col2, col3 = st.columns(3) for metric in selected_metrics: with col1: st.metric(f"{metric} Average", f"{df[metric].mean():.1f}", delta=f"{df[metric].iloc[-1] - df[metric].iloc[0]:.1f}") with col2: st.metric(f"{metric} Min", f"{df[metric].min():.1f}") with col3: st.metric(f"{metric} Max", f"{df[metric].max():.1f}") def compare_assessments(): """Compare different assessment results""" st.markdown("### 🔄 Compare Assessments") col1, col2 = st.columns(2) with col1: st.markdown("#### Previous Assessment") st.metric(label="Risk Score", value="75%", delta="-15%") st.date_input("Assessment Date", value=datetime(2024, 1, 1)) with col2: st.markdown("#### Current Assessment") st.metric(label="Risk Score", value="60%", delta="-5%") st.date_input("Assessment Date", value=datetime.now()) def main(): # Initialize session state if not exists if "page" not in st.session_state: st.session_state.page = "Home" # Sidebar with st.sidebar: st.image("https://img.icons8.com/color/96/000000/hospital-2.png", width=100) st.title("Medical AI Assistant") st.caption("v1.0.0") # Navigation pages = { "🏠 Home": "Home", "🔬 Breast Cancer": "Breast Cancer", "🩺 Diabetes": "Diabetes", "❤️ Heart Disease": "Heart Disease", "🧠 Parkinson's Disease": "Parkinson's Disease" } # Get current page index current_page = st.session_state.page current_key = next(k for k, v in pages.items() if v == current_page) # Navigation radio buttons selected = st.radio( "🧭 Navigation", list(pages.keys()), index=list(pages.keys()).index(current_key) ) # Update page when selection changes if pages[selected] != st.session_state.page: st.session_state.page = pages[selected] # Main content routing try: if st.session_state.page == "Home": home_page() elif st.session_state.page == "Breast Cancer": breast_cancer_prediction() elif st.session_state.page == "Diabetes": diabetes_prediction() elif st.session_state.page == "Heart Disease": heart_disease_prediction() elif st.session_state.page == "Parkinson's Disease": parkinsons_prediction() except Exception as e: st.error(f"Error loading page: {str(e)}") st.session_state.page = "Home" if __name__ == "__main__": main()