Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import mne
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Load open-source LLM (no training needed)
|
7 |
+
model_name = "tiiuae/falcon-7b-instruct"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.float16, device_map="auto")
|
10 |
+
|
11 |
+
def process_eeg(file):
|
12 |
+
# Load EEG data using MNE
|
13 |
+
raw = mne.io.read_raw_fif(file.name, preload=True)
|
14 |
+
# Compute some features (e.g., average band powers)
|
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 |
+
# Create a human-readable summary of features
|
19 |
+
data_summary = f"Alpha power: {alpha_power}, Beta power: {beta_power}. The data shows stable alpha rhythms and slightly elevated beta."
|
20 |
+
|
21 |
+
# Prompt the LLM
|
22 |
+
prompt = f"""You are a neuroscientist analyzing EEG features.
|
23 |
+
Data Summary: {data_summary}
|
24 |
+
|
25 |
+
Provide a concise, user-friendly interpretation of these findings."""
|
26 |
+
|
27 |
+
inputs = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
|
28 |
+
outputs = model.generate(inputs, max_length=200, do_sample=True, top_k=50, top_p=0.95)
|
29 |
+
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
30 |
+
|
31 |
+
return summary
|
32 |
+
|
33 |
+
iface = gr.Interface(
|
34 |
+
fn=process_eeg,
|
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 LLM. No training required!"
|
39 |
+
)
|
40 |
+
|
41 |
+
if __name__ == "__main__":
|
42 |
+
iface.launch()
|