ozilion commited on
Commit
a0415fd
Β·
verified Β·
1 Parent(s): 761f93c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +178 -4
app.py CHANGED
@@ -1,6 +1,180 @@
1
  import gradio as gr
 
 
 
 
2
 
3
- gr.load(
4
- "models/tencent/HunyuanVideo",
5
- provider="fal-ai",
6
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import os
3
+ import torch
4
+ import gc
5
+ from typing import Optional
6
 
7
+ # Check if we're running on Hugging Face Spaces
8
+ IS_SPACES = os.environ.get("SPACE_ID") is not None
9
+
10
+ def check_gpu_memory():
11
+ """Check available GPU memory"""
12
+ if torch.cuda.is_available():
13
+ return torch.cuda.get_device_properties(0).total_memory / 1024**3
14
+ return 0
15
+
16
+ def load_model():
17
+ """Load the HunyuanVideo model with error handling"""
18
+ try:
19
+ # For Hugging Face Spaces, we need to be careful with memory
20
+ if IS_SPACES:
21
+ print("Running on Hugging Face Spaces")
22
+ gpu_memory = check_gpu_memory()
23
+ print(f"Available GPU memory: {gpu_memory:.1f} GB")
24
+
25
+ # Try to load the model
26
+ from transformers import AutoModel, AutoTokenizer
27
+
28
+ model_name = "tencent/HunyuanVideo"
29
+
30
+ # Use CPU if no GPU or limited memory
31
+ device = "cuda" if torch.cuda.is_available() and check_gpu_memory() > 8 else "cpu"
32
+ print(f"Using device: {device}")
33
+
34
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
35
+
36
+ # Load model with appropriate settings for Spaces
37
+ model = AutoModel.from_pretrained(
38
+ model_name,
39
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32,
40
+ device_map="auto" if device == "cuda" else None,
41
+ low_cpu_mem_usage=True
42
+ )
43
+
44
+ return model, tokenizer, device
45
+
46
+ except Exception as e:
47
+ print(f"Error loading model: {e}")
48
+ return None, None, "cpu"
49
+
50
+ # Initialize model
51
+ MODEL, TOKENIZER, DEVICE = load_model()
52
+
53
+ def generate_video(prompt: str, duration: int = 5, resolution: str = "512x512") -> str:
54
+ """Generate video from text prompt"""
55
+
56
+ if MODEL is None:
57
+ return "❌ Model not loaded. This might be due to memory limitations on Hugging Face Spaces."
58
+
59
+ try:
60
+ # Clear GPU cache if using CUDA
61
+ if DEVICE == "cuda":
62
+ torch.cuda.empty_cache()
63
+ gc.collect()
64
+
65
+ # Parse resolution
66
+ width, height = map(int, resolution.split('x'))
67
+
68
+ # Basic validation
69
+ if not prompt.strip():
70
+ return "❌ Please enter a valid prompt."
71
+
72
+ if duration < 1 or duration > 10:
73
+ return "❌ Duration must be between 1-10 seconds."
74
+
75
+ # This is where you would implement the actual video generation
76
+ # For now, return a placeholder message
77
+ return f"""
78
+ βœ… Video generation request processed:
79
+
80
+ πŸ“ Prompt: {prompt}
81
+ ⏱️ Duration: {duration} seconds
82
+ πŸ“ Resolution: {resolution}
83
+ πŸ–₯️ Device: {DEVICE}
84
+
85
+ Note: Actual video generation implementation needed.
86
+ The model is loaded and ready for inference.
87
+ """
88
+
89
+ except Exception as e:
90
+ return f"❌ Error during generation: {str(e)}"
91
+
92
+ def get_system_info():
93
+ """Get system information for debugging"""
94
+ info = f"""
95
+ πŸ–₯️ **System Information:**
96
+ - Python: {os.sys.version.split()[0]}
97
+ - PyTorch: {torch.__version__}
98
+ - CUDA Available: {torch.cuda.is_available()}
99
+ - GPU Memory: {check_gpu_memory():.1f} GB
100
+ - Running on Spaces: {IS_SPACES}
101
+ - Device: {DEVICE}
102
+ - Model Loaded: {'βœ…' if MODEL is not None else '❌'}
103
+ """
104
+ return info
105
+
106
+ # Create Gradio interface
107
+ with gr.Blocks(title="HunyuanVideo Generator", theme=gr.themes.Soft()) as demo:
108
+
109
+ gr.Markdown("# 🎬 HunyuanVideo Text-to-Video Generator")
110
+ gr.Markdown("Generate videos from text descriptions using the HunyuanVideo model.")
111
+
112
+ with gr.Tab("Generate Video"):
113
+ with gr.Row():
114
+ with gr.Column(scale=1):
115
+ prompt_input = gr.Textbox(
116
+ label="πŸ“ Video Description",
117
+ placeholder="A cat playing with a ball of yarn in a sunny garden...",
118
+ lines=3,
119
+ max_lines=5
120
+ )
121
+
122
+ with gr.Row():
123
+ duration_slider = gr.Slider(
124
+ minimum=1,
125
+ maximum=10,
126
+ value=5,
127
+ step=1,
128
+ label="⏱️ Duration (seconds)"
129
+ )
130
+
131
+ resolution_dropdown = gr.Dropdown(
132
+ choices=["256x256", "512x512", "768x768", "1024x1024"],
133
+ value="512x512",
134
+ label="πŸ“ Resolution"
135
+ )
136
+
137
+ generate_btn = gr.Button("🎬 Generate Video", variant="primary", size="lg")
138
+
139
+ with gr.Column(scale=1):
140
+ output_text = gr.Textbox(
141
+ label="πŸ“‹ Output",
142
+ lines=10,
143
+ show_copy_button=True
144
+ )
145
+
146
+ # Event handler
147
+ generate_btn.click(
148
+ fn=generate_video,
149
+ inputs=[prompt_input, duration_slider, resolution_dropdown],
150
+ outputs=output_text
151
+ )
152
+
153
+ # Example prompts
154
+ gr.Examples(
155
+ examples=[
156
+ ["A beautiful sunset over a calm ocean with gentle waves", 5, "512x512"],
157
+ ["A cat gracefully jumping between rooftops in a medieval town", 7, "768x768"],
158
+ ["Cherry blossoms falling in a Japanese garden", 4, "512x512"],
159
+ ["A spacecraft flying through a colorful nebula", 8, "1024x1024"]
160
+ ],
161
+ inputs=[prompt_input, duration_slider, resolution_dropdown]
162
+ )
163
+
164
+ with gr.Tab("System Info"):
165
+ info_button = gr.Button("πŸ” Check System Info")
166
+ info_output = gr.Markdown()
167
+
168
+ info_button.click(
169
+ fn=get_system_info,
170
+ outputs=info_output
171
+ )
172
+
173
+ # Launch the app
174
+ if __name__ == "__main__":
175
+ demo.launch(
176
+ share=False, # Hugging Face Spaces handles sharing
177
+ server_name="0.0.0.0", # Important for Spaces
178
+ server_port=7860, # Default port for Spaces
179
+ show_error=True
180
+ )