fparodi commited on
Commit
907be10
·
verified ·
1 Parent(s): 342ff64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -23
app.py CHANGED
@@ -1,31 +1,76 @@
1
  import gradio as gr
 
2
  import os
3
 
4
  BACKEND_URL = os.environ.get("BACKEND_URL", "").strip()
5
 
6
- with gr.Blocks(theme=gr.themes.Soft(), title="PrimateFace Demo") as demo:
7
- if BACKEND_URL:
8
- gr.HTML(f'''
9
- <div style="padding: 20px;">
10
- <h1 style="text-align: center;">🐵 PrimateFace Detection, Pose & Gaze Demo</h1>
11
- <p style="text-align: center; color: green;">✅ GPU Server Connected</p>
12
- </div>
13
- <iframe
14
- src="{BACKEND_URL}"
15
- width="100%"
16
- height="1000px"
17
- frameborder="0"
18
- style="border: 1px solid #ccc; border-radius: 8px;">
19
- </iframe>
20
- ''')
21
- else:
22
- gr.Markdown("""
23
- # 🐵 PrimateFace Demo
24
-
25
- ### 🔴 Configuration Error
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- The BACKEND_URL environment variable is not set.
28
- Please configure it in the Space settings under "Repository secrets".
29
- """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  demo.launch()
 
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
+ 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()