xiaoyuxi commited on
Commit
1365486
·
1 Parent(s): 0eb8b91
Files changed (1) hide show
  1. app.py +66 -41
app.py CHANGED
@@ -25,14 +25,14 @@ def initialize_backend():
25
  print(f"🔧 Backend API object type: {type(backend_api)}")
26
  print(f"🔧 Backend API object attributes: {dir(backend_api)}")
27
 
28
- # Check if it's an Interface object and has fn
29
- if hasattr(backend_api, 'fn'):
30
  print(f"✅ Backend connection successful!")
31
- print(" Backend API function is available")
32
  BACKEND_AVAILABLE = True
33
  return True
34
  else:
35
- print("❌ Backend API function not found")
36
  print(f"🔧 Available methods: {[attr for attr in dir(backend_api) if not attr.startswith('_')]}")
37
  BACKEND_AVAILABLE = False
38
  return False
@@ -96,15 +96,15 @@ def handle_video_upload(video):
96
  return None, None, [], 50, 756, 3
97
 
98
  try:
99
- if BACKEND_AVAILABLE and backend_api:
100
  # Try to use backend API
101
  try:
102
  print("🔧 Calling backend API for video upload...")
103
 
104
- # For gr.Interface, we can call the function directly
105
- if hasattr(backend_api, 'fn'):
106
- print("🔧 Using backend Interface function")
107
- result = backend_api.fn(video, [], 50, 756, 3)
108
 
109
  print(f"✅ Backend video upload API call successful!")
110
  print(f"🔧 Result type: {type(result)}")
@@ -120,7 +120,7 @@ def handle_video_upload(video):
120
  # Fallback to local processing
121
  pass
122
  else:
123
- print("Backend function not found")
124
  # Fallback to local processing
125
  pass
126
  except Exception as e:
