Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,40 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
from peft import PeftModel
|
| 5 |
-
import os
|
| 6 |
-
|
| 7 |
|
| 8 |
hf_token = os.environ.get("HF_TOKEN")
|
| 9 |
|
|
|
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained("StudioIlios/icd10-model", token=hf_token)
|
|
|
|
|
|
|
| 11 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
"meta-llama/Llama-3.2-3B",
|
| 13 |
token=hf_token,
|
| 14 |
-
|
| 15 |
-
torch_dtype=torch.
|
|
|
|
| 16 |
)
|
| 17 |
|
|
|
|
| 18 |
model = PeftModel.from_pretrained(base_model, "StudioIlios/icd10-model", token=hf_token)
|
|
|
|
| 19 |
|
| 20 |
def predict(clinical_text):
|
| 21 |
inputs = tokenizer(clinical_text, return_tensors="pt")
|
| 22 |
-
|
|
|
|
| 23 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 24 |
return response.replace(clinical_text, "").strip()
|
| 25 |
|
| 26 |
-
demo = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# REST API at /api/predict
|
| 29 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
from peft import PeftModel
|
|
|
|
|
|
|
| 6 |
|
| 7 |
hf_token = os.environ.get("HF_TOKEN")
|
| 8 |
|
| 9 |
+
print("Loading tokenizer...")
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained("StudioIlios/icd10-model", token=hf_token)
|
| 11 |
+
|
| 12 |
+
print("Loading base model in float16 precision...")
|
| 13 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
"meta-llama/Llama-3.2-3B",
|
| 15 |
token=hf_token,
|
| 16 |
+
low_cpu_mem_usage=True,
|
| 17 |
+
torch_dtype=torch.float16,
|
| 18 |
+
device_map="cpu"
|
| 19 |
)
|
| 20 |
|
| 21 |
+
print("Attaching LoRA adapter...")
|
| 22 |
model = PeftModel.from_pretrained(base_model, "StudioIlios/icd10-model", token=hf_token)
|
| 23 |
+
print("Model loaded successfully!")
|
| 24 |
|
| 25 |
def predict(clinical_text):
|
| 26 |
inputs = tokenizer(clinical_text, return_tensors="pt")
|
| 27 |
+
with torch.no_grad():
|
| 28 |
+
outputs = model.generate(**inputs, max_new_tokens=30, temperature=0.1)
|
| 29 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 30 |
return response.replace(clinical_text, "").strip()
|
| 31 |
|
| 32 |
+
demo = gr.Interface(
|
| 33 |
+
fn=predict,
|
| 34 |
+
inputs=gr.Textbox(placeholder="Enter symptom description here..."),
|
| 35 |
+
outputs="text",
|
| 36 |
+
title="ICD-10 Predictor API"
|
| 37 |
+
)
|
| 38 |
|
| 39 |
# REST API at /api/predict
|
| 40 |
demo.launch()
|