#humanize-e inference model. #Developed by Mike Elistra (Mike Koci) #Kaggle Training Notebook: https://www.kaggle.com/code/mick009/notebook9c4e543da9 #Loads the QLoRA adapter trained for Qwen2.5-3B-Instruct fine-tuned to rewrite #AI-generated text so it sounds natural and human-written. #Prompt template (must match training): # INSTR = "Rewrite the following text so it sounds natural and human-written. Keep the same meaning.\n\n{text}" import os import re from typing import Optional import torch # Prompt used during training - keep in sync with training notebook! INSTR = "Rewrite the following text so it sounds natural and human-written. Keep the same meaning.\n\n{text}" BASE_MODEL = os.environ.get("HUMANIZE_BASE_MODEL", "Qwen/Qwen2.5-3B-Instruct") ADAPTER_PATH = os.environ.get( "HUMANIZE_ADAPTER", os.path.join(os.path.dirname(os.path.abspath(__file__)), "humanize-e-adapter"), ) class HumanizeModel: """QLoRA-adapted Qwen2.5-3B-Instruct for humanizing AI text.""" def __init__(self, adapter_path: Optional[str] = None, base_model: Optional[str] = None): self.adapter_path = adapter_path or ADAPTER_PATH self.base_model = base_model or BASE_MODEL self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") self.model = None self.tokenizer = None self._loaded = False if self._adapter_exists(): self._load() else: print(f"[humanize-e] Adapter not found at {self.adapter_path} - using rule-based fallback.") def _adapter_exists(self) -> bool: return os.path.isdir(self.adapter_path) and os.path.exists( os.path.join(self.adapter_path, "adapter_config.json") ) def _load(self): from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig from peft import PeftModel print(f"[humanize-e] Loading tokenizer from {self.adapter_path} ...") self.tokenizer = AutoTokenizer.from_pretrained(self.adapter_path) if self.tokenizer.pad_token is None: self.tokenizer.pad_token = self.tokenizer.eos_token print(f"[humanize-e] Loading base model {self.base_model} ...") if torch.cuda.is_available(): bnb = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.float16, bnb_4bit_use_double_quant=True, ) base = AutoModelForCausalLM.from_pretrained( self.base_model, quantization_config=bnb, device_map="auto" ) else: base = AutoModelForCausalLM.from_pretrained( self.base_model, torch_dtype=torch.float32, device_map="cpu", low_cpu_mem_usage=True ) print(f"[humanize-e] Loading LoRA adapter from {self.adapter_path} ...") self.model = PeftModel.from_pretrained(base, self.adapter_path) self.model.eval() self._loaded = True print("[humanize-e] Model ready.") def build_prompt(self, text: str) -> str: """Same prompt construction as training.""" return self.tokenizer.apply_chat_template( [{"role": "user", "content": INSTR.format(text=text)}], tokenize=False, add_generation_prompt=True, ) @torch.no_grad() def humanize( self, text: str, max_new_tokens: int = 320, temperature: float = 0.8, top_p: float = 0.9, ) -> str: """Humanize AI-generated text. Falls back to rules if model not loaded.""" if not self._loaded or self.model is None: return quick_humanize_fallback(text) enc = self.tokenizer( self.build_prompt(text), return_tensors="pt", add_special_tokens=False ).to(self.model.device) out = self.model.generate( **enc, max_new_tokens=max_new_tokens, do_sample=True, temperature=temperature, top_p=top_p, repetition_penalty=1.05, use_cache=True, pad_token_id=self.tokenizer.pad_token_id, ) return self.tokenizer.decode( out[0][enc["input_ids"].shape[1] :], skip_special_tokens=True ).strip() def quick_humanize_fallback(text: str) -> str: """Rule-based humanization when the trained model isn't available.""" text = text.strip() # AI-style phrases -> human text = re.sub(r"\bIt is important to note that\b", "Note that", text, flags=re.IGNORECASE) text = re.sub(r"\bIn order to\b", "To", text, flags=re.IGNORECASE) text = re.sub(r"\bDue to the fact that\b", "Because", text, flags=re.IGNORECASE) text = re.sub(r"\bFurthermore,?\b", "Also", text, flags=re.IGNORECASE) text = re.sub(r"\bHowever,?\b", "But", text, flags=re.IGNORECASE) text = re.sub(r"\bIt should be noted that\b", "Note that", text, flags=re.IGNORECASE) def smart(old, new): return re.sub(r"\b" + old + r"\b", new, text, flags=re.IGNORECASE) text = smart("Cannot", "Can't") text = smart("Will not", "Won't") text = smart("Do not", "Don't") text = smart("Did not", "Didn't") text = smart("It is clear that", "It's clear that") text = smart("I think that", "I think") text = re.sub(r"\s+([,.!?;:])", r"\1", text) return " ".join(text.split()) if __name__ == "__main__": m = HumanizeModel() demo = ("In order to provide you with the most comprehensive solution to your query, " "it is important to note that we have conducted extensive research on this topic.") print("IN :", demo) print("OUT:", m.humanize(demo))