Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import platform
|
| 4 |
+
import zipfile
|
| 5 |
+
|
| 6 |
+
# --- 1. WINDOWS HATA ENGELLEYİCİ (EN ÜSTTE OLMALI) ---
|
| 7 |
+
if platform.system() == "Windows":
|
| 8 |
+
os.environ['PYSPARK_PYTHON'] = "python"
|
| 9 |
+
os.environ['PYSPARK_DRIVER_PYTHON'] = "python"
|
| 10 |
+
|
| 11 |
+
from pyspark.sql import SparkSession
|
| 12 |
+
from pyspark.ml.feature import VectorAssembler
|
| 13 |
+
from pyspark.ml.classification import LogisticRegressionModel
|
| 14 |
+
|
| 15 |
+
# --- 2. ZIP VE DOSYA YOLLARI ---
|
| 16 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 17 |
+
model_path = os.path.join(current_dir, "heart_model")
|
| 18 |
+
zip_path = os.path.join(current_dir, "heart_model.zip")
|
| 19 |
+
|
| 20 |
+
# Klasör yoksa ama zip varsa klasöre çıkart
|
| 21 |
+
if not os.path.exists(model_path) and os.path.exists(zip_path):
|
| 22 |
+
try:
|
| 23 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 24 |
+
zip_ref.extractall(current_dir) # DİKKAT: zip'in içindeki klasör yapısı önemli
|
| 25 |
+
except Exception as e:
|
| 26 |
+
st.error(f"Zip dosyası açılırken hata: {e}")
|
| 27 |
+
|
| 28 |
+
# --- 3. KRİTİK SPARK OTURUMU (HATA BYPASS EDİCİ) ---
|
| 29 |
+
@st.cache_resource
|
| 30 |
+
def get_spark():
|
| 31 |
+
builder = SparkSession.builder.appName("HeartApp")
|
| 32 |
+
|
| 33 |
+
if platform.system() == "Windows":
|
| 34 |
+
# Windows'ta dosya erişim hatasını (NativeIO) susturan ayarlar
|
| 35 |
+
return builder.master("local[1]") \
|
| 36 |
+
.config("spark.driver.host", "127.0.0.1") \
|
| 37 |
+
.config("spark.hadoop.fs.file.impl", "org.apache.hadoop.fs.RawLocalFileSystem") \
|
| 38 |
+
.config("spark.hadoop.fs.permission.check", "false") \
|
| 39 |
+
.getOrCreate()
|
| 40 |
+
else:
|
| 41 |
+
# Hugging Face (Linux) ayarları
|
| 42 |
+
return builder.master("local[*]").getOrCreate()
|
| 43 |
+
|
| 44 |
+
spark = get_spark()
|
| 45 |
+
|
| 46 |
+
# --- 4. MODEL YÜKLEME ---
|
| 47 |
+
@st.cache_resource
|
| 48 |
+
def load_heart_model():
|
| 49 |
+
# Model yolu kontrolü
|
| 50 |
+
if os.path.exists(model_path):
|
| 51 |
+
try:
|
| 52 |
+
return LogisticRegressionModel.load(model_path)
|
| 53 |
+
except Exception as e:
|
| 54 |
+
# Buradaki hata hala geliyorsa Spark dosyayı bulamıyor demektir
|
| 55 |
+
st.error(f"Spark Hatası: {e}")
|
| 56 |
+
return None
|
| 57 |
+
return None
|
| 58 |
+
|
| 59 |
+
model = load_heart_model()
|
| 60 |
+
|
| 61 |
+
# --- 5. ARAYÜZ ---
|
| 62 |
+
st.set_page_config(page_title="Kalp Analizi", layout="wide")
|
| 63 |
+
st.title("🫀 Kalp Hastalığı Risk Analizi")
|
| 64 |
+
|
| 65 |
+
if model is None:
|
| 66 |
+
st.error("❌ Model hala yüklenemedi!")
|
| 67 |
+
st.info("⚠️ Lütfen şu adımı kontrol et: VS Code sol tarafta 'heart_model' klasörünün içine bak. "
|
| 68 |
+
"Eğer içinde direkt 'data' ve 'metadata' klasörlerini görmüyorsan Spark yükleme yapamaz.")
|
| 69 |
+
st.code(f"Şu yolu kontrol et: {model_path}")
|
| 70 |
+
else:
|
| 71 |
+
# Form ve tahmin kısmı buraya gelecek
|
| 72 |
+
st.success("✅ Model başarıyla bağlandı!")
|
| 73 |
+
with st.form("input_form"):
|
| 74 |
+
# (Önceki form kodlarını buraya ekleyebilirsin)
|
| 75 |
+
submit = st.form_submit_button("Tahmin Et")
|