fullstuckdev commited on
Commit
fe7b9d3
Β·
1 Parent(s): 2916fd2

update script

Browse files
Files changed (3) hide show
  1. app.py +72 -4
  2. config.py +15 -0
  3. requirements.txt +7 -0
app.py CHANGED
@@ -1,7 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
1
+ import os
2
+
3
+ if os.environ.get("SPACES_ZERO_GPU") is not None:
4
+ import spaces
5
+ else:
6
+ class spaces:
7
+ @staticmethod
8
+ def GPU(func):
9
+ def wrapper(*args, **kwargs):
10
+ return func(*args, **kwargs)
11
+
12
+ return wrapper
13
+
14
+ import torch
15
+ from diffusers import MochiPipeline
16
+ from diffusers.utils import export_to_video
17
  import gradio as gr
18
+ import config as cfg
19
+
20
+ # Load the pre-trained model
21
+ pipe = MochiPipeline.from_pretrained(cfg.MODEL_PRE_TRAINED_ID, variant="bf16", torch_dtype=torch.bfloat16)
22
+
23
+ # Enable memory-saving optimizations
24
+ pipe.enable_model_cpu_offload()
25
+ pipe.enable_vae_tiling()
26
+
27
+
28
+ @spaces.GPU(duration=240)
29
+ def generate_video(prompt, num_frames=84, fps=30, high_quality=False):
30
+ if high_quality:
31
+ print("High quality option selected. Requires 42GB VRAM.")
32
+ # Check if running on ZeroGPU
33
+ if os.environ.get("SPACES_ZERO_GPU") is not None:
34
+ raise RuntimeError("High quality option may fail on ZeroGPU environments.")
35
+ with torch.autocast("cuda", torch.bfloat16, cache_enabled=False):
36
+ frames = pipe(prompt, num_frames=num_frames).frames[0]
37
+ else:
38
+ print("Standard quality option selected.")
39
+ frames = pipe(prompt, num_frames=num_frames).frames[0]
40
+
41
+ # Export frames as video
42
+ video_path = "mochi.mp4"
43
+ export_to_video(frames, video_path, fps=fps)
44
+ return video_path
45
+
46
+
47
+ # Create the Gradio interface
48
+ interface = gr.Interface(
49
+ fn=generate_video,
50
+ inputs=[
51
+ gr.Textbox(lines=2, placeholder="Enter your text prompt here... πŸ’‘"),
52
+ gr.Slider(minimum=1, maximum=240, value=84, label="Number of frames 🎞️"),
53
+ gr.Slider(minimum=1, maximum=60, value=30, label="FPS (Frames per second) ⏱️"),
54
+ gr.Checkbox(label="High Quality Output (requires 42GB VRAM, may fail on ZeroGPU)")
55
+ ],
56
+ outputs=gr.Video(),
57
+ title=cfg.TITLE,
58
+ description=cfg.DESCRIPTION,
59
+ examples=cfg.EXAMPLES,
60
+ article=cfg.BUY_ME_A_COFFE
61
+ )
62
 
63
+ # Center the title and description using custom CSS
64
+ interface.css = """
65
+ .interface-title {
66
+ text-align: center;
67
+ }
68
+ .interface-description {
69
+ text-align: center;
70
+ }
71
+ """
72
 
73
+ # Launch the application
74
+ if __name__ == "__main__":
75
+ interface.launch()
config.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ TITLE = "Mochi 1 - Video Generator 🌟"
2
+
3
+ DESCRIPTION = """
4
+ ✨ Generate a video based on a text prompt using the Mochi-1-preview.
5
+ """
6
+
7
+ MODEL_PRE_TRAINED_ID = "genmo/mochi-1-preview"
8
+
9
+ EXAMPLES = [
10
+ ["A majestic dragon soaring over a fiery volcano during a lunar eclipse, casting long shadows on the ancient ruins below. The ground trembles as molten lava flows down the mountainside, and the air is thick with ash and smoke.", 70, 24],
11
+ ["A bustling steampunk city with towering airships navigating the skies, clockwork robots performing various tasks, and intricate networks of pipes and gears running through every building. The streets are filled with people in Victorian attire, and the sky is a perpetual haze of smoke and steam.", 60, 30],
12
+ ["A magical forest filled with luminescent creatures that glow softly in the dim light, ancient ruins covered in mystical runes, and a crystal-clear river flowing under the light of a full moon. The trees are tall and twisted, their branches intertwined to form a canopy that filters the moonlight.", 50, 25],
13
+ ["An underwater city inhabited by mermaids, surrounded by vibrant coral reefs and schools of colorful fish. The buildings are constructed from seashells and coral, with bioluminescent plants lighting the streets. The water is clear and teeming with marine life, creating a serene and otherworldly atmosphere.", 65, 20],
14
+ ["A futuristic space station orbiting a distant exoplanet, equipped with massive solar panels and docking bays filled with various starships. The observation decks offer breathtaking views of the galaxy, with nearby nebulas and distant stars twinkling in the vast expanse of space.", 75, 30]
15
+ ]
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