Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -61,32 +61,29 @@ except Exception as e:
|
|
| 61 |
st.error(f"Details: {str(e)}")
|
| 62 |
|
| 63 |
# Helper Functions
|
| 64 |
-
def adjust_sentence_structure(output_text
|
| 65 |
sentences = output_text.split(". ")
|
| 66 |
adjusted_sentences = []
|
| 67 |
-
|
| 68 |
for sentence in sentences:
|
| 69 |
words = sentence.split()
|
| 70 |
-
if len(words) >
|
| 71 |
split_point = random.randint(len(words) // 3, 2 * len(words) // 3)
|
| 72 |
adjusted_sentences.append(" ".join(words[:split_point]))
|
| 73 |
adjusted_sentences.append(" ".join(words[split_point:]))
|
| 74 |
else:
|
| 75 |
adjusted_sentences.append(sentence)
|
| 76 |
-
|
| 77 |
return ". ".join(adjusted_sentences)
|
| 78 |
|
| 79 |
def check_grammar(output_text):
|
| 80 |
if tool is None:
|
| 81 |
return output_text
|
| 82 |
matches = tool.check(output_text)
|
| 83 |
-
|
| 84 |
-
return corrected_text
|
| 85 |
|
| 86 |
def refine_humanization(output_text, tone, iteration):
|
| 87 |
refinement_prompt = (
|
| 88 |
-
f"Refine the following text to sound
|
| 89 |
-
f"
|
| 90 |
)
|
| 91 |
try:
|
| 92 |
refinement_response = client.chat.completions.create(
|
|
@@ -96,9 +93,9 @@ def refine_humanization(output_text, tone, iteration):
|
|
| 96 |
)
|
| 97 |
refined_text = refinement_response.choices[0].message.content.strip()
|
| 98 |
|
| 99 |
-
# Apply
|
| 100 |
if iteration > 1:
|
| 101 |
-
refined_text = adjust_sentence_structure(refined_text
|
| 102 |
|
| 103 |
return check_grammar(refined_text)
|
| 104 |
except Exception:
|
|
@@ -145,20 +142,15 @@ if st.button("Generate Output"):
|
|
| 145 |
output_chunks = []
|
| 146 |
|
| 147 |
for chunk in text_chunks:
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
)
|
| 153 |
-
else:
|
| 154 |
-
task_prompt = f"Rephrase the following text while maintaining its original meaning: {chunk}"
|
| 155 |
-
|
| 156 |
chat_completion = client.chat.completions.create(
|
| 157 |
messages=[{"role": "user", "content": task_prompt}],
|
| 158 |
model="llama-3.3-70b-versatile",
|
| 159 |
stream=False,
|
| 160 |
)
|
| 161 |
-
|
| 162 |
output_text = chat_completion.choices[0].message.content.strip()
|
| 163 |
for i in range(humanization_depth):
|
| 164 |
output_text = refine_humanization(output_text, tone, i + 1)
|
|
|
|
| 61 |
st.error(f"Details: {str(e)}")
|
| 62 |
|
| 63 |
# Helper Functions
|
| 64 |
+
def adjust_sentence_structure(output_text):
|
| 65 |
sentences = output_text.split(". ")
|
| 66 |
adjusted_sentences = []
|
|
|
|
| 67 |
for sentence in sentences:
|
| 68 |
words = sentence.split()
|
| 69 |
+
if len(words) > 20: # Split overly long sentences
|
| 70 |
split_point = random.randint(len(words) // 3, 2 * len(words) // 3)
|
| 71 |
adjusted_sentences.append(" ".join(words[:split_point]))
|
| 72 |
adjusted_sentences.append(" ".join(words[split_point:]))
|
| 73 |
else:
|
| 74 |
adjusted_sentences.append(sentence)
|
|
|
|
| 75 |
return ". ".join(adjusted_sentences)
|
| 76 |
|
| 77 |
def check_grammar(output_text):
|
| 78 |
if tool is None:
|
| 79 |
return output_text
|
| 80 |
matches = tool.check(output_text)
|
| 81 |
+
return tool.correct(output_text)
|
|
|
|
| 82 |
|
| 83 |
def refine_humanization(output_text, tone, iteration):
|
| 84 |
refinement_prompt = (
|
| 85 |
+
f"Refine the following text to sound natural, human-like, and professional. "
|
| 86 |
+
f"Preserve the original meaning, avoid irrelevant details, and align with a {tone} tone: {output_text}"
|
| 87 |
)
|
| 88 |
try:
|
| 89 |
refinement_response = client.chat.completions.create(
|
|
|
|
| 93 |
)
|
| 94 |
refined_text = refinement_response.choices[0].message.content.strip()
|
| 95 |
|
| 96 |
+
# Apply structural adjustments after the first iteration
|
| 97 |
if iteration > 1:
|
| 98 |
+
refined_text = adjust_sentence_structure(refined_text)
|
| 99 |
|
| 100 |
return check_grammar(refined_text)
|
| 101 |
except Exception:
|
|
|
|
| 142 |
output_chunks = []
|
| 143 |
|
| 144 |
for chunk in text_chunks:
|
| 145 |
+
task_prompt = (
|
| 146 |
+
f"{'Humanize' if task_option == 'Humanize Text' else 'Rephrase'} "
|
| 147 |
+
f"this text while ensuring clarity, professionalism, and original meaning: {chunk}"
|
| 148 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
chat_completion = client.chat.completions.create(
|
| 150 |
messages=[{"role": "user", "content": task_prompt}],
|
| 151 |
model="llama-3.3-70b-versatile",
|
| 152 |
stream=False,
|
| 153 |
)
|
|
|
|
| 154 |
output_text = chat_completion.choices[0].message.content.strip()
|
| 155 |
for i in range(humanization_depth):
|
| 156 |
output_text = refine_humanization(output_text, tone, i + 1)
|