Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,23 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
import pandas as pd
|
| 5 |
import torch
|
| 6 |
import gradio as gr
|
| 7 |
-
from transformers import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from sklearn.model_selection import train_test_split
|
| 9 |
|
| 10 |
-
# 1) Configuration
|
| 11 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 12 |
if not HF_TOKEN:
|
| 13 |
-
raise RuntimeError(
|
|
|
|
|
|
|
| 14 |
|
| 15 |
MODEL_ID = "google/gemma-3n-e2b-it"
|
| 16 |
|
| 17 |
-
#
|
| 18 |
processor = AutoProcessor.from_pretrained(
|
| 19 |
MODEL_ID, trust_remote_code=True, token=HF_TOKEN
|
| 20 |
)
|
|
@@ -23,25 +26,25 @@ tokenizer = AutoTokenizer.from_pretrained(
|
|
| 23 |
)
|
| 24 |
|
| 25 |
def generate_and_export():
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
device = next(model.parameters()).device
|
| 38 |
|
| 39 |
def to_soap(text: str) -> str:
|
| 40 |
-
# wrap the chat‐template + generate call
|
| 41 |
inputs = processor.apply_chat_template(
|
| 42 |
[
|
| 43 |
-
{"role":"system","content":[{"type":"text","text":"You are a medical AI assistant."}]},
|
| 44 |
-
{"role":"user",
|
| 45 |
],
|
| 46 |
add_generation_prompt=True,
|
| 47 |
tokenize=True,
|
|
@@ -54,60 +57,63 @@ def generate_and_export():
|
|
| 54 |
do_sample=True,
|
| 55 |
top_p=0.95,
|
| 56 |
temperature=0.1,
|
| 57 |
-
pad_token_id=processor.tokenizer.eos_token_id
|
|
|
|
| 58 |
)
|
| 59 |
-
# strip off prompt tokens
|
| 60 |
prompt_len = inputs["input_ids"].shape[-1]
|
| 61 |
-
return processor.batch_decode(
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
#
|
| 64 |
docs, gts = [], []
|
| 65 |
for i in range(1, 101):
|
| 66 |
-
doc = to_soap(
|
|
|
|
|
|
|
| 67 |
docs.append(doc)
|
| 68 |
gts.append(to_soap(doc))
|
| 69 |
if i % 20 == 0:
|
| 70 |
torch.cuda.empty_cache()
|
| 71 |
|
| 72 |
-
#
|
| 73 |
df = pd.DataFrame({"doc_note": docs, "ground_truth_soap": gts})
|
| 74 |
train_df, test_df = train_test_split(df, test_size=0.3, random_state=42)
|
| 75 |
|
| 76 |
-
#
|
| 77 |
os.makedirs("outputs", exist_ok=True)
|
| 78 |
|
| 79 |
-
#
|
| 80 |
train_preds = [to_soap(d) for d in train_df["doc_note"]]
|
| 81 |
inf = train_df.reset_index(drop=True).copy()
|
| 82 |
-
inf["id"]
|
| 83 |
inf["predicted_soap"] = train_preds
|
| 84 |
inf[["id","ground_truth_soap","predicted_soap"]].to_csv(
|
| 85 |
"outputs/inference.tsv", sep="\t", index=False
|
| 86 |
)
|
| 87 |
|
| 88 |
-
#
|
| 89 |
test_preds = [to_soap(d) for d in test_df["doc_note"]]
|
| 90 |
pd.DataFrame({
|
| 91 |
-
"id":
|
| 92 |
"predicted_soap": test_preds
|
| 93 |
}).to_csv("outputs/eval.csv", index=False)
|
| 94 |
|
| 95 |
-
# return status + file paths for download
|
| 96 |
return (
|
| 97 |
-
"✅
|
| 98 |
"outputs/inference.tsv",
|
| 99 |
"outputs/eval.csv"
|
| 100 |
)
|
| 101 |
|
| 102 |
-
#
|
| 103 |
with gr.Blocks() as demo:
|
| 104 |
gr.Markdown("# Gemma‑3n SOAP Generator 🩺")
|
| 105 |
-
|
| 106 |
-
status
|
| 107 |
-
inf_file
|
| 108 |
-
eval_file
|
| 109 |
|
| 110 |
-
|
| 111 |
fn=generate_and_export,
|
| 112 |
inputs=None,
|
| 113 |
outputs=[status, inf_file, eval_file]
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import pandas as pd
|
| 3 |
import torch
|
| 4 |
import gradio as gr
|
| 5 |
+
from transformers import (
|
| 6 |
+
AutoProcessor,
|
| 7 |
+
AutoTokenizer,
|
| 8 |
+
AutoModelForImageTextToText
|
| 9 |
+
)
|
| 10 |
from sklearn.model_selection import train_test_split
|
| 11 |
|
|
|
|
| 12 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 13 |
if not HF_TOKEN:
|
| 14 |
+
raise RuntimeError(
|
| 15 |
+
"Missing HF_TOKEN in env vars – add it under Settings → Secrets"
|
| 16 |
+
)
|
| 17 |
|
| 18 |
MODEL_ID = "google/gemma-3n-e2b-it"
|
| 19 |
|
| 20 |
+
# Load processor & tokenizer at top level for fast startup
|
| 21 |
processor = AutoProcessor.from_pretrained(
|
| 22 |
MODEL_ID, trust_remote_code=True, token=HF_TOKEN
|
| 23 |
)
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
def generate_and_export():
|
| 29 |
+
"""
|
| 30 |
+
On button click: load full model, generate 100 notes,
|
| 31 |
+
split 70/30, run inference & eval, save files, return download links.
|
| 32 |
+
"""
|
| 33 |
+
# Load the heavy model here
|
| 34 |
+
model = AutoModelForImageTextToText.from_pretrained(
|
| 35 |
+
MODEL_ID,
|
| 36 |
+
trust_remote_code=True,
|
| 37 |
+
token=HF_TOKEN,
|
| 38 |
+
torch_dtype=torch.float16,
|
| 39 |
+
device_map="auto"
|
| 40 |
+
)
|
| 41 |
device = next(model.parameters()).device
|
| 42 |
|
| 43 |
def to_soap(text: str) -> str:
|
|
|
|
| 44 |
inputs = processor.apply_chat_template(
|
| 45 |
[
|
| 46 |
+
{"role": "system", "content":[{"type":"text","text":"You are a medical AI assistant."}]},
|
| 47 |
+
{"role": "user", "content":[{"type":"text","text":text}]}
|
| 48 |
],
|
| 49 |
add_generation_prompt=True,
|
| 50 |
tokenize=True,
|
|
|
|
| 57 |
do_sample=True,
|
| 58 |
top_p=0.95,
|
| 59 |
temperature=0.1,
|
| 60 |
+
pad_token_id=processor.tokenizer.eos_token_id,
|
| 61 |
+
use_cache=False
|
| 62 |
)
|
|
|
|
| 63 |
prompt_len = inputs["input_ids"].shape[-1]
|
| 64 |
+
return processor.batch_decode(
|
| 65 |
+
out[:, prompt_len:], skip_special_tokens=True
|
| 66 |
+
)[0].strip()
|
| 67 |
|
| 68 |
+
# Generate 100 docs + GTs
|
| 69 |
docs, gts = [], []
|
| 70 |
for i in range(1, 101):
|
| 71 |
+
doc = to_soap(
|
| 72 |
+
"Generate a realistic, concise doctor's progress note for a single patient encounter."
|
| 73 |
+
)
|
| 74 |
docs.append(doc)
|
| 75 |
gts.append(to_soap(doc))
|
| 76 |
if i % 20 == 0:
|
| 77 |
torch.cuda.empty_cache()
|
| 78 |
|
| 79 |
+
# Split 70/30
|
| 80 |
df = pd.DataFrame({"doc_note": docs, "ground_truth_soap": gts})
|
| 81 |
train_df, test_df = train_test_split(df, test_size=0.3, random_state=42)
|
| 82 |
|
| 83 |
+
# Ensure outputs dir
|
| 84 |
os.makedirs("outputs", exist_ok=True)
|
| 85 |
|
| 86 |
+
# Inference on train → inference.tsv
|
| 87 |
train_preds = [to_soap(d) for d in train_df["doc_note"]]
|
| 88 |
inf = train_df.reset_index(drop=True).copy()
|
| 89 |
+
inf["id"] = inf.index + 1
|
| 90 |
inf["predicted_soap"] = train_preds
|
| 91 |
inf[["id","ground_truth_soap","predicted_soap"]].to_csv(
|
| 92 |
"outputs/inference.tsv", sep="\t", index=False
|
| 93 |
)
|
| 94 |
|
| 95 |
+
# Inference on test → eval.csv
|
| 96 |
test_preds = [to_soap(d) for d in test_df["doc_note"]]
|
| 97 |
pd.DataFrame({
|
| 98 |
+
"id": range(1, len(test_preds) + 1),
|
| 99 |
"predicted_soap": test_preds
|
| 100 |
}).to_csv("outputs/eval.csv", index=False)
|
| 101 |
|
|
|
|
| 102 |
return (
|
| 103 |
+
"✅ Done!",
|
| 104 |
"outputs/inference.tsv",
|
| 105 |
"outputs/eval.csv"
|
| 106 |
)
|
| 107 |
|
| 108 |
+
# Build Gradio interface (starts immediately)
|
| 109 |
with gr.Blocks() as demo:
|
| 110 |
gr.Markdown("# Gemma‑3n SOAP Generator 🩺")
|
| 111 |
+
btn = gr.Button("Generate & Export 100 Notes")
|
| 112 |
+
status = gr.Textbox(interactive=False, label="Status")
|
| 113 |
+
inf_file = gr.File(label="Download inference.tsv")
|
| 114 |
+
eval_file = gr.File(label="Download eval.csv")
|
| 115 |
|
| 116 |
+
btn.click(
|
| 117 |
fn=generate_and_export,
|
| 118 |
inputs=None,
|
| 119 |
outputs=[status, inf_file, eval_file]
|