Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,44 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
import os
|
|
|
|
| 3 |
import random
|
|
|
|
| 4 |
from groq import Groq
|
| 5 |
import language_tool_python
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Initialize Groq client
|
| 8 |
GROQ_API_KEY = "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" # Replace with your Groq API key
|
| 9 |
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
| 10 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 11 |
|
| 12 |
-
# Initialize
|
| 13 |
try:
|
| 14 |
tool = language_tool_python.LanguageTool('en-US')
|
| 15 |
-
except
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
# Helper Functions
|
| 20 |
def add_human_noise(output_text):
|
| 21 |
-
noise_phrases = [
|
|
|
|
|
|
|
|
|
|
| 22 |
filler_words = ["uh,", "um,", "so,", "well,", "I guess,"]
|
| 23 |
sentences = output_text.split(". ")
|
| 24 |
noisy_sentences = [
|
|
@@ -34,6 +54,7 @@ def add_human_noise(output_text):
|
|
| 34 |
def adjust_sentence_structure(output_text):
|
| 35 |
sentences = output_text.split(". ")
|
| 36 |
adjusted_sentences = []
|
|
|
|
| 37 |
for sentence in sentences:
|
| 38 |
words = sentence.split()
|
| 39 |
if len(words) > 15:
|
|
@@ -42,15 +63,13 @@ def adjust_sentence_structure(output_text):
|
|
| 42 |
adjusted_sentences.append(" ".join(words[midpoint:]))
|
| 43 |
else:
|
| 44 |
adjusted_sentences.append(sentence)
|
|
|
|
| 45 |
return ". ".join(adjusted_sentences)
|
| 46 |
|
| 47 |
def check_grammar(output_text):
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
return corrected_text
|
| 52 |
-
else:
|
| 53 |
-
return output_text
|
| 54 |
|
| 55 |
def refine_humanization(output_text, tone):
|
| 56 |
refinement_prompt = (
|
|
@@ -70,7 +89,7 @@ def refine_humanization(output_text, tone):
|
|
| 70 |
refined_text = check_grammar(refined_text)
|
| 71 |
return refined_text
|
| 72 |
except Exception:
|
| 73 |
-
return output_text
|
| 74 |
|
| 75 |
def split_text_into_chunks(text, max_words=500):
|
| 76 |
words = text.split()
|
|
@@ -86,11 +105,23 @@ input_word_count = len(input_text.split()) if input_text.strip() else 0
|
|
| 86 |
st.write(f"**Input Word Count:** {input_word_count}")
|
| 87 |
|
| 88 |
# Options
|
| 89 |
-
task_option = st.radio(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
tone = st.selectbox("Select tone (for humanizing):", ["Casual", "Professional", "Neutral", "Engaging", "Friendly"])
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
|
|
|
| 94 |
if st.button("Generate Output"):
|
| 95 |
if not input_text.strip():
|
| 96 |
st.error("Please enter some text to process.")
|
|
@@ -99,18 +130,40 @@ if st.button("Generate Output"):
|
|
| 99 |
with st.spinner("Processing..."):
|
| 100 |
text_chunks = split_text_into_chunks(input_text, max_words=500)
|
| 101 |
output_chunks = []
|
|
|
|
| 102 |
for chunk in text_chunks:
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
chat_completion = client.chat.completions.create(
|
| 105 |
messages=[{"role": "user", "content": task_prompt}],
|
| 106 |
model="llama-3.3-70b-versatile",
|
| 107 |
stream=False,
|
| 108 |
)
|
|
|
|
| 109 |
output_text = chat_completion.choices[0].message.content.strip()
|
| 110 |
for _ in range(humanization_depth):
|
| 111 |
output_text = refine_humanization(output_text, tone)
|
| 112 |
output_chunks.append(output_text)
|
|
|
|
| 113 |
final_output = " ".join(output_chunks)
|
|
|
|
|
|
|
|
|
|
| 114 |
st.text_area("Generated Output:", value=final_output, height=300)
|
|
|
|
|
|
|
| 115 |
except Exception as e:
|
| 116 |
-
st.error(f"An error occurred: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import subprocess
|
| 3 |
import random
|
| 4 |
+
import streamlit as st
|
| 5 |
from groq import Groq
|
| 6 |
import language_tool_python
|
| 7 |
|
| 8 |
+
# Function to check and install Java
|
| 9 |
+
def install_java():
|
| 10 |
+
try:
|
| 11 |
+
# Check if Java is installed
|
| 12 |
+
subprocess.run(["java", "-version"], check=True, capture_output=True)
|
| 13 |
+
st.write("Java is already installed.")
|
| 14 |
+
except FileNotFoundError:
|
| 15 |
+
# Install Java
|
| 16 |
+
st.write("Java is not installed. Installing now...")
|
| 17 |
+
subprocess.run(["apt-get", "update"], check=True)
|
| 18 |
+
subprocess.run(["apt-get", "install", "-y", "default-jre"], check=True)
|
| 19 |
+
st.write("Java installed successfully.")
|
| 20 |
+
|
| 21 |
+
# Run the Java installation check
|
| 22 |
+
install_java()
|
| 23 |
+
|
| 24 |
# Initialize Groq client
|
| 25 |
GROQ_API_KEY = "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" # Replace with your Groq API key
|
| 26 |
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
| 27 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 28 |
|
| 29 |
+
# Initialize LanguageTool
|
| 30 |
try:
|
| 31 |
tool = language_tool_python.LanguageTool('en-US')
|
| 32 |
+
except Exception as e:
|
| 33 |
+
st.error("Error initializing LanguageTool: Please ensure Java is installed.")
|
| 34 |
+
raise e
|
| 35 |
|
| 36 |
# Helper Functions
|
| 37 |
def add_human_noise(output_text):
|
| 38 |
+
noise_phrases = [
|
| 39 |
+
"you know,", "like I said,", "to be honest,", "frankly,",
|
| 40 |
+
"truth be told,", "I mean,", "well, honestly,", "if I'm being real,"
|
| 41 |
+
]
|
| 42 |
filler_words = ["uh,", "um,", "so,", "well,", "I guess,"]
|
| 43 |
sentences = output_text.split(". ")
|
| 44 |
noisy_sentences = [
|
|
|
|
| 54 |
def adjust_sentence_structure(output_text):
|
| 55 |
sentences = output_text.split(". ")
|
| 56 |
adjusted_sentences = []
|
| 57 |
+
|
| 58 |
for sentence in sentences:
|
| 59 |
words = sentence.split()
|
| 60 |
if len(words) > 15:
|
|
|
|
| 63 |
adjusted_sentences.append(" ".join(words[midpoint:]))
|
| 64 |
else:
|
| 65 |
adjusted_sentences.append(sentence)
|
| 66 |
+
|
| 67 |
return ". ".join(adjusted_sentences)
|
| 68 |
|
| 69 |
def check_grammar(output_text):
|
| 70 |
+
matches = tool.check(output_text)
|
| 71 |
+
corrected_text = tool.correct(output_text)
|
| 72 |
+
return corrected_text
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
def refine_humanization(output_text, tone):
|
| 75 |
refinement_prompt = (
|
|
|
|
| 89 |
refined_text = check_grammar(refined_text)
|
| 90 |
return refined_text
|
| 91 |
except Exception:
|
| 92 |
+
return output_text # Return original output if refinement fails
|
| 93 |
|
| 94 |
def split_text_into_chunks(text, max_words=500):
|
| 95 |
words = text.split()
|
|
|
|
| 105 |
st.write(f"**Input Word Count:** {input_word_count}")
|
| 106 |
|
| 107 |
# Options
|
| 108 |
+
task_option = st.radio(
|
| 109 |
+
"Choose an option:",
|
| 110 |
+
("Humanize Text", "Rephrase Text"),
|
| 111 |
+
index=0
|
| 112 |
+
)
|
| 113 |
tone = st.selectbox("Select tone (for humanizing):", ["Casual", "Professional", "Neutral", "Engaging", "Friendly"])
|
| 114 |
|
| 115 |
+
# Depth of Humanization
|
| 116 |
+
humanization_depth = st.slider(
|
| 117 |
+
"Select depth of humanization:",
|
| 118 |
+
min_value=1,
|
| 119 |
+
max_value=5,
|
| 120 |
+
value=3,
|
| 121 |
+
help="Higher values apply more refinements to make the text appear less AI-like."
|
| 122 |
+
)
|
| 123 |
|
| 124 |
+
# Generate Output
|
| 125 |
if st.button("Generate Output"):
|
| 126 |
if not input_text.strip():
|
| 127 |
st.error("Please enter some text to process.")
|
|
|
|
| 130 |
with st.spinner("Processing..."):
|
| 131 |
text_chunks = split_text_into_chunks(input_text, max_words=500)
|
| 132 |
output_chunks = []
|
| 133 |
+
|
| 134 |
for chunk in text_chunks:
|
| 135 |
+
if task_option == "Humanize Text":
|
| 136 |
+
task_prompt = (
|
| 137 |
+
f"Make the following text highly human-readable, engaging, and polished in a {tone} tone. "
|
| 138 |
+
f"Focus on clarity, flow, and avoiding AI-detected phrasing: {chunk}"
|
| 139 |
+
)
|
| 140 |
+
else:
|
| 141 |
+
task_prompt = f"Rephrase the following text while maintaining its original meaning: {chunk}"
|
| 142 |
+
|
| 143 |
chat_completion = client.chat.completions.create(
|
| 144 |
messages=[{"role": "user", "content": task_prompt}],
|
| 145 |
model="llama-3.3-70b-versatile",
|
| 146 |
stream=False,
|
| 147 |
)
|
| 148 |
+
|
| 149 |
output_text = chat_completion.choices[0].message.content.strip()
|
| 150 |
for _ in range(humanization_depth):
|
| 151 |
output_text = refine_humanization(output_text, tone)
|
| 152 |
output_chunks.append(output_text)
|
| 153 |
+
|
| 154 |
final_output = " ".join(output_chunks)
|
| 155 |
+
output_word_count = len(final_output.split())
|
| 156 |
+
|
| 157 |
+
st.success("Done!")
|
| 158 |
st.text_area("Generated Output:", value=final_output, height=300)
|
| 159 |
+
st.write(f"**Output Word Count:** {output_word_count}")
|
| 160 |
+
|
| 161 |
except Exception as e:
|
| 162 |
+
st.error(f"An error occurred: {str(e)}")
|
| 163 |
+
|
| 164 |
+
# Footer
|
| 165 |
+
st.markdown("---")
|
| 166 |
+
st.markdown(
|
| 167 |
+
"<p style='text-align: center; font-size: 14px;'>Designed by: <b>Engr. Makhdoom Muhammad Naeem Hashmi</b></p>",
|
| 168 |
+
unsafe_allow_html=True
|
| 169 |
+
)
|