world44 commited on
Commit
6595ce6
·
verified ·
1 Parent(s): e05bf1a

Initial demo files for humanize-e

Browse files
.env.example ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # humanize-e configuration
2
+
3
+ # Base model to use for inference
4
+ # Can be a HuggingFace repo ID or local path
5
+ HUMANIZE_BASE_MODEL=Qwen/Qwen2.5-3B-Instruct
6
+
7
+ # Path to LoRA adapter folder
8
+ # Options:
9
+ # - Local path: /absolute/path/to/adapter
10
+ # - HuggingFace repo: user/repo-name
11
+ HUMANIZE_ADAPTER=./humanize-e-adapter
.gitattributes CHANGED
@@ -1,35 +1,9 @@
1
- *.7z filter=lfs diff=lfs merge=lfs -text
2
- *.arrow filter=lfs diff=lfs merge=lfs -text
3
  *.bin filter=lfs diff=lfs merge=lfs -text
4
- *.bz2 filter=lfs diff=lfs merge=lfs -text
5
- *.ckpt filter=lfs diff=lfs merge=lfs -text
6
- *.ftz filter=lfs diff=lfs merge=lfs -text
7
- *.gz filter=lfs diff=lfs merge=lfs -text
8
- *.h5 filter=lfs diff=lfs merge=lfs -text
9
- *.joblib filter=lfs diff=lfs merge=lfs -text
10
- *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
- *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
- *.model filter=lfs diff=lfs merge=lfs -text
13
- *.msgpack filter=lfs diff=lfs merge=lfs -text
14
- *.npy filter=lfs diff=lfs merge=lfs -text
15
- *.npz filter=lfs diff=lfs merge=lfs -text
16
- *.onnx filter=lfs diff=lfs merge=lfs -text
17
- *.ot filter=lfs diff=lfs merge=lfs -text
18
- *.parquet filter=lfs diff=lfs merge=lfs -text
19
- *.pb filter=lfs diff=lfs merge=lfs -text
20
- *.pickle filter=lfs diff=lfs merge=lfs -text
21
- *.pkl filter=lfs diff=lfs merge=lfs -text
22
  *.pt filter=lfs diff=lfs merge=lfs -text
23
  *.pth filter=lfs diff=lfs merge=lfs -text
24
- *.rar filter=lfs diff=lfs merge=lfs -text
25
- *.safetensors filter=lfs diff=lfs merge=lfs -text
26
- saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
- *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tar filter=lfs diff=lfs merge=lfs -text
29
- *.tflite filter=lfs diff=lfs merge=lfs -text
30
- *.tgz filter=lfs diff=lfs merge=lfs -text
31
- *.wasm filter=lfs diff=lfs merge=lfs -text
32
- *.xz filter=lfs diff=lfs merge=lfs -text
33
- *.zip filter=lfs diff=lfs merge=lfs -text
34
- *.zst filter=lfs diff=lfs merge=lfs -text
35
- *tfevents* filter=lfs diff=lfs merge=lfs -text
 
1
+ # Git LFS for large model files
2
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
3
  *.bin filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  *.pt filter=lfs diff=lfs merge=lfs -text
5
  *.pth filter=lfs diff=lfs merge=lfs -text
