Clement1290 commited on
Commit
f578e5e
·
verified ·
1 Parent(s): e9102f3

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +77 -0
  2. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn.functional as F
3
+ from transformers import RobertaTokenizerFast, RobertaForSequenceClassification
4
+ import gradio as gr
5
+
6
+
7
+ MODEL_NAME = "Clement1290/261_DetectionAI_GeminiPlusGPT"
8
+
9
+ LABEL_MAP = {
10
+ 0: "Human",
11
+ 1: "AI"
12
+ }
13
+
14
+ # ===== 加载模型 & tokenizer =====
15
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
16
+
17
+ tokenizer = RobertaTokenizerFast.from_pretrained(MODEL_NAME)
18
+ model = RobertaForSequenceClassification.from_pretrained(MODEL_NAME)
19
+ model.to(device)
20
+ model.eval()
21
+
22
+
23
+ def classify_text(text: str):
24
+ if not text.strip():
25
+ return "N/A", "Please input some text.", 0.0
26
+
27
+ inputs = tokenizer(
28
+ text,
29
+ return_tensors="pt",
30
+ truncation=True,
31
+ max_length=512,
32
+ padding=True,
33
+ ).to(device)
34
+
35
+ with torch.no_grad():
36
+ logits = model(**inputs).logits
37
+ probs = F.softmax(logits, dim=-1)[0]
38
+
39
+ pred_label = torch.argmax(probs).item()
40
+ confidence = probs[pred_label].item()
41
+
42
+ label_str = LABEL_MAP.get(pred_label, "Unknown")
43
+ pred_str = f"{pred_label} ({label_str})"
44
+
45
+ return pred_str, f"{label_str}", round(float(confidence), 4)
46
+
47
+
48
+ # ===== Gradio 界面 =====
49
+ title = "Human vs AI Text Classifier"
50
+ description = (
51
+ "RoBERTa-based binary classifier. "
52
+ "Prediction: 0 = Human, 1 = AI."
53
+ )
54
+
55
+ demo = gr.Interface(
56
+ fn=classify_text,
57
+ inputs=gr.Textbox(
58
+ lines=8,
59
+ placeholder="Paste a paragraph here...",
60
+ label="Input text",
61
+ ),
62
+ outputs=[
63
+ gr.Textbox(label="Raw Prediction (id + label)"),
64
+ gr.Textbox(label="Label"),
65
+ gr.Number(label="Confidence score")
66
+ ],
67
+ title=title,
68
+ description=description,
69
+ examples=[
70
+ ["This is a short note I wrote myself about my day and my thoughts."],
71
+ ["As an advanced AI model, I can generate human-like responses across a wide range of tasks."]
72
+ ]
73
+ )
74
+
75
+ if __name__ == "__main__":
76
+ demo.launch()
77
+
requirements.txt ADDED
File without changes