Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import json
|
| 4 |
+
from gradio_client import Client, handle_file
|
| 5 |
+
|
| 6 |
+
backend = Client(os.getenv("BACKEND"), hf_token=os.getenv("TOKEN"))
|
| 7 |
+
JS_FUNC1 = os.getenv("JS_FUNC1")
|
| 8 |
+
JS_FUNC2 = os.getenv("JS_FUNC2")
|
| 9 |
+
|
| 10 |
+
def detect(image):
|
| 11 |
+
try:
|
| 12 |
+
file_1 = handle_file(image)
|
| 13 |
+
except Exception as e:
|
| 14 |
+
gr.Info("Please upload an image file.")
|
| 15 |
+
return "", "", "", {}
|
| 16 |
+
|
| 17 |
+
result_text = backend.predict(
|
| 18 |
+
image=handle_file(image),
|
| 19 |
+
api_name="/detect"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
result = json.loads(result_text)
|
| 23 |
+
if result and result["status"] == "ok":
|
| 24 |
+
overall = result["overall"]
|
| 25 |
+
aigen = result["aigen"]
|
| 26 |
+
deepfake = result["deepfake"]
|
| 27 |
+
|
| 28 |
+
breakdown = {
|
| 29 |
+
"GenAI": 99,
|
| 30 |
+
"Face Manipulation": 99,
|
| 31 |
+
"Diffusion (Flux)": 99,
|
| 32 |
+
"StyleGAN": 0,
|
| 33 |
+
}
|
| 34 |
+
return overall, aigen, deepfake, breakdown
|
| 35 |
+
else:
|
| 36 |
+
raise gr.Error("Error in processing image")
|
| 37 |
+
|
| 38 |
+
custom_css = """
|
| 39 |
+
.result-card {
|
| 40 |
+
background: white;
|
| 41 |
+
border-radius: 20px;
|
| 42 |
+
padding: 24px;
|
| 43 |
+
box-shadow: 0 6px 20px rgba(0,0,0,0.1);
|
| 44 |
+
}
|
| 45 |
+
.result-title {
|
| 46 |
+
font-size: 20px;
|
| 47 |
+
font-weight: bold;
|
| 48 |
+
margin-bottom: 12px;
|
| 49 |
+
}
|
| 50 |
+
.progress-container {
|
| 51 |
+
margin-bottom: 12px;
|
| 52 |
+
}
|
| 53 |
+
.progress-label {
|
| 54 |
+
font-size: 14px;
|
| 55 |
+
font-weight: 500;
|
| 56 |
+
}
|
| 57 |
+
.progress-bar {
|
| 58 |
+
height: 12px;
|
| 59 |
+
border-radius: 6px;
|
| 60 |
+
background: #e5e7eb;
|
| 61 |
+
margin-top: 4px;
|
| 62 |
+
overflow: hidden;
|
| 63 |
+
}
|
| 64 |
+
.progress-fill {
|
| 65 |
+
height: 100%;
|
| 66 |
+
background: linear-gradient(90deg, #ff416c, #ff4b2b);
|
| 67 |
+
}
|
| 68 |
+
"""
|
| 69 |
+
|
| 70 |
+
MARKDOWN0 = """
|
| 71 |
+
# 🔍 DeepFake Detector
|
| 72 |
+
Upload an image and check if it’s AI-generated or manipulated.
|
| 73 |
+
"""
|
| 74 |
+
MARKDOWN3 = """
|
| 75 |
+
<div align="right"><a href="https://faceonlive.com/face-search-online" target='_blank'>Reverse Face Search</a></div>
|
| 76 |
+
<div align="right"><a href="https://faceonlive.com/reverse-image-search" target='_blank'>Reverse Image Search</a></div>
|
| 77 |
+
"""
|
| 78 |
+
|
| 79 |
+
def breakdown_ui(breakdown: dict):
|
| 80 |
+
html = ""
|
| 81 |
+
for label, val in breakdown.items():
|
| 82 |
+
html += f"""
|
| 83 |
+
<div class="progress-container">
|
| 84 |
+
<div class="progress-label">{label}: {val}%</div>
|
| 85 |
+
<div class="progress-bar"><div class="progress-fill" style="width:{val}%;"></div></div>
|
| 86 |
+
</div>
|
| 87 |
+
"""
|
| 88 |
+
return html
|
| 89 |
+
|
| 90 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 91 |
+
gr.Markdown(MARKDOWN0)
|
| 92 |
+
|
| 93 |
+
with gr.Row():
|
| 94 |
+
with gr.Column(scale=1):
|
| 95 |
+
image = gr.Image(type='filepath', height=360, label="Upload Image")
|
| 96 |
+
detect_button = gr.Button("🚀 Detect", elem_classes="button-gradient")
|
| 97 |
+
with gr.Column(scale=2):
|
| 98 |
+
overall = gr.Label(label="Overall Result")
|
| 99 |
+
aigen = gr.Label(label="Generative AI Model")
|
| 100 |
+
deepfake = gr.Label(label="Face Manipulation")
|
| 101 |
+
breakdown_html = gr.HTML(label="Detection Breakdown")
|
| 102 |
+
|
| 103 |
+
gr.Markdown(MARKDOWN3)
|
| 104 |
+
|
| 105 |
+
detect_button.click(
|
| 106 |
+
detect,
|
| 107 |
+
inputs=image,
|
| 108 |
+
outputs=[overall, aigen, deepfake, breakdown_html],
|
| 109 |
+
).then(
|
| 110 |
+
breakdown_ui,
|
| 111 |
+
inputs=[breakdown_html],
|
| 112 |
+
outputs=breakdown_html
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
demo.launch(server_name="0.0.0.0", share=True)
|