import gradio as gr
from api import check_liveness
EXAMPLE_IMAGES = [[f"assets/{i}.jpg"] for i in range(1, 4)]
THRESHOLD = 0.5
def _normalize(d: dict) -> dict:
return {k.strip().replace(" ", "_"): v for k, v in d.items()}
def _format_result_html(results: list) -> str:
if not results or (isinstance(results, dict) and "error" in results):
return ""
if isinstance(results, dict):
results = [results]
max_prob = 0
max_attack = ""
rows = ""
for item in results:
r = _normalize(item)
attack = r.get("attack_method", "Unknown")
prob = r.get("liveness_probability", 0)
score = r.get("liveness_score", 0)
quality = r.get("quality_score", 0)
state = r.get("state", "")
if isinstance(prob, str):
try:
prob = float(prob)
except (ValueError, TypeError):
prob = 0
if prob > max_prob:
max_prob = prob
max_attack = attack
pct = round(max(0, min(prob, 1)) * 100)
bar_color = "#059669" if prob < THRESHOLD else "#dc2626"
rows += f"""
|
{attack}
|
|
{state}
|
"""
is_genuine = max_prob < THRESHOLD
if is_genuine:
badge_text = "โ
GENUINE / LIVE"
badge_color = "#059669"
bg_color = "#ecfdf5"
border_color = "#a7f3d0"
else:
badge_text = f"โ SPOOF / FAKE โ {max_attack}"
badge_color = "#dc2626"
bg_color = "#fef2f2"
border_color = "#fecaca"
return f"""
Liveness Result
{badge_text}
| Attack Method |
Probability |
Status |
{rows}
"""
def process_image(image):
if image is None:
return (
''
"Upload or select an example image
",
{"error": "No image provided"},
)
result = check_liveness(image)
if isinstance(result, dict) and "error" in result:
return (
f'{result["error"]}
',
result,
)
return _format_result_html(result), result
def create_interface():
with gr.Blocks(
title="MiniAiLive ID Document Liveness Detection",
theme=gr.themes.Soft(primary_hue="emerald", neutral_hue="slate"),
css="footer {display: none !important;}",
) as demo:
gr.Markdown(
"""
# ๐ฅ MiniAiLive ID Document Liveness Detection
**Advanced ID Document Liveness Detection ยท On-Premise SDK**
Please Visit Website
Upload a document image to check if it is genuine or spoofed.
"""
)
with gr.Row(equal_height=False):
with gr.Column(scale=1, min_width=400):
image_input = gr.Image(label="Upload Document Image")
submit_btn = gr.Button("Check Liveness", variant="primary", size="lg")
gr.Examples(
examples=EXAMPLE_IMAGES,
inputs=image_input,
label="Example Images",
)
with gr.Column(scale=1, min_width=400):
result_html = gr.HTML(label="Result")
raw_json = gr.JSON(label="Raw Response")
submit_btn.click(
fn=process_image,
inputs=image_input,
outputs=[result_html, raw_json],
)
return demo