sundaram07 commited on
Commit
6adf923
Β·
verified Β·
1 Parent(s): ed60e85

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # πŸ“ Use safe cache directory
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 session state
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 all sentences
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 App UI
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
- # Session state to track if user clicked analyze
 
 
 
62
  if "analysis_done" not in st.session_state:
63
  st.session_state.analysis_done = False
 
 
64
 
65
- # πŸ“‹ Input Area
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 after button press
79
- if st.session_state.get("analysis_done", False):
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"