viskav commited on
Commit
e0ae802
·
verified ·
1 Parent(s): 7213999

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -93
app.py CHANGED
@@ -86,116 +86,99 @@ class HumanizeRequest(BaseModel):
86
 
87
  # ==================== STYLE PROMPTS ====================
88
  STYLE_PROMPTS = {
89
- "professional": """<|system|>Rewrite formally and clearly for business. Keep it professional. No fluff. No extra sentences. Output only the rewritten version.<|end|><|user|>{text}<|end|><|assistant|>""",
90
 
91
- "casual": """<|system|>Rewrite this like a real person texting or talking to a friend. Short. Natural. A bit messy is fine. No perfect grammar needed. Just make it sound human.<|end|><|user|>{text}<|end|><|assistant|>""",
 
92
 
93
- "academic": """<|system|>Rewrite in formal academic tone using precise language. Do not add examples or explanations. Output only the rewritten text.<|end|><|user|>{text}<|end|><|assistant|>""",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
- "marketing": """<|system|>Turn this into punchy, persuasive marketing copy. Make it sound exciting but not cheesy. Short and bold.<|end|><|user|>{text}<|end|><|assistant|>""",
 
96
 
97
- # BEST HUMANIZER PROMPT 2025 (works on IQ2_M/IQ3_XS)
98
- "humanizer": """<|system|>You are a human writer with slight imperfections. Rewrite the following text so it feels like something a real person wrote quickly — not an AI. Use natural flow, small pauses, varied sentence length, and casual transitions. Never sound robotic or overly polished. Do NOT add new information. Do NOT explain. Do NOT start with "Hey" or "Well," or "Here's". Just rewrite it naturally.
99
 
100
- Examples of good style:
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
- Now rewrite this one:
 
 
 
106
  {text}<|end|><|assistant|>""",
107
 
108
- # ULTRA-STRONG HUMANIZER (for max bypass)
109
- "humanizer_strong": """<|system|>Rewrite the text like a real human would write it — casual, fast, natural. Use contractions. Vary sentence length. Never perfect.
 
 
 
 
 
 
 
 
 
110
 
111
- RULES:
112
- - NEVER say "Here is", "Humanized version", "Response", "Output", etc.
113
- - NEVER add labels, instructions, or explanations.
114
- - Output ONLY the rewritten text. Nothing else. No quotes. No colons.
115
 
116
- Example of correct output:
117
- yeah just tried hello world and it actually works lol
118
 
119
- Now do this one:
120
- {text}<|end|><|assistant|>"""
121
  }
122
 
123
 
124
 
125
  STYLE_TEMPERATURES = {
126
- "professional": 0.3,
127
- "casual": 0.7, # was 0.5 → too safe
128
- "academic": 0.4,
129
- "marketing": 0.8, # was 0.6 → too tame
130
- "humanizer": 0.95, # HIGH temperature = burstiness
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
- # Convert to string just in case
141
- raw = str(text).strip()
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
- lower = line.lower()
172
- if any(bad in lower for bad in [
173
- "instruction", "response", "here is", "here's", "output",
174
- "rewritten version", "humanized version", "final answer",
175
- "note:", "important:", "example:"
 
176
  ]):
177
  continue
178
- if line.startswith((">", "-", "*", "•", "#", "[", "(")) and len(line) < 50:
179
- continue # likely a header or label
180
- if len(line) > 5: # ignore junk like "Sure!" or "Okay"
181
- clean_lines.append(line)
182
-
183
- # Use the longest coherent line (works amazingly well)
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=300,
227
  temperature=temperature,
228
- top_p=0.9,
229
- top_k=40, # Add this
230
- repeat_penalty=1.15,
231
- frequency_penalty=0.3, # Increase
232
- presence_penalty=0.2,
233
- stop=["<|end|>", "<|user|>", "<|assistant|>", "\n\n\n", "Human:", "AI:"],
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