fparodi commited on
Commit
dab883e
·
verified ·
1 Parent(s): 687530f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -122
app.py CHANGED
@@ -1,147 +1,73 @@
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
- print(f"Connected to backend at {BACKEND_URL}")
 
 
 
 
 
 
13
  except Exception as e:
14
  client = None
15
  backend_available = False
16
- print(f"Backend not available: {e}")
17
 
18
- def process_media(file_obj, webcam_img, model_type, conf_thresh, max_dets, task_type):
19
- """Main processing function - expects 5 outputs"""
20
- if not client:
21
- return [gr.update()] * 5
22
-
23
  try:
24
- # Handle webcam image - need to pass as file path
25
- webcam_file = None
26
- if webcam_img is not None:
27
- with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp:
28
- webcam_img.save(tmp, 'PNG')
29
- webcam_file = tmp.name
30
-
31
- # Call backend
32
  result = client.predict(
33
- file_obj,
34
- webcam_file,
35
- model_type,
36
- conf_thresh,
37
- max_dets,
38
- task_type,
39
  fn_index=3
40
  )
41
-
42
- # Clean up
43
- if webcam_file and os.path.exists(webcam_file):
44
- os.unlink(webcam_file)
45
-
46
- # Backend returns 7 values but we only need the last 5
47
- # Skip the first 2 (input_file and input_webcam updates)
48
- if len(result) == 7:
49
- return result[2:] # Return only the display components
50
- else:
51
- return result[:5] # Safety fallback
52
-
53
  except Exception as e:
54
- print(f"Error in process_media: {e}")
55
- return [gr.update()] * 5
56
 
57
- # Build simplified interface
58
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
59
- gr.Markdown("# 🐵 PrimateFace Detection, Pose Estimation, and Gaze Demo")
60
 
61
- if not backend_available:
62
- gr.Markdown("### 🔴 GPU Server Offline")
63
- else:
64
- with gr.Row():
65
- with gr.Column():
66
- with gr.Tabs():
67
- with gr.TabItem("Upload"):
68
- input_file = gr.File(label="Upload Image/Video", file_types=["image", "video"])
69
- display_raw_image_file = gr.Image(visible=False)
70
- display_raw_video_file = gr.Video(visible=False)
71
-
72
- with gr.TabItem("Webcam"):
73
- input_webcam = gr.Image(sources=["webcam"], type="pil")
74
- display_raw_image_webcam = gr.Image(visible=False)
75
-
76
- clear_button = gr.Button("Clear All")
77
-
78
- with gr.Column():
79
- gr.Markdown("### Output")
80
- display_processed_image = gr.Image(visible=False)
81
- display_processed_video = gr.Video(visible=False)
82
-
83
- # Examples
84
- gr.Examples(
85
- examples=[
86
- ["images/allocebus_000003.jpeg"],
87
- ["images/tarsius_000120.jpeg"],
88
- ["images/nasalis_proboscis-monkey.png"],
89
- ["images/macaca_000032.jpeg"],
90
- ["images/mandrillus_000011.jpeg"],
91
- ["images/pongo_000006.jpeg"]
92
- ],
93
- inputs=input_file
94
- )
95
 
96
- submit_button = gr.Button("Detect Faces", variant="primary")
 
 
97
 
98
- # Controls
99
- model_choice = gr.Radio(["MMDetection"], value="MMDetection", visible=False)
100
- task_type = gr.Dropdown(
101
- ["Face Detection", "Face Pose Estimation", "Gaze Estimation [experimental]"],
102
- value="Face Detection"
103
- )
104
- conf_threshold = gr.Slider(0.05, 0.95, 0.25, step=0.05, label="Confidence")
105
- max_detections = gr.Slider(1, 10, 3, step=1, label="Max Detections")
106
-
107
- # Simple preview handlers
108
- input_file.change(
109
- lambda f: (gr.update(value=f, visible=bool(f)), gr.update(visible=False)) if f and f.name.endswith(('.jpg','.jpeg','.png')) else (gr.update(visible=False), gr.update(value=f, visible=bool(f))),
110
- inputs=[input_file],
111
- outputs=[display_raw_image_file, display_raw_video_file]
112
- )
113
-
114
- input_webcam.change(
115
- lambda img: gr.update(value=img, visible=bool(img)),
116
- inputs=[input_webcam],
117
- outputs=[display_raw_image_webcam]
118
- )
119
 
120
- # Main processing
121
- submit_button.click(
122
- process_media,
123
- inputs=[input_file, input_webcam, model_choice, conf_threshold, max_detections, task_type],
124
- outputs=[
125
- display_raw_image_file,
126
- display_raw_video_file,
127
- display_raw_image_webcam,
128
- display_processed_image,
129
- display_processed_video
130
- ]
131
- )
132
-
133
- # Clear all
134
- clear_button.click(
135
- lambda: [gr.update(value=None)] * 7,
136
- outputs=[
137
- input_file,
138
- input_webcam,
139
- display_raw_image_file,
140
- display_raw_video_file,
141
- display_raw_image_webcam,
142
- display_processed_image,
143
- display_processed_video
144
- ]
145
- )
146
 
147
  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
+ # Debug the backend API
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 test_process():
24
+ """Test function to debug"""
 
 
 
25
  try:
26
+ # Try calling with minimal inputs
 
 
 
 
 
 
 
27
  result = client.predict(
28
+ None, # file
29
+ None, # webcam
30
+ "MMDetection", # model
31
+ 0.25, # conf
32
+ 3, # max det
33
+ "Face Detection", # task
34
  fn_index=3
35
  )
36
+ print(f"Result: {result}")
37
+ return result
 
 
 
 
 
 
 
 
 
 
38
  except Exception as e:
39
+ print(f"Test error: {e}")
40
+ return str(e)
41
 
42
+ # Minimal test interface
43
+ with gr.Blocks() as demo:
44
+ gr.Markdown("# PrimateFace Demo - Debug Mode")
45
 
46
+ if backend_available:
47
+ # Show API info
48
+ with gr.Accordion("API Info", open=True):
49
+ gr.Code(str(api_info), language="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
+ # Simple test
52
+ test_btn = gr.Button("Test Backend Connection")
53
+ output = gr.Textbox(label="Response")
54
 
55
+ test_btn.click(test_process, outputs=output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
+ # Try using the client's automatic interface loading
58
+ gr.Markdown("---")
59
+ gr.Markdown("### Direct Backend Interface:")
60
+ try:
61
+ # Load the backend interface directly
62
+ backend_interface = gr.load(
63
+ name=BACKEND_URL,
64
+ src="spaces",
65
+ hf_token=None
66
+ )
67
+ backend_interface.render()
68
+ except Exception as e:
69
+ gr.Markdown(f"Could not load interface: {e}")
70
+ else:
71
+ gr.Markdown("### 🔴 Backend Offline")
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  demo.launch()