jbilcke-hf HF Staff commited on
Commit
eb0d432
·
verified ·
1 Parent(s): 19ddf57

Update README.md

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