File size: 10,356 Bytes
d31c634
cfcd65e
86218e7
 
d994b45
b8e5952
ad96f78
9b0f802
d994b45
 
 
 
 
f8ec92b
d994b45
f8ec92b
e9640b0
3ec56f1
d994b45
86218e7
46f0706
d994b45
46f0706
 
 
 
d994b45
46f0706
d994b45
46f0706
 
 
 
 
 
d994b45
 
312e62e
 
 
0381f7e
 
312e62e
 
7e7f172
d994b45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8e5952
 
 
ad96f78
b8e5952
 
0143751
b8e5952
 
 
 
f8ec92b
b8e5952
 
 
 
 
 
 
9b0f802
 
 
0381f7e
9b0f802
0381f7e
 
 
ad96f78
d994b45
 
7e7f172
d994b45
 
0381f7e
 
 
 
7e7f172
 
d31c634
cc2969a
 
 
 
 
 
b900928
d994b45
b900928
c53fa23
 
 
e0ce3d4
e9640b0
5c509dc
e9640b0
 
f8ec92b
 
 
 
 
 
 
e9640b0
 
 
cc2969a
 
214c731
6ebeb61
cc2969a
 
 
66ecbf4
d994b45
 
 
 
312e62e
d994b45
 
 
c53fa23
 
 
 
 
 
 
 
d994b45
c53fa23
d994b45
 
 
 
 
312e62e
 
d994b45
 
c53fa23
 
d994b45
 
c53fa23
 
 
 
 
d994b45
c53fa23
d994b45
c53fa23
 
 
 
 
d994b45
c53fa23
d994b45
c53fa23
 
 
 
 
d994b45
c53fa23
d994b45
c53fa23
d994b45
 
 
 
04c7deb
5c509dc
f8ec92b
5c509dc
eb115c6
 
d994b45
 
 
 
 
 
 
7e7f172
d994b45
 
 
 
 
 
 
 
 
 
 
 
 
46f0706
afad1bb
d994b45
afad1bb
 
 
 
 
 
d994b45
afad1bb
 
d994b45
 
7e7f172
 
d994b45
 
 
 
 
7e7f172
312e62e
d994b45
 
 
 
 
312e62e
d994b45
 
 
afad1bb
afca54c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import gradio as gr
from humanize import paraphrase_text
from gradio_client import Client
from ai_generate import generate
import requests
import http.client
import json
from gptzero_free import GPT2PPL

def on_first_button_click():
    return gr.update(visible=True)

def ai_article_generator(
    topic, text_type, tonality, length, ai_model, api = None
):
    prompt = f"Write a {text_type} about the topic in about {length} words using a {tonality} tone: {topic}"
    print(prompt)
    ai_text = generate(f"Write an article about the topic: {prompt}", ai_model, api)
    return ai_text

def humanize(
    text,    
    model, 
    temperature=1.2, 
    repetition_penalty=1,
    top_k=50,
    length_penalty=1):
    result = paraphrase_text(
    		text=text,
    		model_name=model,
    		temperature=temperature,
    		repetition_penalty=repetition_penalty,
    		top_k=top_k,
    		length_penalty=length_penalty,
    )
    return result

ai_check_options = [
    "Polygraf AI",
    "Sapling AI",
    # "CopyLeaks",
    "GPTZero Free"
]

def ai_generated_test_polygraf(text):
    url = "http://34.66.10.188/ai-vs-human"
    access_key = "6mcemwsFycVVgVjMFwKXki3zJka1r7N4u$Z0Y|x$gecC$hdNtpQf-SpL0+=k;u%BZ" 
    headers = {
        "ACCESS_KEY": access_key
    }
    data = {
    "text" : f"{text}"
    }
    response = requests.post(url, headers=headers, json=data)
    return response.json()

def ai_generated_test_sapling(text):
    response = requests.post(
        "https://api.sapling.ai/api/v1/aidetect",
        json={
            "key": "60L9BPSVPIIOEZM0CD1DQWRBPJIUR7SB",
            "text": f"{text}"
        }
    )
    return { "AI" : response.json()['score'], "HUMAN" : 1 - response.json()['score']}

def ai_generated_test_copyleak(text):

    conn = http.client.HTTPSConnection("api.copyleaks.com")
    payload = payload = "{\n  \"text\": \"" + text + "\"\n}"
    
    headers = {
        'Authorization': "9ea35187-f43a-4dc2-bfdf-25825b78eaf1",
        'Content-Type': "application/json",
        'Accept': "application/json"
    }
    scanID = "9ea35187-f43a-4dc2-bfdf-25825b78eaf1"
    conn.request("POST", f"/v2/writer-detector/{scanID}/check", payload, headers)
    
    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))
    print(data.decode("utf-8")["results"]["summary"])
    return data.decode("utf-8")["results"]["summary"]


gptzero_model = GPT2PPL()

def ai_generated_test_gptzero(text):
    result = gptzero_model(text)
    print(result)
    return result


def ai_check(text, option):
    if option == 'Polygraf AI':
        return ai_generated_test_polygraf(text)
    elif option == 'Sapling AI':
        return ai_generated_test_sapling(text)
    # elif option == 'CopyLeaks':
    #     return ai_generated_test_copyleak(text)    
    elif option == "GPTZero Free":
        return ai_generated_test_gptzero(text)
    else:
        return ai_generated_test_polygraf(text)

def update_visibility_api(model):
    if model in ['OpenAI GPT 3.5', 'OpenAI GPT 4']:
        return gr.update(visible=True)
    else:
        return gr.update(visible=False)

