Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +21 -13
src/streamlit_app.py
CHANGED
@@ -6,7 +6,7 @@ import os
|
|
6 |
from nltk.tokenize import sent_tokenize
|
7 |
from transformers import DistilBertTokenizerFast, TFDistilBertForSequenceClassification
|
8 |
|
9 |
-
# π
|
10 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
|
11 |
|
12 |
# π₯ Download NLTK tokenizer
|
@@ -14,7 +14,7 @@ nltk_data_path = "/tmp/nltk_data"
|
|
14 |
nltk.download("punkt_tab", download_dir=nltk_data_path)
|
15 |
nltk.data.path.append(nltk_data_path)
|
16 |
|
17 |
-
# π Load model & tokenizer once using
|
18 |
@st.cache_resource
|
19 |
def load_model_and_tokenizer():
|
20 |
tokenizer = DistilBertTokenizerFast.from_pretrained(
|
@@ -35,7 +35,7 @@ def predict_sentence_ai_probability(sentence):
|
|
35 |
prob_ai = tf.sigmoid(logits)[0][0].numpy()
|
36 |
return prob_ai
|
37 |
|
38 |
-
# π Analyze
|
39 |
def predict_ai_generated_percentage(text, threshold=0.15):
|
40 |
text = text.strip()
|
41 |
sentences = sent_tokenize(text)
|
@@ -53,30 +53,38 @@ def predict_ai_generated_percentage(text, threshold=0.15):
|
|
53 |
ai_percentage = (ai_sentence_count / total_sentences) * 100 if total_sentences > 0 else 0.0
|
54 |
return ai_percentage, results
|
55 |
|
56 |
-
# π₯οΈ Streamlit
|
57 |
st.set_page_config(page_title="AI Detector", layout="wide")
|
58 |
st.title("π§ AI Content Detector")
|
59 |
-
st.markdown("This app detects the percentage of **AI-generated content** using DistilBERT.")
|
60 |
|
61 |
-
#
|
|
|
|
|
|
|
62 |
if "analysis_done" not in st.session_state:
|
63 |
st.session_state.analysis_done = False
|
|
|
|
|
64 |
|
65 |
-
#
|
66 |
-
user_input = st.text_area("π Paste your text below to check for AI-generated sentences:", height=300)
|
67 |
-
|
68 |
-
# π Analyze Button
|
69 |
if st.button("π Analyze"):
|
|
|
|
|
|
|
|
|
|
|
70 |
if not user_input.strip():
|
71 |
st.warning("β οΈ Please enter some text.")
|
72 |
else:
|
|
|
73 |
ai_percentage, analysis_results = predict_ai_generated_percentage(user_input)
|
74 |
st.session_state.analysis_done = True
|
75 |
-
st.session_state.ai_percentage = ai_percentage
|
76 |
st.session_state.analysis_results = analysis_results
|
|
|
77 |
|
78 |
-
# π€ Show results
|
79 |
-
if st.session_state.
|
80 |
st.subheader("π Sentence-level Analysis")
|
81 |
for i, (sentence, prob, is_ai) in enumerate(st.session_state.analysis_results, start=1):
|
82 |
label = "π’ Human" if not is_ai else "π΄ AI"
|
|
|
6 |
from nltk.tokenize import sent_tokenize
|
7 |
from transformers import DistilBertTokenizerFast, TFDistilBertForSequenceClassification
|
8 |
|
9 |
+
# π Set Hugging Face cache directory (safe for deployments)
|
10 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
|
11 |
|
12 |
# π₯ Download NLTK tokenizer
|
|
|
14 |
nltk.download("punkt_tab", download_dir=nltk_data_path)
|
15 |
nltk.data.path.append(nltk_data_path)
|
16 |
|
17 |
+
# π Load model & tokenizer once using cache
|
18 |
@st.cache_resource
|
19 |
def load_model_and_tokenizer():
|
20 |
tokenizer = DistilBertTokenizerFast.from_pretrained(
|
|
|
35 |
prob_ai = tf.sigmoid(logits)[0][0].numpy()
|
36 |
return prob_ai
|
37 |
|
38 |
+
# π Analyze text
|
39 |
def predict_ai_generated_percentage(text, threshold=0.15):
|
40 |
text = text.strip()
|
41 |
sentences = sent_tokenize(text)
|
|
|
53 |
ai_percentage = (ai_sentence_count / total_sentences) * 100 if total_sentences > 0 else 0.0
|
54 |
return ai_percentage, results
|
55 |
|
56 |
+
# π₯οΈ Streamlit UI setup
|
57 |
st.set_page_config(page_title="AI Detector", layout="wide")
|
58 |
st.title("π§ AI Content Detector")
|
59 |
+
st.markdown("This app detects the percentage of **AI-generated content** using sentence-level analysis with DistilBERT.")
|
60 |
|
61 |
+
# π Text input
|
62 |
+
user_input = st.text_area("π Paste your text below to check for AI-generated sentences:", height=300)
|
63 |
+
|
64 |
+
# β
Initialize session state
|
65 |
if "analysis_done" not in st.session_state:
|
66 |
st.session_state.analysis_done = False
|
67 |
+
st.session_state.analysis_results = None
|
68 |
+
st.session_state.ai_percentage = None
|
69 |
|
70 |
+
# π Analyze button logic
|
|
|
|
|
|
|
71 |
if st.button("π Analyze"):
|
72 |
+
# π§Ή Clear previous cache/state
|
73 |
+
st.session_state.analysis_done = False
|
74 |
+
st.session_state.analysis_results = None
|
75 |
+
st.session_state.ai_percentage = None
|
76 |
+
|
77 |
if not user_input.strip():
|
78 |
st.warning("β οΈ Please enter some text.")
|
79 |
else:
|
80 |
+
# Run fresh analysis
|
81 |
ai_percentage, analysis_results = predict_ai_generated_percentage(user_input)
|
82 |
st.session_state.analysis_done = True
|
|
|
83 |
st.session_state.analysis_results = analysis_results
|
84 |
+
st.session_state.ai_percentage = ai_percentage
|
85 |
|
86 |
+
# π€ Show results if analysis was done
|
87 |
+
if st.session_state.analysis_done:
|
88 |
st.subheader("π Sentence-level Analysis")
|
89 |
for i, (sentence, prob, is_ai) in enumerate(st.session_state.analysis_results, start=1):
|
90 |
label = "π’ Human" if not is_ai else "π΄ AI"
|