Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- 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
|
| 7 |
torch.set_num_threads(2)
|
| 8 |
|
| 9 |
-
st.title("AI Humanizer Lite (CPU Friendly)")
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
for out in outputs:
|
| 29 |
if out["label"].lower() == "ai":
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
def humanize_text(text
|
|
|
|
|
|
|
| 36 |
prompt = f"Rewrite this text naturally: {text}"
|
| 37 |
-
|
| 38 |
-
|
| 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=
|
| 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 |
-
|
|
|
|
| 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.")
|