File size: 4,403 Bytes
1a772db
 
 
 
 
 
 
 
 
 
0c4aff5
1a772db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

##### PREPARATIONS

# libraries
import datetime
import json
import os
import sys
import requests
import streamlit as st


MIN_TEXT_LEN = 300
MAX_TEXT_LEN = 32410
API_URL = "http://checkgpt.app:8880/predict"

# download with progress bar
mybar = None


def show_progress(block_num, block_size, total_size):
    global mybar
    if mybar is None:
        mybar = st.progress(0.0)
    downloaded = block_num * block_size / total_size
    if downloaded <= 1.0:
        mybar.progress(downloaded)
    else:
        mybar.progress(1.0)


##### CONFIG

# page config
st.set_page_config(page_title="CheckGPT - ChatGPT and other big LM detection engine",
                   page_icon=":books:",
                   layout="centered",
                   initial_sidebar_state="collapsed",
                   menu_items=None)

##### HEADER

# title
st.title('CheckGPT - AI-written text detect')


# description
st.write(
    'CheckGPT is a neural network to check if text is generated by '\
                           'big AI LMs (like ChatGPT, GPT3, GPT2, BLOOM, You.com AI and etc).')
st.write('Currently supported languages are: [\'en\'].')
st.write("Use our web app and RestAPI at https://checkgpt.app.\n")
st.write('Already telegram bot is available at: https://t.me/chatgpt_bot.')
st.write('To connect with authors please write to: https://t.me/uberwow | https://CheckGPT.app')


##### PARAMETERS

# title
st.header('Check for AI generated text?')


# input text
input_text = st.text_area('Which text would you like to check?', '')

##### MODELING

# compute readability
if st.button('Check'):

    # compute predictions
    with st.spinner('Computing prediction...'):

        # compute prediction
        if len(input_text) < MIN_TEXT_LEN:
            st.error(f'😐 Minimal text length: {MIN_TEXT_LEN} characters. Your text is {len(input_text)} characters.\n'
                     f'The longer the text, the higher the accuracy.\n')
        elif len(input_text) > MAX_TEXT_LEN:
            st.error('⚠ Seems we got too big input! Please try again with smaller text!')
        else:
            # make json-valid request
            formatted_req = {"text": input_text}
            jsoned_req = json.dumps(formatted_req)

            # try to send request to api
            try:
                r = requests.post(API_URL, data=jsoned_req, headers={'Content-Type': 'application/json'})
                answer = json.loads(r.text)
            except:
                st.error('Some backend RESTAPI error. Please try later!')
                print("Some error while sending POST request to RESTAPI. Please check your connection and try again!")

            # extract data from json anwser
            result = int(answer["result"])
            human_score = round(float(answer["human_score"]), 4)
            chatgpt_score = round(float(answer["ChatGPT_score"]), 4)
            req_id = answer["request_id"]
            execution_time = answer["execution_time"]

            # clear memory
            #gc.collect()

            # print output
            st.metric(label="Prediction", value=result)

            st.write(f"βœ… Total score (AI signs):\n\n    Human-like score: {human_score}%\n    ChatGPT-like score: {chatgpt_score}%\n")
            st.write(f"βš™ Information:\n\n    Execution time: {execution_time} sec.\n    Request ID: {req_id}\n    Prediction: ", result)
            if result == 0:
                st.success(f"πŸ‘¨ Seems like text was written by the human ({human_score}%)!")

            if result == 1:
                st.success(f"πŸ€– Seems to be a ChatGPT generated text! ({chatgpt_score}%)")

            # save log
            with open('logs/' + 'text_' + str(
                    result) + '_' + '{:%Y-%m-%d_%H-%M-%S}'.format(
                    datetime.datetime.now()) + '.log', 'w', encoding="utf-8") as file:
                file.write(input_text)



##### DOCUMENTATION

# header
st.header('More information')

# models
with st.expander('Read about the models'):
    st.write(
        'For ChatGPT generated content detection, we are using statistical and heuristical methods, perplexity, '
        'entropy, '
        'coherence and consistency of the text and some our personal know-how ;).')

# metric

##### CONTACT

# header
st.header("Contact")

# website link

# copyright
st.text("All rights reserved Β© 2023 | https://t.me/uberwow | https://CheckGPT.app")