Spaces:
Running
Running
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,132 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from gradio_client import Client
|
| 3 |
-
import os
|
| 4 |
-
import tempfile
|
| 5 |
-
|
| 6 |
-
BACKEND_URL = os.environ.get("BACKEND_URL", "").strip()
|
| 7 |
-
|
| 8 |
-
try:
|
| 9 |
-
client = Client(BACKEND_URL, headers={"ngrok-skip-browser-warning": "true"})
|
| 10 |
-
backend_available = True
|
| 11 |
-
except:
|
| 12 |
-
client = None
|
| 13 |
-
backend_available = False
|
| 14 |
-
|
| 15 |
-
def process_media(file_obj, webcam_img, model_type, conf_thresh, max_dets, task_type):
|
| 16 |
-
"""Process media - backend expects both file and webcam paths"""
|
| 17 |
-
if not client:
|
| 18 |
-
return [gr.update()] * 5
|
| 19 |
-
|
| 20 |
-
try:
|
| 21 |
-
# Convert webcam PIL to file path if present
|
| 22 |
-
webcam_path = None
|
| 23 |
-
if webcam_img is not None:
|
| 24 |
-
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp:
|
| 25 |
-
webcam_img.save(tmp, 'PNG')
|
| 26 |
-
webcam_path = tmp.name
|
| 27 |
-
|
| 28 |
-
# Backend expects both parameters - use None for missing one
|
| 29 |
-
result = client.predict(
|
| 30 |
-
uploaded_file_obj=file_obj if file_obj else None,
|
| 31 |
-
webcam_image_pil=webcam_path if webcam_path else None,
|
| 32 |
-
model_type_choice=model_type,
|
| 33 |
-
conf_threshold_ui=conf_thresh,
|
| 34 |
-
max_detections_ui=max_dets,
|
| 35 |
-
task_type=task_type,
|
| 36 |
-
api_name="/process_media"
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
# Cleanup
|
| 40 |
-
if webcam_path and os.path.exists(webcam_path):
|
| 41 |
-
os.unlink(webcam_path)
|
| 42 |
-
|
| 43 |
-
return result
|
| 44 |
-
|
| 45 |
-
except Exception as e:
|
| 46 |
-
print(f"Process error: {e}")
|
| 47 |
-
# Return error message in processed image slot
|
| 48 |
-
return [
|
| 49 |
-
gr.update(), # raw image file
|
| 50 |
-
gr.update(), # raw video file
|
| 51 |
-
gr.update(), # raw image webcam
|
| 52 |
-
gr.update(value=None, visible=True), # processed image - show error
|
| 53 |
-
gr.update() # processed video
|
| 54 |
-
]
|
| 55 |
-
|
| 56 |
-
# Simplified interface without complex preview forwarding
|
| 57 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 58 |
-
gr.Markdown("# 🐵 PrimateFace Detection, Pose & Gaze Demo")
|
| 59 |
-
|
| 60 |
-
if not backend_available:
|
| 61 |
-
gr.Markdown("### 🔴 GPU Server Offline")
|
| 62 |
-
else:
|
| 63 |
-
with gr.Row():
|
| 64 |
-
with gr.Column():
|
| 65 |
-
with gr.Tabs():
|
| 66 |
-
with gr.TabItem("Upload"):
|
| 67 |
-
input_file = gr.File(label="Upload Image/Video")
|
| 68 |
-
# Simple local preview
|
| 69 |
-
preview_img = gr.Image(label="Preview", visible=False)
|
| 70 |
-
|
| 71 |
-
with gr.TabItem("Webcam"):
|
| 72 |
-
input_webcam = gr.Image(sources=["webcam"], type="pil")
|
| 73 |
-
|
| 74 |
-
clear_btn = gr.Button("Clear All")
|
| 75 |
-
|
| 76 |
-
with gr.Column():
|
| 77 |
-
gr.Markdown("### Results")
|
| 78 |
-
output_image = gr.Image(label="Processed", visible=False)
|
| 79 |
-
output_video = gr.Video(label="Processed", visible=False)
|
| 80 |
-
|
| 81 |
-
# Examples
|
| 82 |
-
gr.Examples(
|
| 83 |
-
examples=[["images/" + f] for f in [
|
| 84 |
-
"allocebus_000003.jpeg",
|
| 85 |
-
"tarsius_000120.jpeg",
|
| 86 |
-
"nasalis_proboscis-monkey.png",
|
| 87 |
-
"macaca_000032.jpeg",
|
| 88 |
-
"mandrillus_000011.jpeg",
|
| 89 |
-
"pongo_000006.jpeg"
|
| 90 |
-
]],
|
| 91 |
-
inputs=input_file
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
submit_btn = gr.Button("Detect Faces", variant="primary")
|
| 95 |
-
|
| 96 |
-
# Controls
|
| 97 |
-
model_choice = gr.Radio(["MMDetection"], value="MMDetection", visible=False)
|
| 98 |
-
task_type = gr.Dropdown(
|
| 99 |
-
["Face Detection", "Face Pose Estimation", "Gaze Estimation [experimental]"],
|
| 100 |
-
value="Face Detection"
|
| 101 |
-
)
|
| 102 |
-
conf_threshold = gr.Slider(0.05, 0.95, 0.25, step=0.05, label="Confidence")
|
| 103 |
-
max_detections = gr.Slider(1, 10, 3, step=1, label="Max Detections")
|
| 104 |
-
|
| 105 |
-
# Simple local preview for uploaded files
|
| 106 |
-
def show_preview(file):
|
| 107 |
-
if file and file.name.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp')):
|
| 108 |
-
return gr.update(value=file, visible=True)
|
| 109 |
-
return gr.update(visible=False)
|
| 110 |
-
|
| 111 |
-
input_file.change(show_preview, inputs=[input_file], outputs=[preview_img])
|
| 112 |
-
|
| 113 |
-
# Main processing - only use last 3 outputs (skip raw previews)
|
| 114 |
-
def process_and_extract_outputs(*args):
|
| 115 |
-
result = process_media(*args)
|
| 116 |
-
# Return only processed outputs
|
| 117 |
-
return result[-2:] # Just processed image and video
|
| 118 |
-
|
| 119 |
-
submit_btn.click(
|
| 120 |
-
process_and_extract_outputs,
|
| 121 |
-
inputs=[input_file, input_webcam, model_choice, conf_threshold, max_detections, task_type],
|
| 122 |
-
outputs=[output_image, output_video]
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
# Simple clear
|
| 126 |
-
clear_btn.click(
|
| 127 |
-
lambda: [gr.update(value=None), gr.update(value=None), gr.update(visible=False),
|
| 128 |
-
gr.update(visible=False), gr.update(visible=False)],
|
| 129 |
-
outputs=[input_file, input_webcam, preview_img, output_image, output_video]
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|