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()