with gr.Blocks() as demo:
    gr.Markdown("# Polygraf Article Writer")
    with gr.Row():
        with gr.Column(scale=0.7):
            gr.Markdown("## Enter a topic to write an article about:")
            input_topic = gr.Textbox(label="Topic")
            with gr.Row():
                with gr.Column():
                    input_text_type = gr.Dropdown(
                        choices = ['article', 'essay', 'blog', 'report', 'letter'],
                        value = 'article',
                        label="Choose text type of text:"
                    )
                with gr.Column():
                    input_tonality = gr.Dropdown(
                        choices = ["casual", "professional", "formal", "friendly", "humorous"],
                        value = 'casual',
                        label="Choose tonality of text:"
                    )
                with gr.Column():
                    input_length = gr.Number(value=200, label="Choose length of the text:")
            with gr.Row():
                ai_generator = gr.Dropdown(
                    choices = ['Llama 3', 'Groq', 'Mistral', 'Gemma', 'OpenAI GPT 3.5', 'OpenAI GPT 4', 'OpenAI GPT 4o'],
                    value = 'Llama 3',
                    label="Choose AI Model"
                )
                input_api = gr.Textbox(label="API Key", visible=False)
                ai_generator.change(update_visibility_api, ai_generator, input_api)
            ai_article_btn = gr.Button("Generate", visible=True)
            ai_generated = gr.Markdown("### Generated article:", visible=False)
            ai_label = gr.HTML(label="AI", visible=False)
            ai_detector_dropdown = gr.Radio(
                choices=ai_check_options, label="Select AI Detector", value="Polygraf AI", visible=False)
            only_ai_check_btn = gr.Button("AI Check", visible= False)
            bcLabel = gr.Label(label="Source", visible= False)
            humanizer_btn = gr.Button("Humanize", visible=False)
            model_dropdown = gr.Radio(
                choices=[
                    "Base Model",
                    "Large Model",
                    "XL Model",
                    "XL Law Model",
                    "XL Marketing Model",
                    "XL Child Style Model",
                ], 
                value="Large Model",
                label="Select Model Version", 
                visible=False)
            
            humanized_markdown = gr.Markdown("### Humanized article:", visible=False)
            output_label = gr.HTML(label="Humanized", visible= False)
            ai_detector_dropdown2 = gr.Radio(
                choices=ai_check_options, label="Select AI Detector", value="Polygraf AI", visible=False)
            only_ai_check_btn2 = gr.Button("AI Check", visible= False)
            bcLabel2 = gr.Label(label="Source", visible= False)

        with gr.Column(scale=0.3):
                temperature_slider = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, value=1.2, label="Temperature",  visible= False)
                controls_markdown  = gr.Markdown("Controls the randomness of the paraphrase. Higher values generate more varied text.",  visible= False)
                top_k_slider = gr.Slider(
                    minimum=0,
                    maximum=300,
                    step=25,
                    value=50,
                    label="Top k", visible= False
                )
                top_token_markdown  =  gr.Markdown("Limits the number of top tokens considered during generation.",  visible= False)
                repetition_penalty_slider = gr.Slider(
                    minimum=1.0,
                    maximum=2.0,
                    step=0.1,
                    value=1,
                    label="Repetition Penalty",  visible= False
                )
                penalize_repeated_markdown = gr.Markdown("Penalizes repeated words to encourage diverse language use", visible= False)
                length_penalty_slider = gr.Slider(
                    minimum=0.0,
                    maximum=2.0,
                    step=0.1,
                    value=1.0,
                    label="Length Penalty",  visible= False
                )
                penalize_markdown = gr.Markdown("Penalizes shorter outputs.", visible= False)
    

        ai_article_btn.click(
            fn=ai_article_generator,
            inputs=[
                input_topic, 
                input_text_type, 
                input_tonality,
                input_length,
                ai_generator,
                input_api
            ],
            outputs=[ai_label],
        )

        ai_article_btn.click(on_first_button_click, inputs=None, outputs=ai_generated)
        ai_article_btn.click(on_first_button_click, inputs=None, outputs=ai_label)
        ai_article_btn.click(on_first_button_click, inputs=None, outputs=only_ai_check_btn)
        ai_article_btn.click(on_first_button_click, inputs=None, outputs=ai_detector_dropdown)
        only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=bcLabel)
        only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=model_dropdown)
        only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=humanizer_btn)
        only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=temperature_slider)
        only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=controls_markdown)
        only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=top_k_slider)
        only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=top_token_markdown)
        only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=repetition_penalty_slider)
        only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=penalize_repeated_markdown)
        only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=length_penalty_slider)
        only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=penalize_markdown)

        humanizer_btn.click(
            fn=humanize,
            inputs=[
                ai_label,
                model_dropdown,
                temperature_slider,
                repetition_penalty_slider,
                top_k_slider,
                length_penalty_slider,
            ],
            outputs=[output_label],
        )


        only_ai_check_btn.click(
            fn=ai_check,
            inputs=[ai_label, ai_detector_dropdown],
            outputs=[bcLabel],
            api_name="ai_check",
    )
        
        only_ai_check_btn2.click(
            fn=ai_check,
            inputs=[ai_label, ai_detector_dropdown2],
            outputs=[bcLabel2]
    )

        humanizer_btn.click(on_first_button_click, inputs=None, outputs=humanized_markdown)
        humanizer_btn.click(on_first_button_click, inputs=None, outputs=output_label)
        humanizer_btn.click(on_first_button_click, inputs=None, outputs=ai_detector_dropdown2)
        humanizer_btn.click(on_first_button_click, inputs=None, outputs=only_ai_check_btn2)
        humanizer_btn.click(on_first_button_click, inputs=None, outputs=bcLabel2)

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0")