Views
No views yet
LTX-Video-0-9-6-HFIE is a version of LTX-Video 0.9.6 (distilled) that can be deployed to a Hugging Face endpoint.SUPPORT_INPUT_IMAGE_PROMPT which has to be truthy or falsy1import requests
2import base64
3from PIL import Image
4from io import BytesIO
5import os
6
7API_URL = "https://<USE YOR OWN>.aws.endpoints.huggingface.cloud"
8
9API_TOKEN = "<USE YOR OWN>"
10
11def query(payload):
12 response = requests.post(API_URL, headers={
13 "Accept": "application/json",
14 "Authorization": f"Bearer {API_TOKEN}",
15 "Content-Type": "application/json"
16 }, json=payload)
17 return response.json()
18
19def save_video(json_response, filename):
20
21 try:
22 error = json_response["error"]
23 if error:
24 print(error)
25 return
26 except Exception as e:
27 pass
28
29 video_data_uri = ""
30 try:
31 # Extract the video data URI from the response
32 video_data_uri = json_response["video"]
33 except Exception as e:
34 message = str(json_response)
35 print(message)
36 raise ValueError(message)
37
38 # Remove the data URI prefix to get just the base64 data
39 # Assumes format like "data:video/mp4;base64,<actual_base64_data>"
40 base64_data = video_data_uri.split(",")[1]
41
42 # Decode the base64 data
43 video_data = base64.b64decode(base64_data)
44
45 # Write the binary data to an MP4 file
46 with open(filename, "wb") as f:
47 f.write(video_data)
48
49def encode_image(image_path):
50 """
51 Load and encode an image file to base64
52
53 Args:
54 image_path (str): Path to the image file
55
56 Returns:
57 str: Base64 encoded image data URI
58 """
59
60 with Image.open(image_path) as img:
61 # Convert to RGB if necessary
62 if img.mode != "RGB":
63 img = img.convert("RGB")
64
65 # Save image to bytes
66 img_byte_arr = BytesIO()
67 img.save(img_byte_arr, format="JPEG")
68
69 # Encode to base64
70 base64_encoded = base64.b64encode(img_byte_arr.getvalue()).decode('utf-8')
71 return f"data:image/jpeg;base64,{base64_encoded}"
72
73# Example usage with image-to-video generation
74image_filename = "input.jpg" # Path to your input image
75video_filename = "output.mp4"
76
77config = {
78 "inputs": {
79 "prompt": "magnificent underwater footage, clownfishes swimming around coral inside the carribean sea, real gopro footage",
80 # "image": encode_image(image_filename)
81 },
82
83 "parameters": {
84
85 # ------------------- settings for LTX-Video -----------------------
86
87 #"negative_prompt": "saturated, highlight, overexposed, highlighted, overlit, shaking, too bright, worst quality, inconsistent motion, blurry, jittery, distorted, cropped, watermarked, watermark, logo, subtitle, subtitles, lowres",
88
89 # note about resolution:
90 # we cannot use 720 since it cannot be divided by 32
91 #
92 # for a cinematic look:
93 "width": 768,
94 "height": 480,
95
96 # for a vertical video look:
97 #"width": 480,
98 #"height": 768,
99
100 # LTX-Video requires a frame number divisible by 8, plus one frame
101 # note: glitches might appear if you use more than 168 frames
102 "num_frames": (8 * 16) + 1,
103
104 "num_inference_steps": 8,
105
106 "guidance_scale": 1.0,
107
108 #"seed": 1209877,
109
110 # This will double the number of frames.
111 # You can activate this if you want:
112 # - a slow motion effect (in that case use double_num_frames=True and fps=24, 25 or 30)
113 # - a HD soap / video game effect (in that case use double_num_frames=True and fps=60)
114 "double_num_frames": True,
115
116 # controls the number of frames per second
117 # use this in combination with the num_frames and double_num_frames settings to control the duration and "feel" of your video
118 "fps": 60, # typical values are: 24, 25, 30, 60
119
120 # upscale the video using Real-ESRGAN.
121 # This upscaling algorithm is relatively fast,
122 # but might create an uncanny "3D render" or "drawing" effect.
123 "super_resolution": True,
124 }
125}
126
127# Make the API call
128output = query(config)
129
130# Save the video
131save_video(output, video_filename)