Update app.py
Browse files
app.py
CHANGED
|
@@ -1,146 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from llama_cpp import Llama
|
| 5 |
-
import re
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
verbose=False,
|
| 20 |
)
|
| 21 |
print("Model loaded successfully.")
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
app.add_middleware(
|
| 26 |
CORSMiddleware,
|
| 27 |
-
allow_origins=["*"],
|
| 28 |
allow_methods=["*"],
|
| 29 |
allow_headers=["*"],
|
| 30 |
)
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
text: str
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
# Remove common ending tokens and system tags
|
| 38 |
-
clean_text = re.sub(r'\[/?(SYSTEM|USER|ASSISTANT)\]', '', text)
|
| 39 |
-
clean_text = re.sub(r'</?s>', '', clean_text)
|
| 40 |
-
clean_text = re.sub(r'\s+', ' ', clean_text) # Normalize whitespace
|
| 41 |
-
clean_text = clean_text.strip()
|
| 42 |
-
|
| 43 |
-
# Remove the original text if it's repeated
|
| 44 |
-
lines = clean_text.split('\n')
|
| 45 |
-
if len(lines) > 1:
|
| 46 |
-
# Take the most human-like line (usually the last one)
|
| 47 |
-
clean_text = lines[-1].strip()
|
| 48 |
-
|
| 49 |
-
return clean_text
|
| 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 |
max_tokens=512,
|
| 89 |
-
temperature=
|
| 90 |
-
top_p=0.9,
|
| 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 |
-
return {"result": simple_humanized}
|
| 143 |
|
| 144 |
@app.get("/")
|
| 145 |
def health():
|
| 146 |
-
return {"status": "ok", "model":
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import asyncio
|
| 3 |
+
import re
|
| 4 |
+
from typing import Literal
|
| 5 |
from fastapi import FastAPI, HTTPException
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from pydantic import BaseModel
|
| 8 |
from llama_cpp import Llama
|
|
|
|
| 9 |
|
| 10 |
+
# ---------------- MODEL CONFIG ---------------- #
|
| 11 |
+
|
| 12 |
+
# IMPORTANT: For HuggingFace Spaces, the model file is inside the repo folder
|
| 13 |
+
MODEL_PATH = "model/Phi-3.1-mini-4k-instruct-IQ2_M.gguf"
|
| 14 |
+
|
| 15 |
+
# CPU settings for llama.cpp
|
| 16 |
+
N_THREADS = 4
|
| 17 |
+
N_CTX = 4096
|
| 18 |
+
N_BATCH = 512
|
| 19 |
+
N_GPU_LAYERS = 0
|
| 20 |
+
|
| 21 |
+
# Concurrency limit
|
| 22 |
+
MAX_CONCURRENT_REQUESTS = 6
|
| 23 |
+
|
| 24 |
+
# Unique token to force controlled stopping
|
| 25 |
+
END_TOKEN = "###END_OF_RESPONSE###"
|
| 26 |
+
|
| 27 |
+
print("Loading model:", MODEL_PATH)
|
| 28 |
+
llm = Llama(
|
| 29 |
+
model_path=MODEL_PATH,
|
| 30 |
+
n_threads=N_THREADS,
|
| 31 |
+
n_ctx=N_CTX,
|
| 32 |
+
n_batch=N_BATCH,
|
| 33 |
+
n_gpu_layers=N_GPU_LAYERS,
|
| 34 |
verbose=False,
|
| 35 |
)
|
| 36 |
print("Model loaded successfully.")
|
| 37 |
|
| 38 |
+
# ---------------- FASTAPI APP ---------------- #
|
| 39 |
+
|
| 40 |
+
app = FastAPI(title="FormatAI Humanizer Backend")
|
| 41 |
|
| 42 |
app.add_middleware(
|
| 43 |
CORSMiddleware,
|
| 44 |
+
allow_origins=["*"], # allow all origins (Vercel frontend)
|
| 45 |
allow_methods=["*"],
|
| 46 |
allow_headers=["*"],
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# ---------------- REQUEST MODELS ---------------- #
|
| 50 |
+
|
| 51 |
+
class TransformRequest(BaseModel):
|
| 52 |
text: str
|
| 53 |
+
style: Literal["professional", "casual", "academic", "marketing"]
|
| 54 |
|
| 55 |
+
class HumanizeRequest(BaseModel): # legacy
|
| 56 |
+
text: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
|
| 59 |
+
# ---------------- STYLE PROMPTS ---------------- #
|
| 60 |
+
|
| 61 |
+
STYLE_PROMPTS = {
|
| 62 |
+
"professional": (
|
| 63 |
+
"STYLE: PROFESSIONAL\n"
|
| 64 |
+
"Rewrite the user's text in a STRICTLY professional, corporate, formal tone. "
|
| 65 |
+
"Use respectful and clear business language. Do NOT add explanations. Output ONLY the rewritten text, "
|
| 66 |
+
f"then write {END_TOKEN}."
|
| 67 |
+
),
|
| 68 |
+
|
| 69 |
+
"casual": (
|
| 70 |
+
"STYLE: CASUAL\n"
|
| 71 |
+
"Rewrite the user's text in a friendly, natural, conversational tone. Use contractions and human-like flow. "
|
| 72 |
+
"Do NOT add explanations. Output ONLY the rewritten text, then write "
|
| 73 |
+
f"{END_TOKEN}."
|
| 74 |
+
),
|
| 75 |
+
|
| 76 |
+
"academic": (
|
| 77 |
+
"STYLE: ACADEMIC\n"
|
| 78 |
+
"Rewrite the user's text in formal academic language suitable for scholarly work. "
|
| 79 |
+
"Use precise and objective vocabulary. Do NOT add explanations. Output ONLY the rewritten text, "
|
| 80 |
+
f"then write {END_TOKEN}."
|
| 81 |
+
),
|
| 82 |
+
|
| 83 |
+
"marketing": (
|
| 84 |
+
"STYLE: MARKETING\n"
|
| 85 |
+
"Rewrite the user's text in persuasive, benefit-focused marketing copy. "
|
| 86 |
+
"Use strong emotional hooks and punchy messaging. Do NOT add explanations. Output ONLY the rewritten text, "
|
| 87 |
+
f"then write {END_TOKEN}."
|
| 88 |
+
),
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
# ---------------- HELPERS ---------------- #
|
| 93 |
+
|
| 94 |
+
def clean_output(raw: str) -> str:
|
| 95 |
+
"""Strip junk tokens and trim to final output."""
|
| 96 |
+
if not raw:
|
| 97 |
+
return ""
|
| 98 |
+
|
| 99 |
+
# Remove system markers
|
| 100 |
+
raw = re.sub(r"<\|/?(system|assistant|user|end)\|>", "", raw, flags=re.I)
|
| 101 |
+
|
| 102 |
+
# Stop at END_TOKEN
|
| 103 |
+
if END_TOKEN in raw:
|
| 104 |
+
raw = raw.split(END_TOKEN)[0]
|
| 105 |
+
|
| 106 |
+
raw = raw.strip()
|
| 107 |
+
raw = re.sub(r"[ \t]+", " ", raw)
|
| 108 |
+
return raw.strip()
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def build_prompt(text: str, style: str) -> str:
|
| 112 |
+
"""Create strict prompt for selected style."""
|
| 113 |
+
system = STYLE_PROMPTS[style]
|
| 114 |
+
|
| 115 |
+
return (
|
| 116 |
+
f"<|system|>\n{system}\n\n"
|
| 117 |
+
f"<|user|>\n{text}\n\n"
|
| 118 |
+
f"<|assistant|>\n"
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
# ---------------- MODEL CALL ---------------- #
|
| 123 |
+
|
| 124 |
+
async def call_llm(prompt: str, temperature: float = 0.25):
|
| 125 |
+
loop = asyncio.get_event_loop()
|
| 126 |
+
|
| 127 |
+
def sync_call():
|
| 128 |
+
return llm(
|
| 129 |
+
prompt,
|
| 130 |
max_tokens=512,
|
| 131 |
+
temperature=temperature,
|
| 132 |
+
top_p=0.9,
|
| 133 |
+
top_k=40,
|
| 134 |
+
repeat_penalty=1.1,
|
| 135 |
+
stop=[END_TOKEN],
|
| 136 |
+
echo=False,
|
| 137 |
)
|
| 138 |
+
|
| 139 |
+
out = await loop.run_in_executor(None, sync_call)
|
| 140 |
+
|
| 141 |
+
if "choices" in out:
|
| 142 |
+
text = out["choices"][0].get("text", "")
|
| 143 |
+
else:
|
| 144 |
+
text = str(out)
|
| 145 |
+
|
| 146 |
+
return clean_output(text)
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
# ---------------- ENDPOINT: /api/transform ---------------- #
|
| 150 |
+
|
| 151 |
+
@app.post("/api/transform")
|
| 152 |
+
async def transform(req: TransformRequest):
|
| 153 |
+
text = req.text.strip()
|
| 154 |
+
if not text:
|
| 155 |
+
raise HTTPException(400, "Text cannot be empty")
|
| 156 |
+
|
| 157 |
+
if req.style not in STYLE_PROMPTS:
|
| 158 |
+
raise HTTPException(400, "Invalid style")
|
| 159 |
+
|
| 160 |
+
# More creativity for marketing
|
| 161 |
+
temperature = 0.65 if req.style == "marketing" else 0.25
|
| 162 |
+
|
| 163 |
+
prompt = build_prompt(text, req.style)
|
| 164 |
+
transformed = await call_llm(prompt, temperature=temperature)
|
| 165 |
+
|
| 166 |
+
return {
|
| 167 |
+
"original": text,
|
| 168 |
+
"transformed": transformed,
|
| 169 |
+
"style": req.style
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
# ---------------- LEGACY ENDPOINT: /api/humanize ---------------- #
|
| 174 |
+
|
| 175 |
+
@app.post("/api/humanize")
|
| 176 |
+
async def humanize(req: HumanizeRequest):
|
| 177 |
+
"""Old endpoint - always uses casual."""
|
| 178 |
+
prompt = build_prompt(req.text.strip(), "casual")
|
| 179 |
+
out = await call_llm(prompt, temperature=0.4)
|
| 180 |
+
return {"result": out}
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
# ---------------- HEALTH CHECK ---------------- #
|
|
|
|
| 184 |
|
| 185 |
@app.get("/")
|
| 186 |
def health():
|
| 187 |
+
return {"status": "ok", "model": MODEL_PATH}
|