import torchfrom transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfigfrom peft import PeftModel base_id = "mistralai/Mistral-7B-Instruct-v0.3"adapter_id = "txmedai/humanizer-mistral7b-lora" # main = v3# adapter_id, revision="v1" # to pin to a previous version bnb = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16, bnb_4bit_use_double_quant=True) tok = AutoTokenizer.from_pretrained(base_id)base = AutoModelForCausalLM.from_pretrained(base_id, quantization_config=bnb, device_map="auto", torch_dtype=torch.bfloat16)model = PeftModel.from_pretrained(base, adapter_id)model.eval() SYSTEM = ("Rewrite the text below in a casual, conversational human voice. " "Use contractions. Vary sentence length sharply. Drop academic phrasing. " "Keep all the meaning — same facts, same numbers — but completely change the surface style.") def humanize(text, temperature=0.9, top_p=0.95, max_new_tokens=400): msgs = [{"role": "user", "content": f"{SYSTEM}\n\n{text}"}] prompt = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True) ids = tok(prompt, return_tensors="pt", truncation=True, max_length=512).to(model.device) out = model.generate(**ids, max_new_tokens=max_new_tokens, min_new_tokens=60, temperature=temperature, top_p=top_p, repetition_penalty=1.15, do_sample=True, pad_token_id=tok.eos_token_id) return tok.decode(out[0][ids.input_ids.shape[1]:], skip_special_tokens=True).strip()