File size: 12,486 Bytes
1be431a
 
 
eb8fa16
f45e494
45d10c4
 
c0a6bc9
dd9b08a
2019311
2333c59
9df8406
2019311
 
173f4a0
0eaca07
 
 
1be431a
 
 
2333c59
 
1be431a
2333c59
 
 
28cd001
c0a6bc9
28cd001
c0a6bc9
9df8406
 
0eaca07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fc992f
f45e494
 
45d10c4
 
f1ef701
9fc992f
09f0b85
fffd7f2
1be431a
 
 
6d6d84c
 
1be431a
 
 
 
 
 
 
 
9c75413
1be431a
c38b78d
c0a6bc9
6d6d84c
1be431a
 
 
 
 
 
 
 
9c75413
1be431a
41a6a33
45d10c4
9fc992f
9efa03e
1be431a
 
fe15d80
 
 
 
 
d176253
 
 
1be431a
 
eb8fa16
1be431a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ea96b4
1be431a
 
4ffd446
 
 
fe15d80
 
 
4ffd446
2019311
0eaca07
 
 
 
 
 
 
 
2019311
 
 
 
 
 
 
 
 
 
0eaca07
 
 
 
 
 
 
 
 
fe15d80
16a43bc
 
164a7aa
2019311
 
 
f45e494
79b97e2
f45e494
075be7c
 
 
 
 
 
9fc992f
1be431a
 
fe15d80
f1ef701
 
 
 
fe15d80
 
f45e494
 
 
 
1be431a
fe15d80
 
 
9c75413
 
23ec282
9c75413
23ec282
9c75413
 
6d6d84c
 
d176253
6d6d84c
8dcf476
d53b62d
 
9df8406
 
d53b62d
398f756
9df8406
 
 
 
398f756
6d6d84c
 
 
 
 
 
1be431a
 
 
 
 
fe15d80
1be431a
 
 
b373c49
 
 
1be431a
9df8406
 
d53b62d
dee0f90
 
3c8629c
b373c49
 
9df8406
1be431a
 
 
 
 
 
 
 
 
 
 
16a43bc
1be431a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0a6bc9
42f39c4
c0a6bc9
1be431a
 
d176253
 
fe15d80
8125190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d176253
16a43bc
1be431a
 
6d6d84c
 
1be431a
 
 
 
 
 
 
 
9c75413
1be431a
 
 
 
 
8dcf476
fe15d80
1be431a
 
 
 
 
 
9fc992f
09f0b85
1be431a
 
 
d53b62d
 
f45e494
d53b62d
 
 
 
1be431a
c0a6bc9
 
1be431a
6d6d84c
1be431a
 
 
 
 
 
 
 
9c75413
1be431a
 
 
 
 
 
 
d176253
 
173f4a0
d176253
 
 
 
9df8406
 
f45e494
9df8406
 
398f756
9df8406
 
 
f45e494
9df8406
 
 
 
1be431a
 
b373c49
23ec282
 
80f1bd8
23ec282
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import gradio as gr
import numpy as np
from datetime import date
from predictors import predict_bc_scores, predict_mc_scores
from predictors import update,update_main, correct_text, split_text
from analysis import depth_analysis
from predictors import predict_quillbot
from plagiarism import plagiarism_check, build_date, html_highlight
from highlighter import segmented_higlighter
from utils import extract_text_from_pdf, len_validator, extract_text_from_html
import yaml
from functools import partial
from audio import assemblyai_transcribe
import yt_dlp
import os
import pandas as pd
from const import plag_script
from datasets import load_dataset, Dataset

np.set_printoptions(suppress=True)

with open("config.yaml", "r") as file:
    params = yaml.safe_load(file)

model_list = params["MC_OUTPUT_LABELS"]


analyze_and_highlight_bc = partial(segmented_higlighter, model_type="bc")
analyze_and_highlight_quillbot = partial(
    segmented_higlighter, model_type="quillbot"
)



def save_request(email, video_url):
    # Save the email and video URL to the CSV file
    if email is None or email == "":
        return "Please enter your email.", gr.update(visible=True)
    dat = load_dataset(requests_repo)["train"]
    df = dat.to_pandas()
    new_row = pd.DataFrame(
        {"email": [email], "video_url": [video_url], "status": "pending"}
    )
    df = pd.concat([df, new_row], ignore_index=True)
    dat = Dataset.from_pandas(df)
    dat.push_to_hub(requests_repo)
    return "Your request has been saved.", gr.update(visible=False)

def ai_generated_test(option, bias_buster_selected, input):
    if bias_buster_selected:
        input = update(input)
    if option == "Human vs AI":
        return predict_bc_scores(input), None
    elif option == "Human vs AI Source Models":
        return predict_bc_scores(input), predict_mc_scores(input)
    return None, None


