Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,43 +4,74 @@ import os
|
|
4 |
|
5 |
BACKEND_URL = os.environ.get("BACKEND_URL", "").strip()
|
6 |
|
7 |
-
# Create persistent client
|
8 |
try:
|
9 |
client = Client(BACKEND_URL, headers={"ngrok-skip-browser-warning": "true"})
|
10 |
backend_available = True
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
client = None
|
13 |
backend_available = False
|
|
|
14 |
|
15 |
-
def
|
16 |
-
"""
|
17 |
if not client:
|
18 |
-
return [
|
|
|
19 |
try:
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
except Exception as e:
|
22 |
-
print(f"Error
|
23 |
-
|
24 |
-
|
25 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
def process_media(file_obj, webcam_img, model_type, conf_thresh, max_dets, task_type):
|
33 |
-
return forward_to_backend("/process_media", file_obj, webcam_img, model_type, conf_thresh, max_dets, task_type)
|
34 |
|
35 |
-
def
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
def
|
39 |
-
"""
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
# Build the interface
|
46 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
@@ -49,78 +80,93 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
49 |
if not backend_available:
|
50 |
gr.Markdown("### 🔴 GPU Server Offline - Please check back later")
|
51 |
else:
|
52 |
-
gr.Markdown("Upload an image/video or use your webcam.
|
53 |
-
gr.Markdown("Click 'Detect Faces' for results.")
|
54 |
|
55 |
with gr.Row():
|
56 |
with gr.Column(scale=1):
|
57 |
with gr.Tabs():
|
58 |
with gr.TabItem("Upload File"):
|
59 |
-
input_file = gr.File(label="Upload Image or Video Here", file_types=["image", "
|
60 |
-
display_raw_image_file = gr.Image(label="Raw Image Preview", type="
|
61 |
-
display_raw_video_file = gr.Video(label="Raw Video Preview",
|
62 |
|
63 |
with gr.TabItem("Webcam"):
|
64 |
-
gr.
|
65 |
-
|
66 |
-
display_raw_image_webcam = gr.Image(label="Captured Snapshot Preview", type="pil", interactive=False, visible=False)
|
67 |
|
68 |
clear_all_button = gr.Button("Clear All Inputs & Outputs")
|
69 |
|
70 |
with gr.Column(scale=1):
|
71 |
gr.Markdown("### Processed Output")
|
72 |
-
display_processed_image = gr.Image(label="Processed Image", type="pil",
|
73 |
-
display_processed_video = gr.Video(label="Processed Video",
|
74 |
-
|
75 |
-
# Example images - host them on HF Space
|
76 |
-
example_paths = [
|
77 |
-
"images/allocebus_000003.jpeg",
|
78 |
-
"images/tarsius_000120.jpeg",
|
79 |
-
"images/nasalis_proboscis-monkey.png",
|
80 |
-
"images/macaca_000032.jpeg",
|
81 |
-
"images/mandrillus_000011.jpeg",
|
82 |
-
"images/pongo_000006.jpeg"
|
83 |
-
]
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
)
|
91 |
|
92 |
submit_button = gr.Button("Detect Faces", variant="primary", scale=2)
|
93 |
|
94 |
with gr.Column():
|
95 |
gr.Markdown("### Detection Controls")
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
value="Face Detection",
|
100 |
label="Select Task"
|
101 |
)
|
102 |
-
|
103 |
-
|
104 |
|
105 |
-
#
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
# Wire events
|
112 |
-
input_file.change(handle_file_upload_preview, inputs=[input_file], outputs=file_preview_outputs)
|
113 |
-
input_webcam.change(handle_webcam_capture, inputs=[input_webcam], outputs=webcam_outputs)
|
114 |
|
115 |
-
#
|
116 |
-
|
|
|
|
|
|
|
|
|
117 |
|
|
|
118 |
submit_button.click(
|
119 |
process_media,
|
120 |
-
inputs=[input_file, display_raw_image_webcam,
|
121 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
)
|
123 |
|
124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
demo.launch()
|
|
|
4 |
|
5 |
BACKEND_URL = os.environ.get("BACKEND_URL", "").strip()
|
6 |
|
7 |
+
# Create persistent client and check available endpoints
|
8 |
try:
|
9 |
client = Client(BACKEND_URL, headers={"ngrok-skip-browser-warning": "true"})
|
10 |
backend_available = True
|
11 |
+
|
12 |
+
# Debug: Check what endpoints are available
|
13 |
+
print("Available endpoints:")
|
14 |
+
for endpoint in client.endpoints:
|
15 |
+
print(f" - {endpoint}")
|
16 |
+
|
17 |
+
except Exception as e:
|
18 |
client = None
|
19 |
backend_available = False
|
20 |
+
print(f"Backend not available: {e}")
|
21 |
|
22 |
+
def process_media(file_obj, webcam_img, model_type, conf_thresh, max_dets, task_type):
|
23 |
+
"""Main processing function"""
|
24 |
if not client:
|
25 |
+
return [None] * 5
|
26 |
+
|
27 |
try:
|
28 |
+
# The main endpoint is likely at index 0 or 1
|
29 |
+
# Try to find the process_media endpoint
|
30 |
+
result = client.predict(
|
31 |
+
file_obj,
|
32 |
+
webcam_img,
|
33 |
+
model_type,
|
34 |
+
conf_thresh,
|
35 |
+
max_dets,
|
36 |
+
task_type,
|
37 |
+
fn_index=1 # Usually the main function is at index 1
|
38 |
+
)
|
39 |
+
return result
|
40 |
except Exception as e:
|
41 |
+
print(f"Error in process_media: {e}")
|
42 |
+
# Return empty updates for all outputs
|
43 |
+
return [
|
44 |
+
gr.update(visible=False), # display_raw_image_file
|
45 |
+
gr.update(visible=False), # display_raw_video_file
|
46 |
+
gr.update(visible=False), # display_raw_image_webcam
|
47 |
+
gr.update(visible=False), # display_processed_image
|
48 |
+
gr.update(visible=False) # display_processed_video
|
49 |
+
]
|
|
|
|
|
|
|
50 |
|
51 |
+
def handle_file_preview(file_obj):
|
52 |
+
"""Handle file upload and show preview"""
|
53 |
+
if file_obj is None:
|
54 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
|
55 |
+
|
56 |
+
# Check if it's an image or video
|
57 |
+
if file_obj.name.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.webp')):
|
58 |
+
return gr.update(value=file_obj.name, visible=True), gr.update(visible=False), gr.update(visible=False)
|
59 |
+
elif file_obj.name.lower().endswith(('.mp4', '.avi', '.mov', '.mkv', '.webm')):
|
60 |
+
return gr.update(visible=False), gr.update(value=file_obj.name, visible=True), gr.update(visible=False)
|
61 |
+
else:
|
62 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
|
63 |
|
64 |
+
def clear_all():
|
65 |
+
"""Clear all inputs and outputs"""
|
66 |
+
return [
|
67 |
+
gr.update(value=None), # input_file
|
68 |
+
gr.update(value=None), # input_webcam
|
69 |
+
gr.update(visible=False), # display_raw_image_file
|
70 |
+
gr.update(visible=False), # display_raw_video_file
|
71 |
+
gr.update(visible=False), # display_raw_image_webcam
|
72 |
+
gr.update(visible=False), # display_processed_image
|
73 |
+
gr.update(visible=False) # display_processed_video
|
74 |
+
]
|
75 |
|
76 |
# Build the interface
|
77 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
80 |
if not backend_available:
|
81 |
gr.Markdown("### 🔴 GPU Server Offline - Please check back later")
|
82 |
else:
|
83 |
+
gr.Markdown("Upload an image/video or use your webcam. Click 'Detect Faces' for results.")
|
|
|
84 |
|
85 |
with gr.Row():
|
86 |
with gr.Column(scale=1):
|
87 |
with gr.Tabs():
|
88 |
with gr.TabItem("Upload File"):
|
89 |
+
input_file = gr.File(label="Upload Image or Video Here", file_types=["image", "video"])
|
90 |
+
display_raw_image_file = gr.Image(label="Raw Image Preview", type="filepath", visible=False)
|
91 |
+
display_raw_video_file = gr.Video(label="Raw Video Preview", visible=False)
|
92 |
|
93 |
with gr.TabItem("Webcam"):
|
94 |
+
input_webcam = gr.Image(sources=["webcam"], type="pil", label="Click to capture")
|
95 |
+
display_raw_image_webcam = gr.Image(label="Captured", type="pil", visible=False)
|
|
|
96 |
|
97 |
clear_all_button = gr.Button("Clear All Inputs & Outputs")
|
98 |
|
99 |
with gr.Column(scale=1):
|
100 |
gr.Markdown("### Processed Output")
|
101 |
+
display_processed_image = gr.Image(label="Processed Image", type="pil", visible=False)
|
102 |
+
display_processed_video = gr.Video(label="Processed Video", visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
+
# Examples
|
105 |
+
gr.Examples(
|
106 |
+
examples=[
|
107 |
+
["images/allocebus_000003.jpeg"],
|
108 |
+
["images/tarsius_000120.jpeg"],
|
109 |
+
["images/nasalis_proboscis-monkey.png"],
|
110 |
+
["images/macaca_000032.jpeg"],
|
111 |
+
["images/mandrillus_000011.jpeg"],
|
112 |
+
["images/pongo_000006.jpeg"]
|
113 |
+
],
|
114 |
+
inputs=input_file,
|
115 |
+
label="Example Images"
|
116 |
)
|
117 |
|
118 |
submit_button = gr.Button("Detect Faces", variant="primary", scale=2)
|
119 |
|
120 |
with gr.Column():
|
121 |
gr.Markdown("### Detection Controls")
|
122 |
+
model_choice = gr.Radio(["MMDetection"], value="MMDetection", visible=False)
|
123 |
+
task_type = gr.Dropdown(
|
124 |
+
["Face Detection", "Face Pose Estimation", "Gaze Estimation [experimental]"],
|
125 |
value="Face Detection",
|
126 |
label="Select Task"
|
127 |
)
|
128 |
+
conf_threshold = gr.Slider(0.05, 0.95, 0.25, step=0.05, label="Confidence Threshold")
|
129 |
+
max_detections = gr.Slider(1, 10, 3, step=1, label="Max Detections")
|
130 |
|
131 |
+
# Simple file preview
|
132 |
+
input_file.change(
|
133 |
+
handle_file_preview,
|
134 |
+
inputs=[input_file],
|
135 |
+
outputs=[display_raw_image_file, display_raw_video_file, input_file]
|
136 |
+
)
|
|
|
|
|
|
|
137 |
|
138 |
+
# Webcam capture
|
139 |
+
input_webcam.change(
|
140 |
+
lambda img: gr.update(value=img, visible=True) if img else gr.update(visible=False),
|
141 |
+
inputs=[input_webcam],
|
142 |
+
outputs=[display_raw_image_webcam]
|
143 |
+
)
|
144 |
|
145 |
+
# Process button
|
146 |
submit_button.click(
|
147 |
process_media,
|
148 |
+
inputs=[input_file, display_raw_image_webcam, model_choice, conf_threshold, max_detections, task_type],
|
149 |
+
outputs=[
|
150 |
+
display_raw_image_file,
|
151 |
+
display_raw_video_file,
|
152 |
+
display_raw_image_webcam,
|
153 |
+
display_processed_image,
|
154 |
+
display_processed_video
|
155 |
+
]
|
156 |
)
|
157 |
|
158 |
+
# Clear button
|
159 |
+
clear_all_button.click(
|
160 |
+
clear_all,
|
161 |
+
outputs=[
|
162 |
+
input_file,
|
163 |
+
input_webcam,
|
164 |
+
display_raw_image_file,
|
165 |
+
display_raw_video_file,
|
166 |
+
display_raw_image_webcam,
|
167 |
+
display_processed_image,
|
168 |
+
display_processed_video
|
169 |
+
]
|
170 |
+
)
|
171 |
|
172 |
demo.launch()
|