vivv56 commited on
Commit
615c71a
Β·
verified Β·
1 Parent(s): 396953b

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +51 -9
streamlit_app.py CHANGED
@@ -1,6 +1,14 @@
1
  import streamlit as st
 
2
  import torch
3
  from transformers import GPT2LMHeadModel, GPT2TokenizerFast
 
 
 
 
 
 
 
4
 
5
  # -------------------------------
6
  # Load GPT-2 model and tokenizer
@@ -25,30 +33,64 @@ def calculate_perplexity(text):
25
  perplexity = torch.exp(loss).item()
26
  return perplexity
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  # -------------------------------
29
  # Streamlit UI
30
  # -------------------------------
31
- st.set_page_config(page_title="Text Perplexity Checker", page_icon="πŸ“Š", layout="centered")
32
 
33
  st.markdown("""
34
- <h2 style='text-align: center; color: #4CAF50;'>πŸ“Š Text Perplexity Checker</h2>
35
- <p style='text-align: center;'>Check the perplexity of a sentence to estimate its predictability.</p>
36
  """, unsafe_allow_html=True)
37
 
38
  user_input = st.text_area("Enter your sentence here:", height=150)
39
 
40
- if st.button("Calculate Perplexity"):
41
  if user_input.strip() == "":
42
  st.warning("Please enter a sentence before submitting.")
43
  else:
 
 
 
 
 
 
 
44
  perplexity_score = calculate_perplexity(user_input)
45
 
46
- st.subheader("πŸ“ˆ Perplexity Result")
 
 
 
 
 
 
 
 
 
 
47
  st.markdown(f"**Perplexity Score:** {perplexity_score:.2f}")
 
48
 
 
49
  if perplexity_score < 30:
50
- st.info("🧠 Low perplexity: The text is highly predictable (possibly AI-generated).")
51
  elif perplexity_score > 100:
52
- st.info("🧠 High perplexity: The text is more varied and complex (likely human-written).")
53
- else:
54
- st.info("🧠 Moderate perplexity: Could be either human or AI-generated.")
 
1
  import streamlit as st
2
+ import joblib
3
  import torch
4
  from transformers import GPT2LMHeadModel, GPT2TokenizerFast
5
+ import numpy as np
6
+
7
+ # -------------------------------
8
+ # Load Logistic Regression model
9
+ # -------------------------------
10
+ vectorizer = joblib.load('src/vectorizer.pkl')
11
+ model = joblib.load('src/logistic_model.pkl')
12
 
13
  # -------------------------------
14
  # Load GPT-2 model and tokenizer
 
33
  perplexity = torch.exp(loss).item()
34
  return perplexity
35
 
36
+ # -------------------------------
37
+ # Final prediction combining both
38
+ # -------------------------------
39
+ def final_score(ai_prob, perplexity):
40
+ # Normalize perplexity: higher => more human-like (score 0), lower => more AI-like (score 1)
41
+ if perplexity > 300:
42
+ perp_score = 0.0
43
+ elif perplexity < 10:
44
+ perp_score = 1.0
45
+ else:
46
+ perp_score = 1.0 - ((perplexity - 10) / (300 - 10)) # scale to [0,1]
47
+ perp_score = max(0.0, min(1.0, perp_score))
48
+
49
+ # Combine: 70% weight to perplexity, 30% to logistic regression
50
+ final_ai_score = (0.7 * perp_score) + (0.3 * ai_prob)
51
+ return final_ai_score, perp_score
52
+
53
  # -------------------------------
54
  # Streamlit UI
55
  # -------------------------------
56
+ st.set_page_config(page_title="AI Text Detector", page_icon="πŸ€–", layout="centered")
57
 
58
  st.markdown("""
59
+ <h2 style='text-align: center; color: #4CAF50;'>πŸ€– AI vs Human Text Detector</h2>
60
+ <p style='text-align: center;'>Enter a sentence to check if it was written by a human or generated by AI.</p>
61
  """, unsafe_allow_html=True)
62
 
63
  user_input = st.text_area("Enter your sentence here:", height=150)
64
 
65
+ if st.button("Check"):
66
  if user_input.strip() == "":
67
  st.warning("Please enter a sentence before submitting.")
68
  else:
69
+ # Logistic model prediction
70
+ transformed_input = vectorizer.transform([user_input])
71
+ prediction = model.predict_proba(transformed_input)
72
+ ai_prob = prediction[0][1]
73
+ human_prob = prediction[0][0]
74
+
75
+ # Perplexity calculation
76
  perplexity_score = calculate_perplexity(user_input)
77
 
78
+ # Combine scores
79
+ final_ai_score, perp_score = final_score(ai_prob, perplexity_score)
80
+
81
+ # Display Results
82
+ st.subheader("πŸ” Result:")
83
+ if final_ai_score > 0.5:
84
+ st.error("❗ This text is likely **AI-generated**.")
85
+ else:
86
+ st.success("βœ… This text is likely **Human-written**.")
87
+
88
+ st.markdown(f"**Logistic Model Confidence:** {ai_prob:.3f} AI vs {human_prob:.3f} Human")
89
  st.markdown(f"**Perplexity Score:** {perplexity_score:.2f}")
90
+ st.markdown(f"**Combined AI Score:** {final_ai_score:.3f} (Weighted)")
91
 
92
+ # Interpretation
93
  if perplexity_score < 30:
94
+ st.info("🧠 Low perplexity suggests the text is highly predictableβ€”possibly AI-generated.")
95
  elif perplexity_score > 100:
96
+ st.info("🧠 High perplexity suggests human-like variation or complexity.")