File size: 10,577 Bytes
1be431a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9946cc
1be431a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147e661
2e5c3b9
1be431a
 
147e661
2e5c3b9
1be431a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f7f887
1be431a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f7f887
1be431a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e611170
1be431a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from utils import cosineSim, googleSearch, getSentences, parallel_scrap, matchingScore
import gradio as gr
from urllib.request import urlopen, Request
from googleapiclient.discovery import build
import requests
import httpx
import re
from bs4 import BeautifulSoup
import numpy as np
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import asyncio
from scipy.special import softmax
from evaluate import load
from datetime import date
import nltk
import os

np.set_printoptions(suppress=True)


def plagiarism_check(
    input,
    year_from,
    month_from,
    day_from,
    year_to,
    month_to,
    day_to,
    domains_to_skip,
):
    api_key = "AIzaSyCLyCCpOPLZWuptuPAPSg8cUIZhdEMVf6g"
    api_key = "AIzaSyCS1WQDMl1IMjaXtwSd_2rA195-Yc4psQE"
    api_key = "AIzaSyCB61O70B8AC3l5Kk3KMoLb6DN37B7nqIk"
    api_key = "AIzaSyCg1IbevcTAXAPYeYreps6wYWDbU0Kz8tg"
    # api_key = "AIzaSyBrx_pgb6A64wPFQXSGQRgGtukoxVV_0Fk"
    cse_id = "851813e81162b4ed4"

    sentences = getSentences(input)
    urlCount = {}
    ScoreArray = []
    urlList = []

    date_from = build_date(year_from, month_from, day_from)
    date_to = build_date(year_to, month_to, day_to)
    sort_date = f"date:r:{date_from}:{date_to}"
    # get list of URLS to check
    urlCount, ScoreArray = googleSearch(
        sentences,
        urlCount,
        ScoreArray,
        urlList,
        sort_date,
        domains_to_skip,
        api_key,
        cse_id,
    )
    print("Number of URLs: ", len(urlCount))
    # print("Old Score Array:\n")
    # print2D(ScoreArray)

    # Scrape URLs in list
    formatted_tokens = []
    soups = asyncio.run(parallel_scrap(urlList))
    print(len(soups))
    print(
        "Successful scraping: "
        + str(len([x for x in soups if x is not None]))
        + "out of "
        + str(len(urlList))
    )

    # Populate matching scores for scrapped pages
    for i, soup in enumerate(soups):
        print(f"Analyzing {i+1} of {len(soups)} soups........................")
        if soup:
            page_content = soup.text
            for j, sent in enumerate(sentences):
                score = matchingScore(sent, page_content)
                ScoreArray[i][j] = score

    # ScoreArray = asyncio.run(parallel_analyze_2(soups, sentences, ScoreArray))
    # print("New Score Array:\n")
    # print2D(ScoreArray)


    # Gradio formatting section
    sentencePlag = [False] * len(sentences)
    sentenceToMaxURL = [-1] * len(sentences)
    for j in range(len(sentences)):
        if j > 0:
            maxScore = ScoreArray[sentenceToMaxURL[j - 1]][j]
            sentenceToMaxURL[j] = sentenceToMaxURL[j - 1]
        else:
            maxScore = -1
        for i in range(len(ScoreArray)):
            margin = (
                0.1
                if (j > 0 and sentenceToMaxURL[j] == sentenceToMaxURL[j - 1])
                else 0
            )
            if ScoreArray[i][j] - maxScore > margin:
                maxScore = ScoreArray[i][j]
                sentenceToMaxURL[j] = i
        if maxScore > 0.5:
            sentencePlag[j] = True
    
    if (
        (len(sentences) > 1)
        and (sentenceToMaxURL[1] != sentenceToMaxURL[0])
        and (
            ScoreArray[sentenceToMaxURL[0]][0]
            - ScoreArray[sentenceToMaxURL[1]][0]
            < 0.1
        )
    ):
        sentenceToMaxURL[0] = sentenceToMaxURL[1]

    index = np.unique(sentenceToMaxURL)

    urlMap = {}
    for count, i in enumerate(index):
        urlMap[i] = count + 1
    for i, sent in enumerate(sentences):
        formatted_tokens.append(
            (sent, "[" + str(urlMap[sentenceToMaxURL[i]]) + "]")
        )

    formatted_tokens.append(("\n", None))
    formatted_tokens.append(("\n", None))
    formatted_tokens.append(("\n", None))

    urlScore = {}
    for url in index:
        s = [
            ScoreArray[url][sen]
            for sen in range(len(sentences))
            if sentenceToMaxURL[sen] == url
        ]
        urlScore[url] = sum(s) / len(s)

    for ind in index:
        formatted_tokens.append(
            (
                urlList[ind] + " --- Matching Score: " + str(urlScore[ind]),
                "[" + str(urlMap[ind]) + "]",
            )
        )
        formatted_tokens.append(("\n", None))

    print(f"Formatted Tokens: {formatted_tokens}")

    return formatted_tokens


