Spaces:
Runtime error
Runtime error
File size: 5,443 Bytes
95f2072 bcfb3b3 d7d8cc0 bcfb3b3 d7d8cc0 f7c6dc5 27c3fda d7d8cc0 372d293 bcfb3b3 d7d8cc0 bcfb3b3 a240a54 bcfb3b3 d7d8cc0 a7553e2 27c3fda fd64511 27c3fda 4bc60b3 c8133e1 27c3fda 44091f5 7b74aa3 27c3fda bcfb3b3 d7d8cc0 b7d5b9c d7d8cc0 fbb9dbc 61bf30e 1e9e4f9 61bf30e 1e9e4f9 61bf30e 1e9e4f9 61bf30e 8db0e68 61bf30e 8db0e68 61bf30e 7fa2da8 27c3fda 8181252 b6bdfa9 af1a748 d82f91a bc70525 03a498a bc70525 af1a748 8181252 |
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 |
from flask import Flask, request, jsonify
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import RobertaConfig
from transformers import RobertaForSequenceClassification, RobertaTokenizer, RobertaConfig
import torch
from torch import cuda
import gradio as gr
import os
import re
app = Flask(__name__)
ACCESS_TOKEN = os.environ["ACCESS_TOKEN"]
# config = RobertaConfig.from_pretrained("PirateXX/ChatGPT-Text-Detector", use_auth_token= ACCESS_TOKEN)
# model = RobertaForSequenceClassification.from_pretrained("PirateXX/ChatGPT-Text-Detector", use_auth_token= ACCESS_TOKEN, config = config)
device = 'cuda' if cuda.is_available() else 'cpu'
tokenizer = AutoTokenizer.from_pretrained("PirateXX/AI-Content-Detector", use_auth_token= ACCESS_TOKEN)
model = AutoModelForSequenceClassification.from_pretrained("PirateXX/AI-Content-Detector", use_auth_token= ACCESS_TOKEN)
model.to(device)
# model_name = "roberta-base"
# tokenizer = RobertaTokenizer.from_pretrained(model_name, map_location=torch.device('cpu'))
def text_to_sentences(text):
clean_text = text.replace('\n', ' ')
return re.split(r'(?<=[^A-Z].[.?]) +(?=[A-Z])', clean_text)
# function to concatenate sentences into chunks of size 900 or less
def chunks_of_900(text, chunk_size = 900):
sentences = text_to_sentences(text)
chunks = []
current_chunk = ""
for sentence in sentences:
if len(current_chunk + sentence) <= chunk_size:
if len(current_chunk)!=0:
current_chunk += " "+sentence
else:
current_chunk += sentence
else:
chunks.append(current_chunk)
current_chunk = sentence
chunks.append(current_chunk)
return chunks
def predict(query):
tokens = tokenizer.encode(query)
all_tokens = len(tokens)
tokens = tokens[:tokenizer.model_max_length - 2]
used_tokens = len(tokens)
tokens = torch.tensor([tokenizer.bos_token_id] + tokens + [tokenizer.eos_token_id]).unsqueeze(0)
mask = torch.ones_like(tokens)
with torch.no_grad():
logits = model(tokens.to(device), attention_mask=mask.to(device))[0]
probs = logits.softmax(dim=-1)
fake, real = probs.detach().cpu().flatten().numpy().tolist()
return real
def findRealProb(data):
with app.app_context():
if data is None or len(data) == 0:
return ({'error': 'No query provided'})
if len(data) > 9400:
return ({'error': 'Cannot analyze more than 9400 characters!'})
if len(data.split()) > 1500:
return ({'error': 'Cannot analyze more than 1500 words'})
# return {"Real": predict(data)}
chunksOfText = (chunks_of_900(data))
results = []
for chunk in chunksOfText:
outputv1 = predict(chunk)
# outputv2 = predict(chunk, modelv2, tokenizerv2)
label = "AI"
if(outputv1>=0.5):
label = "Human"
results.append({"Text":chunk, "Label": label, "Confidence":(outputv1)})
ans = 0
cnt = 0
for result in results:
length = len(result["Text"])
confidence = result["Confidence"]
cnt += length
ans = ans + (confidence)*(length)
realProb = ans/cnt
label = "AI"
if realProb > 0.7:
label = "Human"
elif realProb > 0.3 and realProb < 0.7:
label = "Might be AI"
return ({"Human": realProb, "AI": 1-realProb, "Label": label, "Chunks": results})
demo = gr.Interface(
fn=findRealProb,
inputs=gr.Textbox(placeholder="Copy and paste here..."),
article = "Visit <a href = \"https://ai-content-detector.online/\">AI Content Detector</a> for better user experience!",
outputs = gr.outputs.JSON(),
# interpretation = "default",
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."])
demo.launch(show_api=False) |