Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Lấy giá trị của biến môi trường
|
| 7 |
+
model_name = os.getenv("model_name") # Lấy giá trị của biến môi trường
|
| 8 |
+
|
| 9 |
+
# Khởi tạo pipeline
|
| 10 |
+
pipe = pipeline("image-classification", model=model_name)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Hàm xử lý ảnh đầu vào
|
| 15 |
+
def detect(image):
|
| 16 |
+
# Chạy mô hình trên ảnh
|
| 17 |
+
results = pipe(image)
|
| 18 |
+
|
| 19 |
+
# Chuyển kết quả thành dạng dễ đọc
|
| 20 |
+
result_dict = {res["label"]: res["score"] for res in results}
|
| 21 |
+
|
| 22 |
+
# Lấy xác suất của từng lớp (DeepFake và Real)
|
| 23 |
+
deepfake_score = result_dict.get("Fake", 0.0)
|
| 24 |
+
real_score = result_dict.get("Real", 0.0)
|
| 25 |
+
|
| 26 |
+
return deepfake_score, real_score
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
custom_css = """
|
| 30 |
+
.button-gradient {
|
| 31 |
+
background: linear-gradient(45deg, #ff416c, #ff4b2b, #ff9b00, #ff416c);
|
| 32 |
+
background-size: 400% 400%;
|
| 33 |
+
border: none;
|
| 34 |
+
padding: 14px 28px;
|
| 35 |
+
font-size: 16px;
|
| 36 |
+
font-weight: bold;
|
| 37 |
+
color: white;
|
| 38 |
+
border-radius: 10px;
|
| 39 |
+
cursor: pointer;
|
| 40 |
+
transition: 0.3s ease-in-out;
|
| 41 |
+
animation: gradientAnimation 2s infinite linear;
|
| 42 |
+
box-shadow: 0 4px 10px rgba(255, 65, 108, 0.6);
|
| 43 |
+
}
|
| 44 |
+
@keyframes gradientAnimation {
|
| 45 |
+
0% { background-position: 0% 50%; }
|
| 46 |
+
25% { background-position: 50% 100%; }
|
| 47 |
+
50% { background-position: 100% 50%; }
|
| 48 |
+
75% { background-position: 50% 0%; }
|
| 49 |
+
100% { background-position: 0% 50%; }
|
| 50 |
+
}
|
| 51 |
+
.button-gradient:hover {
|
| 52 |
+
transform: scale(1.05);
|
| 53 |
+
box-shadow: 0 6px 15px rgba(255, 75, 43, 0.8);
|
| 54 |
+
}
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# Giao diện Gradio
|
| 59 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 60 |
+
gr.Markdown("# 🔍 DeepFake Detector\nUpload an image to check if it's DeepFake or Real.")
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
with gr.Column():
|
| 64 |
+
image = gr.Image(type="pil", label="Upload Image")
|
| 65 |
+
detect_button = gr.Button("Detect")
|
| 66 |
+
|
| 67 |
+
with gr.Column():
|
| 68 |
+
deepfake_label = gr.Label(label="DeepFake Probability")
|
| 69 |
+
real_label = gr.Label(label="Real Probability")
|
| 70 |
+
|
| 71 |
+
detect_button.click(detect, inputs=[image], outputs=[deepfake_label, real_label])
|
| 72 |
+
|
| 73 |
+
# Khởi chạy ứng dụng
|
| 74 |
+
demo.launch()
|