Spaces:
Runtime error
Runtime error
Deploy Gradio app with multiple files
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from utils import generate_video
|
| 3 |
+
import spaces
|
| 4 |
+
import os
|
| 5 |
+
from huggingface_hub import login
|
| 6 |
+
|
| 7 |
+
# Login to Hugging Face (users need to provide their token)
|
| 8 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 9 |
+
if hf_token:
|
| 10 |
+
login(hf_token)
|
| 11 |
+
else:
|
| 12 |
+
raise ValueError("Please set HF_TOKEN environment variable for model access")
|
| 13 |
+
|
| 14 |
+
# Compile the model with ZeroGPU AoT for speed
|
| 15 |
+
from utils import compile_model
|
| 16 |
+
compiled_model = compile_model()
|
| 17 |
+
|
| 18 |
+
@spaces.GPU(duration=120) # Allow time for video generation
|
| 19 |
+
def generate(prompt: str, user: str):
|
| 20 |
+
# Generate video from prompt
|
| 21 |
+
video_path = generate_video(prompt, compiled_model)
|
| 22 |
+
# Save to user's account (placeholder - integrate with HF Hub or database)
|
| 23 |
+
return video_path
|
| 24 |
+
|
| 25 |
+
with gr.Blocks(title="Fast Text-to-Video Generator", theme=gr.themes.Soft()) as demo:
|
| 26 |
+
gr.HTML("""
|
| 27 |
+
<h1 style='text-align: center'>Fast Text-to-Video Generator</h1>
|
| 28 |
+
<p style='text-align: center'>Login to your account to generate personalized videos. Built with <a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank'>anycoder</a>.</p>
|
| 29 |
+
""")
|
| 30 |
+
|
| 31 |
+
login_btn = gr.LoginButton("Sign in with Hugging Face")
|
| 32 |
+
|
| 33 |
+
with gr.Row(visible=False) as main_ui:
|
| 34 |
+
prompt_input = gr.Textbox(label="Enter your text prompt", placeholder="A cat playing in a garden")
|
| 35 |
+
generate_btn = gr.Button("Generate Video")
|
| 36 |
+
video_output = gr.Video(label="Generated Video")
|
| 37 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 38 |
+
|
| 39 |
+
def show_ui():
|
| 40 |
+
return gr.Row(visible=True)
|
| 41 |
+
|
| 42 |
+
def create_video(prompt, request: gr.Request):
|
| 43 |
+
if not request.username:
|
| 44 |
+
return None, "Please login first"
|
| 45 |
+
status = "Generating video... (this is very fast!)"
|
| 46 |
+
video = generate(prompt, request.username)
|
| 47 |
+
status = "Video generated successfully!"
|
| 48 |
+
return video, status
|
| 49 |
+
|
| 50 |
+
login_btn.click(show_ui, outputs=main_ui)
|
| 51 |
+
generate_btn.click(create_video, inputs=[prompt_input], outputs=[video_output, status])
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
demo.launch()
|
config.py
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MODEL_ID = "stabilityai/stable-video-diffusion-img2vid-xt"
|
| 2 |
+
HF_TOKEN_ENV = "HF_TOKEN"
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
torch
|
| 3 |
+
diffusers
|
| 4 |
+
accelerate
|
| 5 |
+
transformers
|
| 6 |
+
huggingface-hub
|
| 7 |
+
imageio
|
| 8 |
+
spaces
|
utils.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from diffusers import StableVideoDiffusionPipeline
|
| 3 |
+
from diffusers.utils import load_image
|
| 4 |
+
import spaces
|
| 5 |
+
|
| 6 |
+
def compile_model():
|
| 7 |
+
# Load the model
|
| 8 |
+
model_id = "stabilityai/stable-video-diffusion-img2vid-xt"
|
| 9 |
+
pipe = StableVideoDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, variant="fp16")
|
| 10 |
+
pipe.to('cuda')
|
| 11 |
+
|
| 12 |
+
@spaces.GPU(duration=1500) # AoT compilation
|
| 13 |
+
def compile_transformer():
|
| 14 |
+
# Capture example inputs
|
| 15 |
+
with spaces.aoti_capture(pipe.unet) as call:
|
| 16 |
+
image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/svd/rocket.png")
|
| 17 |
+
pipe(image).frames
|
| 18 |
+
|
| 19 |
+
# Export and compile
|
| 20 |
+
exported = torch.export.export(
|
| 21 |
+
pipe.unet,
|
| 22 |
+
args=call.args,
|
| 23 |
+
kwargs=call.kwargs,
|
| 24 |
+
)
|
| 25 |
+
return spaces.aoti_compile(exported)
|
| 26 |
+
|
| 27 |
+
compiled_unet = compile_transformer()
|
| 28 |
+
spaces.aoti_apply(compiled_unet, pipe.unet)
|
| 29 |
+
return pipe
|
| 30 |
+
|
| 31 |
+
def generate_video(prompt: str, pipe):
|
| 32 |
+
# For simplicity, use a placeholder image; in real app, generate image from text first
|
| 33 |
+
image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/svd/rocket.png") # Placeholder
|
| 34 |
+
|
| 35 |
+
# Generate video
|
| 36 |
+
frames = pipe(image, decode_chunk_size=8).frames[0]
|
| 37 |
+
|
| 38 |
+
# Save as video (placeholder path)
|
| 39 |
+
import imageio
|
| 40 |
+
video_path = f"/tmp/generated_video_{hash(prompt)}.mp4"
|
| 41 |
+
imageio.mimsave(video_path, frames, fps=7)
|
| 42 |
+
|
| 43 |
+
return video_path
|