File size: 3,700 Bytes
803c132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1575924
803c132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1575924
 
 
 
 
803c132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/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())