Spaces:
Running
Running
File size: 1,603 Bytes
592df58 |
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 |
import os
import gradio as gr
import tempfile
from gradio_client import Client, handle_file
import base64
BACKEND = os.getenv("BACKEND")
JS2 = os.getenv("JS2")
MAIN_HTML = os.getenv("MAIN_HTML")
MAIN_JS = os.getenv("MAIN_JS")
HEAD_HTML = os.getenv("HEAD_HTML")
CSS = os.getenv("CSS")
backend = Client(BACKEND)
def base64_to_image(base64_str):
if ";base64," in base64_str:
base64_str = base64_str.split(";base64,")[1].strip()
return base64.b64decode(base64_str + '=' * (-len(base64_str) % 4))
def search_image(base64_image):
try:
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
temp_file.write(base64_to_image(base64_image))
file = temp_file.name
handle_file(file)
except Exception as e:
print(e)
gr.Info("Please upload an image file.")
return []
result_text = backend.predict(
file=handle_file(file),
token="",
api_name="/search_face"
)
os.remove(file)
return result_text
with gr.Blocks(css=CSS, head=HEAD_HTML, title="DeepSeek? FaceSeek!") as iface:
html = gr.HTML(MAIN_HTML)
js_txt = gr.Textbox(label="Base64-Image", elem_id="base64_image", visible=False)
out_txt = gr.Textbox(label="Result", visible=False)
search_button = gr.Button("🔍 Free Face Search", elem_id="search_btn", visible=False)
search_button.click(search_image, inputs=js_txt, outputs=out_txt).success(None, inputs=[out_txt], js=JS2)
iface.load(None, inputs=None, outputs=html, js=MAIN_JS)
# Launch the interface
iface.launch() |