Shad0ws commited on
Commit
4770499
·
1 Parent(s): 036e486

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +68 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request
2
+ import gradio as gr
3
+ import os
4
+ import re
5
+ app = Flask(__name__)
6
+
7
+ import torch
8
+ from torch import cuda
9
+ from tqdm import tqdm
10
+ from transformers import GPT2LMHeadModel, GPT2TokenizerFast
11
+
12
+ device = 'cuda' if cuda.is_available() else 'cpu'
13
+ model_id = "gpt2"
14
+ modelgpt2 = GPT2LMHeadModel.from_pretrained(model_id).to(device)
15
+ tokenizergpt2 = GPT2TokenizerFast.from_pretrained(model_id)
16
+
17
+ def text_to_sentences(text):
18
+ clean_text = text.replace('\n', ' ')
19
+ return re.split(r'(?<=[^A-Z].[.?]) +(?=[A-Z])', clean_text)
20
+
21
+ def calculatePerplexity(text):
22
+ encodings = tokenizergpt2("\n\n".join([text]), return_tensors="pt")
23
+ max_length = modelgpt2.config.n_positions
24
+ stride = 512
25
+ seq_len = encodings.input_ids.size(1)
26
+
27
+ nlls = []
28
+ prev_end_loc = 0
29
+ for begin_loc in range(0, seq_len, stride):
30
+ end_loc = min(begin_loc + max_length, seq_len)
31
+ trg_len = end_loc - prev_end_loc
32
+ input_ids = encodings.input_ids[:, begin_loc:end_loc].to(device)
33
+ target_ids = input_ids.clone()
34
+ target_ids[:, :-trg_len] = -100
35
+
36
+ with torch.no_grad():
37
+ outputs = modelgpt2(input_ids, labels=target_ids)
38
+ neg_log_likelihood = outputs.loss * trg_len
39
+
40
+ nlls.append(neg_log_likelihood)
41
+
42
+ prev_end_loc = end_loc
43
+ if end_loc == seq_len:
44
+ break
45
+
46
+ ppl = torch.exp(torch.stack(nlls).sum() / end_loc)
47
+
48
+ return ppl.item()
49
+
50
+ def calculatePerplexities(text):
51
+ sentences = text_to_sentences(text)
52
+ perplexities = []
53
+ for sentence in sentences:
54
+ perplexity = calculatePerplexity(sentence)
55
+ label = "Human"
56
+ if perplexity<25:
57
+ label = "AI"
58
+ perplexities.append({"sentence": sentence, "perplexity": perplexity, "label": label})
59
+ return perplexities
60
+
61
+ demo = gr.Interface(
62
+ fn=calculatePerplexities,
63
+ inputs=gr.Textbox(placeholder="Copy and paste here..."),
64
+ article = "Visit <a href = \"https://ai-content-detector.online/\">AI Content Detector</a> for better user experience!",
65
+ outputs=gr.outputs.JSON(),
66
+ interpretation="default",
67
+ examples=["Cristiano Ronaldo is a Portuguese professional soccer player who currently plays as a forward for Manchester United and the Portugal national team. He is widely considered one of the greatest soccer players of all time, having won numerous awards and accolades throughout his career. Ronaldo began his professional career with Sporting CP in Portugal before moving to Manchester United in 2003. He spent six seasons with the club, winning three Premier League titles and one UEFA Champions League title. In 2009, he transferred to Real Madrid for a then-world record transfer fee of $131 million. He spent nine seasons with the club, winning four UEFA Champions League titles, two La Liga titles, and two Copa del Rey titles. In 2018, he transferred to Juventus, where he spent three seasons before returning to Manchester United in 2021. He has also had a successful international career with the Portugal national team, having won the UEFA European Championship in 2016 and the UEFA Nations League in 2019.", "One rule of thumb which applies to everything that we do - professionally and personally : Know what the customer want and deliver. In this case, it is important to know what the organisation what from employee. Connect the same to the KRA. Are you part of a delivery which directly ties to the larger organisational objective. If yes, then the next question is success rate of one’s delivery. If the KRAs are achieved or exceeded, then the employee is entitled for a decent hike."])
68
+ demo.launch(show_api=False)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ flask
2
+ torch
3
+ tqdm
4
+ transformers
5
+ gradio