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

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +29 -14
src/streamlit_app.py CHANGED
@@ -3,28 +3,43 @@ import streamlit as st
3
  import torch
4
  import time
5
 
6
- # Limit CPU threads
7
  torch.set_num_threads(2)
8
 
9
  st.title("AI Humanizer Lite (CPU Friendly)")
10
 
11
- if "model" not in st.session_state:
 
12
  with st.spinner("Loading models (takes ~10s on CPU)..."):
13
- st.session_state.detect_pipe = pipeline("text-classification", model="roberta-base-openai-detector", device=-1)
14
- st.session_state.humanizer_pipe = pipeline("text2text-generation", model="sshleifer/distilbart-cnn-12-6", device=-1)
15
-
16
- def detect_ai(text):
17
- outputs = st.session_state.detect_pipe(text[:256])
18
- # Assume label "AI" means AI-generated with a score
19
- for out in outputs:
20
- if out["label"].lower() == "ai":
21
- return out["score"]
 
 
 
 
 
 
 
 
 
 
22
  return 0.0
23
 
24
- def humanize_text(text):
25
  prompt = f"Rewrite this text naturally: {text}"
26
- result = st.session_state.humanizer_pipe(prompt, max_length=128, num_beams=3)
27
- return result[0]["generated_text"]
 
 
 
 
28
 
29
  text = st.text_area("Paste AI-generated text here", height=200)
30
 
 
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