Spaces:
Running
Running
eheguy commited on
Commit Β·
c872ddc
1
Parent(s): ffd2e88
Integrate few-shot writing examples for enhanced prompt
Browse files- humanizer.py +72 -17
humanizer.py
CHANGED
|
@@ -1,11 +1,52 @@
|
|
| 1 |
import os
|
| 2 |
from groq import Groq
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
# ββ SIMPLE ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 5 |
SIMPLE_PROMPT = """You are a light text editor. Rewrite the text the user sends with these rules only:
|
| 6 |
|
| 7 |
1. Remove any of these phrases entirely: "It is important to note", "In today's fast-paced world", "Furthermore", "Moreover", "In conclusion", "When it comes to", "It is worth noting", "Needless to say", "In the realm of", "It goes without saying", "It is crucial to", "One must consider", "It should be noted".
|
| 8 |
-
2. When choosing words, avoid the most common or obvious choice. Pick the second or third most natural option
|
| 9 |
3. Do NOT change sentence structure, length, or order.
|
| 10 |
4. Do NOT change the meaning, facts, or argument.
|
| 11 |
5. Output ONLY the rewritten text. No preamble. No explanation."""
|
|
@@ -18,25 +59,30 @@ STANDARD_PROMPT = """You are a text restructuring engine. Rewrite the text the u
|
|
| 18 |
3. Flip at least 2 passive voice constructions to active voice.
|
| 19 |
4. Move a subordinate clause from the end of a sentence to the beginning in at least 2 places.
|
| 20 |
5. Add 1 or 2 contractions where formal phrasing sits (e.g. "it is" -> "it's").
|
| 21 |
-
6. Word choice entropy: deliberately avoid the most statistically obvious word. Use the second or third most natural synonym instead. Mix formal and casual register
|
| 22 |
7. Do NOT change the meaning, facts, or argument.
|
| 23 |
8. Output ONLY the rewritten text. No preamble. No explanation."""
|
| 24 |
|
| 25 |
# ββ ENHANCED ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 26 |
-
ENHANCED_PROMPT = """You are an aggressive text humanizer.
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
2. Remove any of these phrases entirely: "It is important to note", "In today's fast-paced world", "Furthermore", "Moreover", "In conclusion", "When it comes to", "It is worth noting", "Needless to say", "In the realm of", "It goes without saying", "It is crucial to", "One must consider", "It should be noted".
|
| 30 |
3. Flip ALL passive voice constructions to active voice.
|
| 31 |
4. Move subordinate clauses to unexpected positions β beginning, middle, or split across sentences.
|
| 32 |
5. Add contractions naturally throughout (aim for 30-40% of sentences).
|
| 33 |
-
6. Add one parenthetical aside
|
| 34 |
-
7. Replace every transition word
|
| 35 |
-
8. Introduce one slightly informal word or phrase per paragraph
|
| 36 |
-
9. Word choice entropy
|
| 37 |
-
10. Restructure at least one
|
| 38 |
-
11.
|
| 39 |
-
12.
|
|
|
|
| 40 |
|
| 41 |
# ββ READABILITY MODIFIERS βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 42 |
READABILITY_MODIFIERS = {
|
|
@@ -54,9 +100,9 @@ PURPOSE_MODIFIERS = {
|
|
| 54 |
"personal": "This is personal writing. Make it warm, genuine, and human. First-person voice is appropriate.",
|
| 55 |
"essay": "This is an essay. Preserve the thesis, supporting arguments, and conclusion structure.",
|
| 56 |
"blog": "This is a blog post. Keep it engaging, punchy, and easy to scan. Use a conversational voice.",
|
| 57 |
-
"email": "This is an email. Keep it concise, direct, and appropriately professional.
|
| 58 |
-
"social media": "This is social media content. Keep it short, punchy, and engaging.
|
| 59 |
-
"report": "This is a formal report. Preserve structure
|
| 60 |
}
|
| 61 |
|
| 62 |
PROMPTS = {
|
|
@@ -78,10 +124,19 @@ async def humanize_text(
|
|
| 78 |
|
| 79 |
client = Groq(api_key=api_key)
|
| 80 |
|
| 81 |
-
# Build
|
| 82 |
base_prompt = PROMPTS.get(mode, STANDARD_PROMPT)
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
system_prompt = f"""{base_prompt}
|
| 87 |
|
|
|
|
| 1 |
import os
|
| 2 |
from groq import Groq
|
| 3 |
|
| 4 |
+
# ββ FEW-SHOT HUMAN WRITING EXAMPLES βββββββββββββββββββββββββββββββββββββββββββ
|
| 5 |
+
# These are genuine human writing samples used to show the model
|
| 6 |
+
# what natural human prose looks like β varied, imperfect, personal.
|
| 7 |
+
|
| 8 |
+
FEW_SHOT_EXAMPLES = """
|
| 9 |
+
Here are examples of genuine human writing. Study the rhythm, word choice,
|
| 10 |
+
sentence variety, and natural imperfections. Imitate this style:
|
| 11 |
+
|
| 12 |
+
EXAMPLE 1 (Blog/Casual):
|
| 13 |
+
"I've been thinking about this for weeks now, and I still don't have a clean answer.
|
| 14 |
+
Social media is weird. Not bad-weird necessarily, just... complicated. You get on
|
| 15 |
+
there to stay in touch with people, and somehow you end up knowing the political
|
| 16 |
+
opinions of your dentist's cousin. Nobody asked for that. And yet here we are,
|
| 17 |
+
scrolling at midnight, wondering why we feel vaguely terrible.
|
| 18 |
+
The thing nobody tells you is that the platform is working exactly as intended."
|
| 19 |
+
|
| 20 |
+
EXAMPLE 2 (Professional/Analytical):
|
| 21 |
+
"The numbers don't lie, but they don't tell the whole story either. Last quarter
|
| 22 |
+
looked rough on paper β revenue down 8%, customer churn up slightly. But dig
|
| 23 |
+
into the cohort data and a different picture emerges. The customers we acquired
|
| 24 |
+
after the rebrand are sticking around. The ones leaving? Almost entirely
|
| 25 |
+
pre-2022 signups who never really fit the new direction anyway.
|
| 26 |
+
That's not a crisis. That's a transition."
|
| 27 |
+
|
| 28 |
+
EXAMPLE 3 (Academic/Formal):
|
| 29 |
+
"The assumption that technological adoption follows a predictable S-curve has
|
| 30 |
+
shaped product strategy for decades. What this model fails to account for,
|
| 31 |
+
however, is the role of social trust in accelerating or stalling diffusion.
|
| 32 |
+
When communities perceive a technology as threatening existing power structures β
|
| 33 |
+
regardless of its actual utility β adoption stalls in ways the curve simply
|
| 34 |
+
cannot predict. The data from the 2019 contact tracing rollout illustrates
|
| 35 |
+
this precisely."
|
| 36 |
+
|
| 37 |
+
EXAMPLE 4 (Personal/Reflective):
|
| 38 |
+
"My grandfather never owned a computer. He died at 91, having written every
|
| 39 |
+
letter by hand, remembered every phone number, and navigated every city
|
| 40 |
+
with a paper map he'd fold wrong every single time. I used to think that
|
| 41 |
+
was a limitation. I'm less sure now. There's something to be said for a
|
| 42 |
+
life where the information you carry is only what your mind decided to keep."
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
# ββ SIMPLE ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 46 |
SIMPLE_PROMPT = """You are a light text editor. Rewrite the text the user sends with these rules only:
|
| 47 |
|
| 48 |
1. Remove any of these phrases entirely: "It is important to note", "In today's fast-paced world", "Furthermore", "Moreover", "In conclusion", "When it comes to", "It is worth noting", "Needless to say", "In the realm of", "It goes without saying", "It is crucial to", "One must consider", "It should be noted".
|
| 49 |
+
2. When choosing words, avoid the most common or obvious choice. Pick the second or third most natural option.
|
| 50 |
3. Do NOT change sentence structure, length, or order.
|
| 51 |
4. Do NOT change the meaning, facts, or argument.
|
| 52 |
5. Output ONLY the rewritten text. No preamble. No explanation."""
|
|
|
|
| 59 |
3. Flip at least 2 passive voice constructions to active voice.
|
| 60 |
4. Move a subordinate clause from the end of a sentence to the beginning in at least 2 places.
|
| 61 |
5. Add 1 or 2 contractions where formal phrasing sits (e.g. "it is" -> "it's").
|
| 62 |
+
6. Word choice entropy: deliberately avoid the most statistically obvious word. Use the second or third most natural synonym instead. Mix formal and casual register.
|
| 63 |
7. Do NOT change the meaning, facts, or argument.
|
| 64 |
8. Output ONLY the rewritten text. No preamble. No explanation."""
|
| 65 |
|
| 66 |
# ββ ENHANCED ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 67 |
+
ENHANCED_PROMPT = """You are an aggressive text humanizer. Study these human writing examples carefully, then rewrite the user's text to match that same natural, human quality:
|
| 68 |
+
|
| 69 |
+
{few_shot_examples}
|
| 70 |
|
| 71 |
+
Now rewrite the user's text following ALL of these rules:
|
| 72 |
+
|
| 73 |
+
1. Vary sentence lengths dramatically. Short punchy sentences (3-6 words) must appear at least twice. Long flowing sentences (30+ words) must appear at least twice. Alternate aggressively β never let the rhythm settle.
|
| 74 |
2. Remove any of these phrases entirely: "It is important to note", "In today's fast-paced world", "Furthermore", "Moreover", "In conclusion", "When it comes to", "It is worth noting", "Needless to say", "In the realm of", "It goes without saying", "It is crucial to", "One must consider", "It should be noted".
|
| 75 |
3. Flip ALL passive voice constructions to active voice.
|
| 76 |
4. Move subordinate clauses to unexpected positions β beginning, middle, or split across sentences.
|
| 77 |
5. Add contractions naturally throughout (aim for 30-40% of sentences).
|
| 78 |
+
6. Add one parenthetical aside that feels like a human thinking out loud β natural, not forced.
|
| 79 |
+
7. Replace every transition word with either a casual connector or nothing at all.
|
| 80 |
+
8. Introduce one slightly informal word or phrase per paragraph.
|
| 81 |
+
9. Word choice entropy: Never pick the most statistically obvious word. Always reach for the second or third most natural option. Mix registers deliberately. Use idioms and collocations that feel instinctive to a human writer.
|
| 82 |
+
10. Restructure at least one paragraph β reorder sentences while keeping meaning intact.
|
| 83 |
+
11. Add one small opinion, observation, or aside that sounds like the writer's voice β not a fact, just a human moment.
|
| 84 |
+
12. Do NOT change the core meaning, facts, or argument. All information must be preserved.
|
| 85 |
+
13. Output ONLY the rewritten text. No preamble. No explanation. No "Here is the rewritten version"."""
|
| 86 |
|
| 87 |
# ββ READABILITY MODIFIERS βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 88 |
READABILITY_MODIFIERS = {
|
|
|
|
| 100 |
"personal": "This is personal writing. Make it warm, genuine, and human. First-person voice is appropriate.",
|
| 101 |
"essay": "This is an essay. Preserve the thesis, supporting arguments, and conclusion structure.",
|
| 102 |
"blog": "This is a blog post. Keep it engaging, punchy, and easy to scan. Use a conversational voice.",
|
| 103 |
+
"email": "This is an email. Keep it concise, direct, and appropriately professional.",
|
| 104 |
+
"social media": "This is social media content. Keep it short, punchy, and engaging.",
|
| 105 |
+
"report": "This is a formal report. Preserve structure and factual precision. Keep the tone authoritative.",
|
| 106 |
}
|
| 107 |
|
| 108 |
PROMPTS = {
|
|
|
|
| 124 |
|
| 125 |
client = Groq(api_key=api_key)
|
| 126 |
|
| 127 |
+
# Build system prompt
|
| 128 |
base_prompt = PROMPTS.get(mode, STANDARD_PROMPT)
|
| 129 |
+
|
| 130 |
+
# Inject few-shot examples into enhanced prompt
|
| 131 |
+
if mode == "enhanced":
|
| 132 |
+
base_prompt = base_prompt.format(few_shot_examples=FEW_SHOT_EXAMPLES)
|
| 133 |
+
|
| 134 |
+
readability_mod = READABILITY_MODIFIERS.get(
|
| 135 |
+
readability.lower(), READABILITY_MODIFIERS["neutral"]
|
| 136 |
+
)
|
| 137 |
+
purpose_mod = PURPOSE_MODIFIERS.get(
|
| 138 |
+
purpose.lower(), PURPOSE_MODIFIERS["professional"]
|
| 139 |
+
)
|
| 140 |
|
| 141 |
system_prompt = f"""{base_prompt}
|
| 142 |
|