fparodi commited on
Commit
5e5ba50
·
verified ·
1 Parent(s): 907be10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -63
app.py CHANGED
@@ -4,73 +4,30 @@ import os
4
 
5
  BACKEND_URL = os.environ.get("BACKEND_URL", "").strip()
6
 
7
- def process_with_backend(file_obj, webcam_img, model_type, conf_thresh, max_dets, task_type):
8
- """Forward request to backend"""
9
  try:
10
- client = Client(BACKEND_URL, headers={"ngrok-skip-browser-warning": "true"})
11
- result = client.predict(
12
- file_obj,
13
- webcam_img,
14
- model_type,
15
- conf_thresh,
16
- max_dets,
17
- task_type,
18
- api_name="/predict"
19
  )
20
- return result
 
21
  except Exception as e:
22
- return [gr.update() for _ in range(5)] + [f"Error: {str(e)}"]
23
-
24
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
25
- gr.Markdown("# 🐵 PrimateFace Detection, Pose Estimation, and Gaze Demo")
26
-
27
- with gr.Row():
28
- with gr.Column(scale=1):
29
- with gr.Tabs():
30
- with gr.TabItem("Upload File"):
31
- input_file = gr.File(label="Upload Image or Video", file_types=["image", "video"])
32
- display_raw_image_file = gr.Image(visible=False)
33
- display_raw_video_file = gr.Video(visible=False)
34
-
35
- with gr.TabItem("Webcam"):
36
- gr.Markdown("Click on feed or press Enter to capture")
37
- input_webcam = gr.Image(sources=["webcam"], type="pil")
38
- display_raw_image_webcam = gr.Image(visible=False)
39
-
40
- clear_button = gr.Button("Clear All Inputs & Outputs")
41
-
42
- with gr.Column(scale=1):
43
- gr.Markdown("### Processed Output")
44
- display_processed_image = gr.Image(visible=False)
45
- display_processed_video = gr.Video(visible=False)
46
-
47
- submit_button = gr.Button("Detect Faces", variant="primary")
48
-
49
- with gr.Column():
50
- gr.Markdown("### Detection Controls")
51
- model_choice = gr.Radio(["MMDetection"], value="MMDetection", visible=False)
52
- task_type = gr.Dropdown(
53
- ["Face Detection", "Face Pose Estimation", "Gaze Estimation [experimental]"],
54
- value="Face Detection",
55
- label="Select Task"
56
  )
57
- conf_threshold = gr.Slider(0.05, 0.95, 0.25, step=0.05, label="Confidence Threshold")
58
- max_detections = gr.Slider(1, 10, 3, step=1, label="Max Detections")
59
-
60
- # Wire events
61
- outputs = [display_raw_image_file, display_raw_video_file, display_raw_image_webcam,
62
- display_processed_image, display_processed_video]
63
-
64
- submit_button.click(
65
- process_with_backend,
66
- inputs=[input_file, input_webcam, model_choice, conf_threshold, max_detections, task_type],
67
- outputs=outputs
68
- )
69
-
70
- # Simplified clear function
71
- clear_button.click(
72
- lambda: [gr.update(value=None) for _ in range(5)],
73
- outputs=outputs
74
  )
75
 
76
  demo.launch()
 
4
 
5
  BACKEND_URL = os.environ.get("BACKEND_URL", "").strip()
6
 
7
+ if BACKEND_URL:
 
8
  try:
9
+ # Load the entire interface from backend
10
+ demo = gr.Interface.load(
11
+ name=BACKEND_URL,
12
+ title="PrimateFace Detection, Pose & Gaze Demo",
13
+ description="Connected to GPU backend server"
 
 
 
 
14
  )
15
+ # Add header to bypass ngrok warning
16
+ demo.headers = {"ngrok-skip-browser-warning": "true"}
17
  except Exception as e:
18
+ # Fallback if backend is down
19
+ demo = gr.Interface(
20
+ fn=lambda x: "GPU server is offline. Please try again later.",
21
+ inputs=gr.Textbox(),
22
+ outputs=gr.Textbox(),
23
+ title="PrimateFace Demo - Offline"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  )
25
+ else:
26
+ demo = gr.Interface(
27
+ fn=lambda x: "Backend URL not configured in Space settings.",
28
+ inputs=gr.Textbox(),
29
+ outputs=gr.Textbox(),
30
+ title="PrimateFace Demo - Not Configured"
 
 
 
 
 
 
 
 
 
 
 
31
  )
32
 
33
  demo.launch()