Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -69,11 +69,11 @@ def add_human_noise(output_text):
|
|
| 69 |
filler_words = ["uh,", "um,", "so,", "well,", "I guess,"]
|
| 70 |
sentences = output_text.split(". ")
|
| 71 |
noisy_sentences = [
|
| 72 |
-
f"{random.choice(noise_phrases)} {sentence}" if random.random() < 0.
|
| 73 |
for sentence in sentences
|
| 74 |
]
|
| 75 |
noisy_sentences = [
|
| 76 |
-
f"{sentence} {random.choice(filler_words)}" if random.random() < 0.
|
| 77 |
for sentence in noisy_sentences
|
| 78 |
]
|
| 79 |
return ". ".join(noisy_sentences)
|
|
@@ -85,7 +85,7 @@ def adjust_sentence_structure(output_text):
|
|
| 85 |
for sentence in sentences:
|
| 86 |
words = sentence.split()
|
| 87 |
if len(words) > 15:
|
| 88 |
-
midpoint = len(words) // 2
|
| 89 |
adjusted_sentences.append(" ".join(words[:midpoint]) + ", you know,")
|
| 90 |
adjusted_sentences.append(" ".join(words[midpoint:]))
|
| 91 |
else:
|
|
@@ -100,11 +100,10 @@ def check_grammar(output_text):
|
|
| 100 |
corrected_text = tool.correct(output_text)
|
| 101 |
return corrected_text
|
| 102 |
|
| 103 |
-
def refine_humanization(output_text, tone):
|
| 104 |
refinement_prompt = (
|
| 105 |
-
f"Refine the following text to feel
|
| 106 |
-
f"Ensure
|
| 107 |
-
f"The tone should be {tone}. Here's the text: {output_text}"
|
| 108 |
)
|
| 109 |
try:
|
| 110 |
refinement_response = client.chat.completions.create(
|
|
@@ -113,9 +112,11 @@ def refine_humanization(output_text, tone):
|
|
| 113 |
stream=False,
|
| 114 |
)
|
| 115 |
refined_text = refinement_response.choices[0].message.content.strip()
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
| 119 |
return refined_text
|
| 120 |
except Exception:
|
| 121 |
return output_text # Return original output if refinement fails
|
|
@@ -163,8 +164,8 @@ if st.button("Generate Output"):
|
|
| 163 |
for chunk in text_chunks:
|
| 164 |
if task_option == "Humanize Text":
|
| 165 |
task_prompt = (
|
| 166 |
-
f"Make the following text
|
| 167 |
-
f"Focus on clarity
|
| 168 |
)
|
| 169 |
else:
|
| 170 |
task_prompt = f"Rephrase the following text while maintaining its original meaning: {chunk}"
|
|
@@ -176,8 +177,8 @@ if st.button("Generate Output"):
|
|
| 176 |
)
|
| 177 |
|
| 178 |
output_text = chat_completion.choices[0].message.content.strip()
|
| 179 |
-
for
|
| 180 |
-
output_text = refine_humanization(output_text, tone)
|
| 181 |
output_chunks.append(output_text)
|
| 182 |
|
| 183 |
final_output = " ".join(output_chunks)
|
|
|
|
| 69 |
filler_words = ["uh,", "um,", "so,", "well,", "I guess,"]
|
| 70 |
sentences = output_text.split(". ")
|
| 71 |
noisy_sentences = [
|
| 72 |
+
f"{random.choice(noise_phrases)} {sentence}" if random.random() < 0.2 else sentence
|
| 73 |
for sentence in sentences
|
| 74 |
]
|
| 75 |
noisy_sentences = [
|
| 76 |
+
f"{sentence} {random.choice(filler_words)}" if random.random() < 0.15 else sentence
|
| 77 |
for sentence in noisy_sentences
|
| 78 |
]
|
| 79 |
return ". ".join(noisy_sentences)
|
|
|
|
| 85 |
for sentence in sentences:
|
| 86 |
words = sentence.split()
|
| 87 |
if len(words) > 15:
|
| 88 |
+
midpoint = random.randint(len(words) // 3, len(words) // 2)
|
| 89 |
adjusted_sentences.append(" ".join(words[:midpoint]) + ", you know,")
|
| 90 |
adjusted_sentences.append(" ".join(words[midpoint:]))
|
| 91 |
else:
|
|
|
|
| 100 |
corrected_text = tool.correct(output_text)
|
| 101 |
return corrected_text
|
| 102 |
|
| 103 |
+
def refine_humanization(output_text, tone, iteration):
|
| 104 |
refinement_prompt = (
|
| 105 |
+
f"Refine the following text to feel natural, slightly imperfect, and conversational. "
|
| 106 |
+
f"Ensure it sounds human and matches a {tone} tone. Here's the text: {output_text}"
|
|
|
|
| 107 |
)
|
| 108 |
try:
|
| 109 |
refinement_response = client.chat.completions.create(
|
|
|
|
| 112 |
stream=False,
|
| 113 |
)
|
| 114 |
refined_text = refinement_response.choices[0].message.content.strip()
|
| 115 |
+
|
| 116 |
+
if iteration > 1:
|
| 117 |
+
refined_text = add_human_noise(refined_text)
|
| 118 |
+
refined_text = adjust_sentence_structure(refined_text)
|
| 119 |
+
|
| 120 |
return refined_text
|
| 121 |
except Exception:
|
| 122 |
return output_text # Return original output if refinement fails
|
|
|
|
| 164 |
for chunk in text_chunks:
|
| 165 |
if task_option == "Humanize Text":
|
| 166 |
task_prompt = (
|
| 167 |
+
f"Make the following text engaging and natural in a {tone} tone. "
|
| 168 |
+
f"Focus on clarity and avoid robotic patterns: {chunk}"
|
| 169 |
)
|
| 170 |
else:
|
| 171 |
task_prompt = f"Rephrase the following text while maintaining its original meaning: {chunk}"
|
|
|
|
| 177 |
)
|
| 178 |
|
| 179 |
output_text = chat_completion.choices[0].message.content.strip()
|
| 180 |
+
for i in range(humanization_depth):
|
| 181 |
+
output_text = refine_humanization(output_text, tone, i + 1)
|
| 182 |
output_chunks.append(output_text)
|
| 183 |
|
| 184 |
final_output = " ".join(output_chunks)
|