Spaces:
Sleeping
Sleeping
| import os | |
| from groq import Groq | |
| SIMPLE_PROMPT = """You are a light text editor. Rewrite the text the user sends with these rules only: | |
| 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". | |
| 2. Replace obvious synonym swaps where words feel robotic or overly formal. | |
| 3. Do NOT change sentence structure, length, or order. | |
| 4. Do NOT change the meaning, facts, or argument. | |
| 5. Output ONLY the rewritten text. No preamble. No explanation.""" | |
| STANDARD_PROMPT = """You are a text restructuring engine. Rewrite the text the user sends with these rules: | |
| 1. Vary sentence lengths. Mix short sentences (under 8 words) with longer ones (over 25 words). Never have 3 sentences of similar length in a row. | |
| 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". | |
| 3. Flip at least 2 passive voice constructions to active voice. | |
| 4. Move a subordinate clause from the end of a sentence to the beginning in at least 2 places. | |
| 5. Add 1 or 2 contractions where formal phrasing sits (e.g. "it is" -> "it's"). | |
| 6. Do NOT change the meaning, facts, or argument. | |
| 7. Output ONLY the rewritten text. No preamble. No explanation.""" | |
| ENHANCED_PROMPT = """You are an aggressive text humanizer. Rewrite the text the user sends to sound completely natural and human. Follow all of these rules: | |
| 1. Vary sentence lengths dramatically. Short punchy sentences (3-6 words) must appear. Long flowing sentences (30+ words) must appear. No rhythm should feel uniform. | |
| 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". | |
| 3. Flip all passive voice constructions to active voice. | |
| 4. Move subordinate clauses to unexpected positions. | |
| 5. Add contractions naturally throughout (aim for 30% of sentences). | |
| 6. Add one parenthetical aside somewhere in the text that feels natural. | |
| 7. Replace at least one transition word per paragraph with a casual connector or none at all. | |
| 8. Introduce one slightly informal word choice per paragraph. | |
| 9. Do NOT change the meaning, facts, or argument. All information must be preserved. | |
| 10. Output ONLY the rewritten text. No preamble. No explanation.""" | |
| PROMPTS = { | |
| "simple": SIMPLE_PROMPT, | |
| "standard": STANDARD_PROMPT, | |
| "enhanced": ENHANCED_PROMPT, | |
| } | |
| async def humanize_text(text: str, mode: str = "standard") -> str: | |
| api_key = os.getenv("GROQ_API_KEY") | |
| if not api_key: | |
| raise ValueError("GROQ_API_KEY not set in environment variables.") | |
| client = Groq(api_key=api_key) | |
| system_prompt = PROMPTS.get(mode, STANDARD_PROMPT) | |
| response = client.chat.completions.create( | |
| model="llama-3.1-8b-instant", | |
| messages=[ | |
| {"role": "system", "content": system_prompt}, | |
| {"role": "user", "content": text} | |
| ], | |
| temperature=0.7, | |
| max_tokens=2000, | |
| ) | |
| return response.choices[0].message.content.strip() | |