Spaces:
Sleeping
Sleeping
Commit ·
5e0335d
0
Parent(s):
Load model from HuggingFace hub
Browse files- README.md +12 -0
- app.py +79 -0
- requirements.txt +6 -0
README.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: ndml-eeg-code-generator
|
| 3 |
+
colorFrom: blue
|
| 4 |
+
colorTo: green
|
| 5 |
+
sdk: gradio
|
| 6 |
+
sdk_version: 4.0.0
|
| 7 |
+
app_file: app.py
|
| 8 |
+
pinned: false
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# OLMo EEG Code Generator
|
| 12 |
+
NDML Lab — Parkinson's and EEG Analysis Code
|
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, torch, gradio as gr
|
| 2 |
+
import torch.nn.functional as F
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
from transformers import AutoTokenizer
|
| 5 |
+
from olmo_core.nn.transformer import TransformerConfig
|
| 6 |
+
|
| 7 |
+
# Load from HuggingFace — works without lab server
|
| 8 |
+
REPO_ID = "Tbain20/olmo2-1b-eeg-v11"
|
| 9 |
+
VOCAB_SIZE = 100278
|
| 10 |
+
DEVICE = "cpu" # HF free tier is CPU only
|
| 11 |
+
PREFIX_I = "### Instruction:\n"
|
| 12 |
+
PREFIX_R = "\n\n### Response:\n"
|
| 13 |
+
|
| 14 |
+
print("Downloading model from HuggingFace...")
|
| 15 |
+
ckpt_path = hf_hub_download(repo_id=REPO_ID, filename="best_model.pt")
|
| 16 |
+
|
| 17 |
+
print("Loading tokenizer...")
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained("allenai/OLMo-2-1124-7B")
|
| 19 |
+
|
| 20 |
+
print("Building model...")
|
| 21 |
+
cfg = TransformerConfig.olmo2_1B(vocab_size=VOCAB_SIZE)
|
| 22 |
+
model = cfg.build()
|
| 23 |
+
ckpt = torch.load(ckpt_path, map_location="cpu")
|
| 24 |
+
model.load_state_dict(ckpt["model_state_dict"])
|
| 25 |
+
model.eval()
|
| 26 |
+
print("Ready")
|
| 27 |
+
|
| 28 |
+
@torch.no_grad()
|
| 29 |
+
def generate_code(prompt, max_new_tokens=300, temperature=0.7):
|
| 30 |
+
if not prompt.strip():
|
| 31 |
+
return "Please enter a prompt."
|
| 32 |
+
full = PREFIX_I + prompt.strip() + PREFIX_R
|
| 33 |
+
ids = tokenizer.encode(full)
|
| 34 |
+
x = torch.tensor([ids], dtype=torch.long)
|
| 35 |
+
for _ in range(max_new_tokens):
|
| 36 |
+
logits = model(x)
|
| 37 |
+
logits = logits[:, -1, :] / temperature
|
| 38 |
+
vals, idxs = torch.topk(logits, k=40)
|
| 39 |
+
probs = F.softmax(vals, dim=-1)
|
| 40 |
+
next_tok = idxs.gather(-1, torch.multinomial(probs, 1))
|
| 41 |
+
x = torch.cat([x, next_tok], dim=1)
|
| 42 |
+
if next_tok.item() == tokenizer.eos_token_id:
|
| 43 |
+
break
|
| 44 |
+
out = tokenizer.decode(x[0].tolist(), skip_special_tokens=True)
|
| 45 |
+
return out[len(full):].strip()
|
| 46 |
+
|
| 47 |
+
EXAMPLES = [
|
| 48 |
+
["Write a Python function using MNE to filter EEG data for beta waves (13-30 Hz)"],
|
| 49 |
+
["Write a Python function to compute beta band power from STN LFP recordings"],
|
| 50 |
+
["Write a Python function to compare beta power between on and off medication Parkinson's patients"],
|
| 51 |
+
["Write a Python function to load TDT block and extract RSn1 LFP stream"],
|
| 52 |
+
["Write a Python function to suppress DBS stimulation artifacts using sample-and-hold"],
|
| 53 |
+
["Write a MATLAB function using FieldTrip to compute beta band power from LFP"],
|
| 54 |
+
]
|
| 55 |
+
|
| 56 |
+
with gr.Blocks(title="OLMo EEG Code Generator") as demo:
|
| 57 |
+
gr.Markdown("# 🧠 OLMo EEG Code Generator\n### NDML Lab — Parkinson's & EEG Analysis Assistant\n*Note: Running on CPU — generation takes 1-2 minutes*")
|
| 58 |
+
with gr.Row():
|
| 59 |
+
with gr.Column():
|
| 60 |
+
prompt_box = gr.Textbox(label="Describe what you need", lines=4,
|
| 61 |
+
placeholder="e.g. Write a Python function using MNE to filter EEG for beta waves")
|
| 62 |
+
temperature = gr.Slider(0.3, 1.2, value=0.7, step=0.05, label="Temperature")
|
| 63 |
+
max_tokens = gr.Slider(100, 500, value=300, step=50, label="Max tokens")
|
| 64 |
+
with gr.Row():
|
| 65 |
+
generate_btn = gr.Button("Generate Code", variant="primary", scale=2)
|
| 66 |
+
clear_btn = gr.Button("Clear", scale=1)
|
| 67 |
+
gr.Examples(examples=EXAMPLES, inputs=prompt_box)
|
| 68 |
+
with gr.Column():
|
| 69 |
+
output_box = gr.Code(label="Generated Code", language="python", lines=25)
|
| 70 |
+
|
| 71 |
+
generate_btn.click(
|
| 72 |
+
fn=lambda p,t,m: generate_code(p, int(m), float(t)),
|
| 73 |
+
inputs=[prompt_box, temperature, max_tokens], outputs=output_box)
|
| 74 |
+
prompt_box.submit(
|
| 75 |
+
fn=lambda p,t,m: generate_code(p, int(m), float(t)),
|
| 76 |
+
inputs=[prompt_box, temperature, max_tokens], outputs=output_box)
|
| 77 |
+
clear_btn.click(fn=lambda: ("",""), outputs=[prompt_box, output_box])
|
| 78 |
+
|
| 79 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.44.0
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
numpy
|
| 5 |
+
huggingface_hub
|
| 6 |
+
ai2-olmo
|