changcheng967 commited on
Commit
8826e0b
·
verified ·
1 Parent(s): c712a46

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +44 -36
src/streamlit_app.py CHANGED
@@ -1,55 +1,63 @@
1
- from transformers import pipeline
2
  import streamlit as st
3
  import torch
4
  import time
5
 
6
- # Limit CPU threads for better responsiveness
7
  torch.set_num_threads(2)
8
 
9
- st.title("AI Humanizer Lite (CPU Friendly)")
10
-
11
- # Load pipelines once per session
12
- if "detect_pipe" not in st.session_state or "humanizer_pipe" not in st.session_state:
13
- with st.spinner("Loading models (takes ~10s on CPU)..."):
14
- st.session_state.detect_pipe = pipeline(
15
- "text-classification",
16
- model="roberta-base-openai-detector",
17
- device=-1,
18
- )
19
- st.session_state.humanizer_pipe = pipeline(
20
- "text2text-generation",
21
- model="sshleifer/distilbart-cnn-12-6",
22
- device=-1,
23
- )
24
-
25
- def detect_ai(text: str) -> float:
26
- try:
27
- outputs = st.session_state.detect_pipe(text[:256])
 
 
 
 
 
 
 
28
  for out in outputs:
29
  if out["label"].lower() == "ai":
30
- return out["score"]
31
- except Exception as e:
32
- st.error(f"Error during AI detection: {e}")
33
- return 0.0
 
34
 
35
- def humanize_text(text: str) -> str:
 
 
36
  prompt = f"Rewrite this text naturally: {text}"
37
- try:
38
- result = st.session_state.humanizer_pipe(prompt, max_length=128, num_beams=3)
39
- return result[0]["generated_text"]
40
- except Exception as e:
41
- st.error(f"Error during text rewriting: {e}")
42
- return text
43
 
44
- text = st.text_area("Paste AI-generated text here", height=200)
45
 
46
- if st.button("Humanize"):
47
  if not text.strip():
48
  st.warning("Please enter some text!")
49
  else:
50
- with st.spinner("Detecting AI content..."):
 
51
  score = detect_ai(text)
52
- st.write(f"AI detection score: {score:.2%}")
 
53
 
54
  if score < 0.5:
55
  st.success("This text looks human already! No need to rewrite.")
 
1
+ from transformers import pipeline, AutoTokenizer
2
  import streamlit as st
3
  import torch
4
  import time
5
 
6
+ # Limit CPU threads for performance
7
  torch.set_num_threads(2)
8
 
9
+ st.title("AI Humanizer Lite (CPU Friendly) — Unlimited Text Detection")
10
+
11
+ @st.cache_resource(show_spinner=False)
12
+ def load_models():
13
+ detect_pipe = pipeline("text-classification", model="roberta-base-openai-detector", device=-1)
14
+ tokenizer = AutoTokenizer.from_pretrained("roberta-base-openai-detector")
15
+ return detect_pipe, tokenizer
16
+
17
+ detect_pipe, tokenizer = load_models()
18
+
19
+ def chunk_text(text, max_tokens=256):
20
+ tokens = tokenizer.tokenize(text)
21
+ chunks = []
22
+ for i in range(0, len(tokens), max_tokens):
23
+ chunk_tokens = tokens[i:i+max_tokens]
24
+ chunk_text = tokenizer.convert_tokens_to_string(chunk_tokens)
25
+ chunks.append(chunk_text)
26
+ return chunks
27
+
28
+ def detect_ai(text):
29
+ chunks = chunk_text(text)
30
+ scores = []
31
+ for chunk in chunks:
32
+ outputs = detect_pipe(chunk)
33
+ # Find "AI" label score or 0.0 fallback
34
+ ai_score = 0.0
35
  for out in outputs:
36
  if out["label"].lower() == "ai":
37
+ ai_score = out["score"]
38
+ scores.append(ai_score)
39
+ # Aggregate results: max score means any AI-like chunk triggers high detection
40
+ final_score = max(scores) if scores else 0.0
41
+ return final_score
42
 
43
+ def humanize_text(text):
44
+ # Use a smaller summarization or rewriting model
45
+ humanizer_pipe = pipeline("text2text-generation", model="sshleifer/distilbart-cnn-12-6", device=-1)
46
  prompt = f"Rewrite this text naturally: {text}"
47
+ result = humanizer_pipe(prompt, max_length=128, num_beams=3)
48
+ return result[0]["generated_text"]
 
 
 
 
49
 
50
+ text = st.text_area("Paste AI-generated text here", height=300)
51
 
52
+ if st.button("Detect & Humanize"):
53
  if not text.strip():
54
  st.warning("Please enter some text!")
55
  else:
56
+ with st.spinner("Detecting AI content on chunks..."):
57
+ start = time.time()
58
  score = detect_ai(text)
59
+ duration = time.time() - start
60
+ st.write(f"AI detection score: {score:.2%} (Processed in {duration:.1f}s)")
61
 
62
  if score < 0.5:
63
  st.success("This text looks human already! No need to rewrite.")