Spaces:
Paused
Paused
| #!/usr/bin/env python3 | |
| """ | |
| InstantSplat API Client | |
| Simple client to submit images and get back the Supabase GLB URL. | |
| Usage: | |
| python api_client.py image1.jpg image2.jpg image3.jpg | |
| Environment variables: | |
| INSTANTSPLAT_SPACE: HuggingFace Space URL (default: use local) | |
| """ | |
| import sys | |
| import os | |
| from gradio_client import Client | |
| def process_images(image_paths, space_url=None, hf_token=None): | |
| """ | |
| Submit images to InstantSplat and get GLB URL. | |
| Args: | |
| image_paths: List of image file paths | |
| space_url: HuggingFace Space URL (optional) | |
| Returns: | |
| dict with results | |
| """ | |
| # Validate inputs | |
| if len(image_paths) < 2: | |
| return { | |
| "status": "error", | |
| "error": "Need at least 2 images (3+ recommended)" | |
| } | |
| for path in image_paths: | |
| if not os.path.exists(path): | |
| return { | |
| "status": "error", | |
| "error": f"File not found: {path}" | |
| } | |
| try: | |
| # Connect to Space | |
| if not space_url: | |
| space_url = "longh37/InstantSplat" | |
| print(f"Connecting to Space: {space_url}") | |
| client = Client(space_url) | |
| print(f"Submitting {len(image_paths)} images for processing...") | |
| for i, img in enumerate(image_paths, 1): | |
| print(f" {i}. {img}") | |
| # Submit job | |
| result = client.predict( | |
| image_paths, | |
| api_name="/predict" | |
| ) | |
| # Unpack results | |
| video_path, ply_url, _, _, glb_path, glb_url = result | |
| # Check if upload succeeded | |
| if glb_url and not glb_url.startswith("Error"): | |
| return { | |
| "status": "success", | |
| "glb_url": glb_url, | |
| "ply_url": ply_url, | |
| "video_available": video_path is not None, | |
| "message": "Processing complete!" | |
| } | |
| else: | |
| return { | |
| "status": "error", | |
| "error": glb_url or "Upload failed" | |
| } | |
| except Exception as e: | |
| return { | |
| "status": "error", | |
| "error": str(e) | |
| } | |
| def main(): | |
| """CLI interface.""" | |
| if len(sys.argv) < 3: | |
| print("Usage: python api_client.py <image1> <image2> [image3 ...]") | |
| print("\nExample:") | |
| print(" python api_client.py img1.jpg img2.jpg img3.jpg") | |
| print("\nEnvironment Variables:") | |
| print(" INSTANTSPLAT_SPACE - HuggingFace Space URL (optional)") | |
| print(" e.g., your-username/InstantSplat") | |
| sys.exit(1) | |
| # Get images from command line | |
| image_paths = sys.argv[1:] | |
| # Get Space URL from environment or use local | |
| space_url = os.environ.get("INSTANTSPLAT_SPACE") | |
| print("=" * 80) | |
| print("InstantSplat API Client") | |
| print("=" * 80) | |
| # Process images | |
| result = process_images(image_paths, space_url) | |
| print("\n" + "=" * 80) | |
| # Display results | |
| if result["status"] == "success": | |
| print("✅ SUCCESS!") | |
| print("-" * 80) | |
| print(f"GLB URL: {result['glb_url']}") | |
| print(f"PLY URL: {result['ply_url']}") | |
| if result['video_available']: | |
| print("Video: Available") | |
| print("-" * 80) | |
| print("\n💡 Tip: You can now download the GLB file:") | |
| print(f" curl -o model.glb '{result['glb_url']}'") | |
| print("=" * 80) | |
| return 0 | |
| else: | |
| print("❌ ERROR!") | |
| print("-" * 80) | |
| print(f"Error: {result['error']}") | |
| print("=" * 80) | |
| return 1 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |