Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,7 +36,7 @@ def ensure_languagetool_server():
|
|
| 36 |
def initialize_language_tool():
|
| 37 |
try:
|
| 38 |
st.write("Initializing LanguageTool...")
|
| 39 |
-
ensure_languagetool_server()
|
| 40 |
tool = language_tool_python.LanguageTool('en-US')
|
| 41 |
st.write("LanguageTool initialized successfully.")
|
| 42 |
return tool
|
|
@@ -48,7 +48,7 @@ def initialize_language_tool():
|
|
| 48 |
install_java()
|
| 49 |
|
| 50 |
# Initialize Groq client
|
| 51 |
-
GROQ_API_KEY = "
|
| 52 |
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
| 53 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 54 |
|
|
@@ -69,13 +69,9 @@ 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.2 else sentence
|
| 77 |
-
for sentence in noisy_sentences
|
| 78 |
-
]
|
| 79 |
return ". ".join(noisy_sentences)
|
| 80 |
|
| 81 |
def adjust_sentence_structure(output_text):
|
|
@@ -84,7 +80,7 @@ def adjust_sentence_structure(output_text):
|
|
| 84 |
|
| 85 |
for sentence in sentences:
|
| 86 |
words = sentence.split()
|
| 87 |
-
if len(words) >
|
| 88 |
split_point = random.randint(len(words) // 3, 2 * len(words) // 3)
|
| 89 |
adjusted_sentences.append(" ".join(words[:split_point]) + ", you know,")
|
| 90 |
adjusted_sentences.append(" ".join(words[split_point:]))
|
|
@@ -102,8 +98,8 @@ def check_grammar(output_text):
|
|
| 102 |
|
| 103 |
def refine_humanization(output_text, tone, iteration):
|
| 104 |
refinement_prompt = (
|
| 105 |
-
f"Refine the following text to
|
| 106 |
-
f"Ensure it
|
| 107 |
)
|
| 108 |
try:
|
| 109 |
refinement_response = client.chat.completions.create(
|
|
@@ -113,12 +109,11 @@ def refine_humanization(output_text, tone, iteration):
|
|
| 113 |
)
|
| 114 |
refined_text = refinement_response.choices[0].message.content.strip()
|
| 115 |
|
| 116 |
-
# Apply noise and adjust structure in subsequent iterations
|
| 117 |
if iteration > 1:
|
| 118 |
refined_text = add_human_noise(refined_text)
|
| 119 |
refined_text = adjust_sentence_structure(refined_text)
|
| 120 |
|
| 121 |
-
return check_grammar(refined_text)
|
| 122 |
except Exception:
|
| 123 |
return output_text # Return original output if refinement fails
|
| 124 |
|
|
|
|
| 36 |
def initialize_language_tool():
|
| 37 |
try:
|
| 38 |
st.write("Initializing LanguageTool...")
|
| 39 |
+
ensure_languagetool_server()
|
| 40 |
tool = language_tool_python.LanguageTool('en-US')
|
| 41 |
st.write("LanguageTool initialized successfully.")
|
| 42 |
return tool
|
|
|
|
| 48 |
install_java()
|
| 49 |
|
| 50 |
# Initialize Groq client
|
| 51 |
+
GROQ_API_KEY = "your-groq-api-key" # Replace with your Groq API key
|
| 52 |
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
| 53 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 54 |
|
|
|
|
| 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 |
return ". ".join(noisy_sentences)
|
| 76 |
|
| 77 |
def adjust_sentence_structure(output_text):
|
|
|
|
| 80 |
|
| 81 |
for sentence in sentences:
|
| 82 |
words = sentence.split()
|
| 83 |
+
if len(words) > 15:
|
| 84 |
split_point = random.randint(len(words) // 3, 2 * len(words) // 3)
|
| 85 |
adjusted_sentences.append(" ".join(words[:split_point]) + ", you know,")
|
| 86 |
adjusted_sentences.append(" ".join(words[split_point:]))
|
|
|
|
| 98 |
|
| 99 |
def refine_humanization(output_text, tone, iteration):
|
| 100 |
refinement_prompt = (
|
| 101 |
+
f"Refine the following text to sound natural, slightly imperfect, and conversational. "
|
| 102 |
+
f"Ensure it aligns with a {tone} tone: {output_text}"
|
| 103 |
)
|
| 104 |
try:
|
| 105 |
refinement_response = client.chat.completions.create(
|
|
|
|
| 109 |
)
|
| 110 |
refined_text = refinement_response.choices[0].message.content.strip()
|
| 111 |
|
|
|
|
| 112 |
if iteration > 1:
|
| 113 |
refined_text = add_human_noise(refined_text)
|
| 114 |
refined_text = adjust_sentence_structure(refined_text)
|
| 115 |
|
| 116 |
+
return check_grammar(refined_text)
|
| 117 |
except Exception:
|
| 118 |
return output_text # Return original output if refinement fails
|
| 119 |
|