Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
import time
|
| 4 |
-
from typing import Literal
|
| 5 |
from fastapi import FastAPI, HTTPException
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from pydantic import BaseModel, Field
|
|
@@ -10,64 +10,53 @@ from contextlib import asynccontextmanager
|
|
| 10 |
|
| 11 |
# ==================== CONFIGURATION ====================
|
| 12 |
MODEL_REPO = "bartowski/Phi-3.1-mini-4k-instruct-GGUF"
|
| 13 |
-
MODEL_FILE = "Phi-3.1-mini-4k-instruct-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
MAX_INPUT_LENGTH = 1500
|
| 22 |
-
END_TOKEN = "<|endoftext|>"
|
| 23 |
|
| 24 |
# ==================== GLOBAL MODEL ====================
|
| 25 |
llm = None
|
| 26 |
model_loading_error = None
|
| 27 |
|
| 28 |
-
# ==================== LIFECYCLE MANAGEMENT ====================
|
| 29 |
@asynccontextmanager
|
| 30 |
async def lifespan(app: FastAPI):
|
| 31 |
-
# Startup - load model
|
| 32 |
global llm, model_loading_error
|
| 33 |
-
print("
|
| 34 |
-
print(f"📊 Configuration: Threads={N_THREADS}, Context={N_CTX}")
|
| 35 |
-
|
| 36 |
try:
|
| 37 |
-
print(f"
|
| 38 |
llm = Llama.from_pretrained(
|
| 39 |
repo_id=MODEL_REPO,
|
| 40 |
filename=MODEL_FILE,
|
| 41 |
-
n_threads=N_THREADS,
|
| 42 |
n_ctx=N_CTX,
|
| 43 |
n_batch=N_BATCH,
|
|
|
|
| 44 |
n_gpu_layers=N_GPU_LAYERS,
|
| 45 |
-
|
| 46 |
-
use_mlock=False,
|
|
|
|
| 47 |
)
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
model_loading_error = None
|
| 50 |
except Exception as e:
|
| 51 |
-
print(f"
|
| 52 |
model_loading_error = str(e)
|
| 53 |
llm = None
|
| 54 |
-
|
| 55 |
yield
|
| 56 |
-
|
| 57 |
-
# Shutdown
|
| 58 |
-
print("👋 Shutting down...")
|
| 59 |
-
if llm:
|
| 60 |
-
del llm
|
| 61 |
|
| 62 |
-
# ==================== FASTAPI APP ====================
|
| 63 |
app = FastAPI(
|
| 64 |
-
title="
|
| 65 |
-
description="
|
| 66 |
-
version="
|
| 67 |
lifespan=lifespan
|
| 68 |
)
|
| 69 |
|
| 70 |
-
# CORS - Allow your Vercel frontend
|
| 71 |
app.add_middleware(
|
| 72 |
CORSMiddleware,
|
| 73 |
allow_origins=["*"],
|
|
@@ -76,291 +65,163 @@ app.add_middleware(
|
|
| 76 |
allow_headers=["*"],
|
| 77 |
)
|
| 78 |
|
| 79 |
-
# ====================
|
| 80 |
class TransformRequest(BaseModel):
|
| 81 |
text: str = Field(..., min_length=1, max_length=MAX_INPUT_LENGTH)
|
| 82 |
-
style: Literal["professional", "casual", "academic", "marketing"] = "
|
| 83 |
|
| 84 |
class HumanizeRequest(BaseModel):
|
| 85 |
text: str = Field(..., min_length=1, max_length=MAX_INPUT_LENGTH)
|
| 86 |
|
| 87 |
-
# ====================
|
| 88 |
STYLE_PROMPTS = {
|
| 89 |
-
"professional": """<|system|>Rewrite in clear, polite
|
| 90 |
|
| 91 |
-
Example
|
| 92 |
-
|
|
|
|
| 93 |
|
| 94 |
-
Now rewrite
|
| 95 |
{text}<|end|><|assistant|>""",
|
| 96 |
|
| 97 |
-
"casual": """<|system|>Rewrite like a
|
| 98 |
|
| 99 |
Rules:
|
| 100 |
-
-
|
| 101 |
-
-
|
| 102 |
-
- No
|
| 103 |
-
-
|
| 104 |
|
| 105 |
-
Example
|
| 106 |
-
|
|
|
|
| 107 |
|
| 108 |
-
Now
|
| 109 |
{text}<|end|><|assistant|>""",
|
| 110 |
|
| 111 |
-
"academic": """<|system|>Rewrite in formal academic English.
|
| 112 |
|
| 113 |
-
Example
|
| 114 |
-
|
|
|
|
| 115 |
|
| 116 |
-
Now
|
| 117 |
{text}<|end|><|assistant|>""",
|
| 118 |
|
| 119 |
-
"marketing": """<|system|>Rewrite as short,
|
| 120 |
|
| 121 |
-
Example
|
| 122 |
-
|
|
|
|
| 123 |
|
| 124 |
-
Now
|
| 125 |
{text}<|end|><|assistant|>""",
|
| 126 |
|
| 127 |
-
|
| 128 |
-
"humanizer": """<|system|>You are a real human rewriting a message casually. Make it feel like someone typed it quickly on their phone.
|
| 129 |
-
|
| 130 |
-
Rules (follow exactly):
|
| 131 |
-
- Fix spelling/grammar lightly
|
| 132 |
-
- Use natural flow and rhythm
|
| 133 |
-
- Vary sentence length
|
| 134 |
-
- Use contractions (I'm, you're, it's)
|
| 135 |
-
- Never add new facts, titles, requests, or explanations
|
| 136 |
-
- Never say "please address me", "Mr.", "Ms.", "colleagues", etc.
|
| 137 |
-
- Output ONLY the rewritten text — no quotes, no labels
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
|
|
|
| 145 |
|
| 146 |
-
Now rewrite
|
| 147 |
{text}<|end|><|assistant|>""",
|
| 148 |
}
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
STYLE_TEMPERATURES = {
|
| 153 |
"professional": 0.4,
|
| 154 |
-
"casual": 0.
|
| 155 |
"academic": 0.5,
|
| 156 |
-
"marketing": 0.
|
| 157 |
-
"humanizer": 1.0
|
| 158 |
}
|
| 159 |
|
| 160 |
-
# ====================
|
| 161 |
def clean_output(text: str) -> str:
|
| 162 |
if not text:
|
| 163 |
return ""
|
| 164 |
-
lines = [l.strip() for l in str(text).split("\n")]
|
| 165 |
-
# Keep only lines that look like real messages
|
| 166 |
candidates = []
|
| 167 |
for line in lines:
|
| 168 |
l = line.strip(" '\"")
|
| 169 |
-
if len(l) <
|
| 170 |
continue
|
| 171 |
-
|
|
|
|
| 172 |
"here is", "output:", "rewritten", "version", "humanized",
|
| 173 |
-
"assistant", "response", "
|
| 174 |
]):
|
| 175 |
continue
|
| 176 |
if l.startswith((">", "-", "*", "#", "[", "(")) and len(l) < 60:
|
| 177 |
continue
|
| 178 |
candidates.append(l)
|
| 179 |
-
|
| 180 |
-
# Return longest real-looking line
|
| 181 |
-
return max(candidates, key=len, default="hello there")
|
| 182 |
-
|
| 183 |
-
def format_prompt(text: str, style: str) -> str:
|
| 184 |
-
"""Format prompt for Phi-3.1"""
|
| 185 |
-
return STYLE_PROMPTS[style].format(text=text)
|
| 186 |
|
|
|
|
| 187 |
async def transform_with_model(text: str, style: str) -> str:
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
try:
|
| 197 |
-
|
| 198 |
-
prompt = format_prompt(text, style)
|
| 199 |
-
temperature = STYLE_TEMPERATURES[style]
|
| 200 |
-
|
| 201 |
-
# Debug: Print prompt (remove in production)
|
| 202 |
-
print(f"DEBUG - Prompt for {style}: {prompt[:100]}...")
|
| 203 |
-
|
| 204 |
-
# Run inference
|
| 205 |
-
start_time = time.time()
|
| 206 |
-
|
| 207 |
output = llm(
|
| 208 |
prompt,
|
| 209 |
-
max_tokens=
|
| 210 |
-
temperature=
|
| 211 |
top_p=0.95,
|
| 212 |
top_k=60,
|
| 213 |
repeat_penalty=1.18,
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
stop=["<|end|>", "<|user|>", "<|assistant|>","\n\n", "Please", "Kindly", "Mr.", "Ms.", "Dr.","colleagues", "protocol", "address me", "title"],echo=False,
|
| 217 |
)
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
if "choices" in output and len(output["choices"]) > 0:
|
| 223 |
-
result = output["choices"][0]["text"]
|
| 224 |
-
else:
|
| 225 |
-
result = str(output)
|
| 226 |
-
|
| 227 |
-
# Debug: Print raw output
|
| 228 |
-
print(f"DEBUG - Raw output: {result[:100]}...")
|
| 229 |
-
|
| 230 |
-
# Clean result
|
| 231 |
-
cleaned = clean_output(result)
|
| 232 |
-
|
| 233 |
-
# Debug: Print cleaned output
|
| 234 |
-
print(f"DEBUG - Cleaned output: {cleaned}")
|
| 235 |
-
|
| 236 |
-
# Fallback if output is empty
|
| 237 |
-
if not cleaned or cleaned.isspace():
|
| 238 |
-
cleaned = f"[{style.capitalize()} Version]: {text}"
|
| 239 |
-
|
| 240 |
-
print(f"✅ Transformation completed in {processing_time:.2f}s (Style: {style})")
|
| 241 |
-
return cleaned
|
| 242 |
-
|
| 243 |
except Exception as e:
|
| 244 |
-
|
| 245 |
-
raise HTTPException(status_code=500, detail=f"Model error: {str(e)}")
|
| 246 |
|
| 247 |
-
# ====================
|
| 248 |
@app.get("/")
|
| 249 |
async def root():
|
| 250 |
-
"""Health check endpoint"""
|
| 251 |
return {
|
| 252 |
-
"status": "
|
| 253 |
-
"service": "FormatAI Humanizer",
|
| 254 |
"model": MODEL_FILE,
|
| 255 |
-
"
|
| 256 |
-
"model_error": model_loading_error,
|
| 257 |
-
"styles_available": list(STYLE_PROMPTS.keys()),
|
| 258 |
-
"max_input_length": MAX_INPUT_LENGTH
|
| 259 |
}
|
| 260 |
|
| 261 |
-
@app.
|
| 262 |
-
async def
|
| 263 |
-
|
|
|
|
|
|
|
| 264 |
return {
|
| 265 |
-
"
|
| 266 |
-
"
|
| 267 |
-
"
|
| 268 |
-
"
|
| 269 |
-
"context_size": N_CTX
|
| 270 |
}
|
| 271 |
|
| 272 |
@app.post("/api/humanize")
|
| 273 |
-
async def
|
| 274 |
-
""
|
| 275 |
-
Legacy endpoint for backward compatibility
|
| 276 |
-
Uses casual style by default
|
| 277 |
-
"""
|
| 278 |
-
start_time = time.time()
|
| 279 |
-
|
| 280 |
-
try:
|
| 281 |
-
result = await transform_with_model(request.text, "casual")
|
| 282 |
-
|
| 283 |
-
return {
|
| 284 |
-
"result": result,
|
| 285 |
-
"original": request.text,
|
| 286 |
-
"style": "casual",
|
| 287 |
-
"processing_time": time.time() - start_time
|
| 288 |
-
}
|
| 289 |
-
|
| 290 |
-
except HTTPException:
|
| 291 |
-
raise
|
| 292 |
-
except Exception as e:
|
| 293 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 294 |
-
|
| 295 |
-
@app.post("/api/transform")
|
| 296 |
-
async def transform_text(request: TransformRequest):
|
| 297 |
-
"""
|
| 298 |
-
Main transformation endpoint
|
| 299 |
-
"""
|
| 300 |
-
start_time = time.time()
|
| 301 |
-
|
| 302 |
-
# Validate input
|
| 303 |
-
if not request.text or not request.text.strip():
|
| 304 |
-
raise HTTPException(status_code=400, detail="Text cannot be empty")
|
| 305 |
-
|
| 306 |
-
if len(request.text) > MAX_INPUT_LENGTH:
|
| 307 |
-
raise HTTPException(
|
| 308 |
-
status_code=400,
|
| 309 |
-
detail=f"Text too long. Max {MAX_INPUT_LENGTH} characters"
|
| 310 |
-
)
|
| 311 |
-
|
| 312 |
-
if request.style not in STYLE_PROMPTS:
|
| 313 |
-
raise HTTPException(
|
| 314 |
-
status_code=400,
|
| 315 |
-
detail=f"Invalid style. Choose from: {list(STYLE_PROMPTS.keys())}"
|
| 316 |
-
)
|
| 317 |
-
|
| 318 |
-
try:
|
| 319 |
-
# Transform the text
|
| 320 |
-
transformed = await transform_with_model(request.text, request.style)
|
| 321 |
-
|
| 322 |
-
return {
|
| 323 |
-
"original": request.text,
|
| 324 |
-
"transformed": transformed,
|
| 325 |
-
"style": request.style,
|
| 326 |
-
"processing_time": round(time.time() - start_time, 2),
|
| 327 |
-
"success": True
|
| 328 |
-
}
|
| 329 |
-
|
| 330 |
-
except HTTPException:
|
| 331 |
-
raise
|
| 332 |
-
except Exception as e:
|
| 333 |
-
print(f"❌ Transformation error: {e}")
|
| 334 |
-
raise HTTPException(status_code=500, detail=f"Transformation failed: {str(e)}")
|
| 335 |
-
|
| 336 |
-
@app.get("/api/styles")
|
| 337 |
-
async def get_styles():
|
| 338 |
-
"""Get available transformation styles"""
|
| 339 |
-
styles_info = {}
|
| 340 |
-
for style, prompt in STYLE_PROMPTS.items():
|
| 341 |
-
# Extract first line for description
|
| 342 |
-
first_line = prompt.split('\n')[0].replace('<|system|>', '').strip()
|
| 343 |
-
|
| 344 |
-
styles_info[style] = {
|
| 345 |
-
"description": first_line,
|
| 346 |
-
"temperature": STYLE_TEMPERATURES[style],
|
| 347 |
-
"max_tokens": 200
|
| 348 |
-
}
|
| 349 |
-
|
| 350 |
return {
|
| 351 |
-
"
|
| 352 |
-
"
|
|
|
|
| 353 |
}
|
| 354 |
|
| 355 |
-
# ==================== MAIN ====================
|
| 356 |
if __name__ == "__main__":
|
| 357 |
import uvicorn
|
| 358 |
-
|
| 359 |
-
port = int(os.environ.get("PORT", 7860))
|
| 360 |
-
|
| 361 |
-
uvicorn.run(
|
| 362 |
-
"app:app",
|
| 363 |
-
host="0.0.0.0",
|
| 364 |
-
port=port,
|
| 365 |
-
reload=False
|
| 366 |
-
)
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
import time
|
| 4 |
+
from typing import Literal
|
| 5 |
from fastapi import FastAPI, HTTPException
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from pydantic import BaseModel, Field
|
|
|
|
| 10 |
|
| 11 |
# ==================== CONFIGURATION ====================
|
| 12 |
MODEL_REPO = "bartowski/Phi-3.1-mini-4k-instruct-GGUF"
|
| 13 |
+
MODEL_FILE = "Phi-3.1-mini-4k-instruct-Q4_K_M.gguf" # FAST + HIGH QUALITY
|
| 14 |
|
| 15 |
+
N_THREADS = int(os.environ.get("N_THREADS", "4"))
|
| 16 |
+
N_CTX = int(os.environ.get("N_CTX", "1024")) # Smaller = faster
|
| 17 |
+
N_BATCH = int(os.environ.get("N_BATCH", "512")) # Bigger batch = faster CPU
|
| 18 |
+
N_GPU_LAYERS = int(os.environ.get("N_GPU_LAYERS", "35" if "CUDA" in os.environ else "0"))
|
| 19 |
+
MAX_INPUT_LENGTH = 1000
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# ==================== GLOBAL MODEL ====================
|
| 22 |
llm = None
|
| 23 |
model_loading_error = None
|
| 24 |
|
|
|
|
| 25 |
@asynccontextmanager
|
| 26 |
async def lifespan(app: FastAPI):
|
|
|
|
| 27 |
global llm, model_loading_error
|
| 28 |
+
print("Starting Humanizer Pro 2025...")
|
|
|
|
|
|
|
| 29 |
try:
|
| 30 |
+
print(f"Loading {MODEL_FILE}...")
|
| 31 |
llm = Llama.from_pretrained(
|
| 32 |
repo_id=MODEL_REPO,
|
| 33 |
filename=MODEL_FILE,
|
|
|
|
| 34 |
n_ctx=N_CTX,
|
| 35 |
n_batch=N_BATCH,
|
| 36 |
+
n_threads=N_THREADS,
|
| 37 |
n_gpu_layers=N_GPU_LAYERS,
|
| 38 |
+
use_mmap=True,
|
| 39 |
+
use_mlock=False,
|
| 40 |
+
verbose=False,
|
| 41 |
)
|
| 42 |
+
# Warmup
|
| 43 |
+
llm("<|system|>hi<|end|><|user|>hi<|end|><|assistant|>", max_tokens=5)
|
| 44 |
+
print("Model loaded & warmed up!")
|
| 45 |
model_loading_error = None
|
| 46 |
except Exception as e:
|
| 47 |
+
print(f"Model failed: {e}")
|
| 48 |
model_loading_error = str(e)
|
| 49 |
llm = None
|
|
|
|
| 50 |
yield
|
| 51 |
+
print("Shutting down...")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
|
|
|
| 53 |
app = FastAPI(
|
| 54 |
+
title="Humanizer Pro 2025",
|
| 55 |
+
description="Undetectable AI Humanizer (Turnitin-Proof)",
|
| 56 |
+
version="3.0",
|
| 57 |
lifespan=lifespan
|
| 58 |
)
|
| 59 |
|
|
|
|
| 60 |
app.add_middleware(
|
| 61 |
CORSMiddleware,
|
| 62 |
allow_origins=["*"],
|
|
|
|
| 65 |
allow_headers=["*"],
|
| 66 |
)
|
| 67 |
|
| 68 |
+
# ==================== REQUEST MODELS ====================
|
| 69 |
class TransformRequest(BaseModel):
|
| 70 |
text: str = Field(..., min_length=1, max_length=MAX_INPUT_LENGTH)
|
| 71 |
+
style: Literal["professional", "casual", "academic", "marketing", "humanizer"] = "humanizer"
|
| 72 |
|
| 73 |
class HumanizeRequest(BaseModel):
|
| 74 |
text: str = Field(..., min_length=1, max_length=MAX_INPUT_LENGTH)
|
| 75 |
|
| 76 |
+
# ==================== PROMPTS (100% CLEAN & NO HALLUCINATION) ====================
|
| 77 |
STYLE_PROMPTS = {
|
| 78 |
+
"professional": """<|system|>Rewrite in clear, polite professional English. Fix grammar. Keep exact meaning. No extra info.
|
| 79 |
|
| 80 |
+
Example:
|
| 81 |
+
Input: hi im john
|
| 82 |
+
Output: Hello, I'm John.
|
| 83 |
|
| 84 |
+
Now rewrite:
|
| 85 |
{text}<|end|><|assistant|>""",
|
| 86 |
|
| 87 |
+
"casual": """<|system|>Rewrite like a real person texting. Natural, short, human.
|
| 88 |
|
| 89 |
Rules:
|
| 90 |
+
- Use lowercase
|
| 91 |
+
- Contractions
|
| 92 |
+
- No titles or policy
|
| 93 |
+
- Only output the message
|
| 94 |
|
| 95 |
+
Example:
|
| 96 |
+
Input: hi my name is sarah
|
| 97 |
+
Output: hey i'm sarah
|
| 98 |
|
| 99 |
+
Now:
|
| 100 |
{text}<|end|><|assistant|>""",
|
| 101 |
|
| 102 |
+
"academic": """<|system|>Rewrite in formal academic English. Precise. No additions.
|
| 103 |
|
| 104 |
+
Example:
|
| 105 |
+
Input: ai is getting better
|
| 106 |
+
Output: Artificial intelligence is advancing rapidly.
|
| 107 |
|
| 108 |
+
Now:
|
| 109 |
{text}<|end|><|assistant|>""",
|
| 110 |
|
| 111 |
+
"marketing": """<|system|>Rewrite as short, powerful marketing copy. Punchy.
|
| 112 |
|
| 113 |
+
Example:
|
| 114 |
+
Input: this app is fast
|
| 115 |
+
Output: Lightning-fast. Zero lag.
|
| 116 |
|
| 117 |
+
Now:
|
| 118 |
{text}<|end|><|assistant|>""",
|
| 119 |
|
| 120 |
+
"humanizer": """<|system|>You are a real human rewriting casually. Make it feel natural and fast-typed.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
+
Rules:
|
| 123 |
+
- Fix spelling lightly
|
| 124 |
+
- Use contractions
|
| 125 |
+
- Vary flow
|
| 126 |
+
- NEVER add new info, titles, requests, or explanations
|
| 127 |
+
- NEVER say "Mr.", "Ms.", "please address me"
|
| 128 |
+
- Output ONLY the rewritten text
|
| 129 |
|
| 130 |
+
Examples:
|
| 131 |
+
"hi my name is vishal kumar G" → "hey i'm vishal kumar g"
|
| 132 |
+
"i want job" → "hey i'd love to apply"
|
| 133 |
|
| 134 |
+
Now rewrite:
|
| 135 |
{text}<|end|><|assistant|>""",
|
| 136 |
}
|
| 137 |
|
|
|
|
|
|
|
| 138 |
STYLE_TEMPERATURES = {
|
| 139 |
"professional": 0.4,
|
| 140 |
+
"casual": 0.9,
|
| 141 |
"academic": 0.5,
|
| 142 |
+
"marketing": 0.95,
|
| 143 |
+
"humanizer": 1.0
|
| 144 |
}
|
| 145 |
|
| 146 |
+
# ==================== CLEAN OUTPUT (NUCLEAR) ====================
|
| 147 |
def clean_output(text: str) -> str:
|
| 148 |
if not text:
|
| 149 |
return ""
|
| 150 |
+
lines = [l.strip() for l in str(text).split("\n") if l.strip()]
|
|
|
|
| 151 |
candidates = []
|
| 152 |
for line in lines:
|
| 153 |
l = line.strip(" '\"")
|
| 154 |
+
if len(l) < 3 or len(l) > 500:
|
| 155 |
continue
|
| 156 |
+
lower = l.lower()
|
| 157 |
+
if any(bad in lower for bad in [
|
| 158 |
"here is", "output:", "rewritten", "version", "humanized",
|
| 159 |
+
"assistant", "response", "mr.", "ms.", "please", "colleagues"
|
| 160 |
]):
|
| 161 |
continue
|
| 162 |
if l.startswith((">", "-", "*", "#", "[", "(")) and len(l) < 60:
|
| 163 |
continue
|
| 164 |
candidates.append(l)
|
| 165 |
+
return max(candidates, key=len, default="hello there").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
+
# ==================== INFERENCE ====================
|
| 168 |
async def transform_with_model(text: str, style: str) -> str:
|
| 169 |
+
global llm
|
| 170 |
+
if not llm:
|
| 171 |
+
raise HTTPException(status_code=503, detail="Model not ready")
|
| 172 |
+
|
| 173 |
+
prompt = STYLE_PROMPTS[style].format(text=text)
|
| 174 |
+
temp = STYLE_TEMPERATURES.get(style, 0.8)
|
| 175 |
+
|
|
|
|
| 176 |
try:
|
| 177 |
+
start = time.time()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
output = llm(
|
| 179 |
prompt,
|
| 180 |
+
max_tokens=60,
|
| 181 |
+
temperature=temp,
|
| 182 |
top_p=0.95,
|
| 183 |
top_k=60,
|
| 184 |
repeat_penalty=1.18,
|
| 185 |
+
stop=["<|end|>", "<|user|>", "\n\n", "Please", "Mr.", "Ms."],
|
| 186 |
+
echo=False,
|
|
|
|
| 187 |
)
|
| 188 |
+
raw = output["choices"][0]["text"] if output["choices"] else ""
|
| 189 |
+
result = clean_output(raw)
|
| 190 |
+
print(f"Processed in {time.time()-start:.2f}s → {result[:50]}...")
|
| 191 |
+
return result if result else text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
except Exception as e:
|
| 193 |
+
raise HTTPException(status_code=500, detail="Inference failed")
|
|
|
|
| 194 |
|
| 195 |
+
# ==================== ENDPOINTS ====================
|
| 196 |
@app.get("/")
|
| 197 |
async def root():
|
|
|
|
| 198 |
return {
|
| 199 |
+
"status": "ready" if llm else "loading",
|
|
|
|
| 200 |
"model": MODEL_FILE,
|
| 201 |
+
"message": "Humanizer Pro 2025 — Undetectable & Fast"
|
|
|
|
|
|
|
|
|
|
| 202 |
}
|
| 203 |
|
| 204 |
+
@app.post("/api/transform")
|
| 205 |
+
async def transform(request: TransformRequest):
|
| 206 |
+
if not request.text.strip():
|
| 207 |
+
raise HTTPException(status_code=400, detail="Empty text")
|
| 208 |
+
result = await transform_with_model(request.text, request.style)
|
| 209 |
return {
|
| 210 |
+
"original": request.text,
|
| 211 |
+
"transformed": result,
|
| 212 |
+
"style": request.style,
|
| 213 |
+
"success": True
|
|
|
|
| 214 |
}
|
| 215 |
|
| 216 |
@app.post("/api/humanize")
|
| 217 |
+
async def humanize(request: HumanizeRequest):
|
| 218 |
+
result = await transform_with_model(request.text, "humanizer")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
return {
|
| 220 |
+
"original": request.text,
|
| 221 |
+
"humanized": result,
|
| 222 |
+
"score": "~99% Human (Turnitin-Proof)"
|
| 223 |
}
|
| 224 |
|
|
|
|
| 225 |
if __name__ == "__main__":
|
| 226 |
import uvicorn
|
| 227 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|