Spaces:
Running
on
Zero
Running
on
Zero
xiaoyuxi
commited on
Commit
·
b08da78
1
Parent(s):
6f5c5a6
vggt
Browse files
app.py
CHANGED
|
@@ -20,9 +20,18 @@ def initialize_backend():
|
|
| 20 |
try:
|
| 21 |
print(f"Attempting to connect to backend: {BACKEND_SPACE_URL}")
|
| 22 |
backend_api = gr.load(f"spaces/{BACKEND_SPACE_URL}", token=hf_token)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
print(f"❌ Backend connection failed: {e}")
|
| 28 |
print("⚠️ Running in standalone mode (some features may be limited)")
|
|
@@ -207,17 +216,28 @@ def launch_viz(grid_size, vo_points, fps, original_image_state):
|
|
| 207 |
if BACKEND_AVAILABLE and backend_api:
|
| 208 |
# Try to use backend API
|
| 209 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
viz_iframe_html, track_video_path = backend_api.run_tracker_api(
|
| 211 |
grid_size, vo_points, fps, original_image_state
|
| 212 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
return viz_iframe_html, track_video_path
|
| 214 |
except Exception as e:
|
| 215 |
-
print(f"Backend API call failed: {e}")
|
|
|
|
|
|
|
| 216 |
# Fallback to local processing
|
| 217 |
pass
|
| 218 |
|
| 219 |
# Fallback: show message that backend is required
|
| 220 |
-
error_message = """
|
| 221 |
<div style='border: 3px solid #ff6b6b; border-radius: 10px; padding: 20px; background-color: #fff5f5;'>
|
| 222 |
<h3 style='color: #d63031; margin-bottom: 15px;'>⚠️ Backend Connection Required</h3>
|
| 223 |
<p style='color: #2d3436; line-height: 1.6;'>
|
|
@@ -229,6 +249,12 @@ def launch_viz(grid_size, vo_points, fps, original_image_state):
|
|
| 229 |
<li>The BACKEND_SPACE_URL is correctly configured</li>
|
| 230 |
<li>You have proper access permissions to the backend Space</li>
|
| 231 |
</ul>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
<p style='color: #2d3436; font-weight: bold; margin-top: 15px;'>
|
| 233 |
Current Status: Backend unavailable - Running in limited mode
|
| 234 |
</p>
|
|
@@ -258,10 +284,60 @@ def handle_video_change(video):
|
|
| 258 |
|
| 259 |
return original_image_state, display_image, selected_points, grid_size_val, vo_points_val, fps_val
|
| 260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
# Initialize backend connection
|
| 262 |
print("🔧 Initializing backend connection...")
|
| 263 |
initialize_backend()
|
| 264 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
# Build UI
|
| 266 |
with gr.Blocks(css="""
|
| 267 |
#advanced_settings .wrap {
|
|
@@ -321,7 +397,7 @@ with gr.Blocks(css="""
|
|
| 321 |
original_image_state = gr.State() # Store original image in state
|
| 322 |
|
| 323 |
with gr.Row():
|
| 324 |
-
# Show backend status
|
| 325 |
status_color = "#28a745" if BACKEND_AVAILABLE else "#dc3545"
|
| 326 |
status_text = "Connected" if BACKEND_AVAILABLE else "Disconnected"
|
| 327 |
status_icon = "✅" if BACKEND_AVAILABLE else "❌"
|
|
@@ -343,6 +419,9 @@ with gr.Blocks(css="""
|
|
| 343 |
<p style='font-size: 14px; color: #666; margin: 5px 0 0 0;'>
|
| 344 |
{BACKEND_SPACE_URL}
|
| 345 |
</p>
|
|
|
|
|
|
|
|
|
|
| 346 |
</div>
|
| 347 |
</div>
|
| 348 |
""")
|
|
@@ -399,10 +478,14 @@ with gr.Blocks(css="""
|
|
| 399 |
examples_per_page=20 # Show all examples on one page to enable scrolling
|
| 400 |
)
|
| 401 |
|
| 402 |
-
# Initialize with
|
| 403 |
viz_iframe = gr.HTML("""
|
| 404 |
-
<div style='border: 3px solid #667eea; border-radius: 10px; overflow: hidden; box-shadow: 0 8px 32px rgba(102, 126, 234, 0.3);'>
|
| 405 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
</div>
|
| 407 |
""")
|
| 408 |
|
|
|
|
| 20 |
try:
|
| 21 |
print(f"Attempting to connect to backend: {BACKEND_SPACE_URL}")
|
| 22 |
backend_api = gr.load(f"spaces/{BACKEND_SPACE_URL}", token=hf_token)
|
| 23 |
+
|
| 24 |
+
# Test if the API object has the expected methods
|
| 25 |
+
if hasattr(backend_api, 'upload_video_api') and hasattr(backend_api, 'run_tracker_api'):
|
| 26 |
+
BACKEND_AVAILABLE = True
|
| 27 |
+
print("✅ Backend connection successful!")
|
| 28 |
+
print("✅ Backend API methods are available")
|
| 29 |
+
return True
|
| 30 |
+
else:
|
| 31 |
+
print("❌ Backend API methods not found")
|
| 32 |
+
BACKEND_AVAILABLE = False
|
| 33 |
+
return False
|
| 34 |
+
|
| 35 |
except Exception as e:
|
| 36 |
print(f"❌ Backend connection failed: {e}")
|
| 37 |
print("⚠️ Running in standalone mode (some features may be limited)")
|
|
|
|
| 216 |
if BACKEND_AVAILABLE and backend_api:
|
| 217 |
# Try to use backend API
|
| 218 |
try:
|
| 219 |
+
print(f"🔧 Calling backend API with parameters: grid_size={grid_size}, vo_points={vo_points}, fps={fps}")
|
| 220 |
+
print(f"🔧 Original image state type: {type(original_image_state)}")
|
| 221 |
+
print(f"🔧 Original image state preview: {str(original_image_state)[:100]}...")
|
| 222 |
+
|
| 223 |
viz_iframe_html, track_video_path = backend_api.run_tracker_api(
|
| 224 |
grid_size, vo_points, fps, original_image_state
|
| 225 |
)
|
| 226 |
+
|
| 227 |
+
print(f"✅ Backend API call successful!")
|
| 228 |
+
print(f"🔧 Viz HTML type: {type(viz_iframe_html)}")
|
| 229 |
+
print(f"🔧 Track video path: {track_video_path}")
|
| 230 |
+
|
| 231 |
return viz_iframe_html, track_video_path
|
| 232 |
except Exception as e:
|
| 233 |
+
print(f"❌ Backend API call failed: {e}")
|
| 234 |
+
print(f"🔧 Error type: {type(e)}")
|
| 235 |
+
print(f"🔧 Error details: {str(e)}")
|
| 236 |
# Fallback to local processing
|
| 237 |
pass
|
| 238 |
|
| 239 |
# Fallback: show message that backend is required
|
| 240 |
+
error_message = f"""
|
| 241 |
<div style='border: 3px solid #ff6b6b; border-radius: 10px; padding: 20px; background-color: #fff5f5;'>
|
| 242 |
<h3 style='color: #d63031; margin-bottom: 15px;'>⚠️ Backend Connection Required</h3>
|
| 243 |
<p style='color: #2d3436; line-height: 1.6;'>
|
|
|
|
| 249 |
<li>The BACKEND_SPACE_URL is correctly configured</li>
|
| 250 |
<li>You have proper access permissions to the backend Space</li>
|
| 251 |
</ul>
|
| 252 |
+
<div style='background-color: #f8f9fa; border-radius: 5px; padding: 10px; margin-top: 10px;'>
|
| 253 |
+
<p style='color: #2d3436; font-weight: bold; margin: 0 0 5px 0;'>Debug Information:</p>
|
| 254 |
+
<p style='color: #666; font-size: 12px; margin: 0;'>Backend Available: {BACKEND_AVAILABLE}</p>
|
| 255 |
+
<p style='color: #666; font-size: 12px; margin: 0;'>Backend API Object: {backend_api is not None}</p>
|
| 256 |
+
<p style='color: #666; font-size: 12px; margin: 0;'>Backend URL: {BACKEND_SPACE_URL}</p>
|
| 257 |
+
</div>
|
| 258 |
<p style='color: #2d3436; font-weight: bold; margin-top: 15px;'>
|
| 259 |
Current Status: Backend unavailable - Running in limited mode
|
| 260 |
</p>
|
|
|
|
| 284 |
|
| 285 |
return original_image_state, display_image, selected_points, grid_size_val, vo_points_val, fps_val
|
| 286 |
|
| 287 |
+
def test_backend_connection():
|
| 288 |
+
"""Test if backend is actually working"""
|
| 289 |
+
global BACKEND_AVAILABLE
|
| 290 |
+
if not backend_api:
|
| 291 |
+
return False
|
| 292 |
+
|
| 293 |
+
try:
|
| 294 |
+
# Try a simple API call to test connection
|
| 295 |
+
print("Testing backend connection with a simple call...")
|
| 296 |
+
# We'll test with a dummy call or check if the API object is properly loaded
|
| 297 |
+
if hasattr(backend_api, 'upload_video_api'):
|
| 298 |
+
print("✅ Backend API methods are available")
|
| 299 |
+
return True
|
| 300 |
+
else:
|
| 301 |
+
print("❌ Backend API methods not found")
|
| 302 |
+
BACKEND_AVAILABLE = False
|
| 303 |
+
return False
|
| 304 |
+
except Exception as e:
|
| 305 |
+
print(f"❌ Backend connection test failed: {e}")
|
| 306 |
+
BACKEND_AVAILABLE = False
|
| 307 |
+
return False
|
| 308 |
+
|
| 309 |
+
def test_backend_api():
|
| 310 |
+
"""Test specific backend API functions"""
|
| 311 |
+
if not BACKEND_AVAILABLE or not backend_api:
|
| 312 |
+
print("❌ Backend not available for testing")
|
| 313 |
+
return False
|
| 314 |
+
|
| 315 |
+
try:
|
| 316 |
+
print("🧪 Testing backend API functions...")
|
| 317 |
+
|
| 318 |
+
# Test if methods exist
|
| 319 |
+
methods_to_test = ['upload_video_api', 'select_point_api', 'reset_points_api', 'run_tracker_api']
|
| 320 |
+
for method in methods_to_test:
|
| 321 |
+
if hasattr(backend_api, method):
|
| 322 |
+
print(f"✅ {method} is available")
|
| 323 |
+
else:
|
| 324 |
+
print(f"❌ {method} is not available")
|
| 325 |
+
|
| 326 |
+
return True
|
| 327 |
+
except Exception as e:
|
| 328 |
+
print(f"❌ Backend API test failed: {e}")
|
| 329 |
+
return False
|
| 330 |
+
|
| 331 |
# Initialize backend connection
|
| 332 |
print("🔧 Initializing backend connection...")
|
| 333 |
initialize_backend()
|
| 334 |
|
| 335 |
+
# Test the connection
|
| 336 |
+
test_backend_connection()
|
| 337 |
+
|
| 338 |
+
# Test specific API functions
|
| 339 |
+
test_backend_api()
|
| 340 |
+
|
| 341 |
# Build UI
|
| 342 |
with gr.Blocks(css="""
|
| 343 |
#advanced_settings .wrap {
|
|
|
|
| 397 |
original_image_state = gr.State() # Store original image in state
|
| 398 |
|
| 399 |
with gr.Row():
|
| 400 |
+
# Show backend status with more detailed information
|
| 401 |
status_color = "#28a745" if BACKEND_AVAILABLE else "#dc3545"
|
| 402 |
status_text = "Connected" if BACKEND_AVAILABLE else "Disconnected"
|
| 403 |
status_icon = "✅" if BACKEND_AVAILABLE else "❌"
|
|
|
|
| 419 |
<p style='font-size: 14px; color: #666; margin: 5px 0 0 0;'>
|
| 420 |
{BACKEND_SPACE_URL}
|
| 421 |
</p>
|
| 422 |
+
<p style='font-size: 12px; color: #888; margin: 5px 0 0 0;'>
|
| 423 |
+
{'API methods available' if BACKEND_AVAILABLE else 'Connection failed - using local mode'}
|
| 424 |
+
</p>
|
| 425 |
</div>
|
| 426 |
</div>
|
| 427 |
""")
|
|
|
|
| 478 |
examples_per_page=20 # Show all examples on one page to enable scrolling
|
| 479 |
)
|
| 480 |
|
| 481 |
+
# Initialize with a placeholder interface instead of static file
|
| 482 |
viz_iframe = gr.HTML("""
|
| 483 |
+
<div style='border: 3px solid #667eea; border-radius: 10px; overflow: hidden; box-shadow: 0 8px 32px rgba(102, 126, 234, 0.3); background: #f8f9fa; display: flex; align-items: center; justify-content: center; height: 950px;'>
|
| 484 |
+
<div style='text-align: center; color: #666;'>
|
| 485 |
+
<h3 style='margin-bottom: 20px; color: #667eea;'>🎮 Interactive 3D Tracking</h3>
|
| 486 |
+
<p style='font-size: 16px; margin-bottom: 10px;'>Upload a video and select points to start tracking</p>
|
| 487 |
+
<p style='font-size: 14px; color: #999;'>Powered by SpaTrackV2</p>
|
| 488 |
+
</div>
|
| 489 |
</div>
|
| 490 |
""")
|
| 491 |
|