Spaces:
Runtime error
Runtime error
Update app-sd.py
Browse files
app-sd.py
CHANGED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gtts import gTTS
|
| 3 |
+
from moviepy.editor import *
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import torch
|
| 7 |
+
from diffusers import StableDiffusionPipeline
|
| 8 |
+
import tempfile
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
# -----------------------------------------------------
|
| 12 |
+
# Load Stable Diffusion model (this will work on GPU)
|
| 13 |
+
# -----------------------------------------------------
|
| 14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
sd_pipeline = StableDiffusionPipeline.from_pretrained(
|
| 16 |
+
"runwayml/stable-diffusion-v1-5",
|
| 17 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 18 |
+
).to(device)
|
| 19 |
+
|
| 20 |
+
# -----------------------------------------------------
|
| 21 |
+
# Function: Generate background image with Stable Diffusion
|
| 22 |
+
# -----------------------------------------------------
|
| 23 |
+
def generate_background(prompt: str):
|
| 24 |
+
image = sd_pipeline(prompt).images[0]
|
| 25 |
+
return image
|
| 26 |
+
|
| 27 |
+
# -----------------------------------------------------
|
| 28 |
+
# Function: Generate video with voice + background
|
| 29 |
+
# -----------------------------------------------------
|
| 30 |
+
def generate_video(prompt, affirmation, with_visuals):
|
| 31 |
+
# Create speech
|
| 32 |
+
text = f"{prompt}. {affirmation}" if affirmation else prompt
|
| 33 |
+
tts = gTTS(text=text, lang="en")
|
| 34 |
+
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 35 |
+
tts.save(temp_audio.name)
|
| 36 |
+
|
| 37 |
+
# Generate background
|
| 38 |
+
if with_visuals:
|
| 39 |
+
bg_img = generate_background(prompt)
|
| 40 |
+
else:
|
| 41 |
+
# fallback: plain white
|
| 42 |
+
bg_img = Image.new("RGB", (720, 480), color=(255, 255, 255))
|
| 43 |
+
|
| 44 |
+
bg_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").name
|
| 45 |
+
bg_img.save(bg_path)
|
| 46 |
+
|
| 47 |
+
# Make video
|
| 48 |
+
clip = ImageClip(bg_path).set_duration(15) # 15 sec video
|
| 49 |
+
audioclip = AudioFileClip(temp_audio.name)
|
| 50 |
+
final = clip.set_audio(audioclip)
|
| 51 |
+
|
| 52 |
+
out_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
| 53 |
+
final.write_videofile(out_path, fps=24)
|
| 54 |
+
|
| 55 |
+
return out_path
|
| 56 |
+
|
| 57 |
+
# -----------------------------------------------------
|
| 58 |
+
# Gradio Interface
|
| 59 |
+
# -----------------------------------------------------
|
| 60 |
+
with gr.Blocks() as demo:
|
| 61 |
+
gr.Markdown("## ✨ Future Journal & Affirmation App with AI Visuals ✨")
|
| 62 |
+
|
| 63 |
+
with gr.Row():
|
| 64 |
+
prompt_in = gr.Textbox(label="Journal Prompt / Affirmation")
|
| 65 |
+
affirmation_in = gr.Textbox(label="Extra Affirmation (optional)")
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
visuals_in = gr.Checkbox(label="Add AI Visuals (Stable Diffusion)", value=True)
|
| 69 |
+
|
| 70 |
+
generate_btn = gr.Button("🎬 Generate Video")
|
| 71 |
+
|
| 72 |
+
output_video = gr.Video(label="Generated Video")
|
| 73 |
+
|
| 74 |
+
def run(prompt, affirmation, visuals):
|
| 75 |
+
return generate_video(prompt, affirmation, visuals)
|
| 76 |
+
|
| 77 |
+
generate_btn.click(run, [prompt_in, affirmation_in, visuals_in], output_video)
|
| 78 |
+
|
| 79 |
+
# Launch
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
demo.launch()
|