Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,58 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import mne
|
|
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
# Load open-source LLM
|
| 7 |
model_name = "tiiuae/falcon-7b-instruct"
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def process_eeg(file):
|
| 12 |
# Load EEG data using MNE
|
|
|
|
| 13 |
raw = mne.io.read_raw_fif(file.name, preload=True)
|
| 14 |
-
|
|
|
|
| 15 |
psd, freqs = mne.time_frequency.psd_welch(raw, fmin=1, fmax=40)
|
|
|
|
|
|
|
| 16 |
alpha_power = compute_band_power(psd, freqs, 8, 12)
|
| 17 |
beta_power = compute_band_power(psd, freqs, 13, 30)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
inputs = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
|
| 28 |
-
outputs = model.generate(
|
|
|
|
|
|
|
| 29 |
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 30 |
-
|
| 31 |
return summary
|
| 32 |
|
| 33 |
iface = gr.Interface(
|
|
@@ -35,7 +60,7 @@ iface = gr.Interface(
|
|
| 35 |
inputs=gr.File(label="Upload your EEG data (FIF format)"),
|
| 36 |
outputs="text",
|
| 37 |
title="NeuroNarrative-Lite: EEG Summary",
|
| 38 |
-
description="Upload EEG data to receive a text-based summary from an open-source
|
| 39 |
)
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import mne
|
| 3 |
+
import numpy as np
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
import torch
|
| 6 |
|
| 7 |
+
# Load an open-source LLM model with no additional training
|
| 8 |
model_name = "tiiuae/falcon-7b-instruct"
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
model_name,
|
| 12 |
+
trust_remote_code=True,
|
| 13 |
+
torch_dtype=torch.float16,
|
| 14 |
+
device_map="auto" # Automatically selects CPU/GPU if available
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
def compute_band_power(psd, freqs, fmin, fmax):
|
| 18 |
+
"""Compute mean band power in the given frequency range."""
|
| 19 |
+
freq_mask = (freqs >= fmin) & (freqs <= fmax)
|
| 20 |
+
# Take the mean across channels and frequencies
|
| 21 |
+
band_psd = psd[:, freq_mask].mean()
|
| 22 |
+
return float(band_psd)
|
| 23 |
|
| 24 |
def process_eeg(file):
|
| 25 |
# Load EEG data using MNE
|
| 26 |
+
# This expects a .fif file containing raw EEG data
|
| 27 |
raw = mne.io.read_raw_fif(file.name, preload=True)
|
| 28 |
+
|
| 29 |
+
# Compute PSD (Power Spectral Density) between 1 and 40 Hz
|
| 30 |
psd, freqs = mne.time_frequency.psd_welch(raw, fmin=1, fmax=40)
|
| 31 |
+
|
| 32 |
+
# Compute simple band powers
|
| 33 |
alpha_power = compute_band_power(psd, freqs, 8, 12)
|
| 34 |
beta_power = compute_band_power(psd, freqs, 13, 30)
|
| 35 |
+
|
| 36 |
+
# Create a short summary of the extracted features
|
| 37 |
+
data_summary = (
|
| 38 |
+
f"Alpha power: {alpha_power:.3f}, Beta power: {beta_power:.3f}. "
|
| 39 |
+
f"The EEG shows stable alpha rhythms and slightly elevated beta activity."
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Prepare the prompt for the language model
|
| 43 |
+
prompt = f"""You are a neuroscientist analyzing EEG features.
|
| 44 |
+
Data Summary: {data_summary}
|
| 45 |
+
|
| 46 |
+
Provide a concise, user-friendly interpretation of these findings in simple terms.
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
# Generate the summary using the LLM
|
| 50 |
inputs = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
|
| 51 |
+
outputs = model.generate(
|
| 52 |
+
inputs, max_length=200, do_sample=True, top_k=50, top_p=0.95
|
| 53 |
+
)
|
| 54 |
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 55 |
+
|
| 56 |
return summary
|
| 57 |
|
| 58 |
iface = gr.Interface(
|
|
|
|
| 60 |
inputs=gr.File(label="Upload your EEG data (FIF format)"),
|
| 61 |
outputs="text",
|
| 62 |
title="NeuroNarrative-Lite: EEG Summary",
|
| 63 |
+
description="Upload EEG data to receive a text-based summary from an open-source language model. No training required!"
|
| 64 |
)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|