TextDetector / app.py
hugfaceguy0001's picture
Upload app.py
fbe14df verified
raw
history blame
1.5 kB
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import gradio as gr
tokenizer = AutoTokenizer.from_pretrained("hugfaceguy0001/AITextDetector")
model = AutoModelForSequenceClassification.from_pretrained("hugfaceguy0001/AITextDetector").to("cuda")
def detect_text(text):
with torch.no_grad():
inputs = tokenizer(text, return_tensors="pt").to("cuda")
logits = model(**inputs).logits
probs = torch.nn.functional.softmax(logits)[0,:]
labels = {model.config.id2label[i]:float(probs[i]) for i in range(3)}
labels = dict(sorted(labels.items(),key=lambda x:x[1],reverse=True))
return labels
myexamples = ["黑神话这个游戏,你喷它优化、喷它地图、喷它空气墙、喷它战斗、喷它剧情;这些东西不管我自己觉得有没有喷一下的必要,可也都或多或少的能理解它为什么被喷的原因。",
"加密通信助长犯罪的说法并不成立,理由如下:隐私权的基本保障:加密技术保障了个人和组织的隐私权,这是基本人权的一部分。无论是个人的日常通信、商业机密,还是新闻记者的来源保护,加密通信都在确保合法和正当的隐私需求不被侵犯。"]
demo = gr.Interface(fn=detect_text,
inputs=gr.Text(label="请输入待检测的文本:"),
outputs=gr.Label(num_top_classes=3),
examples=myexamples
)
demo.launch()