Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +97 -0
- tabtransformer_model.pth +3 -0
- trans_scaler.pkl +3 -0
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Untitled14.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1iYWQPQr4OVakQQHRAlYVFLDwdJ-933Tv
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
with open("app.py", "w") as f:
|
| 11 |
+
f.write("""
|
| 12 |
+
import torch
|
| 13 |
+
import torch.nn as nn
|
| 14 |
+
import torch.nn.functional as F
|
| 15 |
+
import gradio as gr
|
| 16 |
+
import pickle
|
| 17 |
+
import pandas as pd
|
| 18 |
+
|
| 19 |
+
# TabTransformer Model Tanımı
|
| 20 |
+
class TabTransformer(nn.Module):
|
| 21 |
+
def __init__(self, input_dim, num_classes=2, d_model=64, nhead=4, num_layers=3, dropout=0.1):
|
| 22 |
+
super().__init__()
|
| 23 |
+
self.embedding = nn.Linear(input_dim, d_model)
|
| 24 |
+
encoder_layer = nn.TransformerEncoderLayer(
|
| 25 |
+
d_model=d_model, nhead=nhead, dim_feedforward=d_model * 4, dropout=dropout, activation='gelu'
|
| 26 |
+
)
|
| 27 |
+
self.transformer_encoder = nn.TransformerEncoder(encoder_layer, num_layers=num_layers)
|
| 28 |
+
self.fc = nn.Sequential(
|
| 29 |
+
nn.Linear(d_model, d_model // 2),
|
| 30 |
+
nn.ReLU(),
|
| 31 |
+
nn.Dropout(dropout),
|
| 32 |
+
nn.Linear(d_model // 2, num_classes)
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
def forward(self, x):
|
| 36 |
+
x = self.embedding(x)
|
| 37 |
+
x = x.unsqueeze(0) # Add sequence length dimension
|
| 38 |
+
x = self.transformer_encoder(x)
|
| 39 |
+
x = x.squeeze(0) # Remove sequence length dimension
|
| 40 |
+
return self.fc(x)
|
| 41 |
+
|
| 42 |
+
# Kategorik ve sayısal özellikler
|
| 43 |
+
categorical_features = ['Multifocal_PVC', 'Nonsustained_VT', 'gender', 'HTN', 'DM', 'Fullcompansasion']
|
| 44 |
+
numeric_features = ['pvc_percent', 'PVCQRS', 'EF', 'Age', 'PVC_Prematurity_index', 'QRS_ratio',
|
| 45 |
+
'mean_HR', 'symptom_duration', 'QTc_sinus', 'PVCCI_dispersion',
|
| 46 |
+
'CI_variability', 'PVC_Peak_QRS_duration', 'PVCCI', 'PVC_Compansatory_interval']
|
| 47 |
+
|
| 48 |
+
# Model ve scaler'ı yükleme
|
| 49 |
+
model_path = "/content/tabtransformer_model.pth"
|
| 50 |
+
scaler_path = "/content/trans_scaler.pkl"
|
| 51 |
+
|
| 52 |
+
# Model tanımı
|
| 53 |
+
input_dim = len(categorical_features) + len(numeric_features) # Toplam giriş boyutu
|
| 54 |
+
model = TabTransformer(input_dim=input_dim)
|
| 55 |
+
model.load_state_dict(torch.load(model_path, weights_only=True)) # Model ağırlıklarını yükle
|
| 56 |
+
model.eval() # Değerlendirme moduna al
|
| 57 |
+
|
| 58 |
+
# Scaler yükleme
|
| 59 |
+
with open(scaler_path, "rb") as f:
|
| 60 |
+
scaler = pickle.load(f)
|
| 61 |
+
|
| 62 |
+
# Prediction fonksiyonu
|
| 63 |
+
def predict(*inputs):
|
| 64 |
+
# Girdileri kategorik ve sayısal olarak ayır
|
| 65 |
+
cat_inputs = inputs[:len(categorical_features)]
|
| 66 |
+
num_inputs = inputs[len(categorical_features):]
|
| 67 |
+
|
| 68 |
+
# Kategorik girdiler (binary olarak 0/1 kodlama: "Yes" -> 1, "No" -> 0)
|
| 69 |
+
cat_data = [1 if val == "Yes" else 0 for val in cat_inputs]
|
| 70 |
+
|
| 71 |
+
# Sayısal girdiler
|
| 72 |
+
num_data = [float(val) for val in num_inputs]
|
| 73 |
+
|
| 74 |
+
# Veriyi birleştir ve ölçeklendir
|
| 75 |
+
data = pd.DataFrame([cat_data + num_data])
|
| 76 |
+
scaled_data = scaler.transform(data)
|
| 77 |
+
|
| 78 |
+
# Modelden tahmin al
|
| 79 |
+
tensor_data = torch.FloatTensor(scaled_data)
|
| 80 |
+
with torch.no_grad():
|
| 81 |
+
logits = model(tensor_data)
|
| 82 |
+
probabilities = F.softmax(logits, dim=1).numpy()
|
| 83 |
+
|
| 84 |
+
return {"Response Probability": probabilities[0][0], "Non-response Probability": probabilities[0][1]}
|
| 85 |
+
|
| 86 |
+
# Gradio arayüzü
|
| 87 |
+
inputs = (
|
| 88 |
+
[gr.Dropdown(choices=['Yes', 'No'], label=feature) for feature in categorical_features] +
|
| 89 |
+
[gr.Number(label=feature) for feature in numeric_features]
|
| 90 |
+
)
|
| 91 |
+
outputs = gr.Label(label="Prediction")
|
| 92 |
+
interface = gr.Interface(fn=predict, inputs=inputs, outputs=outputs, title="TabTransformer Prediction")
|
| 93 |
+
|
| 94 |
+
# Public URL ile başlat
|
| 95 |
+
interface.launch(share=True)
|
| 96 |
+
|
| 97 |
+
""")
|
tabtransformer_model.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9e75a2321236ab7bf9c7ea489b14ab11a4821e7bc5914d52d90f6c9af66d937d
|
| 3 |
+
size 629806
|
trans_scaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9605c1af143ced1c959d88cf51a96ce0671ad153ab1ce6a0ee85285a2268a9c2
|
| 3 |
+
size 1297
|