6
+
7
+ # JSON config files also work well with LFS
8
+ config.json filter=lfs diff=lfs merge=lfs -text
9
+ adapter_config.json filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
.github/workflows/ci.yml ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [ main, develop ]
6
+ pull_request:
7
+ branches: [ main, develop ]
8
+ workflow_dispatch:
9
+
10
+ jobs:
11
+ test:
12
+ runs-on: ubuntu-latest
13
+ strategy:
14
+ matrix:
15
+ python-version: ['3.10', '3.11', '3.12']
16
+
17
+ steps:
18
+ - name: Checkout code
19
+ uses: actions/checkout@v4
20
+
21
+ - name: Set up Python ${{ matrix.python-version }}
22
+ uses: actions/setup-python@v5
23
+ with:
24
+ python-version: ${{ matrix.python-version }}
25
+
26
+ - name: Cache pip dependencies
27
+ uses: actions/cache@v4
28
+ with:
29
+ path: ~/.cache/pip
30
+ key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
31
+ restore-keys: |
32
+ ${{ runner.os }}-pip-
33
+
34
+ - name: Install dependencies
35
+ run: |
36
+ python -m pip install --upgrade pip
37
+ pip install -r requirements.txt
38
+ pip install fastapi[all]
39
+
40
+ - name: Run unit tests
41
+ run: python -m unittest discover -s tests -p 'test_*.py' -v
42
+
43
+ - name: Check syntax
44
+ run: |
45
+ python -m py_compile model.py app.py
46
+ echo "Syntax check passed"
.gitignore ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ .Python
7
+ build/
8
+ develop-eggs/
9
+ dist/
10
+ downloads/
11
+ eggs/
12
+ .eggs/
13
+ lib/
14
+ lib64/
15
+ parts/
16
+ sdist/
17
+ var/
18
+ wheels/
19
+ *.egg-info/
20
+ .installed.cfg
21
+ *.egg
22
+
23
+ # Virtual environments
24
+ .env
25
+ .venv
26
+ env/
27
+ venv/
28
+ ENV/
29
+ env.bak/
30
+ venv.bak/
31
+
32
+ # Model weights - large, use .gitattributes for LFS
33
+ # Adapter files can be tracked via git LFS:
34
+ # *.safetensors filter=lfs diff=lfs merge=lfs -text
35
+ # *.bin filter=lfs diff=lfs merge=lfs -text
36
+ # *.json filter=lfs diff=lfs merge=lfs -text
37
+ #
38
+ # To ignore completely, uncomment:
39
+ # humanize-e-adapter/
40
+
41
+ # IDE
42
+ .idea/
43
+ .vscode/
44
+ *.swp
45
+ *.swo
46
+ *~
47
+
48
+ # OS
49
+ .DS_Store
50
+ Thumbs.db
51
+
52
+ # Logs
53
+ *.log
54
+
55
+ # Testing
56
+ .pytest_cache/
57
+ .coverage
58
+ htmlcov/
59
+
60
+ # Jupyter
61
+ .ipynb_checkpoints/
62
+
63
+ # Secrets
64
+ *.pem
65
+ *.key
Dockerfile ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Dockerfile for humanize-e
2
+ # Build: docker build -t humanize-e .
3
+ # Run: docker run -p 8000:8000 -e HUMANIZE_ADAPTER=/models humanize-e
4
+
5
+ FROM python:3.11-slim
6
+
7
+ # Install system deps
8
+ RUN apt-get update && apt-get install -y --no-install-recommends \
9
+ git \
10
+ && rm -rf /var/lib/apt/lists/*
11
+
12
+ # Create app directory
13
+ WORKDIR /app
14
+
15
+ # Copy requirements and install deps
16
+ COPY requirements.txt .
17
+ RUN pip install --no-cache-dir -r requirements.txt
18
+
19
+ # Copy source code
20
+ COPY model.py .
21
+ COPY app.py .
22
+
23
+ # Create adapter directory placeholder
24
+ RUN mkdir -p /app/humanize-e-adapter
25
+
26
+ # Expose port
27
+ EXPOSE 8000
28
+
29
+ # Default command
30
+ CMD ["python", "-m", "uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
MODEL_CARD.md ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # humanize-e Adapter
2
+
3
+ A QLoRA LoRA adapter for Qwen2.5-3B-Instruct trained to humanize AI-generated text.
4
+
5
+ ## Model Details
6
+
7
+ - **Base Model:** Qwen/Qwen2.5-3B-Instruct
8
+ - **Fine-tuning Method:** QLoRA (LoRA r=32, alpha=64)
9
+ - **Training Data:** 156 synthetic pairs from Kaggle AI vs Human Text dataset
10
+ - **Developer:** Mike Elistra (Mike Koci)
11
+ - **Training Notebook:** https://www.kaggle.com/code/mick009/notebook9c4e543da9
12
+
13
+ ## Usage
14
+
15
+ ```python
16
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
17
+ from peft import PeftModel
18
+ import torch
19
+
20
+ base_model = "Qwen/Qwen2.5-3B-Instruct"
21
+ adapter_path = "mike-elistra/humanize-e-adapter"
22
+
23
+ tokenizer = AutoTokenizer.from_pretrained(adapter_path)
24
+ model = AutoModelForCausalLM.from_pretrained(
25
+ base_model,
26
+ quantization_config=BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4"),
27
+ device_map="auto"
28
+ )
29
+ model = PeftModel.from_pretrained(model, adapter_path)
30
+ model.eval()
31
+
32
+ # Use with humanize-e inference code
33
+ ```
34
+
35
+ ## Inference API
36
+
37
+ The adapter works with the humanize-e FastAPI service:
38
+ ```bash
39
+ HUMANIZE_ADAPTER=mike-elistra/humanize-e-adapter python app.py
40
+ ```
41
+
42
+ ## Training
43
+
44
+ See Kaggle notebook for full training pipeline:
45
+ https://www.kaggle.com/code/mick009/notebook9c4e543da9
46
+
47
+ ## License
48
+
49
+ MIT
README.md CHANGED
@@ -1,10 +1,281 @@
1
- ---
2
- title: Humanize E
3
- emoji: 🌖
4
- colorFrom: red
5
- colorTo: blue
6
- sdk: static
7
- pinned: false
8
- ---
9
-
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # humanize-e
2
+ <img width="1024" height="559" alt="image" src="https://github.com/user-attachments/assets/b647751e-da57-4fa8-8175-b60800c44440" />
3
+
4
+ > AI Model to Humanize AI Texts
5
+
6
+ A PyTorch-based transformer model (QLoRA fine-tune of Qwen2.5-3B-Instruct) that rewrites AI-generated text to sound more natural and human-written.
7
+
8
+ **Developed by Mike Elistra (Mike Koci)**
9
+
10
+ **Training Notebook:** [Kaggle – humanize-e Training](https://www.kaggle.com/code/mick009/notebook9c4e543da9)
11
+
12
+ **HuggingFace Model:** [mike-elistra/humanize-e-adapter](https://huggingface.co/mike-elistra/humanize-e-adapter)
13
+
14
+ ## Quick Start
15
+
16
+ ### Local Installation
17
+
18
+ ```bash
19
+ # Install dependencies
20
+ pip install -r requirements.txt
21
+
22
+ # Run the server
23
+ python app.py
24
+ # Open http://localhost:8000
25
+ ```
26
+
27
+ ### Docker
28
+
29
+ ```bash
30
+ # Build
31
+ docker build -t humanize-e .
32
+
33
+ # Run (mount adapter weights)
34
+ docker run -p 8000:8000 -v /path/to/adapter:/app/humanize-e-adapter humanize-e
35
+ ```
36
+
37
+ ## Architecture
38
+
39
+ ```
40
+ humanize-e/
41
+ ├── app.py # FastAPI backend with web UI
42
+ ├── model.py # HumanizeModel class (inference)
43
+ ├── Dockerfile # Container build
44
+ ├── requirements.txt # Python dependencies (inference only)
45
+ ├── .gitignore # Git ignore patterns
46
+ └── README.md # This file
47
+ ```
48
+
49
+ ## API Endpoints
50
+
51
+ ### POST /humanize
52
+
53
+ ```bash
54
+ curl -X POST http://localhost:8000/humanize \
55
+ -H "Content-Type: application/json" \
56
+ -d '{"text": "In order to provide you with a comprehensive solution, it is important to note that we have conducted extensive research."}'
57
+ ```
58
+
59
+ **Response:**
60
+ ```json
61
+ {
62
+ "original": "In order to provide you with a comprehensive solution, it is important to note that we have conducted extensive research.",
63
+ "humanized": "To give you a comprehensive solution, we've done extensive research.",
64
+ "model": "qwen2.5-3b-lora"
65
+ }
66
+ ```
67
+
68
+ ### GET /health
69
+
70
+ Returns model status:
71
+ ```json
72
+ {"status": "ok", "model": "qwen2.5-3b-lora", "adapter_path": "/app/humanize-e-adapter"}
73
+ ```
74
+
75
+ ## Model Details
76
+
77
+ | Feature | Description |
78
+ |---------|-------------|
79
+ | **Base** | Qwen2.5-3B-Instruct |
80
+ | **Fine-tuning** | PEFT LoRA (r=32, α=64) |
81
+ | **Quantization** | 4-bit NF4 (GPU) / fp32 (CPU) |
82
+ | **Training Data** | 156 human↔AI-style synthetic pairs from Kaggle AI-vs-Human dataset |
83
+ | **Inference** | CPU/GPU compatible, fallback rule-based when adapter missing |
84
+
85
+ ## Getting the Model Weights
86
+
87
+ ### Base Model Qwen2.5-3B-Instruct
88
+ The base model is downloaded automatically from HuggingFace on first run. You can also cache it manually:
89
+
90
+ ```bash
91
+ huggingface-cli download Qwen/Qwen2.5-3B-Instruct --local-dir ./models/qwen2.5-3b-instruct
92
+ ```
93
+
94
+ Then set:
95
+ ```env
96
+ HUMANIZE_BASE_MODEL=./models/qwen2.5-3b-instruct
97
+ ```
98
+
99
+ ### LoRA Adapter Weights
100
+ The trained LoRA adapter was created on Kaggle. Download it from the training notebook outputs:
101
+
102
+ 1. Open [Kaggle Notebook](https://www.kaggle.com/code/mick009/notebook9c4e543da9)
103
+ 2. Download the `model_files.zip` output
104
+ 3. Extract to `humanize-e-adapter/`:
105
+ ```
106
+ humanize-e-adapter/
107
+ ├── adapter_config.json
108
+ ├── adapter_model.safetensors
109
+ ├── tokenizer.json
110
+ └── ...
111
+ ```
112
+ 4. Run with local path:
113
+ ```bash
114
+ HUMANIZE_ADAPTER=./humanize-e-adapter python app.py
115
+ ```
116
+
117
+ ### Uploading Weights to HuggingFace
118
+ To share the adapter:
119
+
120
+ ```bash
121
+ pip install huggingface_hub
122
+ huggingface-cli login
123
+ python upload_adapter_to_hf.py
124
+ ```
125
+ Or manually:
126
+ ```python
127
+ from huggingface_hub import upload_folder
128
+ upload_folder(
129
+ folder_path="./humanize-e-adapter",
130
+ repo_id="mike-elistra/humanize-e-adapter",
131
+ repo_type="model"
132
+ )
133
+ ```
134
+
135
+ ### Uploading Weights to GitHub
136
+ Adapter files are large → use Git LFS:
137
+
138
+ ```bash
139
+ git lfs install
140
+ git lfs track "humanize-e-adapter/*"
141
+ git add .gitattributes humanize-e-adapter/
142
+ git commit -m "Add LoRA adapter weights"
143
+ git push origin main
144
+ ```
145
+
146
+ The repo is already configured with `.gitattributes` for LFS. Keep weights in `humanize-e-adapter/` and push with LFS.
147
+
148
+ ## Environment Variables
149
+
150
+ | Variable | Default | Description |
151
+ |----------|---------|-------------|
152
+ | `HUMANIZE_BASE_MODEL` | `Qwen/Qwen2.5-3B-Instruct` | HuggingFace model ID |
153
+ | `HUMANIZE_ADAPTER` | `./humanize-e-adapter` | Path to LoRA adapter files |
154
+
155
+ ## Usage in Python
156
+
157
+ ```python
158
+ from model import HumanizeModel
159
+
160
+ model = HumanizeModel()
161
+ result = model.humanize("Your AI-generated text here")
162
+ print(result)
163
+ ```
164
+
165
+ ## Deploying with the Trained Model
166
+
167
+ ### Option 1: Local Drop-in
168
+
169
+ After training, copy adapter files to `humanize-e-adapter/`:
170
+ ```
171
+ humanize-e-adapter/
172
+ ├── adapter_config.json
173
+ ├── adapter_model.safetensors
174
+ ├── tokenizer.json
175
+ ├── tokenizer_config.json
176
+ ├── vocab.json
177
+ ├── merges.txt
178
+ └── special_tokens_map.json
179
+ ```
180
+
181
+ ### Option 2: HuggingFace Hub
182
+
183
+ ```bash
184
+ pip install huggingface_hub
185
+
186
+ # Install
187
+ huggingface-cli login
188
+ huggingface_hub.upload_folder(
189
+ folder_path="path/to/adapter",
190
+ repo_id="mike-elistra/humanize-e-adapter",
191
+ repo_type="model"
192
+ )
193
+
194
+ # Then run:
195
+ HUMANIZE_ADAPTER=mike-elistra/humanize-e-adapter python app.py
196
+ ```
197
+
198
+ ### Option 3: Docker Volume
199
+
200
+ ```bash
201
+ # On HuggingFace Hub
202
+ git lfs install
203
+ git clone https://huggingface.co/mike-elistra/humanize-e-adapter
204
+ docker run -p 8000:8000 -v $(pwd)/humanize-e-adapter:/app/humanize-e-adapter humanize-e
205
+ ```
206
+
207
+ ## Training Notes
208
+
209
+ - Trained on [Kaggle AI vs Human Text Dataset](https://www.kaggle.com/datasets/alitaqishah/ai-vs-human-text-classification-dataset-2026)
210
+ - Synthetic pairs: human texts → AI-style (base model rewrite) → training samples
211
+ - 2 epochs, lr=2e-4, cosine scheduler, weight decay 0.01
212
+ - Loss: cross-entropy, prompt tokens masked (only learn style transfer)
213
+
214
+ ## API Key Management
215
+
216
+ ⚠️ **Security Notice**: This repo may require LLM API keys for generation (not used for inference). Store keys in `.env`:
217
+
218
+ ```env
219
+ HUMANIZE_BASE_MODEL=Qwen/Qwen2.5-3B-Instruct
220
+ HUMANIZE_ADAPTER=/path/to/adapter
221
+ ```
222
+
223
+ ## Development & Training
224
+
225
+ **Developed by:** Mike Elistra (Mike Koci)
226
+
227
+ **Kaggle Training Notebook:** https://www.kaggle.com/code/mick009/notebook9c4e543da9
228
+
229
+ The model was fine-tuned on Kaggle using QLoRA on Qwen2.5-3B-Instruct. The notebook contains the complete training pipeline:
230
+ - Synthetic AI-style generation
231
+ - QLoRA fine-tuning
232
+ - Evaluation metrics
233
+ - Adapter export
234
+
235
+ To retrain or experiment, open the Kaggle notebook and run all cells. The trained adapter should be exported to `humanize-e-adapter/`.
236
+
237
+ ## Testing
238
+
239
+ Run unit tests to verify functionality:
240
+
241
+ ```bash
242
+ # Run all tests
243
+ python -m unittest discover -s tests -p 'test_*.py'
244
+
245
+ # Or run specific test
246
+ python -m unittest tests.test_app.TestAPIEndpoints.test_health_endpoint
247
+ ```
248
+
249
+ Tests cover:
250
+ - Model fallback mode (no adapter)
251
+ - Rule-based humanizer
252
+ - FastAPI endpoints (/health, /humanize, /)
253
+ - Input validation
254
+
255
+ CI runs automatically on every push via GitHub Actions.
256
+
257
+ ## Contributing
258
+
259
+ Contributions are welcome! This project is open for anyone to improve, extend, or fix.
260
+
261
+ **How to contribute:**
262
+ 1. Fork the repository
263
+ 2. Create a feature branch (`git checkout -b feature/your-feature`)
264
+ 3. Make your changes and add tests if applicable
265
+ 4. Run tests locally to ensure everything works
266
+ 5. Commit your changes (`git commit -m 'Add your feature'`)
267
+ 6. Push to your fork and open a Pull Request
268
+
269
+ **Ideas for contributions:**
270
+ - Improved humanization rules for the fallback mode
271
+ - Support for additional base models
272
+ - Performance optimizations
273
+ - Better UI/UX for the web interface
274
+ - Docker improvements
275
+ - Documentation enhancements
276
+
277
+ Everyone is welcome to edit, improve, and contribute to humanize-e!
278
+
279
+ ## License
280
+
281
+ MIT - Feel free to use, modify, and train on your own data!
app.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ FastAPI backend for humanize-e
3
+
4
+ Developed by Mike Elistra (Mike Koci)
5
+ Kaggle Training Notebook: https://www.kaggle.com/code/mick009/notebook9c4e543da9
6
+
7
+ Endpoints:
8
+ - POST /humanize - Humanize text (JSON: {"text": "...", "temperature": 0.8})
9
+ - GET / - Web UI
10
+ - GET /health - Health check
11
+ """
12
+
13
+ import os
14
+ from contextlib import asynccontextmanager
15
+
16
+ from fastapi import FastAPI, HTTPException
17
+ from fastapi.responses import HTMLResponse
18
+ from pydantic import BaseModel
19
+ import uvicorn
20
+
21
+ # Load .env if present
22
+ from dotenv import load_dotenv
23
+ load_dotenv()
24
+
25
+ from model import HumanizeModel
26
+
27
+ model: HumanizeModel | None = None
28
+
29
+
30
+ @asynccontextmanager
31
+ async def lifespan(app: FastAPI):
32
+ global model
33
+ print("[humanize-e] Starting up...")
34
+ model = HumanizeModel()
35
+ print(f"[humanize-e] Model loaded: {model._loaded}")
36
+ yield
37
+ print("[humanize-e] Shutting down...")
38
+
39
+
40
+ app = FastAPI(
41
+ title="humanize-e API",
42
+ description="Humanize AI-generated text with a QLoRA fine-tuned Qwen2.5-3B",
43
+ version="1.0.0",
44
+ lifespan=lifespan,
45
+ )
46
+
47
+
48
+ class HumanizeRequest(BaseModel):
49
+ text: str
50
+ temperature: float = 0.8
51
+
52
+
53
+ class HumanizeResponse(BaseModel):
54
+ original: str
55
+ humanized: str
56
+ model: str # "qwen2.5-3b-lora" or "fallback"
57
+
58
+
59
+ @app.get("/", response_class=HTMLResponse)
60
+ async def root():
61
+ return """
62
+ <!DOCTYPE html>
63
+ <html lang="en">
64
+ <head>
65
+ <meta charset="UTF-8">
66
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
67
+ <title>humanize-e</title>
68
+ <style>
69
+ * { box-sizing: border-box; margin: 0; padding: 0; }
70
+ body {
71
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
72
+ max-width: 800px; margin: 0 auto; padding: 20px;
73
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
74
+ min-height: 100vh; color: #333;
75
+ }
76
+ .container { background: white; border-radius: 12px; padding: 40px;
77
+ box-shadow: 0 10px 40px rgba(0,0,0,0.2); }
78
+ h1 { text-align: center; margin-bottom: 10px;
79
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
80
+ -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
81
+ .subtitle { text-align: center; color: #666; margin-bottom: 30px; }
82
+ label { display: block; margin-bottom: 8px; font-weight: 500; }
83
+ textarea { width: 100%; min-height: 120px; padding: 12px;
84
+ border: 2px solid #e1e5e9; border-radius: 8px; font-size: 14px;
85
+ resize: vertical; font-family: inherit; }
86
+ textarea:focus { outline: none; border-color: #667eea; }
87
+ button { width: 100%; padding: 14px; margin-top: 15px;
88
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
89
+ color: white; border: none; border-radius: 8px; font-size: 16px;
90
+ font-weight: 600; cursor: pointer; }
91
+ button:hover { opacity: 0.9; }
92
+ .result { margin-top: 30px; background: #f8f9fa; border-radius: 8px;
93
+ padding: 20px; border-left: 4px solid #667eea; display: none; }
94
+ .result h3 { color: #667eea; margin-bottom: 12px; }
95
+ .badge { display: inline-block; font-size: 12px; padding: 2px 8px;
96
+ border-radius: 10px; background: #e1e5e9; color: #555; margin-left: 8px; }
97
+ .footer { text-align: center; margin-top: 30px; color: rgba(0,0,0,0.4); font-size: 13px; }
98
+ </style>
99
+ </head>
100
+ <body>
101
+ <div class="container">
102
+ <h1>humanize-e</h1>
103
+ <p class="subtitle">Make AI texts sound more human <span id="badge" class="badge"></span></p>
104
+ <form id="f" onsubmit="go(event)">
105
+ <label for="text">Paste AI-generated text:</label>
106
+ <textarea id="text" required placeholder="Paste your AI-generated text here..."></textarea>
107
+ <button type="submit">Humanize it</button>
108
+ </form>
109
+ <div class="result" id="result">
110
+ <h3>Humanized text</h3>
111
+ <div id="out"></div>
112
+ </div>
113
+ <p class="footer">humanize-e &middot; Qwen2.5-3B-Instruct + QLoRA</p>
114
+ </div>
115
+ <script>
116
+ fetch('/health').then(r => r.json()).then(h => {
117
+ document.getElementById('badge').textContent = h.model;
118
+ });
119
+ async function go(e) {
120
+ e.preventDefault();
121
+ const text = document.getElementById('text').value;
122
+ if (!text.trim()) return;
123
+ const btn = e.target.querySelector('button');
124
+ btn.disabled = true; btn.textContent = 'Humanizing...';
125
+ try {
126
+ const r = await fetch('/humanize', {
127
+ method: 'POST',
128
+ headers: {'Content-Type': 'application/json'},
129
+ body: JSON.stringify({text})
130
+ });
131
+ const d = await r.json();
132
+ document.getElementById('out').textContent = d.humanized;
133
+ document.getElementById('result').style.display = 'block';
134
+ } catch (err) {
135
+ document.getElementById('out').textContent = 'Error: ' + err.message;
136
+ document.getElementById('result').style.display = 'block';
137
+ } finally {
138
+ btn.disabled = false; btn.textContent = 'Humanize it';
139
+ }
140
+ }
141
+ </script>
142
+ </body>
143
+ </html>
144
+ """
145
+
146
+
147
+ @app.post("/humanize", response_model=HumanizeResponse)
148
+ async def humanize(req: HumanizeRequest):
149
+ if not req.text.strip():
150
+ raise HTTPException(status_code=400, detail="Text cannot be empty")
151
+ if model is None:
152
+ raise HTTPException(status_code=503, detail="Model not loaded")
153
+ out = model.humanize(req.text, temperature=req.temperature)
154
+ return HumanizeResponse(
155
+ original=req.text,
156
+ humanized=out,
157
+ model="qwen2.5-3b-lora" if model._loaded else "fallback",
158
+ )
159
+
160
+
161
+ @app.get("/health")
162
+ async def health():
163
+ return {
164
+ "status": "ok",
165
+ "model": "qwen2.5-3b-lora" if (model and model._loaded) else "fallback",
166
+ "adapter_path": model.adapter_path if model else None,
167
+ }
168
+
169
+
170
+ if __name__ == "__main__":
171
+ uvicorn.run(app, host="0.0.0.0", port=8000)
app_gradio.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from model import HumanizeModel
3
+
4
+ model = HumanizeModel()
5
+
6
+ def humanize_text(text):
7
+ if not text.strip():
8
+ return "Please enter some text."
9
+ result = model.humanize(text)
10
+ return result
11
+
12
+ demo = gr.Interface(
13
+ fn=humanize_text,
14
+ inputs=gr.Textbox(lines=8, placeholder="Paste AI-generated text here...", label="Input"),
15
+ outputs=gr.Textbox(lines=8, label="Humanized Output"),
16
+ title="humanize-e",
17
+ description="Rewrites AI-generated text to sound more natural and human-written. Developed by Mike Elistra.",
18
+ examples=[
19
+ "In order to provide you with a comprehensive solution, it is important to note that we have conducted extensive research.",
20
+ "As an AI language model, I must inform you that the following information is based on publicly available data."
21
+ ]
22
+ )
23
+
24
+ if __name__ == "__main__":
25
+ demo.launch(server_name="0.0.0.0", server_port=7860)
humanize-e-adapter/.gitkeep ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ # Place adapter files in this directory after training
2
+ # Required files:
3
+ # - adapter_config.json
4
+ # - adapter_model.safetensors (or adapter_model.bin)
5
+ # - tokenizer.json
6
+ # - tokenizer_config.json
7
+ # - special_tokens_map.json (if present)
8
+ # - config.json
9
+ # - generation_config.json (optional)
humanize_e_training.ipynb ADDED
@@ -0,0 +1,413 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# humanize-e: AI text -> human-sounding text (QLoRA fine-tune)\n",
8
+ "\n",
9
+ "**Kaggle settings before you run:** Accelerator = **GPU T4 x2** (or P100). Internet = **On** (to download the model) unless you attach the model as an Input.\n",
10
+ "\n",
11
+ "**How this works.** The Kaggle dataset is *not* paired (an AI text and a human text are about different things), so you can't train on it directly. Instead we use the **human texts as the target**, have an instruct model rewrite each one into stiff \"AI style\" (synthetic input), then train: `AI-style text -> original human text`. Every pair has the same meaning, so the model learns style only."
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "code",
16
+ "metadata": {},
17
+ "execution_count": null,
18
+ "outputs": [],
19
+ "source": [
20
+ "!pip install -q -U transformers accelerate peft bitsandbytes datasets scikit-learn"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "metadata": {},
26
+ "execution_count": null,
27
+ "outputs": [],
28
+ "source": [
29
+ "import os, glob, random, gc\n",
30
+ "import numpy as np, pandas as pd, torch\n",
31
+ "from datasets import Dataset\n",
32
+ "from tqdm.auto import tqdm\n",
33
+ "\n",
34
+ "SEED = 42\n",
35
+ "random.seed(SEED); np.random.seed(SEED); torch.manual_seed(SEED)\n",
36
+ "\n",
37
+ "# ---- Base model: pick ONE ----\n",
38
+ "# A) Internet ON: downloads from Hugging Face\n",
39
+ "MODEL_NAME = \"Qwen/Qwen2.5-3B-Instruct\"\n",
40
+ "# B) Internet OFF: Add Input -> Models -> \"Qwen2.5 3B Instruct\", then paste the folder that contains config.json\n",
41
+ "# MODEL_NAME = \"/kaggle/input/qwen2.5/transformers/3b-instruct/1\"\n",
42
+ "\n",
43
+ "# ---- Dataset columns: check the printout in the next cell and edit if needed ----\n",
44
+ "TEXT_COL, LABEL_COL = \"text_content\", \"label\"\n",
45
+ "HUMAN_VALUE, AI_VALUE = \"human\", \"ai\" # use \"0\"/\"1\" etc. if labels are numeric\n",
46
+ "\n",
47
+ "N_PAIRS = 2000 # more = better, but synthetic generation takes longer\n",
48
+ "MIN_WORDS, MAX_WORDS = 40, 180\n",
49
+ "SMOKE_TEST = True # True = tiny 20-step run to check the loss. Set False for the real run.\n",
50
+ "\n",
51
+ "print(\"GPUs:\", torch.cuda.device_count(), [torch.cuda.get_device_name(i) for i in range(torch.cuda.device_count())])"
52
+ ]
53
+ },
54
+ {
55
+ "cell_type": "markdown",
56
+ "metadata": {},
57
+ "source": [
58
+ "## 1. Load the dataset and inspect it"
59
+ ]
60
+ },
61
+ {
62
+ "cell_type": "code",
63
+ "metadata": {},
64
+ "execution_count": null,
65
+ "outputs": [],
66
+ "source": [
67
+ "csvs = glob.glob(\"/kaggle/input/**/*.csv\", recursive=True)\n",
68
+ "print(csvs)\n",
69
+ "CSV_PATH = csvs[0] # change if several CSVs are listed\n",
70
+ "df = pd.read_csv(CSV_PATH)\n",
71
+ "print(df.shape)\n",
72
+ "print(df.columns.tolist())\n",
73
+ "display(df.head(3))\n",
74
+ "assert TEXT_COL in df.columns and LABEL_COL in df.columns, f\"Fix TEXT_COL / LABEL_COL. Columns are: {df.columns.tolist()}\"\n",
75
+ "print(df[LABEL_COL].value_counts())"
76
+ ]
77
+ },
78
+ {
79
+ "cell_type": "code",
80
+ "metadata": {},
81
+ "execution_count": null,
82
+ "outputs": [],
83
+ "source": [
84
+ "is_human = df[LABEL_COL].astype(str).str.lower() == str(HUMAN_VALUE).lower()\n",
85
+ "human = df.loc[is_human, TEXT_COL].dropna().astype(str).drop_duplicates()\n",
86
+ "wc = human.str.split().str.len()\n",
87
+ "human = human[(wc >= MIN_WORDS) & (wc <= MAX_WORDS)]\n",
88
+ "print(\"usable human texts:\", len(human))\n",
89
+ "assert len(human) > 0, \"No human texts found - check HUMAN_VALUE and the label values printed above.\"\n",
90
+ "human_texts = human.sample(min(N_PAIRS, len(human)), random_state=SEED).tolist()\n",
91
+ "print(\"using:\", len(human_texts))"
92
+ ]
93
+ },
94
+ {
95
+ "cell_type": "markdown",
96
+ "metadata": {},
97
+ "source": [
98
+ "## 2. Load the model (4-bit)"
99
+ ]
100
+ },
101
+ {
102
+ "cell_type": "code",
103
+ "metadata": {},
104
+ "execution_count": null,
105
+ "outputs": [],
106
+ "source": [
107
+ "from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n",
108
+ "\n",
109
+ "tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)\n",
110
+ "if tokenizer.pad_token is None:\n",
111
+ " tokenizer.pad_token = tokenizer.eos_token\n",
112
+ "\n",
113
+ "bnb = BitsAndBytesConfig(\n",
114
+ " load_in_4bit=True, bnb_4bit_quant_type=\"nf4\",\n",
115
+ " bnb_4bit_compute_dtype=torch.float16, # T4/P100 have no bf16\n",
116
+ " bnb_4bit_use_double_quant=True,\n",
117
+ ")\n",
118
+ "model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, quantization_config=bnb, device_map=\"auto\")\n",
119
+ "print(\"loaded\", MODEL_NAME)"
120
+ ]
121
+ },
122
+ {
123
+ "cell_type": "markdown",
124
+ "metadata": {},
125
+ "source": [
126
+ "## 3. Build synthetic pairs: human text -> \"AI-style\" version\n",
127
+ "The model rewrites each human text in stiff, generic AI style. We then flip it: input = AI-style, target = the real human text."
128
+ ]
129
+ },
130
+ {
131
+ "cell_type": "code",
132
+ "metadata": {},
133
+ "execution_count": null,
134
+ "outputs": [],
135
+ "source": [
136
+ "AI_STYLE_SYS = (\n",
137
+ " \"Rewrite the user's text the way a generic AI assistant would: formal, polished, balanced and \"\n",
138
+ " \"slightly verbose, using phrases like 'it is important to note' and transitions like 'Moreover' or \"\n",
139
+ " \"'Furthermore'. No slang, no typos, no contractions, no personal quirks. Keep exactly the same meaning \"\n",
140
+ " \"and a similar length. Output ONLY the rewritten text.\"\n",
141
+ ")\n",
142
+ "\n",
143
+ "@torch.no_grad()\n",
144
+ "def to_ai_style(texts, bs=8, max_new_tokens=320):\n",
145
+ " tokenizer.padding_side = \"left\"\n",
146
+ " order = np.argsort([len(t) for t in texts]) # batch similar lengths together\n",
147
+ " results = [None] * len(texts)\n",
148
+ " for i in tqdm(range(0, len(texts), bs)):\n",
149
+ " idx = order[i:i+bs]\n",
150
+ " prompts = [tokenizer.apply_chat_template(\n",
151
+ " [{\"role\": \"system\", \"content\": AI_STYLE_SYS}, {\"role\": \"user\", \"content\": texts[j]}],\n",
152
+ " tokenize=False, add_generation_prompt=True) for j in idx]\n",
153
+ " enc = tokenizer(prompts, return_tensors=\"pt\", padding=True, truncation=True, max_length=768).to(model.device)\n",
154
+ " out = model.generate(**enc, max_new_tokens=max_new_tokens, do_sample=True, temperature=0.7,\n",
155
+ " top_p=0.9, pad_token_id=tokenizer.pad_token_id)\n",
156
+ " dec = tokenizer.batch_decode(out[:, enc[\"input_ids\"].shape[1]:], skip_special_tokens=True)\n",
157
+ " for j, d in zip(idx, dec):\n",
158
+ " results[j] = d.strip()\n",
159
+ " return results\n",
160
+ "\n",
161
+ "PAIRS_CSV = \"/kaggle/working/pairs.csv\"\n",
162
+ "if os.path.exists(PAIRS_CSV):\n",
163
+ " pairs_df = pd.read_csv(PAIRS_CSV)\n",
164
+ " print(\"loaded cached pairs:\", len(pairs_df))\n",
165
+ "else:\n",
166
+ " ai_versions = to_ai_style(human_texts)\n",
167
+ " pairs_df = pd.DataFrame({\"ai_text\": ai_versions, \"human_text\": human_texts})\n",
168
+ " pairs_df.to_csv(PAIRS_CSV, index=False)\n",
169
+ " print(\"saved\", len(pairs_df))\n",
170
+ "torch.cuda.empty_cache(); gc.collect()"
171
+ ]
172
+ },
173
+ {
174
+ "cell_type": "code",
175
+ "metadata": {},
176
+ "execution_count": null,
177
+ "outputs": [],
178
+ "source": [
179
+ "# Clean: drop empty / too short / unchanged generations\n",
180
+ "pairs_df = pairs_df.dropna()\n",
181
+ "pairs_df = pairs_df[pairs_df.ai_text.str.split().str.len() >= 20]\n",
182
+ "pairs_df = pairs_df[pairs_df.ai_text.str.strip() != pairs_df.human_text.str.strip()]\n",
183
+ "print(\"clean pairs:\", len(pairs_df))\n",
184
+ "\n",
185
+ "for _, r in pairs_df.head(2).iterrows():\n",
186
+ " print(\"AI-STYLE:\", r.ai_text, \"\\nHUMAN :\", r.human_text, \"\\n\" + \"-\"*80)\n",
187
+ "\n",
188
+ "split = Dataset.from_pandas(pairs_df.reset_index(drop=True)).train_test_split(test_size=0.05, seed=SEED)\n",
189
+ "train_ds, eval_ds = split[\"train\"], split[\"test\"]\n",
190
+ "print(len(train_ds), len(eval_ds))"
191
+ ]
192
+ },
193
+ {
194
+ "cell_type": "markdown",
195
+ "metadata": {},
196
+ "source": [
197
+ "## 4. Add LoRA and tokenize (loss only on the human completion)"
198
+ ]
199
+ },
200
+ {
201
+ "cell_type": "code",
202
+ "metadata": {},
203
+ "execution_count": null,
204
+ "outputs": [],
205
+ "source": [
206
+ "from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training\n",
207
+ "\n",
208
+ "model = prepare_model_for_kbit_training(model, use_gradient_checkpointing=True)\n",
209
+ "lora_config = LoraConfig(\n",
210
+ " r=32, lora_alpha=64, lora_dropout=0.05, bias=\"none\", task_type=\"CAUSAL_LM\",\n",
211
+ " target_modules=[\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\", \"gate_proj\", \"up_proj\", \"down_proj\"], # Qwen/Llama names\n",
212
+ ")\n",
213
+ "model = get_peft_model(model, lora_config)\n",
214
+ "model.print_trainable_parameters()"
215
+ ]
216
+ },
217
+ {
218
+ "cell_type": "code",
219
+ "metadata": {},
220
+ "execution_count": null,
221
+ "outputs": [],
222
+ "source": [
223
+ "INSTR = \"Rewrite the following text so it sounds natural and human-written. Keep the same meaning.\\n\\n{text}\"\n",
224
+ "\n",
225
+ "def build_prompt(text):\n",
226
+ " return tokenizer.apply_chat_template(\n",
227
+ " [{\"role\": \"user\", \"content\": INSTR.format(text=text)}], tokenize=False, add_generation_prompt=True)\n",
228
+ "\n",
229
+ "MAX_PROMPT, MAX_COMP = 512, 320\n",
230
+ "tokenizer.padding_side = \"right\"\n",
231
+ "\n",
232
+ "def tokenize_fn(batch):\n",
233
+ " out = {\"input_ids\": [], \"attention_mask\": [], \"labels\": []}\n",
234
+ " for ai, hu in zip(batch[\"ai_text\"], batch[\"human_text\"]):\n",
235
+ " p = tokenizer(build_prompt(ai), add_special_tokens=False)[\"input_ids\"][:MAX_PROMPT]\n",
236
+ " c = tokenizer(hu, add_special_tokens=False)[\"input_ids\"][:MAX_COMP] + [tokenizer.eos_token_id]\n",
237
+ " ids = p + c\n",
238
+ " out[\"input_ids\"].append(ids)\n",
239
+ " out[\"attention_mask\"].append([1] * len(ids))\n",
240
+ " out[\"labels\"].append([-100] * len(p) + c) # prompt masked, completion learned\n",
241
+ " return out\n",
242
+ "\n",
243
+ "tok_train = train_ds.map(tokenize_fn, batched=True, remove_columns=train_ds.column_names)\n",
244
+ "tok_eval = eval_ds.map(tokenize_fn, batched=True, remove_columns=eval_ds.column_names)\n",
245
+ "\n",
246
+ "# Sanity check: labels must NOT be all -100\n",
247
+ "ex = tok_train[0]\n",
248
+ "n_learn = sum(l != -100 for l in ex[\"labels\"])\n",
249
+ "print(\"tokens:\", len(ex[\"input_ids\"]), \"| learned tokens:\", n_learn)\n",
250
+ "assert n_learn > 0\n",
251
+ "print(tokenizer.decode([l for l in ex[\"labels\"] if l != -100])[:300])"
252
+ ]
253
+ },
254
+ {
255
+ "cell_type": "markdown",
256
+ "metadata": {},
257
+ "source": [
258
+ "## 5. Train\n",
259
+ "Run once with `SMOKE_TEST = True` (loss should be a real number and drop). Then set it to `False` and re-run this cell for the full run."
260
+ ]
261
+ },
262
+ {
263
+ "cell_type": "code",
264
+ "metadata": {},
265
+ "execution_count": null,
266
+ "outputs": [],
267
+ "source": [
268
+ "from transformers import TrainingArguments, Trainer, DataCollatorForSeq2Seq\n",
269
+ "\n",
270
+ "args = TrainingArguments(\n",
271
+ " output_dir=\"/kaggle/working/humanize-e-ckpt\",\n",
272
+ " num_train_epochs=2,\n",
273
+ " max_steps=20 if SMOKE_TEST else -1,\n",
274
+ " per_device_train_batch_size=2, per_device_eval_batch_size=2,\n",
275
+ " gradient_accumulation_steps=8,\n",
276
+ " learning_rate=2e-4, lr_scheduler_type=\"cosine\", warmup_ratio=0.05,\n",
277
+ " weight_decay=0.01,\n",
278
+ " fp16=True, # use bf16=True instead on L4/A100\n",
279
+ " optim=\"paged_adamw_8bit\",\n",
280
+ " gradient_checkpointing=True, gradient_checkpointing_kwargs={\"use_reentrant\": False},\n",
281
+ " eval_strategy=\"steps\", eval_steps=50,\n",
282
+ " save_strategy=\"steps\", save_steps=100, save_total_limit=2,\n",
283
+ " logging_steps=5, group_by_length=False,\n",
284
+ " remove_unused_columns=False, report_to=\"none\",\n",
285
+ ")\n",
286
+ "\n",
287
+ "trainer = Trainer(\n",
288
+ " model=model, args=args,\n",
289
+ " train_dataset=tok_train, eval_dataset=tok_eval,\n",
290
+ " data_collator=DataCollatorForSeq2Seq(tokenizer, padding=True, label_pad_token_id=-100),\n",
291
+ ")\n",
292
+ "trainer.train()"
293
+ ]
294
+ },
295
+ {
296
+ "cell_type": "markdown",
297
+ "metadata": {},
298
+ "source": [
299
+ "## 6. Save the LoRA adapter"
300
+ ]
301
+ },
302
+ {
303
+ "cell_type": "code",
304
+ "metadata": {},
305
+ "execution_count": null,
306
+ "outputs": [],
307
+ "source": [
308
+ "SAVE_DIR = \"/kaggle/working/humanize-e-adapter\"\n",
309
+ "trainer.model.save_pretrained(SAVE_DIR)\n",
310
+ "tokenizer.save_pretrained(SAVE_DIR)\n",
311
+ "print(\"saved to\", SAVE_DIR)"
312
+ ]
313
+ },
314
+ {
315
+ "cell_type": "markdown",
316
+ "metadata": {},
317
+ "source": [
318
+ "## 7. Try it (same prompt template as training)"
319
+ ]
320
+ },
321
+ {
322
+ "cell_type": "code",
323
+ "metadata": {},
324
+ "execution_count": null,
325
+ "outputs": [],
326
+ "source": [
327
+ "model.eval()\n",
328
+ "\n",
329
+ "@torch.no_grad()\n",
330
+ "def humanize(text, max_new_tokens=320, temperature=0.8):\n",
331
+ " enc = tokenizer(build_prompt(text), return_tensors=\"pt\", add_special_tokens=False).to(model.device)\n",
332
+ " out = model.generate(**enc, max_new_tokens=max_new_tokens, do_sample=True, temperature=temperature,\n",
333
+ " top_p=0.9, repetition_penalty=1.05, use_cache=True, pad_token_id=tokenizer.pad_token_id)\n",
334
+ " return tokenizer.decode(out[0][enc[\"input_ids\"].shape[1]:], skip_special_tokens=True).strip()\n",
335
+ "\n",
336
+ "tests = [\n",
337
+ " \"In order to provide you with the most comprehensive solution, it is important to note that we have conducted extensive research on this topic.\",\n",
338
+ " \"Moreover, the analysis reveals several important implications that should be carefully considered by all stakeholders.\",\n",
339
+ "]\n",
340
+ "for t in tests:\n",
341
+ " print(\"AI :\", t); print(\"HUMAN:\", humanize(t), \"\\n\")"
342
+ ]
343
+ },
344
+ {
345
+ "cell_type": "markdown",
346
+ "metadata": {},
347
+ "source": [
348
+ "## 8. Rough quality check with the Kaggle dataset as a detector\n",
349
+ "We train a quick TF-IDF classifier on the dataset (AI vs human), **excluding the held-out eval texts**, and compare its \"AI probability\" before vs after humanizing. This is only a proxy. It does not predict how real detectors (GPTZero, Turnitin, etc.) will score the text."
350
+ ]
351
+ },
352
+ {
353
+ "cell_type": "code",
354
+ "metadata": {},
355
+ "execution_count": null,
356
+ "outputs": [],
357
+ "source": [
358
+ "from sklearn.feature_extraction.text import TfidfVectorizer\n",
359
+ "from sklearn.linear_model import LogisticRegression\n",
360
+ "\n",
361
+ "d = df[[TEXT_COL, LABEL_COL]].dropna()\n",
362
+ "d = d[~d[TEXT_COL].astype(str).isin(set(eval_ds[\"human_text\"]))]\n",
363
+ "y = (d[LABEL_COL].astype(str).str.lower() == str(AI_VALUE).lower()).astype(int)\n",
364
+ "vec = TfidfVectorizer(ngram_range=(1, 2), min_df=3, max_features=200000, sublinear_tf=True)\n",
365
+ "clf = LogisticRegression(max_iter=1000).fit(vec.fit_transform(d[TEXT_COL].astype(str)), y)\n",
366
+ "\n",
367
+ "n = min(40, len(eval_ds))\n",
368
+ "src = eval_ds[\"ai_text\"][:n]\n",
369
+ "outs = [humanize(t) for t in tqdm(src)]\n",
370
+ "p_before = clf.predict_proba(vec.transform(src))[:, 1].mean()\n",
371
+ "p_after = clf.predict_proba(vec.transform(outs))[:, 1].mean()\n",
372
+ "p_real = clf.predict_proba(vec.transform(eval_ds[\"human_text\"][:n]))[:, 1].mean()\n",
373
+ "print(f\"P(AI) AI-style input : {p_before:.2f}\")\n",
374
+ "print(f\"P(AI) model output : {p_after:.2f}\")\n",
375
+ "print(f\"P(AI) real human text: {p_real:.2f}\")"
376
+ ]
377
+ },
378
+ {
379
+ "cell_type": "markdown",
380
+ "metadata": {},
381
+ "source": [
382
+ "## 9. Load the adapter later (new notebook / your app)\n",
383
+ "```python\n",
384
+ "import torch\n",
385
+ "from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n",
386
+ "from peft import PeftModel\n",
387
+ "\n",
388
+ "BASE = \"Qwen/Qwen2.5-3B-Instruct\"\n",
389
+ "tok = AutoTokenizer.from_pretrained(\"humanize-e-adapter\")\n",
390
+ "base = AutoModelForCausalLM.from_pretrained(BASE, device_map=\"auto\",\n",
391
+ " quantization_config=BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type=\"nf4\", bnb_4bit_compute_dtype=torch.float16))\n",
392
+ "model = PeftModel.from_pretrained(base, \"humanize-e-adapter\").eval()\n",
393
+ "# then reuse build_prompt() + humanize() from section 7\n",
394
+ "```\n",
395
+ "Upload the adapter folder to Hugging Face with `huggingface_hub.upload_folder(...)`, or download it from `/kaggle/working`.\n",
396
+ "\n",
397
+ "**Tips:** if outputs look too similar to the input, raise `N_PAIRS` and train 3 epochs. If they ramble, lower the temperature to 0.6. Mix in other human styles (news, social, academic) by sampling evenly across the dataset's domain column if it has one."
398
+ ]
399
+ }
400
+ ],
401
+ "metadata": {
402
+ "kernelspec": {
403
+ "display_name": "Python 3",
404
+ "language": "python",
405
+ "name": "python3"
406
+ },
407
+ "language_info": {
408
+ "name": "python"
409
+ }
410
+ },
411
+ "nbformat": 4,
412
+ "nbformat_minor": 4
413
+ }
model.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #humanize-e inference model.
3
+
4
+ #Developed by Mike Elistra (Mike Koci)
5
+ #Kaggle Training Notebook: https://www.kaggle.com/code/mick009/notebook9c4e543da9
6
+
7
+ #Loads the QLoRA adapter trained for Qwen2.5-3B-Instruct fine-tuned to rewrite
8
+ #AI-generated text so it sounds natural and human-written.
9
+
10
+ #Prompt template (must match training):
11
+ # INSTR = "Rewrite the following text so it sounds natural and human-written. Keep the same meaning.\n\n{text}"
12
+
13
+
14
+ import os
15
+ import re
16
+ from typing import Optional
17
+
18
+ import torch
19
+
20
+ # Prompt used during training - keep in sync with training notebook!
21
+ INSTR = "Rewrite the following text so it sounds natural and human-written. Keep the same meaning.\n\n{text}"
22
+
23
+ BASE_MODEL = os.environ.get("HUMANIZE_BASE_MODEL", "Qwen/Qwen2.5-3B-Instruct")
24
+ ADAPTER_PATH = os.environ.get(
25
+ "HUMANIZE_ADAPTER",
26
+ os.path.join(os.path.dirname(os.path.abspath(__file__)), "humanize-e-adapter"),
27
+ )
28
+
29
+
30
+ class HumanizeModel:
31
+ """QLoRA-adapted Qwen2.5-3B-Instruct for humanizing AI text."""
32
+
33
+ def __init__(self, adapter_path: Optional[str] = None, base_model: Optional[str] = None):
34
+ self.adapter_path = adapter_path or ADAPTER_PATH
35
+ self.base_model = base_model or BASE_MODEL
36
+ self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
37
+ self.model = None
38
+ self.tokenizer = None
39
+ self._loaded = False
40
+
41
+ if self._adapter_exists():
42
+ self._load()
43
+ else:
44
+ print(f"[humanize-e] Adapter not found at {self.adapter_path} - using rule-based fallback.")
45
+
46
+ def _adapter_exists(self) -> bool:
47
+ return os.path.isdir(self.adapter_path) and os.path.exists(
48
+ os.path.join(self.adapter_path, "adapter_config.json")
49
+ )
50
+
51
+ def _load(self):
52
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
53
+ from peft import PeftModel
54
+
55
+ print(f"[humanize-e] Loading tokenizer from {self.adapter_path} ...")
56
+ self.tokenizer = AutoTokenizer.from_pretrained(self.adapter_path)
57
+ if self.tokenizer.pad_token is None:
58
+ self.tokenizer.pad_token = self.tokenizer.eos_token
59
+
60
+ print(f"[humanize-e] Loading base model {self.base_model} ...")
61
+ if torch.cuda.is_available():
62
+ bnb = BitsAndBytesConfig(
63
+ load_in_4bit=True,
64
+ bnb_4bit_quant_type="nf4",
65
+ bnb_4bit_compute_dtype=torch.float16,
66
+ bnb_4bit_use_double_quant=True,
67
+ )
68
+ base = AutoModelForCausalLM.from_pretrained(
69
+ self.base_model, quantization_config=bnb, device_map="auto"
70
+ )
71
+ else:
72
+ base = AutoModelForCausalLM.from_pretrained(
73
+ self.base_model, torch_dtype=torch.float32, device_map="cpu", low_cpu_mem_usage=True
74
+ )
75
+
76
+ print(f"[humanize-e] Loading LoRA adapter from {self.adapter_path} ...")
77
+ self.model = PeftModel.from_pretrained(base, self.adapter_path)
78
+ self.model.eval()
79
+ self._loaded = True
80
+ print("[humanize-e] Model ready.")
81
+
82
+ def build_prompt(self, text: str) -> str:
83
+ """Same prompt construction as training."""
84
+ return self.tokenizer.apply_chat_template(
85
+ [{"role": "user", "content": INSTR.format(text=text)}],
86
+ tokenize=False,
87
+ add_generation_prompt=True,
88
+ )
89
+
90
+ @torch.no_grad()
91
+ def humanize(
92
+ self,
93
+ text: str,
94
+ max_new_tokens: int = 320,
95
+ temperature: float = 0.8,
96
+ top_p: float = 0.9,
97
+ ) -> str:
98
+ """Humanize AI-generated text. Falls back to rules if model not loaded."""
99
+ if not self._loaded or self.model is None:
100
+ return quick_humanize_fallback(text)
101
+
102
+ enc = self.tokenizer(
103
+ self.build_prompt(text), return_tensors="pt", add_special_tokens=False
104
+ ).to(self.model.device)
105
+
106
+ out = self.model.generate(
107
+ **enc,
108
+ max_new_tokens=max_new_tokens,
109
+ do_sample=True,
110
+ temperature=temperature,
111
+ top_p=top_p,
112
+ repetition_penalty=1.05,
113
+ use_cache=True,
114
+ pad_token_id=self.tokenizer.pad_token_id,
115
+ )
116
+ return self.tokenizer.decode(
117
+ out[0][enc["input_ids"].shape[1] :], skip_special_tokens=True
118
+ ).strip()
119
+
120
+
121
+ def quick_humanize_fallback(text: str) -> str:
122
+ """Rule-based humanization when the trained model isn't available."""
123
+ text = text.strip()
124
+
125
+ # AI-style phrases -> human
126
+ text = re.sub(r"\bIt is important to note that\b", "Note that", text, flags=re.IGNORECASE)
127
+ text = re.sub(r"\bIn order to\b", "To", text, flags=re.IGNORECASE)
128
+ text = re.sub(r"\bDue to the fact that\b", "Because", text, flags=re.IGNORECASE)
129
+ text = re.sub(r"\bFurthermore,?\b", "Also", text, flags=re.IGNORECASE)
130
+ text = re.sub(r"\bHowever,?\b", "But", text, flags=re.IGNORECASE)
131
+ text = re.sub(r"\bIt should be noted that\b", "Note that", text, flags=re.IGNORECASE)
132
+
133
+ def smart(old, new):
134
+ return re.sub(r"\b" + old + r"\b", new, text, flags=re.IGNORECASE)
135
+
136
+ text = smart("Cannot", "Can't")
137
+ text = smart("Will not", "Won't")
138
+ text = smart("Do not", "Don't")
139
+ text = smart("Did not", "Didn't")
140
+ text = smart("It is clear that", "It's clear that")
141
+ text = smart("I think that", "I think")
142
+
143
+ text = re.sub(r"\s+([,.!?;:])", r"\1", text)
144
+ return " ".join(text.split())
145
+
146
+
147
+ if __name__ == "__main__":
148
+ m = HumanizeModel()
149
+ demo = ("In order to provide you with the most comprehensive solution to your query, "
150
+ "it is important to note that we have conducted extensive research on this topic.")
151
+ print("IN :", demo)
152
+ print("OUT:", m.humanize(demo))
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ # humanize-e: inference requirements
2
+ torch>=2.0.0; platform_system != "Darwin"
3
+ torch>=2.0.0; platform_machine == "arm64" # macOS M1+
4
+ transformers>=4.40.0
5
+ peft>=0.8.0
6
+ bitsandbytes>=0.43.0
7
+ fastapi>=0.100.0
8
+ uvicorn[standard]>=0.23.0
9
+ python-dotenv>=1.0.0
tests/__init__.py ADDED
File without changes
tests/test_app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Unit tests for app.py endpoints."""
2
+ import os
3
+ import sys
4
+ import unittest
5
+
6
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
7
+
8
+ from fastapi.testclient import TestClient
9
+ from app import app
10
+
11
+
12
+ class TestAPIEndpoints(unittest.TestCase):
13
+ @classmethod
14
+ def setUpClass(cls):
15
+ cls.client = TestClient(app)
16
+ # Trigger lifespan
17
+ cls.client.__enter__()
18
+
19
+ @classmethod
20
+ def tearDownClass(cls):
21
+ cls.client.__exit__(None, None, None)
22
+
23
+ def test_health_endpoint(self):
24
+ """Health check should return ok status."""
25
+ response = self.client.get("/health")
26
+ self.assertEqual(response.status_code, 200)
27
+
28
+ data = response.json()
29
+ self.assertEqual(data["status"], "ok")
30
+ self.assertIn("model", data)
31
+ self.assertIn(data["model"], ["qwen2.5-3b-lora", "fallback"])
32
+
33
+ def test_humanize_endpoint_fallback(self):
34
+ """Humanize endpoint should work in fallback mode."""
35
+ response = self.client.post(
36
+ "/humanize",
37
+ json={"text": "In order to provide you with results, it is important to note that data was collected."}
38
+ )
39
+ self.assertEqual(response.status_code, 200)
40
+
41
+ data = response.json()
42
+ self.assertIn("original", data)
43
+ self.assertIn("humanized", data)
44
+ self.assertIn("model", data)
45
+ self.assertEqual(data["model"], "fallback")
46
+ self.assertNotEqual(data["original"], data["humanized"])
47
+
48
+ def test_humanize_empty_text(self):
49
+ """Empty text should return 400."""
50
+ response = self.client.post("/humanize", json={"text": " "})
51
+ self.assertEqual(response.status_code, 400)
52
+
53
+ def test_humanize_missing_text(self):
54
+ """Missing text field should return 422 validation error."""
55
+ response = self.client.post("/humanize", json={})
56
+ self.assertEqual(response.status_code, 422)
57
+
58
+ def test_root_endpoint(self):
59
+ """Root should serve HTML UI."""
60
+ response = self.client.get("/")
61
+ self.assertEqual(response.status_code, 200)
62
+ self.assertIn("text/html", response.headers["content-type"])
63
+ self.assertIn("humanize-e", response.text)
64
+
65
+
66
+ if __name__ == '__main__':
67
+ unittest.main()
tests/test_model.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Unit tests for model.py"""
2
+ import os
3
+ import sys
4
+ import unittest
5
+ from unittest.mock import patch, MagicMock
6
+
7
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
8
+
9
+ from model import HumanizeModel, quick_humanize_fallback
10
+
11
+
12
+ class TestHumanizeModel(unittest.TestCase):
13
+ def test_init_without_adapter(self):
14
+ """Model should load in fallback mode when adapter not found."""
15
+ with patch('model.HumanizeModel._adapter_exists', return_value=False):
16
+ model = HumanizeModel()
17
+ self.assertFalse(model._loaded)
18
+ self.assertIsNone(model.model)
19
+ self.assertIsNone(model.tokenizer)
20
+
21
+ def test_fallback_humanize(self):
22
+ """Rule-based fallback should return human-like text."""
23
+ input_text = "In order to provide you with the most comprehensive solution, it is important to note that we have conducted extensive research."
24
+ result = quick_humanize_fallback(input_text)
25
+
26
+ self.assertIsInstance(result, str)
27
+ self.assertIn("To", result)
28
+ self.assertNotIn("In order to", result)
29
+ self.assertNotIn("it is important to note that", result.lower())
30
+
31
+ def test_fallback_preserves_meaning(self):
32
+ """Fallback should keep core content."""
33
+ input_text = "The model cannot process the data."
34
+ result = quick_humanize_fallback(input_text)
35
+
36
+ self.assertIn("Can't", result)
37
+ self.assertIn("model", result.lower())
38
+ self.assertIn("data", result.lower())
39
+
40
+ def test_fallback_empty_string(self):
41
+ """Fallback should handle empty strings."""
42
+ result = quick_humanize_fallback("")
43
+ self.assertEqual(result, "")
44
+
45
+ def test_build_prompt_requires_tokenizer(self):
46
+ """build_prompt requires loaded tokenizer."""
47
+ model = HumanizeModel()
48
+ model._loaded = False
49
+ # Should not raise when tokenizer is None, but build_prompt is not called in fallback
50
+ self.assertFalse(model._loaded)
51
+
52
+
53
+ if __name__ == '__main__':
54
+ unittest.main()
upload_adapter_to_hf.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Upload humanize-e LoRA adapter to HuggingFace Hub.
4
+ Requires HF_TOKEN env var or huggingface-cli login.
5
+ """
6
+ import os
7
+ from pathlib import Path
8
+ from huggingface_hub import HfApi, upload_folder
9
+
10
+ REPO_ID = os.getenv("HF_REPO_ID", "mike-elistra/humanize-e-adapter")
11
+ ADAPTER_DIR = os.getenv("HUMANIZE_ADAPTER", "./humanize-e-adapter")
12
+
13
+ def main():
14
+ adapter_path = Path(ADAPTER_DIR)
15
+ if not adapter_path.exists() or not any(adapter_path.iterdir()):
16
+ print(f"Adapter directory {adapter_path} is empty or missing. Nothing to upload.")
17
+ return
18
+
19
+ print(f"Uploading {adapter_path} to {REPO_ID} ...")
20
+ upload_folder(
21
+ folder_path=str(adapter_path),
22
+ repo_id=REPO_ID,
23
+ repo_type="model",
24
+ commit_message="Upload humanize-e LoRA adapter",
25
+ )
26
+ print("Upload complete.")
27
+ print(f"Run with: HUMANIZE_ADAPTER={REPO_ID} python app.py")
28
+
29
+ if __name__ == "__main__":
30
+ main()