Spaces:
Runtime error
Runtime error
File size: 11,630 Bytes
2504dcc dc21fc5 2504dcc dc21fc5 2504dcc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | """
Hugging Face Space β Sleep Stage Classification
================================================
Gradio app that serves the pre-trained CNN model for inference.
Callable from any frontend via the Gradio API.
Space URL: https://<your-username>-sleep-stage-classifier.hf.space
"""
import io
import os
import json
import numpy as np
import pandas as pd
import gradio as gr
import torch
import torch.nn as nn
from collections import Counter
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Constants
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
SFREQ = 100
EPOCH_SAMPLES = 3000 # 30 seconds Γ 100 Hz
STAGES = ["Wake", "N1", "N2", "N3", "N4", "REM"]
MODEL_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "sleep_stage_cnn.pth")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Model Definition (must match training architecture exactly)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class SleepStageCNN(nn.Module):
"""
1D Convolutional Neural Network for Sleep Stage Classification.
Architecture matches the training notebook.
"""
def __init__(self, n_channels=1, n_classes=6):
super().__init__()
self.network = nn.Sequential(
# Block 1: large receptive field for slow-wave features
nn.Conv1d(n_channels, 32, kernel_size=50, stride=6),
nn.BatchNorm1d(32),
nn.ReLU(),
nn.MaxPool1d(8),
# Block 2: finer feature extraction
nn.Conv1d(32, 64, kernel_size=8),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.MaxPool1d(8),
# Classifier head
nn.Flatten(),
nn.Linear(64 * 6, 128),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(128, n_classes),
)
def forward(self, x):
return self.network(x)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Load Model at startup
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
device = torch.device("cpu")
model = SleepStageCNN(n_channels=1, n_classes=6)
if os.path.exists(MODEL_PATH):
checkpoint = torch.load(
MODEL_PATH, map_location=device, weights_only=False
)
if isinstance(checkpoint, dict) and "model_state_dict" in checkpoint:
state_dict = checkpoint["model_state_dict"]
else:
state_dict = checkpoint
# Remap bare Sequential keys (e.g. "0.weight") β "network.0.weight"
if any(k.split(".")[0].isdigit() for k in state_dict.keys()):
state_dict = {"network." + k: v for k, v in state_dict.items()}
model.load_state_dict(state_dict)
model.eval().to(device)
print(f"β
Model loaded from {MODEL_PATH}")
else:
raise FileNotFoundError(
f"Model file not found at {MODEL_PATH}. "
"Upload sleep_stage_cnn.pth to this Space."
)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Inference Function
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def classify_eeg(signal: np.ndarray) -> dict:
"""
Run inference on a 1D EEG signal.
Parameters
----------
signal : np.ndarray
Raw EEG data (1D array, assumed 100 Hz sampling rate).
Returns
-------
dict with keys:
- epochs: list of {epoch, stage, confidence}
- summary: dict of stage β "count (percentage%)"
"""
if len(signal) < EPOCH_SAMPLES:
return {
"error": (
f"Signal too short. Need at least {EPOCH_SAMPLES} samples "
f"(30s at 100 Hz), got {len(signal)}."
)
}
predictions = []
for i in range(0, len(signal) - EPOCH_SAMPLES + 1, EPOCH_SAMPLES):
epoch = signal[i: i + EPOCH_SAMPLES]
# Z-score normalize
mean = epoch.mean()
std = epoch.std()
if std == 0:
std = 1.0
epoch_norm = (epoch - mean) / std
# Forward pass
x = torch.tensor(
epoch_norm, dtype=torch.float32
).unsqueeze(0).unsqueeze(0).to(device)
with torch.no_grad():
logits = model(x)
probs = torch.softmax(logits, dim=1).cpu().numpy()[0]
pred_idx = int(logits.argmax().item())
predictions.append({
"epoch": len(predictions) + 1,
"stage": STAGES[pred_idx],
"confidence": round(float(max(probs)), 4),
"probabilities": {
STAGES[j]: round(float(probs[j]), 4)
for j in range(len(STAGES))
},
})
# Summary statistics
counts = Counter(p["stage"] for p in predictions)
total = len(predictions)
return {
"epochs": predictions,
"summary": {
stage: {
"count": counts.get(stage, 0),
"percentage": round(counts.get(stage, 0) / total * 100, 1)
}
for stage in STAGES
},
}
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# File Processor (called by Gradio UI)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def process_file(file) -> tuple:
"""
Process uploaded EEG file and return readable results + raw JSON.
Parameters
----------
file : file-like or str path
Uploaded CSV / TXT / NPY file.
Returns
-------
(text_output, json_output)
"""
if file is None:
return "β οΈ Please upload a file.", None
try:
# Determine file type and load signal
name = file.name.lower() if hasattr(file, "name") else str(file).lower()
if name.endswith(".npy"):
signal = np.load(file)
if signal.ndim > 1:
signal = signal.flatten()
else:
# CSV or TXT β first column
df = pd.read_csv(file, header=None, sep=None, engine="python")
signal = df.iloc[:, 0].values.astype(np.float64)
# Run inference
result = classify_eeg(signal)
if "error" in result:
return f"β {result['error']}", None
# Build readable text output
lines = []
lines.append(f"π Total epochs classified: {len(result['epochs'])}")
lines.append("")
lines.append("π Stage Distribution:")
lines.append("-" * 40)
for stage, stats in result["summary"].items():
bar = "β" * int(stats["percentage"] / 2)
lines.append(f" {stage:6s}: {stats['count']:4d} ({stats['percentage']:5.1f}%) {bar}")
lines.append("")
lines.append("π Epoch Details (first 20):")
lines.append("-" * 40)
for ep in result["epochs"][:20]:
lines.append(
f" Epoch {ep['epoch']:>3d}: {ep['stage']:5s} "
f"confidence {ep['confidence']*100:.1f}%"
)
text_output = "\n".join(lines)
json_output = result # Gradio will auto-serialize to JSON
return text_output, json_output
except Exception as e:
return f"β Error: {str(e)}", None
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Gradio Interface
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with gr.Blocks(
title="Sleep Stage Classifier",
theme=gr.themes.Soft(
primary_hue="blue",
secondary_hue="slate",
),
) as demo:
gr.Markdown(
"""
# π΄ Sleep Stage Classification
Upload a **CSV**, **TXT**, or **NPY** file containing raw EEG signal data.
The model assumes a **100 Hz sampling rate** and classifies the signal
into 30-second epochs.
| Stage | Description |
|-------|-------------|
| **Wake** | Awake, eyes open/closed |
| **N1** | Light sleep, transition |
| **N2** | Deeper sleep, spindles + K-complexes |
| **N3** | Slow-wave sleep (deep) |
| **N4** | Very deep slow-wave sleep |
| **REM** | Rapid eye movement (dreaming) |
"""
)
with gr.Row():
with gr.Column(scale=1):
file_input = gr.File(
label="Upload EEG file",
file_types=[".csv", ".txt", ".npy"],
)
btn = gr.Button("π Classify", variant="primary", size="lg")
gr.Markdown("π‘ **Tip:** Upload a single-column CSV with EEG amplitude values (100 Hz).")
with gr.Column(scale=2):
text_output = gr.Textbox(
label="Results",
lines=20,
interactive=False,
)
json_output = gr.JSON(
label="Raw JSON (for API integration)",
)
btn.click(
fn=process_file,
inputs=[file_input],
outputs=[text_output, json_output],
)
gr.Markdown(
"""
---
### π API Access
You can call this Space programmatically from any frontend:
```bash
pip install gradio_client
```
```python
from gradio_client import Client
client = Client("<your-username>/sleep-stage-classifier")
result = client.predict(file="path/to/eeg.csv")
print(result)
```
Or from JavaScript in your Lovable app:
```javascript
import { Client } from "@gradio/client";
const client = await Client.connect(
"https://<your-username>-sleep-stage-classifier.hf.space"
);
const result = await client.predict("/predict", { file: yourFile });
```
"""
)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Launch
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if __name__ == "__main__":
demo.launch()
|