PushkarKumar's picture
Upload 4 files
47b4be9 verified
import gradio as gr
import numpy as np
from tensorflow.keras.models import load_model
import joblib
# Load the trained model and scaler
model = load_model("seizure_model.h5")
scaler = joblib.load("scaler.pkl")
def process_input(raw_input):
# Handle pasted input (space, tab, newline separated)
try:
eeg_array = np.array([float(x) for x in raw_input.strip().split()])
except:
return "โŒ Invalid input: Please enter only numbers separated by spaces, tabs, or newlines."
if len(eeg_array) > 178:
eeg_array = eeg_array[:178]
elif len(eeg_array) < 178:
return "โŒ Input must contain at least 178 EEG values."
eeg_scaled = scaler.transform(eeg_array.reshape(1, -1))
eeg_cnn_input = eeg_scaled.reshape(1, 178, 1)
prediction = model.predict(eeg_cnn_input)[0].argmax() + 1
if prediction == 1:
return "๐Ÿšจ Seizure Detected (Class 1)"
else:
return f"โœ… Normal EEG (Class {prediction})"
def predict_from_file(file):
raw = file.read().decode("utf-8")
return process_input(raw)
def predict_from_text(raw_input):
return process_input(raw_input)
# Gradio UI
with gr.Blocks(theme=gr.themes.Soft(), title="RakshAI - Seizure Detection") as app:
gr.Markdown("## ๐Ÿง  RakshAI - Seizure Detection")
gr.Markdown("Upload a `.txt` file or paste **178 EEG values** separated by spaces, tabs, or newlines.")
with gr.Row():
textbox = gr.Textbox(lines=10, label="๐Ÿ“ Paste EEG Values")
file = gr.File(label="๐Ÿ“‚ Upload EEG File (.txt)", file_types=[".txt"])
predict_btn = gr.Button("๐Ÿš€ Predict Seizure Status")
output = gr.Textbox(label="๐Ÿ”ฎ Prediction Output")
predict_btn.click(fn=predict_from_text, inputs=textbox, outputs=output)
file.change(fn=predict_from_file, inputs=file, outputs=output)
app.launch()