Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import StableDiffusionPipeline
|
| 4 |
+
from moviepy.editor import ImageClip, concatenate_videoclips, AudioFileClip
|
| 5 |
+
from moviepy.video.fx.all import resize
|
| 6 |
+
from PIL import Image, ImageDraw
|
| 7 |
+
import numpy as np
|
| 8 |
+
import os
|
| 9 |
+
from TTS.api import TTS
|
| 10 |
+
import random
|
| 11 |
+
|
| 12 |
+
device = "cpu"
|
| 13 |
+
|
| 14 |
+
# Load SDXL (lighter config for CPU)
|
| 15 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 16 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 17 |
+
torch_dtype=torch.float32,
|
| 18 |
+
use_safetensors=True
|
| 19 |
+
).to(device)
|
| 20 |
+
|
| 21 |
+
# Load Coqui TTS
|
| 22 |
+
tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False)
|
| 23 |
+
|
| 24 |
+
def generate_script(prompt):
|
| 25 |
+
hooks = [
|
| 26 |
+
"They laugh when you start.",
|
| 27 |
+
"They doubt your discipline.",
|
| 28 |
+
"They mock your silence.",
|
| 29 |
+
"But they fear your results."
|
| 30 |
+
]
|
| 31 |
+
random.shuffle(hooks)
|
| 32 |
+
return hooks
|
| 33 |
+
|
| 34 |
+
def generate_image(text, index):
|
| 35 |
+
prompt = f"dark cinematic, luxury wealth, night city, dramatic lighting, 9:16 portrait, {text}"
|
| 36 |
+
image = pipe(prompt, height=1024, width=576).images[0]
|
| 37 |
+
path = f"scene_{index}.png"
|
| 38 |
+
image.save(path)
|
| 39 |
+
return path
|
| 40 |
+
|
| 41 |
+
def add_subtitle(image_path, text):
|
| 42 |
+
img = Image.open(image_path)
|
| 43 |
+
draw = ImageDraw.Draw(img)
|
| 44 |
+
w, h = img.size
|
| 45 |
+
|
| 46 |
+
draw.text((40, h - 180), text, fill="white")
|
| 47 |
+
|
| 48 |
+
new_path = image_path.replace(".png", "_text.png")
|
| 49 |
+
img.save(new_path)
|
| 50 |
+
return new_path
|
| 51 |
+
|
| 52 |
+
def create_kenburns_clip(image_path, duration=5):
|
| 53 |
+
clip = ImageClip(image_path).set_duration(duration)
|
| 54 |
+
clip = clip.resize(height=1280)
|
| 55 |
+
clip = clip.fx(resize, lambda t: 1 + 0.05 * t) # Slow zoom
|
| 56 |
+
return clip
|
| 57 |
+
|
| 58 |
+
def generate_voice(script):
|
| 59 |
+
full_text = " ".join(script)
|
| 60 |
+
tts.tts_to_file(text=full_text, file_path="voice.wav")
|
| 61 |
+
return "voice.wav"
|
| 62 |
+
|
| 63 |
+
def create_video(image_paths, voice_path):
|
| 64 |
+
clips = []
|
| 65 |
+
for img in image_paths:
|
| 66 |
+
clip = create_kenburns_clip(img)
|
| 67 |
+
clips.append(clip)
|
| 68 |
+
|
| 69 |
+
final = concatenate_videoclips(clips, method="compose")
|
| 70 |
+
|
| 71 |
+
if os.path.exists("music.mp3"):
|
| 72 |
+
bg_music = AudioFileClip("music.mp3").volumex(0.3)
|
| 73 |
+
voice = AudioFileClip(voice_path)
|
| 74 |
+
final = final.set_audio(voice.audio_fadein(1))
|
| 75 |
+
else:
|
| 76 |
+
voice = AudioFileClip(voice_path)
|
| 77 |
+
final = final.set_audio(voice)
|
| 78 |
+
|
| 79 |
+
output_path = "final_dark_money.mp4"
|
| 80 |
+
final.write_videofile(output_path, fps=24)
|
| 81 |
+
|
| 82 |
+
return output_path
|
| 83 |
+
|
| 84 |
+
def generate_video(prompt):
|
| 85 |
+
scenes = generate_script(prompt)
|
| 86 |
+
|
| 87 |
+
images = []
|
| 88 |
+
for i, scene in enumerate(scenes):
|
| 89 |
+
img = generate_image(scene, i)
|
| 90 |
+
img_with_text = add_subtitle(img, scene)
|
| 91 |
+
images.append(img_with_text)
|
| 92 |
+
|
| 93 |
+
voice = generate_voice(scenes)
|
| 94 |
+
video = create_video(images, voice)
|
| 95 |
+
|
| 96 |
+
return video
|
| 97 |
+
|
| 98 |
+
iface = gr.Interface(
|
| 99 |
+
fn=generate_video,
|
| 100 |
+
inputs=gr.Textbox(label="Enter Dark Money Motivation Prompt"),
|
| 101 |
+
outputs=gr.Video(label="Generated Reel"),
|
| 102 |
+
title="🔥 Dark Money AI Shorts Generator PRO"
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
iface.launch()
|