chi77 commited on
Commit
10be7c8
·
verified ·
1 Parent(s): 098986f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
4
+
5
+ model_path = "你的模型名稱或資料夾路徑" # 根據你模型的位置來填
6
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
7
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
8
+ model.eval()
9
+
10
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
+ model.to(device)
12
+
13
+ def detect_ai(text):
14
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True).to(device)
15
+ with torch.no_grad():
16
+ outputs = model(**inputs)
17
+ probs = torch.softmax(outputs.logits, dim=1).squeeze()
18
+ human_prob = probs[0].item()
19
+ ai_prob = probs[1].item()
20
+ label = "AI 生成" if ai_prob > human_prob else "人類撰寫"
21
+ return f"預測:{label}\nAI 機率:{ai_prob:.2%}\n人類機率:{human_prob:.2%}"
22
+
23
+ demo = gr.Interface(
24
+ fn=detect_ai,
25
+ inputs=gr.Textbox(label="請貼上要分析的文字", lines=10, placeholder="貼上或輸入內容..."),
26
+ outputs="text",
27
+ title="AI vs 人類文字偵測器",
28
+ description="這個工具可以幫助你判斷輸入的文字是 AI 生成還是人類撰寫。",
29
+ )
30
+
31
+ demo.launch()