|
|
| import gradio as gr |
| import numpy as np |
| from tensorflow.keras.models import load_model |
| import joblib |
|
|
| |
| model = load_model("seizure_model.h5") |
| scaler = joblib.load("scaler.pkl") |
|
|
| def process_input(raw_input): |
| |
| 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) |
|
|
| |
| 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() |
|
|