Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
+
from moviepy.editor import ImageSequenceClip
|
| 5 |
+
import random
|
| 6 |
+
|
| 7 |
+
def generate_video(prompt, duration_seconds, start_image=None):
|
| 8 |
+
if not prompt:
|
| 9 |
+
prompt = "NaturalVid AI video"
|
| 10 |
+
|
| 11 |
+
duration_seconds = max(5, min(60, int(duration_seconds))) # limit between 5-60 seconds
|
| 12 |
+
|
| 13 |
+
width, height = 512, 288
|
| 14 |
+
fps = 24
|
| 15 |
+
total_frames = int(duration_seconds * fps)
|
| 16 |
+
|
| 17 |
+
frames = []
|
| 18 |
+
for i in range(total_frames):
|
| 19 |
+
img = Image.new("RGB", (width, height), color=(10, 20, 40))
|
| 20 |
+
draw = ImageDraw.Draw(img)
|
| 21 |
+
|
| 22 |
+
# Moving text
|
| 23 |
+
text = prompt[:65]
|
| 24 |
+
x = 40 + int(12 * np.sin(i / 10.0))
|
| 25 |
+
y = height // 2 - 40
|
| 26 |
+
draw.text((x, y), text, fill=(255, 255, 255))
|
| 27 |
+
|
| 28 |
+
# Natural moving particles (mist/lights)
|
| 29 |
+
for _ in range(12):
|
| 30 |
+
px = (i * 4 + random.randint(0, 60)) % (width - 10)
|
| 31 |
+
py = random.randint(30, height - 30) + int(10 * np.sin(i / 7.0))
|
| 32 |
+
draw.ellipse((px, py, px + 6, py + 6), fill=(180, 220, 255))
|
| 33 |
+
|
| 34 |
+
frame = np.array(img)
|
| 35 |
+
frames.append(frame)
|
| 36 |
+
|
| 37 |
+
# Create real video
|
| 38 |
+
clip = ImageSequenceClip(frames, fps=fps)
|
| 39 |
+
output_path = "/tmp/naturalvid_output.mp4"
|
| 40 |
+
clip.write_videofile(output_path, codec="libx264", audio=False, verbose=False, logger=None)
|
| 41 |
+
|
| 42 |
+
return output_path
|
| 43 |
+
|
| 44 |
+
with gr.Blocks(title="NaturalVid AI", theme=gr.themes.Soft()) as demo:
|
| 45 |
+
gr.Markdown("# 🌟 NaturalVid AI\n**Your personal free text-to-video generator**")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
with gr.Column(scale=2):
|
| 49 |
+
prompt = gr.Textbox(
|
| 50 |
+
label="Describe your video",
|
| 51 |
+
placeholder="A serene mountain lake at sunrise, gentle mist rising, birds flying slowly, cinematic",
|
| 52 |
+
lines=5
|
| 53 |
+
)
|
| 54 |
+
duration = gr.Slider(
|
| 55 |
+
minimum=5,
|
| 56 |
+
maximum=60,
|
| 57 |
+
value=10,
|
| 58 |
+
step=5,
|
| 59 |
+
label="Video Length (seconds)"
|
| 60 |
+
)
|
| 61 |
+
start_image = gr.Image(label="Optional Start Image", type="pil")
|
| 62 |
+
btn = gr.Button("🚀 Generate Natural Video", variant="primary", size="large")
|
| 63 |
+
|
| 64 |
+
with gr.Column(scale=1):
|
| 65 |
+
output_video = gr.Video(label="🎥 Your Generated Video", height=400)
|
| 66 |
+
|
| 67 |
+
gr.Markdown("💡 Choose length from 5 to 60 seconds. On free hardware longer videos take more time.")
|
| 68 |
+
|
| 69 |
+
btn.click(
|
| 70 |
+
fn=generate_video,
|
| 71 |
+
inputs=[prompt, duration, start_image],
|
| 72 |
+
outputs=output_video
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
demo.launch()
|