Spaces:
Running
Running
File size: 5,615 Bytes
10a4a0a d47e042 10a4a0a d47e042 10a4a0a b7f710c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
import gradio as gr
from PIL import Image
from gradio_app.inference import run_inference
from gradio_app.components import (
list_reference_files, list_mapping_files,
list_classifier_files, list_edgeface_files
)
from gradio_app.project_info import (
CONTENT_DESCRIPTION, CONTENT_OUTTRO,
CONTENT_IN_1, CONTENT_IN_2,
CONTENT_OUT_1, CONTENT_OUT_2
)
from glob import glob
import os
def create_image_io_row():
"""Create the row for image input and output display."""
with gr.Row(elem_classes=["image-io-row"]):
image_input = gr.Image(type="pil", label="Upload Image")
output = gr.HTML(label="Inference Results", elem_classes=["results-container"])
return image_input, output
def create_model_settings_row():
"""Create the row for model files and settings."""
with gr.Row():
with gr.Column():
with gr.Group(elem_classes=["section-group"]):
gr.Markdown("### Model Files", elem_classes=["section-title"])
ref_dict = gr.Dropdown(
choices=["Select a file"] + list_reference_files(),
label="Reference Dict JSON",
value="data/reference_data/reference_image_data.json"
)
index_map = gr.Dropdown(
choices=["Select a file"] + list_mapping_files(),
label="Index to Class Mapping JSON",
value="ckpts/index_to_class_mapping.json"
)
classifier_model = gr.Dropdown(
choices=["Select a file"] + list_classifier_files(),
label="Classifier Model (.pth)",
value="ckpts/SlimFace_efficientnet_b3_full_model.pth"
)
edgeface_model = gr.Dropdown(
choices=["Select a file"] + list_edgeface_files(),
label="EdgeFace Model (.pt)",
value="ckpts/idiap/edgeface_s_gamma_05.pt"
)
with gr.Column():
with gr.Group(elem_classes=["section-group"]):
gr.Markdown("### Advanced Settings", elem_classes=["section-title"])
algorithm = gr.Dropdown(
choices=["yolo", "mtcnn", "retinaface"],
label="Detection Algorithm",
value="yolo"
)
accelerator = gr.Dropdown(
choices=["auto", "cpu", "cuda", "mps"],
label="Accelerator",
value="auto"
)
resolution = gr.Slider(
minimum=128,
maximum=512,
step=32,
label="Image Resolution",
value=300
)
similarity_threshold = gr.Slider(
minimum=0.1,
maximum=1.0,
step=0.05,
label="Similarity Threshold",
value=0.3
)
return ref_dict, index_map, classifier_model, edgeface_model, algorithm, accelerator, resolution, similarity_threshold
# Load local CSS file
CSS = open("apps/gradio_app/static/styles.css").read()
def create_interface():
"""Create the Gradio interface for SlimFace."""
with gr.Blocks(css=CSS, theme=gr.themes.Soft()) as demo:
gr.Markdown("# SlimFace Demonstration")
gr.Markdown(CONTENT_DESCRIPTION)
gr.Markdown(CONTENT_IN_1)
gr.HTML(CONTENT_IN_2)
image_input, output = create_image_io_row()
ref_dict, index_map, classifier_model, edgeface_model, algorithm, accelerator, resolution, similarity_threshold = create_model_settings_row()
# Add example image gallery as a row of columns
with gr.Group():
gr.Markdown("### Example Images")
example_images = glob("apps/assets/examples/*.[jp][pn][gf]")
if example_images:
with gr.Row(elem_classes=["example-row"]):
for img_path in example_images:
with gr.Column(min_width=120):
gr.Image(
value=img_path,
label=os.path.basename(img_path),
type="filepath",
height=100,
elem_classes=["example-image"]
)
gr.Button(f"Use {os.path.basename(img_path)}").click(
fn=lambda x=img_path: Image.open(x),
outputs=image_input
)
else:
gr.Markdown("No example images found in apps/assets/examples/")
with gr.Row():
submit_btn = gr.Button("Run Inference", variant="primary", elem_classes=["centered-button"])
submit_btn.click(
fn=run_inference,
inputs=[
image_input,
ref_dict,
index_map,
classifier_model,
edgeface_model,
algorithm,
accelerator,
resolution,
similarity_threshold
],
outputs=output
)
gr.Markdown(CONTENT_OUTTRO)
gr.HTML(CONTENT_OUT_1)
gr.Markdown(CONTENT_OUT_2)
return demo
def main():
"""Launch the Gradio interface."""
demo = create_interface()
demo.launch()
if __name__ == "__main__":
main() |