Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,177 @@
|
|
1 |
import gradio as gr
|
2 |
from gradio_client import Client
|
3 |
import os
|
|
|
4 |
|
5 |
BACKEND_URL = os.environ.get("BACKEND_URL", "").strip()
|
6 |
|
7 |
-
#
|
8 |
try:
|
9 |
client = Client(BACKEND_URL, headers={"ngrok-skip-browser-warning": "true"})
|
10 |
backend_available = True
|
11 |
-
|
12 |
-
# Print detailed API info
|
13 |
-
print("\n=== BACKEND API INFO ===")
|
14 |
-
api_info = client.view_api(all_endpoints=True)
|
15 |
-
print(api_info)
|
16 |
-
print("========================\n")
|
17 |
-
|
18 |
except Exception as e:
|
19 |
client = None
|
20 |
backend_available = False
|
21 |
-
print(f"Backend error: {e}")
|
22 |
|
23 |
-
def
|
24 |
-
"""
|
|
|
|
|
|
|
25 |
try:
|
26 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
result = client.predict(
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
)
|
36 |
-
|
|
|
|
|
|
|
|
|
37 |
return result
|
|
|
38 |
except Exception as e:
|
39 |
-
print(f"
|
40 |
-
return
|
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 |
else:
|
71 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
demo.launch()
|
|
|
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 |
+
# Create persistent client
|
9 |
try:
|
10 |
client = Client(BACKEND_URL, headers={"ngrok-skip-browser-warning": "true"})
|
11 |
backend_available = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
except Exception as e:
|
13 |
client = None
|
14 |
backend_available = False
|
|
|
15 |
|
16 |
+
def process_media(file_obj, webcam_img, model_type, conf_thresh, max_dets, task_type):
|
17 |
+
"""Process media using backend API"""
|
18 |
+
if not client:
|
19 |
+
return [gr.update()] * 5
|
20 |
+
|
21 |
try:
|
22 |
+
# Convert webcam PIL to file path
|
23 |
+
webcam_path = None
|
24 |
+
if webcam_img is not None:
|
25 |
+
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp:
|
26 |
+
webcam_img.save(tmp, 'PNG')
|
27 |
+
webcam_path = tmp.name
|
28 |
+
|
29 |
+
# Call backend with proper api_name
|
30 |
result = client.predict(
|
31 |
+
uploaded_file_obj=file_obj,
|
32 |
+
webcam_image_pil=webcam_path,
|
33 |
+
model_type_choice=model_type,
|
34 |
+
conf_threshold_ui=conf_thresh,
|
35 |
+
max_detections_ui=max_dets,
|
36 |
+
task_type=task_type,
|
37 |
+
api_name="/process_media"
|
38 |
)
|
39 |
+
|
40 |
+
# Cleanup temp file
|
41 |
+
if webcam_path and os.path.exists(webcam_path):
|
42 |
+
os.unlink(webcam_path)
|
43 |
+
|
44 |
return result
|
45 |
+
|
46 |
except Exception as e:
|
47 |
+
print(f"Error: {e}")
|
48 |
+
return [gr.update()] * 5
|
49 |
|
50 |
+
def handle_file_preview(file_obj):
|
51 |
+
"""Handle file upload preview"""
|
52 |
+
if not client or not file_obj:
|
53 |
+
return [gr.update()] * 5
|
54 |
|
55 |
+
try:
|
56 |
+
return client.predict(
|
57 |
+
file_obj=file_obj,
|
58 |
+
api_name="/handle_file_upload_preview"
|
59 |
+
)
|
60 |
+
except:
|
61 |
+
return [gr.update()] * 5
|
62 |
+
|
63 |
+
def handle_webcam_capture(webcam_img):
|
64 |
+
"""Handle webcam capture"""
|
65 |
+
if not client or not webcam_img:
|
66 |
+
return [gr.update()] * 4
|
67 |
+
|
68 |
+
try:
|
69 |
+
# Save PIL to temp file
|
70 |
+
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp:
|
71 |
+
webcam_img.save(tmp, 'PNG')
|
72 |
+
temp_path = tmp.name
|
73 |
|
74 |
+
result = client.predict(
|
75 |
+
snapshot_from_feed=temp_path,
|
76 |
+
api_name="/handle_webcam_capture"
|
77 |
+
)
|
78 |
|
79 |
+
# Cleanup
|
80 |
+
if os.path.exists(temp_path):
|
81 |
+
os.unlink(temp_path)
|
82 |
+
|
83 |
+
return result
|
84 |
+
except:
|
85 |
+
return [gr.update()] * 4
|
86 |
+
|
87 |
+
def clear_all():
|
88 |
+
"""Clear all inputs and outputs"""
|
89 |
+
if not client:
|
90 |
+
return [gr.update(value=None)] * 7
|
91 |
+
|
92 |
+
try:
|
93 |
+
return client.predict(api_name="/clear_all_media_and_outputs")
|
94 |
+
except:
|
95 |
+
return [gr.update(value=None)] * 7
|
96 |
+
|
97 |
+
# Build interface
|
98 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
99 |
+
gr.Markdown("# 🐵 PrimateFace Detection, Pose Estimation, and Gaze Demo")
|
100 |
+
|
101 |
+
if not backend_available:
|
102 |
+
gr.Markdown("### 🔴 GPU Server Offline")
|
103 |
else:
|
104 |
+
with gr.Row():
|
105 |
+
with gr.Column(scale=1):
|
106 |
+
with gr.Tabs():
|
107 |
+
with gr.TabItem("Upload File"):
|
108 |
+
input_file = gr.File(label="Upload Image or Video Here", file_types=["image", "video"])
|
109 |
+
display_raw_image_file = gr.Image(label="Raw Image Preview", visible=False)
|
110 |
+
display_raw_video_file = gr.Video(label="Raw Video Preview", visible=False)
|
111 |
+
|
112 |
+
with gr.TabItem("Webcam"):
|
113 |
+
gr.Markdown("Click on feed or press Enter to capture")
|
114 |
+
input_webcam = gr.Image(sources=["webcam"], type="pil")
|
115 |
+
display_raw_image_webcam = gr.Image(label="Captured Snapshot Preview", visible=False)
|
116 |
+
|
117 |
+
clear_button = gr.Button("Clear All Inputs & Outputs")
|
118 |
+
|
119 |
+
with gr.Column(scale=1):
|
120 |
+
gr.Markdown("### Processed Output")
|
121 |
+
display_processed_image = gr.Image(label="Processed Image", visible=False)
|
122 |
+
display_processed_video = gr.Video(label="Processed Video", visible=False)
|
123 |
+
|
124 |
+
# Examples
|
125 |
+
gr.Examples(
|
126 |
+
examples=[
|
127 |
+
["images/allocebus_000003.jpeg"],
|
128 |
+
["images/tarsius_000120.jpeg"],
|
129 |
+
["images/nasalis_proboscis-monkey.png"],
|
130 |
+
["images/macaca_000032.jpeg"],
|
131 |
+
["images/mandrillus_000011.jpeg"],
|
132 |
+
["images/pongo_000006.jpeg"]
|
133 |
+
],
|
134 |
+
inputs=input_file
|
135 |
+
)
|
136 |
+
|
137 |
+
submit_button = gr.Button("Detect Faces", variant="primary")
|
138 |
+
|
139 |
+
# Controls
|
140 |
+
model_choice = gr.Radio(["MMDetection"], value="MMDetection", visible=False)
|
141 |
+
task_type = gr.Dropdown(
|
142 |
+
["Face Detection", "Face Pose Estimation", "Gaze Estimation [experimental]"],
|
143 |
+
value="Face Detection",
|
144 |
+
label="Select Task"
|
145 |
+
)
|
146 |
+
conf_threshold = gr.Slider(0.05, 0.95, 0.25, step=0.05, label="Confidence Threshold")
|
147 |
+
max_detections = gr.Slider(1, 10, 3, step=1, label="Max Detections")
|
148 |
+
|
149 |
+
# Wire events
|
150 |
+
input_file.change(
|
151 |
+
handle_file_preview,
|
152 |
+
inputs=[input_file],
|
153 |
+
outputs=[display_raw_image_file, display_raw_video_file, input_file,
|
154 |
+
display_processed_image, display_processed_video]
|
155 |
+
)
|
156 |
+
|
157 |
+
input_webcam.change(
|
158 |
+
handle_webcam_capture,
|
159 |
+
inputs=[input_webcam],
|
160 |
+
outputs=[display_raw_image_webcam, input_webcam,
|
161 |
+
display_processed_image, display_processed_video]
|
162 |
+
)
|
163 |
+
|
164 |
+
submit_button.click(
|
165 |
+
process_media,
|
166 |
+
inputs=[input_file, input_webcam, model_choice, conf_threshold, max_detections, task_type],
|
167 |
+
outputs=[display_raw_image_file, display_raw_video_file, display_raw_image_webcam,
|
168 |
+
display_processed_image, display_processed_video]
|
169 |
+
)
|
170 |
+
|
171 |
+
clear_button.click(
|
172 |
+
clear_all,
|
173 |
+
outputs=[input_file, input_webcam, display_raw_image_file, display_raw_video_file,
|
174 |
+
display_raw_image_webcam, display_processed_image, display_processed_video]
|
175 |
+
)
|
176 |
|
177 |
demo.launch()
|