from unsloth import FastModelimport torch model, tokenizer = FastModel.from_pretrained( model_name = "rajveer43/gemma-4-E4B-medical-legal-finance-qa", load_in_4bit = True,)FastModel.for_inference(model) SYSTEM_PROMPTS = { "medical": ( "You are a highly knowledgeable medical AI assistant. " "Answer clinical and biomedical questions accurately. " "Always advise consulting a licensed physician for personal medical decisions." ), "legal": ( "You are an expert legal AI assistant. " "Provide accurate information about laws, precedents, and legal concepts. " "Always clarify responses are not a substitute for licensed legal counsel." ), "finance": ( "You are a knowledgeable financial AI assistant. " "Answer questions about markets, instruments, and economic concepts accurately. " "Remind users this is not personalized financial advice." ),} def ask(question: str, domain: str = "medical", max_new_tokens: int = 256) -> str: messages = [ {"role": "system", "content": SYSTEM_PROMPTS[domain]}, {"role": "user", "content": question}, ] prompt = tokenizer.apply_chat_template( messages, tokenize=False, add_generation_prompt=True, ) # FIX: Bypass the Unsloth-patched multimodal processor entirely. # For text-only inference, call the underlying fast tokenizer directly # so the image processor pipeline is never triggered. underlying_tokenizer = tokenizer.tokenizer # unwrap: Gemma4Processor -> PreTrainedTokenizerFast inputs = underlying_tokenizer( prompt, return_tensors="pt", padding=True, ).to(model.device) with torch.no_grad(): output = model.generate( **inputs, max_new_tokens=max_new_tokens, temperature=0.1, top_p=0.9, do_sample=True, ) input_len = inputs["input_ids"].shape[-1] return underlying_tokenizer.decode(output[0][input_len:], skip_special_tokens=True) # Medicalprint(ask("What is the first-line treatment for Type 2 diabetes?", domain="medical"))# Legalprint(ask("What constitutes a breach of contract under common law?", domain="legal"))# Financeprint(ask("What is the difference between ROE and ROA?", domain="finance"))