Update app.py
Browse files
app.py
CHANGED
|
@@ -86,116 +86,99 @@ class HumanizeRequest(BaseModel):
|
|
| 86 |
|
| 87 |
# ==================== STYLE PROMPTS ====================
|
| 88 |
STYLE_PROMPTS = {
|
| 89 |
-
"professional": """<|system|>Rewrite
|
| 90 |
|
| 91 |
-
|
|
|
|
| 92 |
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
|
| 100 |
-
|
| 101 |
-
"hey man just saw this and thought it was pretty cool"
|
| 102 |
-
"yeah so i was thinking about that earlier and tbh it makes sense"
|
| 103 |
-
"wait that actually works better than i expected lol"
|
| 104 |
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
| 106 |
{text}<|end|><|assistant|>""",
|
| 107 |
|
| 108 |
-
#
|
| 109 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
- Output ONLY the rewritten text. Nothing else. No quotes. No colons.
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
|
| 119 |
-
Now
|
| 120 |
-
{text}<|end|><|assistant|>"""
|
| 121 |
}
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
STYLE_TEMPERATURES = {
|
| 126 |
-
"professional": 0.
|
| 127 |
-
"casual": 0.
|
| 128 |
-
"academic": 0.
|
| 129 |
-
"marketing": 0.
|
| 130 |
-
"humanizer":
|
| 131 |
-
"humanizer_strong": 1.1 # Go above 1.0 — Phi-3 handles it well
|
| 132 |
}
|
| 133 |
|
| 134 |
# ==================== HELPER FUNCTIONS ====================
|
| 135 |
def clean_output(text: str) -> str:
|
| 136 |
-
"""Aggressively clean Phi-3 output — removes ALL junk, keeps only the real humanized text"""
|
| 137 |
if not text:
|
| 138 |
return ""
|
| 139 |
-
|
| 140 |
-
#
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
# === STEP 1: Remove all Phi-3 special tokens ===
|
| 144 |
-
raw = re.sub(r"<\|[^>]*\|>", "", raw) # <|end|>, <|assistant|>, etc.
|
| 145 |
-
|
| 146 |
-
# === STEP 2: Remove common garbage patterns (case-insensitive) ===
|
| 147 |
-
junk_patterns = [
|
| 148 |
-
r"^(Human|AI|Assistant|System|User|Response|Output|Rewritten|Result|Final|Answer)[\s:]*.*?(?=\n|$)",
|
| 149 |
-
r"^(Instruction|Task|Prompt)[\s\d:]*.*?(?=\n|$)",
|
| 150 |
-
r"^\s*[\*\-•·]\s*(Humanized|Rewritten|Output|Result).*",
|
| 151 |
-
r"^[\"']?Here.?s.*[:\-]?\s*",
|
| 152 |
-
r"^[\"']?(The )?(rewritten|humanized|final|output).*?[:\-\.]\s*",
|
| 153 |
-
r"^.*(version|text|is|below).*:",
|
| 154 |
-
r"^\s*[\(\[]?[Hh]umanized.*[\)\]]?\s*:?",
|
| 155 |
-
r"^\s*[\(\[]?[Rr]ewritten.*[\)\]]?\s*:?",
|
| 156 |
-
r"^\s*Output\s*:",
|
| 157 |
-
r"^\s*>\s*", # Remove markdown quotes
|
| 158 |
-
r"^.*?(\btext\b|\bcontent\b|\bpassage\b).*?:", # "Original text:", "Input text:"
|
| 159 |
-
]
|
| 160 |
-
|
| 161 |
-
for pattern in junk_patterns:
|
| 162 |
-
raw = re.sub(pattern, "", raw, flags=re.IGNORECASE | re.MULTILINE)
|
| 163 |
-
|
| 164 |
-
# === STEP 3: Extract only the actual rewritten message ===
|
| 165 |
-
# Most real humanized text appears after the first colon or newline
|
| 166 |
-
lines = [line.strip() for line in raw.split("\n") if line.strip()]
|
| 167 |
-
|
| 168 |
-
# Filter out obvious instruction lines
|
| 169 |
-
clean_lines = []
|
| 170 |
for line in lines:
|
| 171 |
-
|
| 172 |
-
if
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
"
|
|
|
|
| 176 |
]):
|
| 177 |
continue
|
| 178 |
-
if
|
| 179 |
-
continue
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
if clean_lines:
|
| 185 |
-
cleaned = max(clean_lines, key=len)
|
| 186 |
-
else:
|
| 187 |
-
cleaned = raw.strip()
|
| 188 |
-
|
| 189 |
-
# === STEP 4: Final cleanup ===
|
| 190 |
-
cleaned = cleaned.strip(" '\"")
|
| 191 |
-
cleaned = re.sub(r"\s+", " ", cleaned) # normalize spaces
|
| 192 |
-
cleaned = cleaned.strip()
|
| 193 |
-
|
| 194 |
-
# If still empty or too short, fallback
|
| 195 |
-
if not cleaned or len(cleaned) < 3:
|
| 196 |
-
cleaned = "hello world" # or return original? up to you
|
| 197 |
-
|
| 198 |
-
return cleaned
|
| 199 |
|
| 200 |
def format_prompt(text: str, style: str) -> str:
|
| 201 |
"""Format prompt for Phi-3.1"""
|
|
@@ -223,15 +206,14 @@ async def transform_with_model(text: str, style: str) -> str:
|
|
| 223 |
|
| 224 |
output = llm(
|
| 225 |
prompt,
|
| 226 |
-
max_tokens=
|
| 227 |
temperature=temperature,
|
| 228 |
-
top_p=0.
|
| 229 |
-
top_k=
|
| 230 |
-
repeat_penalty=1.
|
| 231 |
-
frequency_penalty=0.
|
| 232 |
-
presence_penalty=0.
|
| 233 |
-
stop=["<|end|>", "<|user|>", "<|assistant|>",
|
| 234 |
-
echo=False,
|
| 235 |
)
|
| 236 |
|
| 237 |
processing_time = time.time() - start_time
|
|
|
|
| 86 |
|
| 87 |
# ==================== STYLE PROMPTS ====================
|
| 88 |
STYLE_PROMPTS = {
|
| 89 |
+
"professional": """<|system|>Rewrite in clear, polite, professional English. Fix grammar and spelling. Keep the exact same meaning. Do not add any new information, examples, titles, or explanations. Output only the rewritten text.
|
| 90 |
|
| 91 |
+
Example input: hi im john
|
| 92 |
+
Example output: Hello, I'm John.
|
| 93 |
|
| 94 |
+
Now rewrite this:
|
| 95 |
+
{text}<|end|><|assistant|>""",
|
| 96 |
+
|
| 97 |
+
"casual": """<|system|>Rewrite like a normal person texting or talking to a friend. Use lowercase, contractions, light slang if it fits. Fix basic errors. Never sound robotic.
|
| 98 |
+
|
| 99 |
+
Rules:
|
| 100 |
+
- No titles (Mr, Dr, etc)
|
| 101 |
+
- No company policy
|
| 102 |
+
- No extra sentences
|
| 103 |
+
- Output only the message, nothing else
|
| 104 |
+
|
| 105 |
+
Example input: hi my name is sarah pls call me
|
| 106 |
+
Example output: hey i'm sarah, just call me whatever
|
| 107 |
+
|
| 108 |
+
Now rewrite:
|
| 109 |
+
{text}<|end|><|assistant|>""",
|
| 110 |
+
|
| 111 |
+
"academic": """<|system|>Rewrite in formal academic English. Use precise vocabulary and proper structure. Do not add references, examples, or explanations. Preserve original meaning exactly.
|
| 112 |
|
| 113 |
+
Example input: climate change bad for earth
|
| 114 |
+
Example output: Climate change poses significant threats to the planet.
|
| 115 |
|
| 116 |
+
Now rewrite this only:
|
| 117 |
+
{text}<|end|><|assistant|>""",
|
| 118 |
|
| 119 |
+
"marketing": """<|system|>Rewrite as short, punchy, persuasive marketing copy. Make it exciting and benefit-focused. No fluff, no lies.
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
+
Example input: this app saves time
|
| 122 |
+
Example output: Save hours every week with one tap.
|
| 123 |
+
|
| 124 |
+
Now rewrite only:
|
| 125 |
{text}<|end|><|assistant|>""",
|
| 126 |
|
| 127 |
+
# THIS IS YOUR UNDETECTABLE TURNITIN-PROOF HUMANIZER (2025)
|
| 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 |
+
Correct examples:
|
| 140 |
+
Input: hi my name is vishal kumar G
|
| 141 |
+
Output: hey i'm vishal kumar g
|
|
|
|
| 142 |
|
| 143 |
+
Input: i want to apply for job
|
| 144 |
+
Output: hey i'd love to apply for the job
|
| 145 |
|
| 146 |
+
Now rewrite this one exactly like a human would:
|
| 147 |
+
{text}<|end|><|assistant|>""",
|
| 148 |
}
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
STYLE_TEMPERATURES = {
|
| 153 |
+
"professional": 0.4,
|
| 154 |
+
"casual": 0.85,
|
| 155 |
+
"academic": 0.5,
|
| 156 |
+
"marketing": 0.9,
|
| 157 |
+
"humanizer": 1.0 # High temperature = real human unpredictability
|
|
|
|
| 158 |
}
|
| 159 |
|
| 160 |
# ==================== HELPER FUNCTIONS ====================
|
| 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) < 5 or len(l) > 500:
|
| 170 |
+
continue
|
| 171 |
+
if any(bad in l.lower() for bad in [
|
| 172 |
+
"here is", "output:", "rewritten", "version", "humanized",
|
| 173 |
+
"assistant", "response", "instruction", "mr.", "ms.", "please address"
|
| 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"""
|
|
|
|
| 206 |
|
| 207 |
output = llm(
|
| 208 |
prompt,
|
| 209 |
+
max_tokens=80, # Short = safe from hallucination
|
| 210 |
temperature=temperature,
|
| 211 |
+
top_p=0.95,
|
| 212 |
+
top_k=60,
|
| 213 |
+
repeat_penalty=1.18,
|
| 214 |
+
frequency_penalty=0.2,
|
| 215 |
+
presence_penalty=0.15,
|
| 216 |
+
stop=["<|end|>", "<|user|>", "<|assistant|>","\n\n", "Please", "Kindly", "Mr.", "Ms.", "Dr.","colleagues", "protocol", "address me", "title"],echo=False,
|
|
|
|
| 217 |
)
|
| 218 |
|
| 219 |
processing_time = time.time() - start_time
|