# COMBINED
def main(
    ai_option,
    plag_option,
    input,
    year_from,
    month_from,
    day_from,
    year_to,
    month_to,
    day_to,
    domains_to_skip,
    source_block_size,
):

    formatted_tokens = html_highlight(
        plag_option,
        input,
        year_from,
        month_from,
        day_from,
        year_to,
        month_to,
        day_to,
        domains_to_skip,
        source_block_size,
    )
    depth_analysis_plot = depth_analysis(input, bias_buster_selected)
    bc_score = predict_bc_scores(input)
    mc_score = predict_mc_scores(input)
    quilscore = predict_quillbot(input, bias_buster_selected)

    return (
        bc_score,
        mc_score,
        formatted_tokens,
        depth_analysis_plot,
        quilscore,
    )


# START OF GRADIO

title = "AI Detection and Source Analysis"
months = {
    "January": "01",
    "February": "02",
    "March": "03",
    "April": "04",
    "May": "05",
    "June": "06",
    "July": "07",
    "August": "08",
    "September": "09",
    "October": "10",
    "November": "11",
    "December": "12",
}


with gr.Blocks() as demo:
    today = date.today()
    # dd/mm/YY
    d1 = today.strftime("%d/%B/%Y")
    d1 = d1.split("/")

    domain_list = ["com", "org", "net", "int", "edu", "gov", "mil"]
    gr.Markdown(
        """
    # AI Detection and Source Analysis
    """
    )
    with gr.Row():
        input_text = gr.Textbox(label="Input text", lines=6, placeholder="")
        file_input = gr.File(label="Upload PDF")
        file_input.change(
            fn=extract_text_from_pdf, inputs=file_input, outputs=input_text
        )


    with gr.Column(visible=False) as request_row:
        with gr.Row():
            email_input = gr.Textbox(label="Email")
            youtube_url_input = gr.Textbox(label="YouTube Video URL")
        with gr.Row():
            video_submit_btn = gr.Button("Submit Video Request")


    with gr.Row():
        url_input = gr.Textbox(
            label="Input Page URL to check", lines=1, placeholder="")
        url_input.change(
            fn=extract_text_from_html, inputs=url_input, outputs=input_text)

        audio_url_input = gr.Textbox(label="Input YouTube URL to check", lines=1, placeholder="")
        audio_url_input.change(
            fn=assemblyai_transcribe, inputs=audio_url_input, outputs=input_text
        )
    

    video_submit_btn.click(
        fn=save_request,
        inputs=[email_input, youtube_url_input],
        outputs=[input_text, request_row],
        api_name="video_request",
    )

    char_count = gr.Textbox(label="Minumum Character Limit Check")
    input_text.change(fn=len_validator, inputs=input_text, outputs=char_count)

    with gr.Row():
        btn = gr.Button("Deception Filter")
        out = gr.Textbox(label="Corrected Full Input", interactive=False)
        corrections_output = gr.Textbox(label="Corrections", interactive=False)
        btn.click(fn=update_main, inputs=input_text, outputs=[out, corrections_output])

    with gr.Row(): 
        models = gr.Dropdown(
            model_list,
            value=model_list,
            multiselect=True,
            label="Models to test against",
        )
        
    with gr.Row():
        with gr.Column():
            ai_option = gr.Radio(
                [
                    "Human vs AI",
                    "Human vs AI Source Models",
                ],
                label="Choose an option please.",
            )
        
        with gr.Column():
            bias_buster_selected = gr.Checkbox(label="Bias Remover")
        
        with gr.Column():
            plag_option = gr.Radio(
                ["Standard", "Advanced"], label="Choose an option please."
            )
    with gr.Row():
        source_block_size = gr.Dropdown(
            choices=["Sentence", "Paragraph"],
            label="Source Check Granularity",
            value="Sentence",
            interactive=True,
        )

    with gr.Row():
        with gr.Column():
            only_ai_btn = gr.Button("AI Check")
        with gr.Column():
            only_plagiarism_btn = gr.Button("Source Check")

        with gr.Column():
            quillbot_check = gr.Button("Humanized Text Check")

    with gr.Row():
        with gr.Column():
            bc_highlighter_button = gr.Button("Human vs. AI Highlighter")
        with gr.Column():
            quillbot_highlighter_button = gr.Button("Humanized Highlighter")

    with gr.Row():
        depth_analysis_btn = gr.Button("Detailed Writing Analysis")

    with gr.Row():
        full_check_btn = gr.Button("Full Check")

    gr.Markdown(
        """
        ## Output
        """
    )

    with gr.Row():
        with gr.Column():
            bcLabel = gr.Label(label="Source")
        with gr.Column():
            mcLabel = gr.Label(label="Creator")
    with gr.Row():
        with gr.Column():
            bc_highlighter_output = gr.HTML(label="Human vs. AI Highlighter")

    with gr.Row():
        with gr.Column():
            QLabel = gr.Label(label="Humanized")

    with gr.Row():
        quillbot_highlighter_output = gr.HTML(label="Humanized Highlighter")

    with gr.Group():
        with gr.Row():
            month_from = gr.Dropdown(
                choices=months,
                label="From Month",
                value="January",
                interactive=True,
            )
            day_from = gr.Textbox(label="From Day", value="01")
            year_from = gr.Textbox(label="From Year", value="2000")
            # from_date_button = gr.Button("Submit")

        with gr.Row():
            month_to = gr.Dropdown(
                choices=months,
                label="To Month",
                value=d1[1],
                interactive=True,
            )
            day_to = gr.Textbox(label="To Day", value=d1[0])
            year_to = gr.Textbox(label="To Year", value=d1[2])
            # to_date_button = gr.Button("Submit")
        with gr.Row():
            domains_to_skip = gr.Dropdown(
                domain_list,
                multiselect=True,
                label="Domain To Skip",
            )

    with gr.Row():
        with gr.Column():
            sentenceBreakdown = gr.HTML(
                label="Source Detection Sentence Breakdown",
                value="Source Detection Sentence Breakdown",
            )

    with gr.Row():
        with gr.Column():
            writing_analysis_plot = gr.Plot(label="Writing Analysis Plot")
        with gr.Column():
            interpretation = """
<h2>Writing Analysis Interpretation</h2>
<ul>
    <li><b>Lexical Diversity</b>: This feature measures the range of unique words used in a text.
        <ul>
            <li>🤖 Higher tends to be AI.</li>
        </ul>
    </li>
    <li><b>Vocabulary Level</b>: This feature assesses the complexity of the words used in a text.
        <ul>
            <li>🤖 Higher tends to be AI.</li>
        </ul>
    </li>
    <li><b>Unique Words</b>: This feature counts the number of words that appear only once within the text.
        <ul>
            <li>🤖 Higher tends to be AI.</li>
        </ul>
    </li>
    <li><b>Determiner Use</b>: This feature tracks the frequency of articles and quantifiers in the text.
        <ul>
            <li>🤖 Higher tends to be AI.</li>
        </ul>
    </li>
    <li><b>Punctuation Variety</b>: This feature indicates the diversity of punctuation marks used in the text.
        <ul>
            <li>👤 Higher tends to be Human.</li>
        </ul>
    </li>
    <li><b>Sentence Depth</b>: This feature evaluates the complexity of the sentence structures used in the text.
        <ul>
            <li>🤖 Higher tends to be AI.</li>
        </ul>
    </li>
    <li><b>Vocabulary Stability</b>: This feature measures the consistency of vocabulary use throughout the text.
        <ul>
            <li>🤖 Higher tends to be AI.</li>
        </ul>
    </li>
    <li><b>Entity Ratio</b>: This feature calculates the proportion of named entities, such as names and places, within the text.
        <ul>
            <li>👤 Higher tends to be Human.</li>
        </ul>
    </li>
    <li><b>Perplexity</b>: This feature assesses the predictability of the text based on the sequence of words.
        <ul>
            <li>👤 Higher tends to be Human.</li>
        </ul>
    </li>
</ul>

"""
            gr.HTML(interpretation, label="Interpretation of Writing Analysis")

    full_check_btn.click(
        fn=main,
        inputs=[
            ai_option,
            plag_option,
            input_text,
            year_from,
            month_from,
            day_from,
            year_to,
            month_to,
            day_to,
            domains_to_skip,
            source_block_size,
        ],
        outputs=[
            bcLabel,
            mcLabel,
            sentenceBreakdown,
            writing_analysis_plot,
            QLabel,
        ],
        api_name="main",
    )

    only_ai_btn.click(
        fn=ai_generated_test,
        inputs=[ai_option, bias_buster_selected, input_text],
        outputs=[bcLabel, mcLabel],
        api_name="ai_check",
    )

    quillbot_check.click(
        fn=predict_quillbot,
        inputs=[input_text, bias_buster_selected],
        outputs=[QLabel],
        api_name="quillbot_check",
    )

    only_plagiarism_btn.click(
        # fn=plagiarism_check,
        fn=html_highlight,
        inputs=[
            plag_option,
            input_text,
            year_from,
            month_from,
            day_from,
            year_to,
            month_to,
            day_to,
            domains_to_skip,
            source_block_size,
        ],
        outputs=[
            sentenceBreakdown,
        ],
        api_name="plagiarism_check",
    )

    depth_analysis_btn.click(
        fn=depth_analysis,
        inputs=[input_text, bias_buster_selected],
        outputs=[writing_analysis_plot],
        api_name="depth_analysis",
    )

    quillbot_highlighter_button.click(
        fn=analyze_and_highlight_quillbot,
        inputs=[input_text, bias_buster_selected],
        outputs=[quillbot_highlighter_output],
        api_name="humanized_highlighter",
    )

    bc_highlighter_button.click(
        fn=analyze_and_highlight_bc,
        inputs=[input_text, bias_buster_selected],
        outputs=[bc_highlighter_output],
        api_name="bc_highlighter",
    )

    date_from = ""
    date_to = ""

if __name__ == "__main__":
    demo.launch(
        share=True, server_name="0.0.0.0", server_port=80, auth=("polygraf-admin", "test@aisd")
    )