@@ -153,19 +153,30 @@ def select_point(original_img: str, sel_pix: list, point_type: str, evt: gr.Sele
153
  return None, []
154
 
155
  try:
156
- if BACKEND_AVAILABLE and backend_api:
157
  # Try to use backend API
158
  try:
159
  print(f"🔧 Calling backend select point API: x={evt.index[0]}, y={evt.index[1]}, type={point_type}")
160
 
161
- # For gr.Interface, we can call the function directly
162
- if hasattr(backend_api, 'fn'):
163
- print("🔧 Using backend Interface function for point selection")
164
- # Note: This might not work as expected since the interface expects different inputs
165
- # For now, fallback to local processing
166
- pass
 
 
 
 
 
 
 
 
 
 
 
167
  else:
168
- print("Backend function not found")
169
  # Fallback to local processing
170
  pass
171
  except Exception as e:
@@ -228,19 +239,30 @@ def reset_points(original_img: str, sel_pix):
228
  return None, []
229
 
230
  try:
231
- if BACKEND_AVAILABLE and backend_api:
232
  # Try to use backend API
233
  try:
234
  print("🔧 Calling backend reset points API")
235
 
236
- # For gr.Interface, we can call the function directly
237
- if hasattr(backend_api, 'fn'):
238
- print("🔧 Using backend Interface function for reset")
239
- # Note: This might not work as expected since the interface expects different inputs
240
- # For now, fallback to local processing
241
- pass
 
 
 
 
 
 
 
 
 
 
 
242
  else:
243
- print("Backend function not found")
244
  # Fallback to local processing
245
  pass
246
  except Exception as e:
@@ -275,18 +297,18 @@ def launch_viz(grid_size, vo_points, fps, original_image_state):
275
  return None, None
276
 
277
  try:
278
- if BACKEND_AVAILABLE and backend_api:
279
  # Try to use backend API
280
  try:
281
  print(f"🔧 Calling backend API with parameters: grid_size={grid_size}, vo_points={vo_points}, fps={fps}")
282
  print(f"🔧 Original image state type: {type(original_image_state)}")
283
  print(f"🔧 Original image state preview: {str(original_image_state)[:100]}...")
284
 
285
- # For gr.Interface, we can call the function directly
286
- if hasattr(backend_api, 'fn'):
287
- print("🔧 Using backend Interface function")
288
  # For now, we'll use empty points since we're in local mode
289
- result = backend_api.fn(None, [], grid_size, vo_points, fps)
290
 
291
  print(f"✅ Backend API call successful!")
292
  print(f"🔧 Result type: {type(result)}")
@@ -302,7 +324,7 @@ def launch_viz(grid_size, vo_points, fps, original_image_state):
302
  # Fallback to error message
303
  pass
304
  else:
305
- print("Backend function not found")
306
  # Fallback to error message
307
  pass
308
  except Exception as e:
@@ -331,6 +353,7 @@ def launch_viz(grid_size, vo_points, fps, original_image_state):
331
  <p style='color: #666; font-size: 12px; margin: 0;'>Backend API Object: {backend_api is not None}</p>
332
  <p style='color: #666; font-size: 12px; margin: 0;'>Backend URL: {BACKEND_SPACE_URL}</p>
333
  <p style='color: #666; font-size: 12px; margin: 0;'>Backend Type: {type(backend_api) if backend_api else 'None'}</p>
 
334
  </div>
335
  <p style='color: #2d3436; font-weight: bold; margin-top: 15px;'>
336
  Current Status: Backend unavailable - Running in limited mode
@@ -370,12 +393,13 @@ def test_backend_connection():
370
  try:
371
  # Try a simple API call to test connection
372
  print("Testing backend connection with a simple call...")
373
- # Check if we have fn available
374
- if hasattr(backend_api, 'fn'):
375
- print("✅ Backend API function is available")
 
376
  return True
377
  else:
378
- print("❌ Backend API function not found")
379
  BACKEND_AVAILABLE = False
380
  return False
381
  except Exception as e:
@@ -392,12 +416,13 @@ def test_backend_api():
392
  try:
393
  print("🧪 Testing backend API functions...")
394
 
395
- # Test if fn exists
396
- if hasattr(backend_api, 'fn'):
397
- print("✅ Backend has function available")
398
- print(f"✅ Function type: {type(backend_api.fn)}")
 
399
  else:
400
- print("❌ No function available in backend")
401
 
402
  return True
403
  except Exception as e:
 
25
  print(f"🔧 Backend API object type: {type(backend_api)}")
26
  print(f"🔧 Backend API object attributes: {dir(backend_api)}")
27
 
28
+ # Check if it's a Blocks object and has fns
29
+ if hasattr(backend_api, 'fns') and backend_api.fns:
30
  print(f"✅ Backend connection successful!")
31
+ print(f"🔧 Available functions in fns: {list(backend_api.fns.keys())}")
32
  BACKEND_AVAILABLE = True
33
  return True
34
  else:
35
+ print("❌ Backend API functions not found")
36
  print(f"🔧 Available methods: {[attr for attr in dir(backend_api) if not attr.startswith('_')]}")
37
  BACKEND_AVAILABLE = False
38
  return False
 
96
  return None, None, [], 50, 756, 3
97
 
98
  try:
99
+ if BACKEND_AVAILABLE and backend_api and hasattr(backend_api, 'fns'):
100
  # Try to use backend API
101
  try:
102
  print("🔧 Calling backend API for video upload...")
103
 
104
+ # For gr.Blocks, we use predict method
105
+ if 0 in backend_api.fns:
106
+ print("🔧 Using backend Blocks predict method")
107
+ result = backend_api.predict(0, [video, [], 50, 756, 3])
108
 
109
  print(f"✅ Backend video upload API call successful!")
110
  print(f"🔧 Result type: {type(result)}")
 
120
  # Fallback to local processing
121
  pass
122
  else:
123
+ print("Backend function 0 not found")
124
  # Fallback to local processing
125
  pass
126
  except Exception as e:
 
153
  return None, []
154
 
155
  try:
156
+ if BACKEND_AVAILABLE and backend_api and hasattr(backend_api, 'fns'):
157
  # Try to use backend API
158
  try:
159
  print(f"🔧 Calling backend select point API: x={evt.index[0]}, y={evt.index[1]}, type={point_type}")
160
 
161
+ # For gr.Blocks, we use predict method
162
+ if 1 in backend_api.fns:
163
+ print("🔧 Using backend Blocks predict method for point selection")
164
+ result = backend_api.predict(1, [original_img, sel_pix, point_type, evt.index[0], evt.index[1]])
165
+
166
+ print(f"✅ Backend select point API call successful!")
167
+ print(f"🔧 Result type: {type(result)}")
168
+
169
+ # Parse the result - expect (display_image, new_sel_pix)
170
+ if isinstance(result, tuple) and len(result) == 2:
171
+ display_image_b64, new_sel_pix = result
172
+ display_image = base64_to_image(display_image_b64)
173
+ return display_image, new_sel_pix
174
+ else:
175
+ print("Backend returned unexpected format, using local fallback")
176
+ # Fallback to local processing
177
+ pass
178
  else:
179
+ print("Backend function 1 (select point) not found")
180
  # Fallback to local processing
181
  pass
182
  except Exception as e:
 
239
  return None, []
240
 
241
  try:
242
+ if BACKEND_AVAILABLE and backend_api and hasattr(backend_api, 'fns'):
243
  # Try to use backend API
244
  try:
245
  print("🔧 Calling backend reset points API")
246
 
247
+ # For gr.Blocks, we use predict method
248
+ if 2 in backend_api.fns:
249
+ print("🔧 Using backend Blocks predict method for reset")
250
+ result = backend_api.predict(2, [original_img, sel_pix])
251
+
252
+ print(f"✅ Backend reset points API call successful!")
253
+ print(f"🔧 Result type: {type(result)}")
254
+
255
+ # Parse the result - expect (display_image, new_sel_pix)
256
+ if isinstance(result, tuple) and len(result) == 2:
257
+ display_image_b64, new_sel_pix = result
258
+ display_image = base64_to_image(display_image_b64)
259
+ return display_image, new_sel_pix
260
+ else:
261
+ print("Backend returned unexpected format, using local fallback")
262
+ # Fallback to local processing
263
+ pass
264
  else:
265
+ print("Backend function 2 (reset points) not found")
266
  # Fallback to local processing
267
  pass
268
  except Exception as e:
 
297
  return None, None
298
 
299
  try:
300
+ if BACKEND_AVAILABLE and backend_api and hasattr(backend_api, 'fns'):
301
  # Try to use backend API
302
  try:
303
  print(f"🔧 Calling backend API with parameters: grid_size={grid_size}, vo_points={vo_points}, fps={fps}")
304
  print(f"🔧 Original image state type: {type(original_image_state)}")
305
  print(f"🔧 Original image state preview: {str(original_image_state)[:100]}...")
306
 
307
+ # For gr.Blocks, we use predict method
308
+ if 0 in backend_api.fns:
309
+ print("🔧 Using backend Blocks predict method")
310
  # For now, we'll use empty points since we're in local mode
311
+ result = backend_api.predict(0, [None, [], grid_size, vo_points, fps])
312
 
313
  print(f"✅ Backend API call successful!")
314
  print(f"🔧 Result type: {type(result)}")
 
324
  # Fallback to error message
325
  pass
326
  else:
327
+ print("Backend function 0 not found")
328
  # Fallback to error message
329
  pass
330
  except Exception as e:
 
353
  <p style='color: #666; font-size: 12px; margin: 0;'>Backend API Object: {backend_api is not None}</p>
354
  <p style='color: #666; font-size: 12px; margin: 0;'>Backend URL: {BACKEND_SPACE_URL}</p>
355
  <p style='color: #666; font-size: 12px; margin: 0;'>Backend Type: {type(backend_api) if backend_api else 'None'}</p>
356
+ <p style='color: #666; font-size: 12px; margin: 0;'>Available Functions: {list(backend_api.fns.keys()) if backend_api and hasattr(backend_api, 'fns') else 'None'}</p>
357
  </div>
358
  <p style='color: #2d3436; font-weight: bold; margin-top: 15px;'>
359
  Current Status: Backend unavailable - Running in limited mode
 
393
  try:
394
  # Try a simple API call to test connection
395
  print("Testing backend connection with a simple call...")
396
+ # Check if we have fns available
397
+ if hasattr(backend_api, 'fns') and backend_api.fns:
398
+ print("✅ Backend API functions are available")
399
+ print(f"🔧 Available function indices: {list(backend_api.fns.keys())}")
400
  return True
401
  else:
402
+ print("❌ Backend API functions not found")
403
  BACKEND_AVAILABLE = False
404
  return False
405
  except Exception as e:
 
416
  try:
417
  print("🧪 Testing backend API functions...")
418
 
419
+ # Test if fns exist and show available indices
420
+ if hasattr(backend_api, 'fns') and backend_api.fns:
421
+ print(f"✅ Backend has {len(backend_api.fns)} functions available")
422
+ for idx in backend_api.fns.keys():
423
+ print(f"✅ Function {idx} is available")
424
  else:
425
+ print("❌ No functions available in backend")
426
 
427
  return True
428
  except Exception as e: