viskav commited on
Commit
8ac0572
·
verified ·
1 Parent(s): fe2c466

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -58
app.py CHANGED
@@ -87,75 +87,39 @@ class HumanizeRequest(BaseModel):
87
  # ==================== STYLE PROMPTS ====================
88
  STYLE_PROMPTS = {
89
  "professional": """<|system|>
90
- You are a professional writing assistant. Rewrite the text below in formal, corporate business language.
91
-
92
- IMPORTANT RULES:
93
- 1. Output ONLY the rewritten text
94
- 2. No explanations, no notes
95
- 3. Keep the same meaning
96
- 4. Use formal vocabulary
97
- 5. Proper grammar and structure
98
- <|end|>
99
  <|user|>
100
- Text to rewrite: {text}
101
-
102
- Rewritten (professional):<|end|>
103
  <|assistant|>""",
104
 
105
  "casual": """<|system|>
106
- You are a casual writing assistant. Rewrite the text below in friendly, natural, conversational English.
107
-
108
- IMPORTANT RULES:
109
- 1. Output ONLY the rewritten text
110
- 2. No explanations, no notes
111
- 3. Keep the same meaning
112
- 4. Use contractions (I'm, don't, etc.)
113
- 5. Sound like a real person speaking
114
- <|end|>
115
  <|user|>
116
- Text to rewrite: {text}
117
-
118
- Rewritten (casual):<|end|>
119
  <|assistant|>""",
120
 
121
  "academic": """<|system|>
122
- You are an academic writing assistant. Rewrite the text below in formal scholarly language.
123
-
124
- IMPORTANT RULES:
125
- 1. Output ONLY the rewritten text
126
- 2. No explanations, no notes
127
- 3. Keep the same meaning
128
- 4. Use precise academic vocabulary
129
- 5. Maintain formal structure
130
- <|end|>
131
  <|user|>
132
- Text to rewrite: {text}
133
-
134
- Rewritten (academic):<|end|>
135
  <|assistant|>""",
136
 
137
  "marketing": """<|system|>
138
- You are a marketing copywriter. Rewrite the text below into persuasive marketing language.
139
-
140
- IMPORTANT RULES:
141
- 1. Output ONLY the rewritten text
142
- 2. No explanations, no notes
143
- 3. Keep the same meaning
144
- 4. Use emotional hooks and benefits
145
- 5. Make it engaging and compelling
146
- <|end|>
147
  <|user|>
148
- Text to rewrite: {text}
149
-
150
- Rewritten (marketing):<|end|>
151
  <|assistant|>"""
152
  }
153
 
154
  STYLE_TEMPERATURES = {
155
  "professional": 0.3,
156
- "casual": 0.6,
157
  "academic": 0.4,
158
- "marketing": 0.7
159
  }
160
 
161
  # ==================== HELPER FUNCTIONS ====================
@@ -164,10 +128,31 @@ def clean_output(text: str) -> str:
164
  if not text:
165
  return ""
166
 
167
- # Remove Phi-3.1 special tokens
168
- clean = re.sub(r'<\|[^>]+\|>', '', text)
169
- clean = re.sub(r'Rewritten\s*\([^)]+\):', '', clean, flags=re.IGNORECASE)
170
- clean = re.sub(r'Text to rewrite:.*', '', clean, flags=re.DOTALL)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
 
172
  # Clean whitespace
173
  clean = re.sub(r'\s+', ' ', clean)
@@ -197,16 +182,19 @@ async def transform_with_model(text: str, style: str) -> str:
197
  prompt = format_prompt(text, style)
198
  temperature = STYLE_TEMPERATURES[style]
199
 
 
 
 
200
  # Run inference
201
  start_time = time.time()
202
 
203
  output = llm(
204
  prompt,
205
- max_tokens=min(400, len(text) + 100),
206
  temperature=temperature,
207
- top_p=0.9,
208
  repeat_penalty=1.1,
209
- stop=[END_TOKEN, "<|end|>", "\n\n"],
210
  echo=False,
211
  )
212
 
@@ -218,9 +206,15 @@ async def transform_with_model(text: str, style: str) -> str:
218
  else:
219
  result = str(output)
220
 
 
 
 
221
  # Clean result
222
  cleaned = clean_output(result)
223
 
 
 
 
224
  # Fallback if output is empty
225
  if not cleaned or cleaned.isspace():
226
  cleaned = f"[{style.capitalize()} Version]: {text}"
@@ -332,7 +326,7 @@ async def get_styles():
332
  styles_info[style] = {
333
  "description": first_line,
334
  "temperature": STYLE_TEMPERATURES[style],
335
- "max_tokens": 400
336
  }
337
 
