fparodi commited on
Commit
9946ae7
·
verified ·
1 Parent(s): f499593

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -70
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
- except:
 
 
 
 
 
 
12
  client = None
13
  backend_available = False
 
14
 
15
- def forward_to_backend(fn_name, *args):
16
- """Generic function to forward any call to backend"""
17
  if not client:
18
- return [gr.update() for _ in range(7)]
 
19
  try:
20
- return client.predict(*args, api_name=fn_name)
 
 
 
 
 
 
 
 
 
 
 
21
  except Exception as e:
22
- print(f"Error calling {fn_name}: {e}")
23
- return [gr.update() for _ in range(7)]
24
-
25
- # Wrapper functions
26
- def handle_file_upload_preview(file_obj):
27
- return forward_to_backend("/handle_file_upload_preview", file_obj)
28
-
29
- def handle_webcam_capture(snapshot):
30
- return forward_to_backend("/handle_webcam_capture", snapshot)
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 clear_all_media_and_outputs():
36
- return forward_to_backend("/clear_all_media_and_outputs")
 
 
 
 
 
 
 
 
 
 
37
 
38
- def handle_example_select(evt: gr.SelectData):
39
- """Handle example selection locally and update input_file"""
40
- # Extract the image path from the dataset
41
- if isinstance(evt.value, dict) and 'image' in evt.value:
42
- return evt.value['image']
43
- return None
 
 
 
 
 
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. For webcam, press 'Enter' to take a snapshot.")
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", ".mp4", ".avi", ".mov", ".mkv", ".webm", ".gif"])
60
- display_raw_image_file = gr.Image(label="Raw Image Preview", type="pil", interactive=False, visible=False)
61
- display_raw_video_file = gr.Video(label="Raw Video Preview", interactive=False, visible=False)
62
 
63
  with gr.TabItem("Webcam"):
64
- gr.Markdown("**Using the Webcam:** Click on feed or press Enter to capture")
65
- input_webcam = gr.Image(sources=["webcam"], type="pil", label="Live Webcam")
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", interactive=False, visible=False)
73
- display_processed_video = gr.Video(label="Processed Video", interactive=False, visible=False)
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
- example_dataset = gr.Dataset(
86
- components=["image"],
87
- samples=[[path] for path in example_paths],
88
- label="Example Images (Click to use)",
89
- samples_per_page=6
 
 
 
 
 
 
 
90
  )
91
 
92
  submit_button = gr.Button("Detect Faces", variant="primary", scale=2)
93
 
94
  with gr.Column():
95
  gr.Markdown("### Detection Controls")
96
- model_choice_radio = gr.Radio(choices=["MMDetection"], value="MMDetection", label="Inferencer", visible=False)
97
- task_type_dropdown = gr.Dropdown(
98
- choices=["Face Detection", "Face Pose Estimation", "Gaze Estimation [experimental]"],
99
  value="Face Detection",
100
  label="Select Task"
101
  )
102
- conf_slider = gr.Slider(minimum=0.05, maximum=0.95, value=0.25, step=0.05, label="Confidence Threshold")
103
- max_det_slider = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Max Detections")
104
 
105
- # Define outputs
106
- file_preview_outputs = [display_raw_image_file, display_raw_video_file, input_file, display_processed_image, display_processed_video]
107
- webcam_outputs = [display_raw_image_webcam, input_webcam, display_processed_image, display_processed_video]
108
- process_outputs = [display_raw_image_file, display_raw_video_file, display_raw_image_webcam, display_processed_image, display_processed_video]
109
- clear_outputs = [input_file, input_webcam, display_raw_image_file, display_raw_video_file, display_raw_image_webcam, display_processed_image, display_processed_video]
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
- # Handle example selection
116
- example_dataset.select(handle_example_select, outputs=[input_file])
 
 
 
 
117
 
 
118
  submit_button.click(
119
  process_media,
120
- inputs=[input_file, display_raw_image_webcam, model_choice_radio, conf_slider, max_det_slider, task_type_dropdown],
121
- outputs=process_outputs
 
 
 
 
 
 
122
  )
123
 
124
- clear_all_button.click(clear_all_media_and_outputs, outputs=clear_outputs)
 
 
 
 
 
 
 
 
 
 
 
 
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()