Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,39 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
import os
|
4 |
-
import cv2
|
5 |
|
6 |
-
|
7 |
-
|
8 |
|
9 |
-
|
10 |
-
VIDEO_OUTPUT = "generated_video.mp4"
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
# --- Function to create video from frames ---
|
20 |
def create_video_from_frames(frame_folder, output_path, fps=2):
|
|
|
21 |
images = sorted([img for img in os.listdir(frame_folder) if img.endswith(".png")])
|
22 |
-
if not images:
|
23 |
-
raise ValueError("No frames generated.")
|
24 |
frame = cv2.imread(os.path.join(frame_folder, images[0]))
|
25 |
height, width, _ = frame.shape
|
26 |
-
|
27 |
video = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
|
28 |
for img in images:
|
29 |
video.write(cv2.imread(os.path.join(frame_folder, img)))
|
30 |
video.release()
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
return VIDEO_OUTPUT
|
37 |
-
|
38 |
-
# --- Gradio UI ---
|
39 |
-
iface = gr.Interface(
|
40 |
-
fn=generate_video,
|
41 |
-
inputs=gr.Textbox(lines=3, placeholder="Describe your scene here..."),
|
42 |
-
outputs=gr.Video(),
|
43 |
-
title="AI Text-to-Video Generator (No manual assets needed)"
|
44 |
-
)
|
45 |
|
46 |
if __name__ == "__main__":
|
47 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
|
|
|
|
3 |
|
4 |
+
HF_API_TOKEN = "YOUR_HF_API_TOKEN"
|
5 |
+
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1"
|
6 |
|
7 |
+
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
|
|
8 |
|
9 |
+
def query(payload):
|
10 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
11 |
+
return response.content
|
12 |
+
|
13 |
+
def generate_video(prompt):
|
14 |
+
os.makedirs("frames", exist_ok=True)
|
15 |
+
for i in range(10):
|
16 |
+
image_bytes = query({"inputs": prompt})
|
17 |
+
with open(f"frames/frame_{i:03d}.png", "wb") as f:
|
18 |
+
f.write(image_bytes)
|
19 |
+
|
20 |
+
create_video_from_frames("frames", "generated_video.mp4", fps=2)
|
21 |
+
return "generated_video.mp4"
|
22 |
|
|
|
23 |
def create_video_from_frames(frame_folder, output_path, fps=2):
|
24 |
+
import cv2
|
25 |
images = sorted([img for img in os.listdir(frame_folder) if img.endswith(".png")])
|
|
|
|
|
26 |
frame = cv2.imread(os.path.join(frame_folder, images[0]))
|
27 |
height, width, _ = frame.shape
|
|
|
28 |
video = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
|
29 |
for img in images:
|
30 |
video.write(cv2.imread(os.path.join(frame_folder, img)))
|
31 |
video.release()
|
32 |
|
33 |
+
iface = gr.Interface(fn=generate_video,
|
34 |
+
inputs=gr.Textbox(lines=3),
|
35 |
+
outputs=gr.Video(),
|
36 |
+
title="Text to Video AI")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
if __name__ == "__main__":
|
39 |
+
iface.launch()
|