Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,58 @@
|
|
| 1 |
-
import spaces
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
-
import argparse # Keep argparse, but we'll modify its use
|
| 5 |
import sys
|
| 6 |
import time
|
| 7 |
import os
|
| 8 |
import random
|
| 9 |
-
|
| 10 |
-
# Assuming your app.py is in the root of your cloned/forked repo.
|
| 11 |
sys.path.append(".") # Correct path for Hugging Face Space
|
| 12 |
from skyreelsinfer import TaskType
|
| 13 |
from skyreelsinfer.offload import OffloadConfig
|
| 14 |
from skyreelsinfer.skyreels_video_infer import SkyReelsVideoInfer
|
| 15 |
from diffusers.utils import export_to_video
|
| 16 |
from diffusers.utils import load_image
|
| 17 |
-
import torch
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction = False
|
| 21 |
-
torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction = False
|
| 22 |
-
torch.backends.cudnn.allow_tf32 = False
|
| 23 |
-
torch.backends.cudnn.deterministic = False
|
| 24 |
-
torch.backends.cudnn.benchmark = False
|
| 25 |
-
torch.set_float32_matmul_precision("highest")
|
| 26 |
-
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 27 |
|
| 28 |
-
# --- Model Loading
|
| 29 |
-
predictor = None
|
| 30 |
|
| 31 |
def get_transformer_model_id(task_type: str) -> str:
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
@spaces.GPU(duration=90)
|
| 35 |
def init_predictor(task_type: str):
|
| 36 |
global predictor
|
| 37 |
try:
|
| 38 |
predictor = SkyReelsVideoInfer(
|
| 39 |
task_type=TaskType.I2V if task_type == "i2v" else TaskType.T2V,
|
| 40 |
model_id=get_transformer_model_id(task_type),
|
| 41 |
-
quant_model=True,
|
| 42 |
-
|
| 43 |
-
is_offload=True, # Keep offload for CPU
|
| 44 |
offload_config=OffloadConfig(
|
| 45 |
high_cpu_memory=True,
|
| 46 |
parameters_level=True,
|
| 47 |
-
compiler_transformer=False, #
|
| 48 |
-
)
|
|
|
|
| 49 |
)
|
| 50 |
-
# Explicitly move the predictor to CPU (CRUCIAL)
|
| 51 |
-
if hasattr(predictor, 'pipe') and hasattr(predictor.pipe, 'to'): #check to make sure the predictor has a pipe and to() method
|
| 52 |
-
predictor.pipe.to("cpu")
|
| 53 |
return "Model loaded successfully!"
|
|
|
|
|
|
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
return f"Error loading model: {e}"
|
| 56 |
|
| 57 |
-
|
| 58 |
def generate_video(prompt, seed, image=None, task_type=None):
|
| 59 |
global predictor
|
| 60 |
|
| 61 |
-
# Input Type Validation
|
| 62 |
if task_type == "i2v" and not isinstance(image, str):
|
| 63 |
-
return "Error: For i2v,
|
| 64 |
if not isinstance(prompt, str) or not isinstance(seed, (int, float)):
|
| 65 |
-
return "Error: Invalid input types
|
| 66 |
-
|
| 67 |
|
| 68 |
if seed == -1:
|
| 69 |
random.seed(time.time())
|
|
@@ -71,14 +60,14 @@ def generate_video(prompt, seed, image=None, task_type=None):
|
|
| 71 |
|
| 72 |
kwargs = {
|
| 73 |
"prompt": prompt,
|
| 74 |
-
"height":
|
| 75 |
-
"width":
|
| 76 |
-
"num_frames":
|
| 77 |
-
"num_inference_steps":
|
| 78 |
-
"seed": int(seed),
|
| 79 |
"guidance_scale": 6.0,
|
| 80 |
"embedded_guidance_scale": 1.0,
|
| 81 |
-
"negative_prompt": "
|
| 82 |
"cfg_for": False,
|
| 83 |
}
|
| 84 |
|
|
@@ -88,35 +77,33 @@ def generate_video(prompt, seed, image=None, task_type=None):
|
|
| 88 |
try:
|
| 89 |
kwargs["image"] = load_image(image=image)
|
| 90 |
except Exception as e:
|
| 91 |
-
|
| 92 |
|
| 93 |
try:
|
| 94 |
-
#Ensure Predictor is Loaded
|
| 95 |
if predictor is None:
|
| 96 |
-
return "Error: Model not initialized.
|
| 97 |
|
| 98 |
output = predictor.inference(kwargs)
|
| 99 |
save_dir = f"./result/{task_type}"
|
| 100 |
os.makedirs(save_dir, exist_ok=True)
|
| 101 |
-
video_out_file = f"{save_dir}/{prompt[:100].replace('/','')}_{int(seed)}.mp4"
|
| 102 |
print(f"Generating video, local path: {video_out_file}")
|
| 103 |
export_to_video(output, video_out_file, fps=24)
|
| 104 |
-
return video_out_file, str(kwargs)
|
| 105 |
|
| 106 |
except Exception as e:
|
| 107 |
-
return f"Error during
|
| 108 |
|
| 109 |
# --- Gradio Interface ---
|
| 110 |
-
# We'll define a single interface that handles BOTH i2v and t2v
|
| 111 |
with gr.Blocks() as demo:
|
| 112 |
with gr.Row():
|
| 113 |
task_type_dropdown = gr.Dropdown(
|
| 114 |
choices=["i2v", "t2v"], label="Task Type", value="t2v"
|
| 115 |
-
)
|
| 116 |
load_model_button = gr.Button("Load Model")
|
| 117 |
model_status = gr.Textbox(label="Model Status")
|
| 118 |
with gr.Row():
|
| 119 |
-
with gr.Column():
|
| 120 |
prompt = gr.Textbox(label="Input Prompt")
|
| 121 |
seed = gr.Number(label="Random Seed", value=-1)
|
| 122 |
image = gr.Image(label="Upload Image (for i2v)", type="filepath")
|
|
@@ -125,20 +112,14 @@ with gr.Blocks() as demo:
|
|
| 125 |
output_video = gr.Video(label="Generated Video")
|
| 126 |
output_params = gr.Textbox(label="Output Parameters")
|
| 127 |
|
| 128 |
-
# Load Model Button Logic
|
| 129 |
load_model_button.click(
|
| 130 |
fn=init_predictor,
|
| 131 |
inputs=[task_type_dropdown],
|
| 132 |
outputs=[model_status]
|
| 133 |
)
|
| 134 |
|
| 135 |
-
# Submit Button Logic (Handles both i2v and t2v)
|
| 136 |
submit_button.click(
|
| 137 |
fn=generate_video,
|
| 138 |
-
inputs=[prompt, seed, image, task_type_dropdown],
|
| 139 |
outputs=[output_video, output_params],
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
# --- Launch the App ---
|
| 143 |
-
# No need for argparse in app.py for Hugging Face Spaces
|
| 144 |
-
demo.launch() # Don't use demo.launch() inside HuggingFace Spaces.
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import sys
|
| 3 |
import time
|
| 4 |
import os
|
| 5 |
import random
|
| 6 |
+
|
|
|
|
| 7 |
sys.path.append(".") # Correct path for Hugging Face Space
|
| 8 |
from skyreelsinfer import TaskType
|
| 9 |
from skyreelsinfer.offload import OffloadConfig
|
| 10 |
from skyreelsinfer.skyreels_video_infer import SkyReelsVideoInfer
|
| 11 |
from diffusers.utils import export_to_video
|
| 12 |
from diffusers.utils import load_image
|
| 13 |
+
import torch
|
| 14 |
+
from huggingface_hub import HfApi
|
| 15 |
+
from huggingface_hub.utils import RepositoryNotFoundError, RevisionNotFoundError, EntryNotFoundError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# --- Model Loading ---
|
| 18 |
+
predictor = None
|
| 19 |
|
| 20 |
def get_transformer_model_id(task_type: str) -> str:
|
| 21 |
+
if task_type == "i2v":
|
| 22 |
+
return "Skywork/skyreels-v1-Hunyuan-i2v"
|
| 23 |
+
else:
|
| 24 |
+
return "Skywork/skyreels-v1-Hunyuan-t2v"
|
| 25 |
|
|
|
|
| 26 |
def init_predictor(task_type: str):
|
| 27 |
global predictor
|
| 28 |
try:
|
| 29 |
predictor = SkyReelsVideoInfer(
|
| 30 |
task_type=TaskType.I2V if task_type == "i2v" else TaskType.T2V,
|
| 31 |
model_id=get_transformer_model_id(task_type),
|
| 32 |
+
quant_model=True,
|
| 33 |
+
is_offload=True,
|
|
|
|
| 34 |
offload_config=OffloadConfig(
|
| 35 |
high_cpu_memory=True,
|
| 36 |
parameters_level=True,
|
| 37 |
+
# compiler_transformer=False, # Keep this consistent
|
| 38 |
+
),
|
| 39 |
+
use_multiprocessing=False, # Still pass this, though it's ignored
|
| 40 |
)
|
|
|
|
|
|
|
|
|
|
| 41 |
return "Model loaded successfully!"
|
| 42 |
+
|
| 43 |
+
except (RepositoryNotFoundError, RevisionNotFoundError, EntryNotFoundError) as e:
|
| 44 |
+
return f"Error: Model not found. Details: {e}"
|
| 45 |
except Exception as e:
|
| 46 |
return f"Error loading model: {e}"
|
| 47 |
|
| 48 |
+
|
| 49 |
def generate_video(prompt, seed, image=None, task_type=None):
|
| 50 |
global predictor
|
| 51 |
|
|
|
|
| 52 |
if task_type == "i2v" and not isinstance(image, str):
|
| 53 |
+
return "Error: For i2v, provide a valid image file path.", "{}"
|
| 54 |
if not isinstance(prompt, str) or not isinstance(seed, (int, float)):
|
| 55 |
+
return "Error: Invalid input types.", "{}"
|
|
|
|
| 56 |
|
| 57 |
if seed == -1:
|
| 58 |
random.seed(time.time())
|
|
|
|
| 60 |
|
| 61 |
kwargs = {
|
| 62 |
"prompt": prompt,
|
| 63 |
+
"height": 256, # Reduced for faster CPU processing
|
| 64 |
+
"width": 256, # Reduced for faster CPU processing
|
| 65 |
+
"num_frames": 24, # Reduced for faster CPU processing
|
| 66 |
+
"num_inference_steps": 10, # Reduced for faster CPU processing
|
| 67 |
+
"seed": int(seed),
|
| 68 |
"guidance_scale": 6.0,
|
| 69 |
"embedded_guidance_scale": 1.0,
|
| 70 |
+
"negative_prompt": "bad quality", #shorter prompt
|
| 71 |
"cfg_for": False,
|
| 72 |
}
|
| 73 |
|
|
|
|
| 77 |
try:
|
| 78 |
kwargs["image"] = load_image(image=image)
|
| 79 |
except Exception as e:
|
| 80 |
+
return f"Error loading image: {e}", "{}"
|
| 81 |
|
| 82 |
try:
|
|
|
|
| 83 |
if predictor is None:
|
| 84 |
+
return "Error: Model not initialized.", "{}"
|
| 85 |
|
| 86 |
output = predictor.inference(kwargs)
|
| 87 |
save_dir = f"./result/{task_type}"
|
| 88 |
os.makedirs(save_dir, exist_ok=True)
|
| 89 |
+
video_out_file = f"{save_dir}/{prompt[:100].replace('/','')}_{int(seed)}.mp4"
|
| 90 |
print(f"Generating video, local path: {video_out_file}")
|
| 91 |
export_to_video(output, video_out_file, fps=24)
|
| 92 |
+
return video_out_file, str(kwargs)
|
| 93 |
|
| 94 |
except Exception as e:
|
| 95 |
+
return f"Error during generation: {e}", "{}"
|
| 96 |
|
| 97 |
# --- Gradio Interface ---
|
|
|
|
| 98 |
with gr.Blocks() as demo:
|
| 99 |
with gr.Row():
|
| 100 |
task_type_dropdown = gr.Dropdown(
|
| 101 |
choices=["i2v", "t2v"], label="Task Type", value="t2v"
|
| 102 |
+
)
|
| 103 |
load_model_button = gr.Button("Load Model")
|
| 104 |
model_status = gr.Textbox(label="Model Status")
|
| 105 |
with gr.Row():
|
| 106 |
+
with gr.Column():
|
| 107 |
prompt = gr.Textbox(label="Input Prompt")
|
| 108 |
seed = gr.Number(label="Random Seed", value=-1)
|
| 109 |
image = gr.Image(label="Upload Image (for i2v)", type="filepath")
|
|
|
|
| 112 |
output_video = gr.Video(label="Generated Video")
|
| 113 |
output_params = gr.Textbox(label="Output Parameters")
|
| 114 |
|
|
|
|
| 115 |
load_model_button.click(
|
| 116 |
fn=init_predictor,
|
| 117 |
inputs=[task_type_dropdown],
|
| 118 |
outputs=[model_status]
|
| 119 |
)
|
| 120 |
|
|
|
|
| 121 |
submit_button.click(
|
| 122 |
fn=generate_video,
|
| 123 |
+
inputs=[prompt, seed, image, task_type_dropdown],
|
| 124 |
outputs=[output_video, output_params],
|
| 125 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|