Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,53 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from utils import load_model, preprocess_text
|
| 3 |
-
import nltk
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
result
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# Reset check state
|
| 54 |
st.session_state.check_clicked = False
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from utils import load_model, preprocess_text
|
| 3 |
+
import nltk
|
| 4 |
+
|
| 5 |
+
model = load_model('./models/best_model.joblib')
|
| 6 |
+
|
| 7 |
+
min_words_number = 100
|
| 8 |
+
|
| 9 |
+
def check_generated_text(text):
|
| 10 |
+
filtered_text = preprocess_text(text)
|
| 11 |
+
prediction = model.predict([filtered_text])
|
| 12 |
+
return not int(prediction[0])
|
| 13 |
+
|
| 14 |
+
# Load styles
|
| 15 |
+
with open("styles.css") as f:
|
| 16 |
+
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
| 17 |
+
|
| 18 |
+
# Title
|
| 19 |
+
st.title("Generated Text Checker")
|
| 20 |
+
|
| 21 |
+
# Initialize session state
|
| 22 |
+
if "check_clicked" not in st.session_state:
|
| 23 |
+
st.session_state.check_clicked = False
|
| 24 |
+
|
| 25 |
+
# Use a form to isolate the check action
|
| 26 |
+
with st.form("text_check_form"):
|
| 27 |
+
user_input = st.text_area(
|
| 28 |
+
f"Enter text to check",
|
| 29 |
+
height=400,
|
| 30 |
+
placeholder=f"Paste your generated text here... it should be at least {min_words_number} words"
|
| 31 |
+
)
|
| 32 |
+
submitted = st.form_submit_button("Check text")
|
| 33 |
+
|
| 34 |
+
# Handle form submission
|
| 35 |
+
if submitted:
|
| 36 |
+
st.session_state.check_clicked = True
|
| 37 |
+
|
| 38 |
+
# Only run check when button is clicked
|
| 39 |
+
if st.session_state.check_clicked:
|
| 40 |
+
with st.spinner("Checking text..."):
|
| 41 |
+
current_length = len(user_input.split())
|
| 42 |
+
|
| 43 |
+
if current_length >= min_words_number:
|
| 44 |
+
result = check_generated_text(user_input)
|
| 45 |
+
if result:
|
| 46 |
+
st.info("✅ The text appears to be human-written!")
|
| 47 |
+
else:
|
| 48 |
+
st.info("🤖 The text appears to be AI-generated.")
|
| 49 |
+
else:
|
| 50 |
+
st.warning(f"Please enter at least {min_words_number} words.")
|
| 51 |
+
|
| 52 |
+
# Reset check state
|
|
|
|
| 53 |
st.session_state.check_clicked = False
|