File size: 4,443 Bytes
096b848
 
 
 
 
 
a846075
e6cf9a3
 
096b848
e6cf9a3
9a2f96a
e6cf9a3
096b848
9a2f96a
096b848
9a2f96a
 
096b848
 
e6cf9a3
096b848
dec4ce8
de0c75e
096b848
a846075
dec4ce8
 
e6cf9a3
 
 
a846075
e6cf9a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dec4ce8
 
e6cf9a3
 
 
 
 
 
 
 
 
 
 
a846075
e6cf9a3
 
 
 
a846075
e6cf9a3
 
096b848
 
 
e6cf9a3
 
 
 
 
 
 
 
 
 
 
a846075
 
 
 
 
 
e6cf9a3
a846075
 
096b848
dec4ce8
e6cf9a3
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import streamlit as st
import os
from pyspark.sql import SparkSession
from pyspark.ml.classification import LogisticRegressionModel
from pyspark.ml.feature import VectorAssembler

# --- 1. SAYFA AYARLARI ---
st.set_page_config(page_title="Heart Predictor", page_icon="🫀", layout="wide")

@st.cache_resource
def initialize_app():
    spark = SparkSession.builder \
        .appName("HeartApp") \
        .master("local[*]") \
        .config("spark.driver.bindAddress", "127.0.0.1") \
        .getOrCreate()
    model = LogisticRegressionModel.load("heart_model")
    return spark, model

try:
    spark, model = initialize_app()
except Exception as e:
    st.error(f"Error / Hata: {e}")
    st.stop()

# --- 2. BAŞLIKLAR ---
st.title("🫀 Heart Disease Risk Prediction")
st.subheader("Kalp Hastalığı Risk Tahmini")

st.markdown("---")

# --- 3. HIZLI TEST BUTONLARI ---
st.markdown("### 🧪 Quick Test Scenarios / Hızlı Test Senaryoları")
col_b1, col_b2 = st.columns(2)

if 'preset' not in st.session_state:
    st.session_state.preset = "default"

if col_b1.button("🟢 Load Low Risk (Healthy) / Düşük Risk (Sağlıklı)"):
    st.session_state.preset = "low"
if col_b2.button("🔴 Load High Risk (Patient) / Yüksek Risk (Hasta)"):
    st.session_state.preset = "high"

# Senaryo değerlerini tanımlayalım
if st.session_state.preset == "low":
    d = [25, 0, 1, 110, 180, 0, 0, 180, 0, 0.0, 2, 0, 1]
elif st.session_state.preset == "high":
    d = [65, 1, 0, 160, 290, 1, 2, 105, 1, 3.2, 1, 3, 3]
else:
    d = [50, 1, 1, 120, 210, 0, 1, 145, 0, 1.0, 1, 0, 2]

st.markdown("---")

# --- 4. GİRİŞ ALANLARI (SIDEBAR) ---
st.sidebar.header("📋 Input Data / Veri Girişi")

age = st.sidebar.slider("Age / Yaş", 1, 100, d[0])
sex = st.sidebar.radio("Sex / Cinsiyet", [1, 0], index=(0 if d[1]==1 else 1), format_func=lambda x: "Male/Erkek (1)" if x == 1 else "Female/Kadın (0)")
cp = st.sidebar.selectbox("Chest Pain / Göğüs Ağrısı (0-3)", [0, 1, 2, 3], index=d[2])
trestbps = st.sidebar.number_input("Blood Pressure / Tansiyon", 50, 250, d[3])
chol = st.sidebar.number_input("Cholesterol / Kolesterol", 100, 600, d[4])
fbs = st.sidebar.selectbox("Fasting Sugar / Şeker > 120", [0, 1], index=d[5], format_func=lambda x: "Yes/Evet (1)" if x == 1 else "No/Hayır (0)")
restecg = st.sidebar.selectbox("Resting ECG / EKG (0-2)", [0, 1, 2], index=d[6])
thalach = st.sidebar.slider("Max Heart Rate / Maks. Kalp Hızı", 50, 220, d[7])
exang = st.sidebar.radio("Exercise Angina / Egzersiz Ağrısı", [0, 1], index=d[8], format_func=lambda x: "Yes/Evet (1)" if x == 1 else "No/Hayır (0)")
oldpeak = st.sidebar.number_input("Oldpeak", 0.0, 10.0, d[9], step=0.1)
slope = st.sidebar.selectbox("Slope (0-2)", [0, 1, 2], index=d[10])
ca = st.sidebar.selectbox("Major Vessels / Damar Sayısı (0-4)", [0, 1, 2, 3, 4], index=d[11])
thal = st.sidebar.selectbox("Thalassemia / Thal (0-3)", [0, 1, 2, 3], index=d[12])

# --- 5. TAHMİN BÖLÜMÜ (MANTIK TERSİNE ÇEVRİLDİ) ---
if st.button("🚀 RUN ANALYSIS / ANALİZİ ÇALIŞTIR"):
    input_data = spark.createDataFrame([{
        'age': age, 'sex': sex, 'cp': cp, 'trestbps': trestbps, 'chol': chol,
        'fbs': fbs, 'restecg': restecg, 'thalach': thalach, 'exang': exang,
        'oldpeak': oldpeak, 'slope': slope, 'ca': ca, 'thal': thal
    }])
    
    feature_cols = ['age', 'sex', 'cp', 'trestbps', 'chol', 'fbs', 'restecg', 'thalach', 'exang', 'oldpeak', 'slope', 'ca', 'thal']
    assembler = VectorAssembler(inputCols=feature_cols, outputCol="features")
    final_df = assembler.transform(input_data)
    
    prediction = model.transform(final_df)
    res = prediction.select("prediction").collect()[0][0]
    prob = prediction.select("probability").collect()[0][0]

    st.markdown("### 📊 Result / Sonuç:")
    
    # MANTIK DÜZELTİLDİ: 
    # Eğer senin modelinde 0=Risk, 1=Sağlıklı ise aşağısı doğru çalışacaktır.
    if res == 0.0: 
        st.error(f"🚨 **HIGH RISK / YÜKSEK RİSK** (Prob: %{round(prob[0]*100, 2)})")
        st.write("The model indicates potential risk. / Modele göre riskli durum tespit edildi.")
    else:
        st.success(f"💚 **LOW RISK / DÜŞÜK RİSK** (Prob: %{round(prob[1]*100, 2)})")
        st.write("The model indicates a healthy profile. / Modele göre risk düşük görünmektedir.")

st.divider()
st.caption("Disclaimer: For educational purposes only. / Tıbbi tavsiye niteliği taşımaz.")