338
  return {
 
87
  # ==================== STYLE PROMPTS ====================
88
  STYLE_PROMPTS = {
89
  "professional": """<|system|>
90
+ You are a professional writing assistant. Rewrite the following text in formal, corporate business language.
91
+ IMPORTANT: Output ONLY the rewritten text, nothing else.<|end|>
 
 
 
 
 
 
 
92
  <|user|>
93
+ Text to rewrite: {text}<|end|>
 
 
94
  <|assistant|>""",
95
 
96
  "casual": """<|system|>
97
+ You are a casual writing assistant. Rewrite the following text in friendly, natural, conversational English.
98
+ IMPORTANT: Output ONLY the rewritten text, nothing else.<|end|>
 
 
 
 
 
 
 
99
  <|user|>
100
+ Text to rewrite: {text}<|end|>
 
 
101
  <|assistant|>""",
102
 
103
  "academic": """<|system|>
104
+ You are an academic writing assistant. Rewrite the following text in formal scholarly language.
105
+ IMPORTANT: Output ONLY the rewritten text, nothing else.<|end|>
 
 
 
 
 
 
 
106
  <|user|>
107
+ Text to rewrite: {text}<|end|>
 
 
108
  <|assistant|>""",
109
 
110
  "marketing": """<|system|>
111
+ You are a marketing copywriter. Rewrite the following text into persuasive marketing language.
112
+ IMPORTANT: Output ONLY the rewritten text, nothing else.<|end|>
 
 
 
 
 
 
 
113
  <|user|>
114
+ Text to rewrite: {text}<|end|>
 
 
115
  <|assistant|>"""
116
  }
117
 
118
  STYLE_TEMPERATURES = {
119
  "professional": 0.3,
120
+ "casual": 0.5,
121
  "academic": 0.4,
122
+ "marketing": 0.6
123
  }
124
 
125
  # ==================== HELPER FUNCTIONS ====================
 
128
  if not text:
129
  return ""
130
 
131
+ # Remove emojis and special characters
132
+ emoji_pattern = re.compile("["
133
+ u"\U0001F600-\U0001F64F" # emoticons
134
+ u"\U0001F300-\U0001F5FF" # symbols & pictographs
135
+ u"\U0001F680-\U0001F6FF" # transport & map symbols
136
+ u"\U0001F1E0-\U0001F1FF" # flags
137
+ "]+", flags=re.UNICODE)
138
+
139
+ clean = emoji_pattern.sub(r'', text)
140
+
141
+ # Remove Phi-3.1 special tokens if any remain
142
+ clean = re.sub(r'<\|[^>]+\|>', '', clean)
143
+
144
+ # Remove "Text to rewrite:" and similar instruction text
145
+ clean = re.sub(r'(Text to rewrite|Rewritten|Humanized|Assistant:|Assistant response:).*?:', '', clean, flags=re.IGNORECASE)
146
+
147
+ # Remove any remaining instruction words
148
+ instruction_words = [
149
+ "here is the", "here's the", "here is", "here's",
150
+ "the rewritten text is:", "the rewritten text:",
151
+ "rewritten text:", "humanized text:", "output:"
152
+ ]
153
+
154
+ for word in instruction_words:
155
+ clean = re.sub(fr'\b{re.escape(word)}\b', '', clean, flags=re.IGNORECASE)
156
 
157
  # Clean whitespace
158
  clean = re.sub(r'\s+', ' ', clean)
 
182
  prompt = format_prompt(text, style)
183
  temperature = STYLE_TEMPERATURES[style]
184
 
185
+ # Debug: Print prompt (remove in production)
186
+ print(f"DEBUG - Prompt for {style}: {prompt[:100]}...")
187
+
188
  # Run inference
189
  start_time = time.time()
190
 
191
  output = llm(
192
  prompt,
193
+ max_tokens=min(200, len(text) + 50),
194
  temperature=temperature,
195
+ top_p=0.95,
196
  repeat_penalty=1.1,
197
+ stop=["<|end|>", "\n"],
198
  echo=False,
199
  )
200
 
 
206
  else:
207
  result = str(output)
208
 
209
+ # Debug: Print raw output
210
+ print(f"DEBUG - Raw output: {result[:100]}...")
211
+
212
  # Clean result
213
  cleaned = clean_output(result)
214
 
215
+ # Debug: Print cleaned output
216
+ print(f"DEBUG - Cleaned output: {cleaned}")
217
+
218
  # Fallback if output is empty
219
  if not cleaned or cleaned.isspace():
220
  cleaned = f"[{style.capitalize()} Version]: {text}"
 
326
  styles_info[style] = {
327
  "description": first_line,
328
  "temperature": STYLE_TEMPERATURES[style],
329
+ "max_tokens": 200
330
  }
331
 
332
  return {