Spaces:
Build error
Build error
File size: 6,137 Bytes
244b0b6 | 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | import gradio as gr
from typing import Dict, Any
from models.data_manager import DataManager
from models.image_processor import (
image_search_performer,
image_search_performers,
find_faces_in_sprite
)
class WebInterface:
def __init__(self, data_manager: DataManager, default_threshold: float = 0.5):
"""
Initialize the web interface.
Parameters:
data_manager: DataManager instance
default_threshold: Default confidence threshold
"""
self.data_manager = data_manager
self.default_threshold = default_threshold
def image_search(self, img, threshold, results):
"""Wrapper for the image search function"""
return image_search_performer(img, self.data_manager, threshold, results)
def multiple_image_search(self, img, threshold, results):
"""Wrapper for the multiple image search function"""
return image_search_performers(img, self.data_manager, threshold, results)
def vector_search(self, vector_json, threshold, results):
"""Wrapper for the vector search function (deprecated)"""
return {'status': 'not implemented'}
def _create_image_search_interface(self):
"""Create the single face search interface"""
with gr.Blocks() as interface:
gr.Markdown("# Who is in the photo?")
gr.Markdown("Upload an image of a person and we'll tell you who it is.")
with gr.Row():
with gr.Column():
img_input = gr.Image()
threshold = gr.Slider(
label="threshold",
minimum=0.0,
maximum=1.0,
value=self.default_threshold
)
results_count = gr.Slider(
label="results",
minimum=0,
maximum=50,
value=3,
step=1
)
search_btn = gr.Button("Search")
with gr.Column():
output = gr.JSON(label="Results")
search_btn.click(
fn=self.image_search,
inputs=[img_input, threshold, results_count],
outputs=output
)
return interface
def _create_multiple_image_search_interface(self):
"""Create the multiple face search interface"""
with gr.Blocks() as interface:
gr.Markdown("# Who is in the photo?")
gr.Markdown("Upload an image of a person(s) and we'll tell you who it is.")
with gr.Row():
with gr.Column():
img_input = gr.Image(type="pil")
threshold = gr.Slider(
label="threshold",
minimum=0.0,
maximum=1.0,
value=self.default_threshold
)
results_count = gr.Slider(
label="results",
minimum=0,
maximum=50,
value=3,
step=1
)
search_btn = gr.Button("Search")
with gr.Column():
output = gr.JSON(label="Results")
search_btn.click(
fn=self.multiple_image_search,
inputs=[img_input, threshold, results_count],
outputs=output
)
return interface
def _create_vector_search_interface(self):
"""Create the vector search interface (deprecated)"""
with gr.Blocks() as interface:
gr.Markdown("# Vector Search (deprecated)")
with gr.Row():
with gr.Column():
vector_input = gr.Textbox()
threshold = gr.Slider(
label="threshold",
minimum=0.0,
maximum=1.0,
value=self.default_threshold
)
results_count = gr.Slider(
label="results",
minimum=0,
maximum=50,
value=3,
step=1
)
search_btn = gr.Button("Search")
with gr.Column():
output = gr.JSON(label="Results")
search_btn.click(
fn=self.vector_search,
inputs=[vector_input, threshold, results_count],
outputs=output
)
return interface
def _create_faces_in_sprite_interface(self):
"""Create the faces in sprite interface"""
with gr.Blocks() as interface:
gr.Markdown("# Find Faces in Sprite")
with gr.Row():
with gr.Column():
img_input = gr.Image()
vtt_input = gr.Textbox(label="VTT file")
search_btn = gr.Button("Process")
with gr.Column():
output = gr.JSON(label="Results")
search_btn.click(
fn=find_faces_in_sprite,
inputs=[img_input, vtt_input],
outputs=output
)
return interface
def launch(self, server_name="0.0.0.0", server_port=7860, share=True):
"""Launch the web interface"""
with gr.Blocks() as demo:
with gr.Tabs() as tabs:
with gr.TabItem("Single Face Search"):
self._create_image_search_interface()
with gr.TabItem("Multiple Face Search"):
self._create_multiple_image_search_interface()
with gr.TabItem("Vector Search"):
self._create_vector_search_interface()
with gr.TabItem("Faces in Sprite"):
self._create_faces_in_sprite_interface()
demo.queue().launch(share=share, ssr_mode=False)
|