|
import gradio as gr |
|
import numpy as np |
|
import pandas as pd |
|
import wfdb |
|
import tensorflow as tf |
|
from scipy import signal |
|
import os |
|
|
|
|
|
model = tf.keras.models.load_model("model.h5") |
|
|
|
|
|
def preprocess_ecg(file_path): |
|
|
|
record = wfdb.rdrecord(file_path.replace(".dat", "")) |
|
ecg_signal = record.p_signal[:, 0] |
|
|
|
|
|
target_fs = 360 |
|
num_samples = int(len(ecg_signal) * target_fs / record.fs) |
|
ecg_resampled = signal.resample(ecg_signal, num_samples) |
|
|
|
|
|
ecg_normalized = (ecg_resampled - np.mean(ecg_resampled)) / np.std(ecg_resampled) |
|
|
|
|
|
if len(ecg_normalized) < 3600: |
|
ecg_normalized = np.pad(ecg_normalized, (0, 3600 - len(ecg_normalized)), "constant") |
|
else: |
|
ecg_normalized = ecg_normalized[:3600] |
|
|
|
|
|
ecg_input = ecg_normalized.reshape(1, 3600, 1) |
|
return ecg_input |
|
|
|
|
|
def predict_ecg(file): |
|
|
|
file_path = file.name |
|
|
|
|
|
ecg_data = preprocess_ecg(file_path) |
|
|
|
|
|
prediction = model.predict(ecg_data) |
|
|
|
|
|
label = "Abnormal" if prediction[0][0] > 0.5 else "Normal" |
|
confidence = float(prediction[0][0]) if label == "Abnormal" else float(1 - prediction[0][0]) |
|
|
|
return f"Prediction: {label}\nConfidence: {confidence:.2%}" |
|
|
|
|
|
interface = gr.Interface( |
|
fn=predict_ecg, |
|
inputs=gr.File(label="Upload ECG File (.dat format)"), |
|
outputs=gr.Textbox(label="ECG Interpretation"), |
|
title="Automated ECG Interpretation", |
|
description="Upload an ECG file in WFDB format (.dat) to get an automated interpretation." |
|
) |
|
|
|
|
|
interface.launch() |