Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,33 @@
|
|
1 |
-
import gradio as gr
|
2 |
from flask import Flask, request
|
|
|
|
|
3 |
from transformers import RobertaForSequenceClassification, RobertaTokenizer, RobertaConfig
|
4 |
import torch
|
|
|
|
|
5 |
import os
|
6 |
import re
|
7 |
-
|
8 |
-
from typing import List
|
9 |
-
from transformers import GPT2LMHeadModel, GPT2TokenizerFast
|
10 |
app = Flask(__name__)
|
11 |
|
12 |
ACCESS_TOKEN = os.environ["ACCESS_TOKEN"]
|
13 |
-
config = RobertaConfig.from_pretrained("PirateXX/
|
14 |
-
model = RobertaForSequenceClassification.from_pretrained("PirateXX/
|
15 |
|
16 |
-
|
17 |
-
tokenizer =
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
modelgpt2 = GPT2LMHeadModel.from_pretrained(model_id).to(device)
|
22 |
-
tokenizergpt2 = GPT2TokenizerFast.from_pretrained(model_id)
|
23 |
|
24 |
def text_to_sentences(text):
|
25 |
clean_text = text.replace('\n', ' ')
|
26 |
return re.split(r'(?<=[^A-Z].[.?]) +(?=[A-Z])', clean_text)
|
27 |
|
28 |
# function to concatenate sentences into chunks of size 900 or less
|
29 |
-
def chunks_of_900(text, chunk_size=900):
|
30 |
sentences = text_to_sentences(text)
|
31 |
chunks = []
|
32 |
current_chunk = ""
|
@@ -42,7 +43,7 @@ def chunks_of_900(text, chunk_size=900):
|
|
42 |
chunks.append(current_chunk)
|
43 |
return chunks
|
44 |
|
45 |
-
def predict(query
|
46 |
tokens = tokenizer.encode(query)
|
47 |
all_tokens = len(tokens)
|
48 |
tokens = tokens[:tokenizer.model_max_length - 2]
|
@@ -72,59 +73,12 @@ def findRealProb(text):
|
|
72 |
realProb = ans/cnt
|
73 |
return {"Real": realProb, "Fake": 1-realProb}, results
|
74 |
|
75 |
-
|
76 |
-
def text_to_sentences(text):
|
77 |
-
clean_text = text.replace('\n', ' ')
|
78 |
-
return re.split(r'(?<=[^A-Z].[.?]) +(?=[A-Z])', clean_text)
|
79 |
-
|
80 |
-
def calculatePerplexity(text):
|
81 |
-
encodings = tokenizergpt2("\n\n".join([text]), return_tensors="pt")
|
82 |
-
max_length = modelgpt2.config.n_positions
|
83 |
-
stride = 512
|
84 |
-
seq_len = encodings.input_ids.size(1)
|
85 |
-
|
86 |
-
nlls = []
|
87 |
-
prev_end_loc = 0
|
88 |
-
for begin_loc in range(0, seq_len, stride):
|
89 |
-
end_loc = min(begin_loc + max_length, seq_len)
|
90 |
-
trg_len = end_loc - prev_end_loc
|
91 |
-
input_ids = encodings.input_ids[:, begin_loc:end_loc].to(device)
|
92 |
-
target_ids = input_ids.clone()
|
93 |
-
target_ids[:, :-trg_len] = -100
|
94 |
-
|
95 |
-
with torch.no_grad():
|
96 |
-
outputs = modelgpt2(input_ids, labels=target_ids)
|
97 |
-
neg_log_likelihood = outputs.loss * trg_len
|
98 |
-
|
99 |
-
nlls.append(neg_log_likelihood)
|
100 |
-
|
101 |
-
prev_end_loc = end_loc
|
102 |
-
if end_loc == seq_len:
|
103 |
-
break
|
104 |
-
|
105 |
-
ppl = torch.exp(torch.stack(nlls).sum() / end_loc)
|
106 |
-
|
107 |
-
return ppl.item()
|
108 |
-
|
109 |
-
@app.post("/getPerplexities")
|
110 |
-
async def calculate_perplexities(data: List[str]):
|
111 |
-
perplexities = []
|
112 |
-
for text in data:
|
113 |
-
sentences = text_to_sentences(text)
|
114 |
-
for sentence in sentences:
|
115 |
-
perplexity = calculatePerplexity(sentence)
|
116 |
-
label = "Human"
|
117 |
-
if perplexity < 25:
|
118 |
-
label = "AI"
|
119 |
-
perplexities.append({"sentence": sentence, "perplexity": perplexity, "label": label})
|
120 |
-
return perplexities
|
121 |
-
|
122 |
demo = gr.Interface(
|
123 |
fn=findRealProb,
|
124 |
inputs=gr.Textbox(placeholder="Copy and paste here..."),
|
125 |
article = "Visit <a href = \"https://ai-content-detector.online/\">AI Content Detector</a> for better user experience!",
|
126 |
outputs=gr.outputs.JSON(),
|
127 |
-
interpretation="default",
|
128 |
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."])
|
129 |
|
130 |
demo.launch(show_api=False)
|
|
|
|
|
1 |
from flask import Flask, request
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
from transformers import RobertaConfig
|
4 |
from transformers import RobertaForSequenceClassification, RobertaTokenizer, RobertaConfig
|
5 |
import torch
|
6 |
+
from torch import cuda
|
7 |
+
import gradio as gr
|
8 |
import os
|
9 |
import re
|
10 |
+
|
|
|
|
|
11 |
app = Flask(__name__)
|
12 |
|
13 |
ACCESS_TOKEN = os.environ["ACCESS_TOKEN"]
|
14 |
+
# config = RobertaConfig.from_pretrained("PirateXX/ChatGPT-Text-Detector", use_auth_token= ACCESS_TOKEN)
|
15 |
+
# model = RobertaForSequenceClassification.from_pretrained("PirateXX/ChatGPT-Text-Detector", use_auth_token= ACCESS_TOKEN, config = config)
|
16 |
|
17 |
+
device = 'cuda' if cuda.is_available() else 'cpu'
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained("PirateXX/AI-Content-Detector", use_auth_token= ACCESS_TOKEN)
|
19 |
+
model = AutoModelForSequenceClassification.from_pretrained("PirateXX/AI-Content-Detector", use_auth_token= ACCESS_TOKEN)
|
20 |
+
model.to(device)
|
21 |
|
22 |
+
# model_name = "roberta-base"
|
23 |
+
# tokenizer = RobertaTokenizer.from_pretrained(model_name, map_location=torch.device('cpu'))
|
|
|
|
|
24 |
|
25 |
def text_to_sentences(text):
|
26 |
clean_text = text.replace('\n', ' ')
|
27 |
return re.split(r'(?<=[^A-Z].[.?]) +(?=[A-Z])', clean_text)
|
28 |
|
29 |
# function to concatenate sentences into chunks of size 900 or less
|
30 |
+
def chunks_of_900(text, chunk_size = 900):
|
31 |
sentences = text_to_sentences(text)
|
32 |
chunks = []
|
33 |
current_chunk = ""
|
|
|
43 |
chunks.append(current_chunk)
|
44 |
return chunks
|
45 |
|
46 |
+
def predict(query):
|
47 |
tokens = tokenizer.encode(query)
|
48 |
all_tokens = len(tokens)
|
49 |
tokens = tokens[:tokenizer.model_max_length - 2]
|
|
|
73 |
realProb = ans/cnt
|
74 |
return {"Real": realProb, "Fake": 1-realProb}, results
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
demo = gr.Interface(
|
77 |
fn=findRealProb,
|
78 |
inputs=gr.Textbox(placeholder="Copy and paste here..."),
|
79 |
article = "Visit <a href = \"https://ai-content-detector.online/\">AI Content Detector</a> for better user experience!",
|
80 |
outputs=gr.outputs.JSON(),
|
81 |
+
# interpretation="default",
|
82 |
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."])
|
83 |
|
84 |
demo.launch(show_api=False)
|