File size: 1,364 Bytes
bad6e6b 19d148a bad6e6b 19d148a bad6e6b 19d148a bad6e6b 19d148a bad6e6b 0441f0b 19d148a bad6e6b 19d148a bad6e6b 05bed8e 19d148a |
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 |
import gradio as gr
import requests
HF_API_TOKEN = "YOUR_HF_API_TOKEN"
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1"
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.content
def generate_video(prompt):
os.makedirs("frames", exist_ok=True)
for i in range(10):
image_bytes = query({"inputs": prompt})
with open(f"frames/frame_{i:03d}.png", "wb") as f:
f.write(image_bytes)
create_video_from_frames("frames", "generated_video.mp4", fps=2)
return "generated_video.mp4"
def create_video_from_frames(frame_folder, output_path, fps=2):
import cv2
images = sorted([img for img in os.listdir(frame_folder) if img.endswith(".png")])
frame = cv2.imread(os.path.join(frame_folder, images[0]))
height, width, _ = frame.shape
video = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
for img in images:
video.write(cv2.imread(os.path.join(frame_folder, img)))
video.release()
iface = gr.Interface(fn=generate_video,
inputs=gr.Textbox(lines=3),
outputs=gr.Video(),
title="Text to Video AI")
if __name__ == "__main__":
iface.launch()
|