import os import time import torch import gradio as gr from transformers import AutoTokenizer, AutoModelForCausalLM from peft import PeftModel BASE_MODEL = "Qwen/Qwen3-1.7B" ADAPTER_PATH = "HK2184/medqa-qwen3-lora" print("Loading tokenizer...") tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True) tokenizer.pad_token = tokenizer.eos_token tokenizer.padding_side = "left" print("Loading model...") DTYPE = torch.bfloat16 if torch.cuda.is_available() else torch.float32 base = AutoModelForCausalLM.from_pretrained( BASE_MODEL, torch_dtype=DTYPE, device_map="cpu", trust_remote_code=True, low_cpu_mem_usage=False, ) model = PeftModel.from_pretrained( base, ADAPTER_PATH, is_trainable=False, ) model = model.merge_and_unload() model = model.to(DTYPE) model.eval() print("Ready!") DEVICE_INFO = f"{'GPU (ROCm)' if torch.cuda.is_available() else 'CPU'}" query_count = {"total": 0} EXAMPLES = [ ["Which artery is occluded in inferior MI with ST elevation in leads II, III, aVF?", "Left anterior descending artery", "Right coronary artery", "Left circumflex artery", "Left main coronary artery"], ["First-line treatment for hypertensive emergency?", "Oral amlodipine", "IV labetalol or IV nitroprusside", "Sublingual nifedipine", "IM hydralazine"], ["Most common cause of community-acquired pneumonia?", "Klebsiella pneumoniae", "Streptococcus pneumoniae", "Haemophilus influenzae", "Mycoplasma pneumoniae"], ["Drug of choice for absence seizures?", "Phenytoin", "Carbamazepine", "Ethosuximide", "Valproate"], ["A patient with sickle cell disease presents with acute chest pain and hypoxia. What is this called?", "Pulmonary embolism", "Acute chest syndrome", "Pneumonia", "Pleuritis"], ["Which vitamin deficiency causes Wernicke encephalopathy?", "Vitamin B12", "Vitamin B1 (Thiamine)", "Vitamin B6", "Vitamin C"], ["What is the antidote for acetaminophen overdose?", "Naloxone", "Flumazenil", "N-acetylcysteine", "Atropine"], ["A 60-year-old smoker presents with hemoptysis and weight loss. Most likely diagnosis?", "Tuberculosis", "Lung carcinoma", "Pulmonary embolism", "Bronchiectasis"], ] SUBJECTS = [ "All Subjects", "Cardiology", "Pharmacology", "Pulmonology", "Neurology", "Endocrinology", "Infectious Disease", "Emergency Medicine" ] SUBJECT_EXAMPLES = { "Cardiology": [EXAMPLES[0], EXAMPLES[1]], "Pharmacology": [EXAMPLES[3], EXAMPLES[6]], "Pulmonology": [EXAMPLES[2], EXAMPLES[7]], "Neurology": [EXAMPLES[3], EXAMPLES[5]], "Endocrinology": [], "Infectious Disease": [EXAMPLES[2]], "Emergency Medicine": [EXAMPLES[1], EXAMPLES[4]], } history_store = [] def autogenerate_options(question): if not question.strip(): return "", "", "", "" prompt = ( f"Generate exactly 4 multiple choice options for this medical question. " f"One must be correct, three must be plausible but wrong.\n" f"Question: {question}\n\n" f"Reply ONLY in this exact format, nothing else:\n" f"A)