Spaces:
Running
on
Zero
Running
on
Zero
First commit
Browse files- README.md +4 -3
- app.py +136 -0
- requirements.txt +7 -0
README.md
CHANGED
|
@@ -4,10 +4,11 @@ emoji: π¨
|
|
| 4 |
colorFrom: purple
|
| 5 |
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.
|
| 8 |
app_file: app.py
|
| 9 |
-
pinned:
|
| 10 |
-
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 4 |
colorFrom: purple
|
| 5 |
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.5.0
|
| 8 |
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
short_description: Generate a video based on a text prompt using Mochi
|
| 12 |
---
|
| 13 |
|
| 14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install required packages
|
| 2 |
+
#!pip install accelerate torch gradio transformers git+https://github.com/huggingface/diffusers sentencepiece opencv-python
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Define a fallback for environments without GPU
|
| 7 |
+
if os.environ.get("SPACES_ZERO_GPU") is not None:
|
| 8 |
+
import spaces
|
| 9 |
+
else:
|
| 10 |
+
class spaces:
|
| 11 |
+
@staticmethod
|
| 12 |
+
def GPU(func):
|
| 13 |
+
def wrapper(*args, **kwargs):
|
| 14 |
+
return func(*args, **kwargs)
|
| 15 |
+
return wrapper
|
| 16 |
+
|
| 17 |
+
import torch
|
| 18 |
+
from diffusers import DiffusionPipeline
|
| 19 |
+
from diffusers.utils import export_to_video
|
| 20 |
+
import gradio as gr
|
| 21 |
+
|
| 22 |
+
# Application configuration
|
| 23 |
+
TITLE = "AI Video Generator π"
|
| 24 |
+
DESCRIPTION = """\
|
| 25 |
+
π Transform your imagination into stunning videos using advanced AI technology with Mochi-1-preview.\
|
| 26 |
+
Experience the magic of generative art! π₯
|
| 27 |
+
"""
|
| 28 |
+
BUY_ME_A_COFFEE = """
|
| 29 |
+
<a href="https://buymeacoffee.com/ruslanmv" target="_blank">
|
| 30 |
+
<button style="background-color: #FFDD00; border: none; color: black;
|
| 31 |
+
padding: 10px 20px; text-align: center;
|
| 32 |
+
text-decoration: none; display: inline-block;
|
| 33 |
+
font-size: 16px; margin: 4px 2px; cursor: pointer;
|
| 34 |
+
border-radius: 10px;">\
|
| 35 |
+
β Buy Me a Coffee
|
| 36 |
+
</button>
|
| 37 |
+
</a>
|
| 38 |
+
"""
|
| 39 |
+
MODEL_PRE_TRAINED_ID = "genmo/mochi-1-preview"
|
| 40 |
+
EXAMPLES = [
|
| 41 |
+
[
|
| 42 |
+
"A colossal griffin perched atop a crumbling gothic castle, its golden wings outstretched against a blood-red sunset. Below, a raging battle between knights and goblins unfolds amidst the ruins of a once-great city. The air is filled with the clash of steel, the cries of the wounded, and the roar of the griffin's echoing cry.",
|
| 43 |
+
90,
|
| 44 |
+
30,
|
| 45 |
+
],
|
| 46 |
+
[
|
| 47 |
+
"A serene mountaintop monastery above the clouds, with monks practicing \"\
|
| 48 |
+
Tai Chi at sunrise. The scene is filled with golden sunlight and \"\
|
| 49 |
+
swirling mist, as cherry blossoms fall gently in the breeze.",
|
| 50 |
+
70,
|
| 51 |
+
24,
|
| 52 |
+
],
|
| 53 |
+
[
|
| 54 |
+
"An enchanted meadow where unicorns graze among glowing wildflowers. \"\
|
| 55 |
+
Wisps of light float in the air, and a sparkling waterfall cascades into \"\
|
| 56 |
+
a crystal-clear pond surrounded by colorful butterflies.",
|
| 57 |
+
60,
|
| 58 |
+
25,
|
| 59 |
+
],
|
| 60 |
+
[
|
| 61 |
+
"A sprawling underwater utopia with bioluminescent architecture, giant \"\
|
| 62 |
+
jellyfish drifting gracefully, and schools of exotic fish weaving \"\
|
| 63 |
+
through coral tunnels. The city is alive with vibrant marine life.",
|
| 64 |
+
80,
|
| 65 |
+
30,
|
| 66 |
+
],
|
| 67 |
+
[
|
| 68 |
+
"A vast alien desert with shimmering sands of gold and silver, \"\
|
| 69 |
+
punctuated by colossal crystal spires. Twin suns set in the distance, \"\
|
| 70 |
+
casting long, surreal shadows across the dunes.",
|
| 71 |
+
75,
|
| 72 |
+
28,
|
| 73 |
+
],
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
# Load the pre-trained model
|
| 77 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 78 |
+
MODEL_PRE_TRAINED_ID, variant="bf16", torch_dtype=torch.bfloat16
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# Enable memory-saving optimizations
|
| 82 |
+
pipe.enable_model_cpu_offload()
|
| 83 |
+
pipe.enable_vae_tiling()
|
| 84 |
+
|
| 85 |
+
@spaces.GPU(duration=240)
|
| 86 |
+
def generate_video(prompt, num_frames=84, fps=30, high_quality=False):
|
| 87 |
+
"""Generate a video based on the input prompt."""
|
| 88 |
+
if high_quality:
|
| 89 |
+
print("High quality option selected. Requires 42GB VRAM.")
|
| 90 |
+
if os.environ.get("SPACES_ZERO_GPU") is not None:
|
| 91 |
+
raise RuntimeError("High quality option may fail on ZeroGPU environments.")
|
| 92 |
+
with torch.autocast("cuda", torch.bfloat16, cache_enabled=False):
|
| 93 |
+
frames = pipe(prompt, num_frames=num_frames).frames[0]
|
| 94 |
+
else:
|
| 95 |
+
print("Standard quality option selected.")
|
| 96 |
+
frames = pipe(prompt, num_frames=num_frames).frames[0]
|
| 97 |
+
|
| 98 |
+
video_path = "generated_video.mp4"
|
| 99 |
+
export_to_video(frames, video_path, fps=fps)
|
| 100 |
+
return video_path
|
| 101 |
+
|
| 102 |
+
# Define the Gradio interface
|
| 103 |
+
interface = gr.Interface(
|
| 104 |
+
fn=generate_video,
|
| 105 |
+
inputs=[
|
| 106 |
+
gr.Textbox(lines=2, placeholder="Enter a vivid text prompt... π"),
|
| 107 |
+
gr.Slider(minimum=1, maximum=240, value=84, label="Frames π₯"),
|
| 108 |
+
gr.Slider(minimum=1, maximum=60, value=30, label="FPS (Frames Per Second) β±"),
|
| 109 |
+
gr.Checkbox(label="High Quality (Requires 42GB VRAM) π "),
|
| 110 |
+
],
|
| 111 |
+
outputs=gr.Video(label="Generated Video"),
|
| 112 |
+
title=TITLE,
|
| 113 |
+
description=DESCRIPTION,
|
| 114 |
+
examples=EXAMPLES,
|
| 115 |
+
article=BUY_ME_A_COFFEE,
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# Apply custom CSS for better alignment
|
| 119 |
+
interface.css = """
|
| 120 |
+
.interface-title {
|
| 121 |
+
text-align: center;
|
| 122 |
+
font-size: 2em;
|
| 123 |
+
color: #4A90E2;
|
| 124 |
+
font-family: 'Arial', sans-serif;
|
| 125 |
+
}
|
| 126 |
+
.interface-description {
|
| 127 |
+
text-align: center;
|
| 128 |
+
font-size: 1.2em;
|
| 129 |
+
color: #333333;
|
| 130 |
+
margin-bottom: 20px;
|
| 131 |
+
}
|
| 132 |
+
"""
|
| 133 |
+
|
| 134 |
+
# Launch the Gradio application
|
| 135 |
+
if __name__ == "__main__":
|
| 136 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
accelerate
|
| 2 |
+
torch
|
| 3 |
+
gradio
|
| 4 |
+
transformers
|
| 5 |
+
git+https://github.com/huggingface/diffusers
|
| 6 |
+
sentencepiece
|
| 7 |
+
opencv-python
|