Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +73 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 5 |
+
|
| 6 |
+
# ================= CONFIG ================= #
|
| 7 |
+
MODEL_DIR = "finetuned_model"
|
| 8 |
+
MAX_INPUT_LEN = 1024
|
| 9 |
+
MAX_OUTPUT_LEN = 256
|
| 10 |
+
NUM_BEAMS = 4
|
| 11 |
+
|
| 12 |
+
PROMPT = (
|
| 13 |
+
"Generate a structured SOAP clinical summary with clearly separated "
|
| 14 |
+
"Subjective (S), Objective (O), Assessment (A), and Plan (P) sections "
|
| 15 |
+
"from the following medical dialogue:\n"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# ================= LOAD MODEL ================= #
|
| 19 |
+
print("Loading model and tokenizer...")
|
| 20 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
|
| 21 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_DIR)
|
| 22 |
+
model.eval()
|
| 23 |
+
|
| 24 |
+
# ================= INFERENCE FUNCTION ================= #
|
| 25 |
+
def generate_soap(dialogue):
|
| 26 |
+
if not dialogue or len(dialogue.strip()) == 0:
|
| 27 |
+
return "Please enter a medical dialogue."
|
| 28 |
+
|
| 29 |
+
inputs = tokenizer(
|
| 30 |
+
PROMPT + dialogue,
|
| 31 |
+
return_tensors="pt",
|
| 32 |
+
truncation=True,
|
| 33 |
+
max_length=MAX_INPUT_LEN
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
output_ids = model.generate(
|
| 38 |
+
**inputs,
|
| 39 |
+
max_length=MAX_OUTPUT_LEN,
|
| 40 |
+
num_beams=NUM_BEAMS,
|
| 41 |
+
no_repeat_ngram_size=3,
|
| 42 |
+
repetition_penalty=1.3,
|
| 43 |
+
length_penalty=1.0,
|
| 44 |
+
early_stopping=True
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
return tokenizer.decode(
|
| 48 |
+
output_ids[0],
|
| 49 |
+
skip_special_tokens=True
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# ================= GRADIO UI ================= #
|
| 53 |
+
iface = gr.Interface(
|
| 54 |
+
fn=generate_soap,
|
| 55 |
+
inputs=gr.Textbox(
|
| 56 |
+
lines=10,
|
| 57 |
+
placeholder="Enter doctor–patient medical dialogue here...",
|
| 58 |
+
label="Medical Dialogue"
|
| 59 |
+
),
|
| 60 |
+
outputs=gr.Textbox(
|
| 61 |
+
lines=12,
|
| 62 |
+
label="Generated SOAP Clinical Summary"
|
| 63 |
+
),
|
| 64 |
+
title="SOAP Clinical Summary Generator",
|
| 65 |
+
description="Fine-tuned FLAN-T5 model for SOAP note generation.",
|
| 66 |
+
examples=[
|
| 67 |
+
["Patient reports fever and cough for three days. No history of chronic illness."],
|
| 68 |
+
["Patient complains of chest pain and shortness of breath during exertion."]
|
| 69 |
+
],
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
fastapi
|
| 4 |
+
uvicorn
|
| 5 |
+
pydantic
|
| 6 |
+
sentencepiece
|