File size: 5,730 Bytes
6595ce6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

#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))