"""
AI DETECTION SECTION
"""

text_bc_model_path = "polygraf-ai/ai-text-bc-bert-1-4m"
text_bc_tokenizer = AutoTokenizer.from_pretrained(text_bc_model_path)
text_bc_model = AutoModelForSequenceClassification.from_pretrained(text_bc_model_path)

text_mc_model_path = "polygraf-ai/ai-text-mc-v5-lighter-spec"
text_mc_tokenizer = AutoTokenizer.from_pretrained(text_mc_model_path)
text_mc_model = AutoModelForSequenceClassification.from_pretrained(text_mc_model_path)

def remove_special_characters(text):
    cleaned_text = re.sub(r'[^a-zA-Z0-9\s]', '', text)
    return cleaned_text

def predict_bc(model, tokenizer, text):
    tokens = tokenizer(
        text, padding=True, truncation=True, return_tensors="pt"
    )["input_ids"]
    output = model(tokens)
    output_norm = softmax(output.logits.detach().numpy(), 1)[0]
    print("BC Score: ", output_norm)
    bc_score = {"AI": output_norm[1].item(), "HUMAN": output_norm[0].item()}
    return bc_score


def predict_mc(model, tokenizer, text):
    tokens = tokenizer(
        text, padding=True, truncation=True, return_tensors="pt"
    )["input_ids"]
    output = model(tokens)
    output_norm = softmax(output.logits.detach().numpy(), 1)[0]
    print("MC Score: ", output_norm)
    mc_score = {}
    label_map = ["GPT 3.5", "GPT 4", "CLAUDE", "BARD", "LLAMA 2"]
    for score, label in zip(output_norm, label_map):
        mc_score[label.upper()] = score.item()
    return mc_score


def ai_generated_test(input, models):

    cleaned_text = remove_special_characters(input)
    bc_score = predict_bc(text_bc_model, text_bc_tokenizer, cleaned_text)
    mc_score = predict_mc(text_mc_model, text_mc_tokenizer, cleaned_text)
    
    sum_prob = 1 - bc_score["HUMAN"]
    for key, value in mc_score.items():
        mc_score[key] = value * sum_prob

    return bc_score, mc_score


# COMBINED
def main(
    input,
    models,
    year_from,
    month_from,
    day_from,
    year_to,
    month_to,
    day_to,
    domains_to_skip,
):
    bc_score, mc_score = ai_generated_test(input, models)
    formatted_tokens = plaigiarism_check(
        input,
        year_from,
        month_from,
        day_from,
        year_to,
        month_to,
        day_to,
        domains_to_skip,
    )
    return (
        bc_score,
        mc_score,
        formatted_tokens,
    )


def build_date(year, month, day):
    return f"{year}{months[month]}{day}"


# START OF GRADIO

title = "Copyright Checker"
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("/")

    model_list = ["GPT 3.5", "GPT 4", "CLAUDE", "BARD", "LLAMA2"]
    domain_list = ["com", "org", "net", "int", "edu", "gov", "mil"]
    gr.Markdown(
        """
    # Copyright Checker 
    """
    )
    input_text = gr.Textbox(label="Input text", lines=5, placeholder="")
    
    with gr.Row():
        with gr.Column():
            only_ai_btn = gr.Button("AI Check")
        with gr.Column():
            only_plagiarism_btn = gr.Button("Plagiarism Check")
        with gr.Column():
            submit_btn = gr.Button("Full Check")
    gr.Markdown(
        """
        ## 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():
            bcLabel = gr.Label(label="Source")
        with gr.Column():
            mcLabel = gr.Label(label="Creator")

    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.HighlightedText(
                label="Plagiarism Sentence Breakdown",
                combine_adjacent=True,
                color_map={
                    "[1]": "red",
                    "[2]": "orange",
                    "[3]": "yellow",
                    "[4]": "green",
                },
            )

    submit_btn.click(
        fn=main,
        inputs=[
            input_text,
            models,
            year_from,
            month_from,
            day_from,
            year_to,
            month_to,
            day_to,
            domains_to_skip,
        ],
        outputs=[
            bcLabel,
            mcLabel,
            sentenceBreakdown,
        ],
        api_name="main",
    )

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

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

    date_from = ""
    date_to = ""

demo.launch()