-
Notifications
You must be signed in to change notification settings - Fork 3
/
streamlit_app.py
94 lines (76 loc) · 3.28 KB
/
streamlit_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import streamlit as st
import random
from perplexity import Perplexity, classify_perplexity
from transformers import BertTokenizerFast, GPT2LMHeadModel
from content import *
st.set_page_config(layout="centered")
p = Perplexity()
@st.experimental_singleton
def load_model():
# code to load the model
tokenizer = BertTokenizerFast.from_pretrained('bert-base-chinese')
model = GPT2LMHeadModel.from_pretrained("ckiplab/gpt2-base-chinese")
return tokenizer, model
tokenizer, model = load_model()
p.set_model(tokenizer, model)
random_list = [m1, m2, m3, m4, m5, m6, h1, h2, h3, h4, h5, h6, h7]
MAX_CHARS = 3000
st.title("ChatGPT Chinese Text Detector")
st.markdown("\n")
st.subheader("A tool for detecting machine-generated Chinese text")
st.markdown("\n")
text_area = st.empty()
col1, col2, col3, col4, col5 = st.columns(5)
text = text_area.text_area("Enter your Chinese text here:", \
max_chars=MAX_CHARS, height=450)
if col1.button("Random text result"):
random_text = "".join(random.choices(random_list))
text_area.text_area("Enter your Chinese text here:", random_text, \
max_chars=MAX_CHARS, height=450)
if random_text:
score = p.calculate(random_text)
if score:
col2.metric("Perplexity: ", score, "")
result = classify_perplexity(score)
if result == "AI":
st.warning("This text is most likely written by AI (ChatGPT)", icon="⚠️")
elif result == "Human":
st.success("This text is most likely written by Human", icon="🐵")
else:
st.warning("It is hard to tell if this text is written by a human or AI", icon="❓")
else:
st.warning("Please try again")
else:
st.warning("Please add text above")
if col3.button('Get your text result'):
if text:
if len(text) < 10:
st.error("Text is too short. Please input more than 10 characters.")
elif len(text) > MAX_CHARS:
st.error("Text is too long. Please input less than 2000 characters.")
else:
score = p.calculate(text)
if score:
col4.metric("Perplexity: ", score, "")
result = classify_perplexity(score)
if result == "AI":
st.warning("This text is most likely written by AI (ChatGPT)", icon="⚠️")
elif result == "Human":
st.success("This text is most likely written by Human", icon="🐵")
else:
st.warning("It is hard to tell if this text is written by a human or AI", icon="❓")
else:
st.warning("Please try again")
else:
st.warning("Please add text above")
st.sidebar.header("Author: ")
st.sidebar.subheader("RUI-LONG CHENG 鄭瑞龍")
st.sidebar.markdown("\n")
st.sidebar.header("Contact Information: ")
st.sidebar.markdown("Email: [email protected]")
st.sidebar.markdown("Follow me on [GitHub](https://github.com/RUI-LONG) or [Facebook](https://www.facebook.com/ruilongz/)")
st.sidebar.markdown("\n")
st.sidebar.markdown("\n")
st.sidebar.header("Refference: ")
st.sidebar.markdown("- [GPT Zreo](https://etedward-gptzero-main-zqgfwb.streamlit.app/)")
st.sidebar.markdown("- [OpenAI](https://openai-openai-detector.hf.space/)")