Update example.py
Browse files- example.py +90 -10
example.py
CHANGED
@@ -1,17 +1,97 @@
|
|
1 |
import requests
|
|
|
2 |
|
|
|
|
|
|
|
3 |
API_URL = "https://<use your own Inference Endpoint here>.endpoints.huggingface.cloud"
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
"Content-Type": "application/json"
|
8 |
-
}
|
9 |
|
10 |
def query(payload):
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
|
|
14 |
output = query({
|
15 |
-
|
16 |
-
|
17 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import requests
|
2 |
+
import base64
|
3 |
|
4 |
+
# Important: the NVIDIA L40S will only support small resolutions, short length and no post-processing.
|
5 |
+
# If you want those features, you might need to use the NVIDIA A100.
|
6 |
+
# Use your own Inference Endpoint URL
|
7 |
API_URL = "https://<use your own Inference Endpoint here>.endpoints.huggingface.cloud"
|
8 |
+
|
9 |
+
# Use you own API token
|
10 |
+
API_TOKEN = "hf_<replace by your own Hugging Face token>"
|
|
|
|
|
11 |
|
12 |
def query(payload):
|
13 |
+
response = requests.post(API_URL, headers={
|
14 |
+
"Accept": "application/json",
|
15 |
+
"Authorization": f"Bearer {API_TOKEN}",
|
16 |
+
"Content-Type": "application/json"
|
17 |
+
}, json=payload)
|
18 |
+
return response.json()
|
19 |
+
|
20 |
+
def save_video(json_response):
|
21 |
+
video_data_uri = ""
|
22 |
+
try:
|
23 |
+
# Extract the video data URI from the response
|
24 |
+
video_data_uri = json_response["video"]
|
25 |
+
except Exception as e:
|
26 |
+
message = str(json_response)
|
27 |
+
print(message)
|
28 |
+
raise ValueError(message)
|
29 |
+
|
30 |
+
# Remove the data URI prefix to get just the base64 data
|
31 |
+
# Assumes format like "data:video/mp4;base64,<actual_base64_data>"
|
32 |
+
base64_data = video_data_uri.split(",")[1]
|
33 |
+
|
34 |
+
# Decode the base64 data
|
35 |
+
video_data = base64.b64decode(base64_data)
|
36 |
+
|
37 |
+
# Write the binary data to an MP4 file
|
38 |
+
with open("video.mp4", "wb") as f:
|
39 |
+
f.write(video_data)
|
40 |
|
41 |
+
# Make the API call
|
42 |
output = query({
|
43 |
+
"inputs": {
|
44 |
+
"prompt": "Portrait photo, selfie of a beautiful young caucasian woman called Charlotte, wearing a pastel-blue hoodie. She is livestreaming from NYC streets. She looks straight into the camera, looking serious, and she talks. The camera is fixed, static, a medium-shot centered on her face. 4K webcam footage. Intricate details, super resolution, sharp image, award winning."
|
45 |
+
},
|
46 |
+
"parameters": {
|
47 |
+
# ------------------- settings for LTX-Video -----------------------
|
48 |
+
# for a vertical video look
|
49 |
+
"width": 480,
|
50 |
+
"height": 768,
|
51 |
+
|
52 |
+
# LTX-Video requires a frame number divisible by 8, plus one frame
|
53 |
+
# note: glitches might appear if you use more than 168 frames
|
54 |
+
"num_frames": (8 * 14) + 1,
|
55 |
+
|
56 |
+
# using 30 steps seems to be enough for most cases, otherwise use 50 for best quality
|
57 |
+
# I think using a large number of steps (> 30) might create some overexposure and saturation
|
58 |
+
"num_inference_steps": 40,
|
59 |
+
|
60 |
+
# done testing using 4.0 and 5.0, seems alright in most cases
|
61 |
+
"guidance_scale": 5.0,
|
62 |
+
|
63 |
+
# seed: -1,
|
64 |
+
|
65 |
+
# ------------------- settings for Varnish -----------------------
|
66 |
+
# This will double the number of frames.
|
67 |
+
# You can activate this if you want:
|
68 |
+
# - a slow motion effect (in that case use double_num_frames=True and fps=24, 25 or 30)
|
69 |
+
# - a HD soap / video game effect (in that case use double_num_frames=True and fps=60)
|
70 |
+
"double_num_frames": True,
|
71 |
+
|
72 |
+
# controls the number of frames per second
|
73 |
+
# use this in combination with the num_frames and double_num_frames settings to control the duration and "feel" of your video
|
74 |
+
"fps": 60, # typical values are: 24, 25, 30, 60
|
75 |
+
|
76 |
+
# upscale the video using Real-ESRGAN.
|
77 |
+
# This upscaling algorithm is relatively fast,
|
78 |
+
# but might create an uncanny "3D render" or "drawing" effect.
|
79 |
+
"super_resolution": True,
|
80 |
+
|
81 |
+
# for cosmetic purposes and get a "cinematic" feel, you can optionally add some film grain.
|
82 |
+
# it is not recommended to add film grain if your theme doesn't match (film grain is great for black & white, retro looks)
|
83 |
+
# and if you do, adding more than 12% will start to negatively impact file size (video codecs aren't great are compression film grain)
|
84 |
+
# 0% = no grain
|
85 |
+
# 10% = a bit of grain
|
86 |
+
"grain_amount": 10, # value between 0-100
|
87 |
+
|
88 |
+
# the following parameters are a work in progress
|
89 |
+
"enable_audio": False,
|
90 |
+
#"audio_prompt": "voices, voice, talking, speaking, speech",
|
91 |
+
#"audio_negative_prompt": "",
|
92 |
+
|
93 |
+
}
|
94 |
+
})
|
95 |
+
|
96 |
+
# Save the video
|
97 |
+
save_video(output)
|