MonkeyDAnh commited on
Commit
581a46a
·
verified ·
1 Parent(s): a137566

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +68 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import gradio as gr
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
+ import torch
5
+ import torch.nn.functional as F
6
+
7
+ # 1. Tải mô hình và tokenizer từ Hugging Face Hub
8
+ model_name = "MonkeyDAnh/Model-AI-Percentage-used-v2" # Thay thế bằng tên repo model của bạn
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
11
+
12
+ # Kiểm tra nếu có GPU thì sử dụng GPU
13
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
+ model.to(device)
15
+ model.eval() # Chuyển mô hình sang chế độ đánh giá
16
+
17
+ # 2. Định nghĩa hàm dự đoán
18
+ def predict_ai_human(text):
19
+ """
20
+ Hàm dự đoán tỷ lệ AI/Human dựa trên văn bản đầu vào.
21
+ """
22
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
23
+ inputs = {k: v.to(device) for k, v in inputs.items()} # Chuyển inputs sang device
24
+
25
+ with torch.no_grad():
26
+ outputs = model(**inputs)
27
+
28
+ # Lấy logits và chuyển đổi sang xác suất
29
+ # Giả sử mô hình của bạn có 2 lớp: 0 (Human), 1 (AI)
30
+ # Hoặc ngược lại, tùy thuộc vào cách bạn huấn luyện
31
+ # Cần kiểm tra thứ tự nhãn của mô hình của bạn!
32
+ # Ví dụ: model.config.id2label nếu có
33
+ # Đối với model binary classification, thường có 2 output logits
34
+ # Output thường là logits cho mỗi lớp.
35
+ # Nếu mô hình của bạn dự đoán "AI percentage", thì output có thể là 1 giá trị duy nhất
36
+ # hoặc 2 giá trị logits (cho Human và AI).
37
+
38
+ # Nếu output là logits cho 2 lớp (ví dụ: [logit_human, logit_ai])
39
+ logits = outputs.logits
40
+ probabilities = F.softmax(logits, dim=1) # Áp dụng softmax để có xác suất
41
+
42
+ # Lấy xác suất của lớp "AI" (giả sử lớp 1 là AI)
43
+ # Bạn cần xác nhận lại id của lớp AI từ quá trình huấn luyện của bạn
44
+ # Ví dụ: nếu model.config.id2label = {0: 'human', 1: 'ai'}
45
+ ai_probability = probabilities[:, 1].item() * 100 # Xác suất của lớp AI
46
+ human_probability = probabilities[:, 0].item() * 100 # Xác suất của lớp Human
47
+
48
+ return f"AI Percentage: {ai_probability:.2f}% (Human Percentage: {human_probability:.2f}%)"
49
+
50
+
51
+ # 3. Tạo giao diện Gradio
52
+ # gr.Interface(fn, inputs, outputs, title, description)
53
+ # fn: Hàm dự đoán
54
+ # inputs: Các thành phần đầu vào (ví dụ: gr.Textbox)
55
+ # outputs: Các thành phần đầu ra (ví dụ: gr.Textbox)
56
+ # title, description: Tiêu đề và mô tả cho giao diện
57
+
58
+ iface = gr.Interface(
59
+ fn=predict_ai_human,
60
+ inputs=gr.Textbox(lines=10, label="Nhập văn bản vào đây để kiểm tra"),
61
+ outputs=gr.Textbox(label="Kết quả dự đoán"),
62
+ title="Phân loại văn bản AI/Human",
63
+ description="Một demo nhỏ để kiểm tra tỷ lệ văn bản được tạo bởi AI so với văn bản của con người."
64
+ )
65
+
66
+ # 4. Khởi chạy giao diện
67
+ if __name__ == "__main__":
68
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ transformers
3
+ torch
4
+ pandas