from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfigfrom peft import PeftModelimport torch base_model = AutoModelForCausalLM.from_pretrained( "mistralai/Mistral-7B-Instruct-v0.2", quantization_config=BitsAndBytesConfig(load_in_4bit=True), device_map="auto")model = PeftModel.from_pretrained(base_model, "Kamal1729/legal-clause-extractor-mistral")tokenizer = AutoTokenizer.from_pretrained("Kamal1729/legal-clause-extractor-mistral") def extract_clause(contract_text, clause_type): instruction = f"""You are a legal AI assistant. Analyze the following contract text and extract structured information about the {clause_type} clause present. Contract Text:{contract_text} Return ONLY a valid JSON object with exactly these fields:- clause_type, key_terms, obligations, risk_level, parties_involved""" prompt = f"<s>[INST] {instruction} [/INST]" inputs = tokenizer(prompt, return_tensors="pt").to("cuda") outputs = model.generate(**inputs, max_new_tokens=256, do_sample=False) response = tokenizer.decode(outputs[0], skip_special_tokens=True) return response.split("[/INST]")[-1].strip()