File size: 3,174 Bytes
75ce186
 
 
 
 
 
 
 
 
 
 
6b4cb7a
75ce186
6b4cb7a
75ce186
 
6b4cb7a
75ce186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b4cb7a
 
75ce186
 
 
6b4cb7a
 
75ce186
 
 
 
 
6b4cb7a
75ce186
 
 
 
 
 
6b4cb7a
75ce186
 
 
 
 
 
 
 
 
6b4cb7a
 
 
 
 
 
 
75ce186
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
import spaces

SYSTEM_PROMPT = """You are OncLLM, a specialized oncology clinical decision support assistant.

CRITICAL RULES:
1. Provide evidence-based medical information with proper citations when available
2. Use clinical/formal tone appropriate for healthcare professionals
3. For drug information, include: FDA-approved indications, standard dosing, common adverse effects, black box warnings, contraindications
4. Always include this disclaimer: "This information is for clinical decision support only. All treatment decisions must be made by qualified healthcare providers."
5. When discussing treatment recommendations, mention evidence levels when known
6. Be accurate and conservative - if uncertain, say so

You specialize in: breast cancer, lung cancer, colorectal cancer, prostate cancer, melanoma, lymphoma, leukemia, pancreatic cancer, ovarian cancer, kidney cancer, liver cancer, brain tumors, gastric cancer, and multiple myeloma."""

print("🏥 OncLLM - Loading oncology model...")
print("=" * 50)

BASE_MODEL = "Qwen/Qwen2.5-14B-Instruct"

tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
if tokenizer.pad_token is None:
    tokenizer.pad_token = tokenizer.eos_token

print(f"Loading base model: {BASE_MODEL}")
base_model = AutoModelForCausalLM.from_pretrained(
    BASE_MODEL,
    torch_dtype=torch.float16,
    device_map="auto",
    trust_remote_code=True
)

print("Loading LoRA adapter...")
model = PeftModel.from_pretrained(base_model, "./adapter")
model.eval()

print("✅ OncLLM ready!")
print("=" * 50)

@spaces.GPU
def oncology_chat(message, history):
    messages = [{"role": "system", "content": SYSTEM_PROMPT}]
    
    for msg in history:
        messages.append({"role": msg["role"], "content": msg["content"]})
    
    messages.append({"role": "user", "content": message})
    
    text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    inputs = tokenizer(text, return_tensors="pt", max_length=4096, truncation=True).to(model.device)
    
    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_new_tokens=1024,
            temperature=0.1,
            do_sample=True,
            top_p=0.9,
            repetition_penalty=1.1,
            pad_token_id=tokenizer.eos_token_id
        )
    
    response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
    return response

EXAMPLES = [
    "What is the first-line treatment for EGFR-mutated NSCLC?",
    "What are the black box warnings for pembrolizumab?",
    "What is the standard dosing for osimertinib?",
    "What are the treatment options for HER2-positive breast cancer?",
]

demo = gr.ChatInterface(
    fn=oncology_chat,
    title="🏥 OncLLM - Oncology Clinical Decision Support",
    description="A fine-tuned **Qwen 2.5 14B** model for oncology.\n\n⚠️ **Disclaimer:** For clinical decision support only.",
    examples=EXAMPLES,
    type="messages"
)

if __name__ == "__main__":
    demo.launch()