Spaces:
Sleeping

MarketingVideo / app.py
sanelyhusen's picture
Create app.py
18b9f25 verified
Raw History Blame Contribute Delete
9.13 kB
import gradio as gr
import torch
from diffusers import CogVideoXPipeline
from diffusers.utils import export_to_video
import os
import tempfile
import uuid
# ============================================
# DoThatShi Video Generator - Hugging Face Space
# ============================================
# Model configuration - Using CogVideoX (open source, free)
MODEL_ID = "THUDM/CogVideoX-2b" # 2B parameter version, lighter for free tier
# Custom CSS for DoThatShi branding
custom_css = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;800&display=swap');
.gradio-container {
font-family: 'Inter', sans-serif !important;
background: linear-gradient(135deg, #0f0f23 0%, #1a1a2e 50%, #16213e 100%) !important;
min-height: 100vh;
}
.header {
text-align: center;
padding: 2rem 0;
margin-bottom: 1rem;
}
.brand-title {
font-size: 3rem;
font-weight: 800;
background: linear-gradient(90deg, #ff6b6b, #feca57, #48dbfb, #ff9ff3);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
margin-bottom: 0.5rem;
}
.brand-subtitle {
color: #a0a0a0;
font-size: 1.1rem;
font-weight: 400;
}
.generate-btn {
background: linear-gradient(90deg, #ff6b6b, #feca57) !important;
color: #0f0f23 !important;
font-weight: 700 !important;
border: none !important;
border-radius: 12px !important;
padding: 12px 32px !important;
font-size: 1.1rem !important;
transition: all 0.3s ease !important;
}
.generate-btn:hover {
transform: translateY(-2px);
box-shadow: 0 8px 25px rgba(255, 107, 107, 0.4) !important;
}
.input-box {
border-radius: 12px !important;
border: 2px solid #2d2d44 !important;
background: #1a1a2e !important;
color: #ffffff !important;
}
.input-box:focus {
border-color: #ff6b6b !important;
box-shadow: 0 0 15px rgba(255, 107, 107, 0.3) !important;
}
.video-output {
border-radius: 16px !important;
overflow: hidden !important;
border: 2px solid #2d2d44 !important;
}
.settings-panel {
background: rgba(26, 26, 46, 0.8) !important;
border-radius: 16px !important;
padding: 1.5rem !important;
border: 1px solid #2d2d44 !important;
}
.footer {
text-align: center;
padding: 2rem;
color: #666;
font-size: 0.9rem;
}
.status-badge {
display: inline-block;
padding: 4px 12px;
border-radius: 20px;
font-size: 0.85rem;
font-weight: 600;
margin-top: 10px;
}
.status-ready {
background: rgba(72, 219, 251, 0.2);
color: #48dbfb;
border: 1px solid #48dbfb;
}
.status-generating {
background: rgba(254, 202, 87, 0.2);
color: #feca57;
border: 1px solid #feca57;
}
"""
# Global pipeline (loaded once)
pipe = None
def load_pipeline():
"""Load the video generation pipeline"""
global pipe
if pipe is None:
print("🎬 Loading DoThatShi Video Generator...")
pipe = CogVideoXPipeline.from_pretrained(
MODEL_ID,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
)
if torch.cuda.is_available():
pipe.to("cuda")
# Enable memory efficient attention
pipe.enable_sequential_cpu_offload()
else:
pipe.to("cpu")
print("⚠️ Running on CPU - generation will be slower")
return pipe
def generate_video(
prompt,
negative_prompt,
num_frames,
fps,
guidance_scale,
num_inference_steps,
seed,
width,
height
):
"""Generate video from text prompt"""
# Load pipeline
pipeline = load_pipeline()
# Set seed for reproducibility
if seed == -1:
seed = torch.randint(0, 2**32, (1,)).item()
generator = torch.Generator(device="cuda" if torch.cuda.is_available() else "cpu").manual_seed(seed)
# Generate video
print(f"🎨 Generating: {prompt[:50]}...")
video = pipeline(
prompt=prompt,
negative_prompt=negative_prompt,
num_frames=num_frames,
num_inference_steps=num_inference_steps,
guidance_scale=guidance_scale,
generator=generator,
width=width,
height=height,
).frames[0]
# Export to file
output_path = os.path.join(tempfile.gettempdir(), f"dothatshi_{uuid.uuid4().hex[:8]}.mp4")
export_to_video(video, output_path, fps=fps)
return output_path, f"βœ… Generated with seed: {seed}"
# ============================================
# Gradio Interface
# ============================================
with gr.Blocks(css=custom_css, title="DoThatShi Video Generator") as demo:
# Header
gr.HTML("""
<div class="header">
<h1 class="brand-title">DoThatShi</h1>
<p class="brand-subtitle">AI Video Generator β€” Create stunning videos from text</p>
<span class="status-badge status-ready">πŸš€ Ready to Generate</span>
</div>
""")
with gr.Row():
# Left Panel - Controls
with gr.Column(scale=1):
gr.HTML('<div class="settings-panel">')
prompt = gr.Textbox(
label="πŸ“ Your Video Prompt",
placeholder="A cinematic shot of a futuristic city at sunset, neon lights reflecting on wet streets...",
lines=4,
elem_classes=["input-box"]
)
negative_prompt = gr.Textbox(
label="🚫 Negative Prompt (what to avoid)",
value="blurry, low quality, distorted, ugly, deformed",
lines=2,
elem_classes=["input-box"]
)
with gr.Row():
width = gr.Slider(256, 1280, value=720, step=64, label="Width")
height = gr.Slider(256, 1280, value=480, step=64, label="Height")
with gr.Row():
num_frames = gr.Slider(8, 49, value=16, step=1, label="Frames")
fps = gr.Slider(1, 30, value=8, step=1, label="FPS")
with gr.Row():
guidance_scale = gr.Slider(1, 20, value=6, step=0.5, label="Guidance Scale")
num_inference_steps = gr.Slider(10, 100, value=50, step=5, label="Steps")
seed = gr.Number(value=-1, label="🎲 Seed (-1 for random)", precision=0)
generate_btn = gr.Button(
"✨ Generate Video",
variant="primary",
elem_classes=["generate-btn"]
)
gr.HTML('</div>')
# Right Panel - Output
with gr.Column(scale=1):
video_output = gr.Video(
label="🎬 Generated Video",
elem_classes=["video-output"],
autoplay=True
)
status_text = gr.Textbox(
label="Status",
interactive=False,
value="Click 'Generate Video' to create your first DoThatShi video!"
)
# Tips section
gr.HTML("""
<div style="margin-top: 20px; padding: 15px; background: rgba(255,107,107,0.1); border-radius: 12px; border: 1px solid rgba(255,107,107,0.3);">
<h4 style="color: #ff6b6b; margin-bottom: 10px;">πŸ’‘ Pro Tips for Better Videos</h4>
<ul style="color: #ccc; font-size: 0.9rem; line-height: 1.6;">
<li>Be specific: "cinematic 4K drone shot of..."</li>
<li>Add motion words: "rotating, flowing, walking"</li>
<li>Include lighting: "golden hour, neon, soft ambient"</li>
<li>Start with 16 frames for quick testing</li>
<li>Use 49 frames for final high-quality output</li>
</ul>
</div>
""")
# Examples
gr.Examples(
examples=[
["A serene Japanese garden with cherry blossoms falling in slow motion, soft pink petals dancing in the wind, golden sunlight filtering through", "blurry, low quality", 16, 8, 6, 50, -1, 720, 480],
["Cyberpunk street market at night, holographic signs, steam rising from food stalls, people in futuristic clothing walking by", "distorted, ugly", 24, 12, 7, 50, 42, 720, 480],
["Majestic eagle soaring over snow-capped mountains, dramatic clouds, cinematic wide shot, golden hour lighting", "blurry, dark", 16, 8, 6, 50, -1, 720, 480],
],
inputs=[prompt, negative_prompt, num_frames, fps, guidance_scale, num_inference_steps, seed, width, height],
label="🎯 Quick Start Examples"
)
# Footer
gr.HTML("""
<div class="footer">
<p>πŸ”₯ Powered by CogVideoX | Built for DoThatShi Brand</p>
<p style="font-size: 0.8rem; margin-top: 5px;">Free tier: CPU generation (~5-10 min/video) | Upgrade to GPU for 30x speed</p>
</div>
""")
# Event handler
generate_btn.click(
fn=generate_video,
inputs=[prompt, negative_prompt, num_frames, fps, guidance_scale, num_inference_steps, seed, width, height],
outputs=[video_output, status_text]
)
# Launch
if __name__ == "__main__":
demo.launch()