Update app.py
Browse files
app.py
CHANGED
|
@@ -1,154 +1,495 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
-
|
| 5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
| 6 |
-
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
-
"A
|
|
|
|
| 58 |
]
|
| 59 |
|
|
|
|
| 60 |
css = """
|
| 61 |
#col-container {
|
| 62 |
margin: 0 auto;
|
| 63 |
-
max-width:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
"""
|
| 66 |
|
| 67 |
-
|
|
|
|
| 68 |
with gr.Column(elem_id="col-container"):
|
| 69 |
-
gr.Markdown("
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
|
|
|
|
|
|
|
|
|
| 4 |
import torch
|
| 5 |
+
import gc
|
| 6 |
+
from typing import Optional, Tuple
|
| 7 |
+
import spaces # For ZeroGPU support
|
| 8 |
+
|
| 9 |
+
# Image generation models
|
| 10 |
+
from diffusers import (
|
| 11 |
+
DiffusionPipeline, StableDiffusionPipeline,
|
| 12 |
+
StableDiffusionXLPipeline, AutoPipelineForText2Image,
|
| 13 |
+
AnimateDiffPipeline, DiffusionPipeline as VideoPipeline
|
| 14 |
+
)
|
| 15 |
|
| 16 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 17 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 18 |
+
MAX_IMAGE_SIZE = 1024
|
| 19 |
|
| 20 |
+
# Model configurations optimized for different hardware
|
| 21 |
+
MODEL_CONFIGS = {
|
| 22 |
+
"Image Models": {
|
| 23 |
+
"SDXL-Turbo (Fast)": {
|
| 24 |
+
"repo_id": "stabilityai/sdxl-turbo",
|
| 25 |
+
"pipeline_class": "auto",
|
| 26 |
+
"cpu_friendly": True,
|
| 27 |
+
"vram_usage": "Low",
|
| 28 |
+
"default_steps": 2,
|
| 29 |
+
"default_guidance": 0.0,
|
| 30 |
+
"torch_dtype": torch.float16 if torch.cuda.is_available() else torch.float32
|
| 31 |
+
},
|
| 32 |
+
"SD 1.5 (CPU Optimized)": {
|
| 33 |
+
"repo_id": "runwayml/stable-diffusion-v1-5",
|
| 34 |
+
"pipeline_class": "sd15",
|
| 35 |
+
"cpu_friendly": True,
|
| 36 |
+
"vram_usage": "Low",
|
| 37 |
+
"default_steps": 20,
|
| 38 |
+
"default_guidance": 7.5,
|
| 39 |
+
"torch_dtype": torch.float32
|
| 40 |
+
},
|
| 41 |
+
"SD 2.1 (Balanced)": {
|
| 42 |
+
"repo_id": "stabilityai/stable-diffusion-2-1",
|
| 43 |
+
"pipeline_class": "sd21",
|
| 44 |
+
"cpu_friendly": False,
|
| 45 |
+
"vram_usage": "Medium",
|
| 46 |
+
"default_steps": 25,
|
| 47 |
+
"default_guidance": 7.5,
|
| 48 |
+
"torch_dtype": torch.float16 if torch.cuda.is_available() else torch.float32
|
| 49 |
+
},
|
| 50 |
+
"SDXL Base (High Quality)": {
|
| 51 |
+
"repo_id": "stabilityai/stable-diffusion-xl-base-1.0",
|
| 52 |
+
"pipeline_class": "sdxl",
|
| 53 |
+
"cpu_friendly": False,
|
| 54 |
+
"vram_usage": "High",
|
| 55 |
+
"default_steps": 30,
|
| 56 |
+
"default_guidance": 7.5,
|
| 57 |
+
"torch_dtype": torch.float16 if torch.cuda.is_available() else torch.float32
|
| 58 |
+
}
|
| 59 |
+
},
|
| 60 |
+
"Video Models": {
|
| 61 |
+
"AnimateDiff (Motion)": {
|
| 62 |
+
"repo_id": "guoyww/animatediff-motion-adapter-v1-5-2",
|
| 63 |
+
"pipeline_class": "animatediff",
|
| 64 |
+
"cpu_friendly": False,
|
| 65 |
+
"vram_usage": "High",
|
| 66 |
+
"default_steps": 25,
|
| 67 |
+
"default_guidance": 7.5,
|
| 68 |
+
"torch_dtype": torch.float16 if torch.cuda.is_available() else torch.float32
|
| 69 |
+
},
|
| 70 |
+
"Zeroscope v2 (Text-to-Video)": {
|
| 71 |
+
"repo_id": "cerspense/zeroscope_v2_576w",
|
| 72 |
+
"pipeline_class": "video",
|
| 73 |
+
"cpu_friendly": False,
|
| 74 |
+
"vram_usage": "Very High",
|
| 75 |
+
"default_steps": 40,
|
| 76 |
+
"default_guidance": 9.0,
|
| 77 |
+
"torch_dtype": torch.float16 if torch.cuda.is_available() else torch.float32
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
}
|
| 81 |
|
| 82 |
+
# Global pipeline cache
|
| 83 |
+
current_pipeline = None
|
| 84 |
+
current_model_name = None
|
| 85 |
|
| 86 |
+
def clear_pipeline():
|
| 87 |
+
"""Clear current pipeline to free memory"""
|
| 88 |
+
global current_pipeline
|
| 89 |
+
if current_pipeline is not None:
|
| 90 |
+
del current_pipeline
|
| 91 |
+
current_pipeline = None
|
| 92 |
+
gc.collect()
|
| 93 |
+
if torch.cuda.is_available():
|
| 94 |
+
torch.cuda.empty_cache()
|
| 95 |
|
| 96 |
+
def get_pipeline_class(pipeline_type: str):
|
| 97 |
+
"""Get the appropriate pipeline class"""
|
| 98 |
+
if pipeline_type == "auto":
|
| 99 |
+
return AutoPipelineForText2Image
|
| 100 |
+
elif pipeline_type == "sd15":
|
| 101 |
+
return StableDiffusionPipeline
|
| 102 |
+
elif pipeline_type == "sd21":
|
| 103 |
+
return StableDiffusionPipeline
|
| 104 |
+
elif pipeline_type == "sdxl":
|
| 105 |
+
return StableDiffusionXLPipeline
|
| 106 |
+
elif pipeline_type == "animatediff":
|
| 107 |
+
return AnimateDiffPipeline
|
| 108 |
+
elif pipeline_type == "video":
|
| 109 |
+
return VideoPipeline
|
| 110 |
+
else:
|
| 111 |
+
return DiffusionPipeline
|
| 112 |
|
| 113 |
+
def load_model(model_name: str, model_type: str = "Image Models"):
|
| 114 |
+
"""Load a model with memory optimization"""
|
| 115 |
+
global current_pipeline, current_model_name
|
| 116 |
+
|
| 117 |
+
if current_model_name == model_name and current_pipeline is not None:
|
| 118 |
+
return current_pipeline
|
| 119 |
+
|
| 120 |
+
# Clear previous model
|
| 121 |
+
clear_pipeline()
|
| 122 |
+
|
| 123 |
+
config = MODEL_CONFIGS[model_type][model_name]
|
| 124 |
+
pipeline_class = get_pipeline_class(config["pipeline_class"])
|
| 125 |
+
|
| 126 |
+
try:
|
| 127 |
+
# Load with optimizations
|
| 128 |
+
pipe = pipeline_class.from_pretrained(
|
| 129 |
+
config["repo_id"],
|
| 130 |
+
torch_dtype=config["torch_dtype"],
|
| 131 |
+
use_safetensors=True,
|
| 132 |
+
variant="fp16" if torch.cuda.is_available() and config["torch_dtype"] == torch.float16 else None
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# Apply optimizations based on hardware
|
| 136 |
+
if torch.cuda.is_available():
|
| 137 |
+
pipe = pipe.to(device)
|
| 138 |
+
# Enable memory efficient attention
|
| 139 |
+
if hasattr(pipe, 'enable_attention_slicing'):
|
| 140 |
+
pipe.enable_attention_slicing()
|
| 141 |
+
if hasattr(pipe, 'enable_xformers_memory_efficient_attention'):
|
| 142 |
+
try:
|
| 143 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 144 |
+
except:
|
| 145 |
+
pass
|
| 146 |
+
else:
|
| 147 |
+
pipe = pipe.to(device)
|
| 148 |
+
# CPU optimizations
|
| 149 |
+
if hasattr(pipe, 'enable_attention_slicing'):
|
| 150 |
+
pipe.enable_attention_slicing()
|
| 151 |
+
|
| 152 |
+
current_pipeline = pipe
|
| 153 |
+
current_model_name = model_name
|
| 154 |
+
return pipe
|
| 155 |
+
|
| 156 |
+
except Exception as e:
|
| 157 |
+
return f"Error loading model: {str(e)}"
|
| 158 |
|
| 159 |
+
@spaces.GPU(duration=60) # ZeroGPU support
|
| 160 |
+
def generate_image(
|
| 161 |
+
model_name: str,
|
| 162 |
+
prompt: str,
|
| 163 |
+
negative_prompt: str,
|
| 164 |
+
seed: int,
|
| 165 |
+
randomize_seed: bool,
|
| 166 |
+
width: int,
|
| 167 |
+
height: int,
|
| 168 |
+
guidance_scale: float,
|
| 169 |
+
num_inference_steps: int,
|
| 170 |
+
progress=gr.Progress(track_tqdm=True),
|
| 171 |
+
) -> Tuple[Optional[np.ndarray], int, str]:
|
| 172 |
+
|
| 173 |
+
if not prompt.strip():
|
| 174 |
+
return None, seed, "Please enter a prompt"
|
| 175 |
+
|
| 176 |
+
try:
|
| 177 |
+
# Load model
|
| 178 |
+
pipe = load_model(model_name, "Image Models")
|
| 179 |
+
if isinstance(pipe, str): # Error message
|
| 180 |
+
return None, seed, pipe
|
| 181 |
+
|
| 182 |
+
# Handle seed
|
| 183 |
+
if randomize_seed:
|
| 184 |
+
seed = random.randint(0, MAX_SEED)
|
| 185 |
+
|
| 186 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 187 |
+
|
| 188 |
+
# Adjust parameters for CPU
|
| 189 |
+
if device == "cpu":
|
| 190 |
+
width = min(width, 512)
|
| 191 |
+
height = min(height, 512)
|
| 192 |
+
num_inference_steps = min(num_inference_steps, 20)
|
| 193 |
+
|
| 194 |
+
# Generate image
|
| 195 |
+
with torch.autocast(device):
|
| 196 |
+
result = pipe(
|
| 197 |
+
prompt=prompt,
|
| 198 |
+
negative_prompt=negative_prompt if negative_prompt.strip() else None,
|
| 199 |
+
guidance_scale=guidance_scale,
|
| 200 |
+
num_inference_steps=num_inference_steps,
|
| 201 |
+
width=width,
|
| 202 |
+
height=height,
|
| 203 |
+
generator=generator,
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
image = result.images[0]
|
| 207 |
+
return image, seed, "✅ Image generated successfully!"
|
| 208 |
+
|
| 209 |
+
except Exception as e:
|
| 210 |
+
error_msg = f"❌ Generation failed: {str(e)}"
|
| 211 |
+
if "out of memory" in str(e).lower():
|
| 212 |
+
error_msg += "\n💡 Try: Lower resolution, fewer steps, or use a CPU-friendly model"
|
| 213 |
+
return None, seed, error_msg
|
| 214 |
|
| 215 |
+
@spaces.GPU(duration=120) # Longer duration for video
|
| 216 |
+
def generate_video(
|
| 217 |
+
model_name: str,
|
| 218 |
+
prompt: str,
|
| 219 |
+
negative_prompt: str,
|
| 220 |
+
seed: int,
|
| 221 |
+
randomize_seed: bool,
|
| 222 |
+
num_frames: int,
|
| 223 |
+
guidance_scale: float,
|
| 224 |
+
num_inference_steps: int,
|
| 225 |
+
progress=gr.Progress(track_tqdm=True),
|
| 226 |
+
) -> Tuple[Optional[str], int, str]:
|
| 227 |
+
|
| 228 |
+
if not prompt.strip():
|
| 229 |
+
return None, seed, "Please enter a prompt"
|
| 230 |
+
|
| 231 |
+
if device == "cpu":
|
| 232 |
+
return None, seed, "❌ Video generation requires GPU"
|
| 233 |
+
|
| 234 |
+
try:
|
| 235 |
+
# Load model
|
| 236 |
+
pipe = load_model(model_name, "Video Models")
|
| 237 |
+
if isinstance(pipe, str): # Error message
|
| 238 |
+
return None, seed, pipe
|
| 239 |
+
|
| 240 |
+
# Handle seed
|
| 241 |
+
if randomize_seed:
|
| 242 |
+
seed = random.randint(0, MAX_SEED)
|
| 243 |
+
|
| 244 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 245 |
+
|
| 246 |
+
# Generate video
|
| 247 |
+
with torch.autocast(device):
|
| 248 |
+
if "animatediff" in model_name.lower():
|
| 249 |
+
result = pipe(
|
| 250 |
+
prompt=prompt,
|
| 251 |
+
negative_prompt=negative_prompt if negative_prompt.strip() else None,
|
| 252 |
+
num_frames=num_frames,
|
| 253 |
+
guidance_scale=guidance_scale,
|
| 254 |
+
num_inference_steps=num_inference_steps,
|
| 255 |
+
generator=generator,
|
| 256 |
+
)
|
| 257 |
+
# Save as GIF
|
| 258 |
+
video_path = "output_video.gif"
|
| 259 |
+
result.export_to_gif(video_path)
|
| 260 |
+
else:
|
| 261 |
+
result = pipe(
|
| 262 |
+
prompt=prompt,
|
| 263 |
+
negative_prompt=negative_prompt if negative_prompt.strip() else None,
|
| 264 |
+
num_frames=num_frames,
|
| 265 |
+
guidance_scale=guidance_scale,
|
| 266 |
+
num_inference_steps=num_inference_steps,
|
| 267 |
+
generator=generator,
|
| 268 |
+
)
|
| 269 |
+
# Save as MP4
|
| 270 |
+
video_path = "output_video.mp4"
|
| 271 |
+
result.export_to_video(video_path)
|
| 272 |
+
|
| 273 |
+
return video_path, seed, "✅ Video generated successfully!"
|
| 274 |
+
|
| 275 |
+
except Exception as e:
|
| 276 |
+
error_msg = f"❌ Video generation failed: {str(e)}"
|
| 277 |
+
if "out of memory" in str(e).lower():
|
| 278 |
+
error_msg += "\n💡 Try: Fewer frames, lower steps, or switch to image generation"
|
| 279 |
+
return None, seed, error_msg
|
| 280 |
|
| 281 |
+
def get_model_defaults(model_name: str, model_type: str):
|
| 282 |
+
"""Get default values for selected model"""
|
| 283 |
+
if model_type in MODEL_CONFIGS and model_name in MODEL_CONFIGS[model_type]:
|
| 284 |
+
config = MODEL_CONFIGS[model_type][model_name]
|
| 285 |
+
return config["default_steps"], config["default_guidance"]
|
| 286 |
+
return 20, 7.5
|
| 287 |
|
| 288 |
+
# Example prompts
|
| 289 |
+
image_examples = [
|
| 290 |
+
"A majestic dragon flying over a mystical forest, detailed, 8k",
|
| 291 |
+
"Cyberpunk cityscape at night, neon lights, futuristic",
|
| 292 |
+
"Portrait of a wise old wizard with glowing eyes",
|
| 293 |
+
"Serene mountain lake at sunset, photorealistic"
|
| 294 |
+
]
|
| 295 |
|
| 296 |
+
video_examples = [
|
| 297 |
+
"A cat walking through a magical garden",
|
| 298 |
+
"Ocean waves crashing on a beach at sunset",
|
| 299 |
+
"A butterfly flying around colorful flowers",
|
| 300 |
+
"Clouds moving across a blue sky"
|
| 301 |
]
|
| 302 |
|
| 303 |
+
# CSS for better styling
|
| 304 |
css = """
|
| 305 |
#col-container {
|
| 306 |
margin: 0 auto;
|
| 307 |
+
max-width: 900px;
|
| 308 |
+
}
|
| 309 |
+
.model-info {
|
| 310 |
+
padding: 10px;
|
| 311 |
+
margin: 5px 0;
|
| 312 |
+
border-radius: 5px;
|
| 313 |
+
background-color: #f0f0f0;
|
| 314 |
+
}
|
| 315 |
+
.status-success {
|
| 316 |
+
color: #28a745;
|
| 317 |
+
}
|
| 318 |
+
.status-error {
|
| 319 |
+
color: #dc3545;
|
| 320 |
}
|
| 321 |
"""
|
| 322 |
|
| 323 |
+
# Main Gradio interface
|
| 324 |
+
with gr.Blocks(css=css, title="Multi-Model AI Generator") as demo:
|
| 325 |
with gr.Column(elem_id="col-container"):
|
| 326 |
+
gr.Markdown("# 🎨 Multi-Model AI Generator")
|
| 327 |
+
gr.Markdown("Generate images and videos using various AI models optimized for different hardware configurations.")
|
| 328 |
+
|
| 329 |
+
# Hardware info
|
| 330 |
+
hardware_info = f"🖥️ **Device**: {device.upper()}"
|
| 331 |
+
if torch.cuda.is_available():
|
| 332 |
+
gpu_name = torch.cuda.get_device_name(0)
|
| 333 |
+
vram_gb = torch.cuda.get_device_properties(0).total_memory / 1e9
|
| 334 |
+
hardware_info += f" ({gpu_name}, {vram_gb:.1f}GB VRAM)"
|
| 335 |
+
gr.Markdown(hardware_info)
|
| 336 |
+
|
| 337 |
+
with gr.Tabs():
|
| 338 |
+
# IMAGE GENERATION TAB
|
| 339 |
+
with gr.TabItem("🖼️ Image Generation"):
|
| 340 |
+
with gr.Row():
|
| 341 |
+
with gr.Column(scale=3):
|
| 342 |
+
img_prompt = gr.Text(
|
| 343 |
+
label="Prompt",
|
| 344 |
+
placeholder="Describe the image you want to generate...",
|
| 345 |
+
lines=2
|
| 346 |
+
)
|
| 347 |
+
with gr.Column(scale=1):
|
| 348 |
+
img_generate_btn = gr.Button("🎨 Generate Image", variant="primary", size="lg")
|
| 349 |
+
|
| 350 |
+
with gr.Row():
|
| 351 |
+
with gr.Column(scale=2):
|
| 352 |
+
img_model_dropdown = gr.Dropdown(
|
| 353 |
+
choices=list(MODEL_CONFIGS["Image Models"].keys()),
|
| 354 |
+
value="SDXL-Turbo (Fast)",
|
| 355 |
+
label="Model",
|
| 356 |
+
info="Choose based on your hardware capabilities"
|
| 357 |
+
)
|
| 358 |
+
|
| 359 |
+
# Model info display
|
| 360 |
+
img_model_info = gr.Markdown("", elem_classes="model-info")
|
| 361 |
+
|
| 362 |
+
with gr.Column(scale=3):
|
| 363 |
+
img_result = gr.Image(label="Generated Image", height=400)
|
| 364 |
+
|
| 365 |
+
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 366 |
+
with gr.Row():
|
| 367 |
+
img_negative_prompt = gr.Text(
|
| 368 |
+
label="Negative Prompt",
|
| 369 |
+
placeholder="What you don't want in the image...",
|
| 370 |
+
lines=1
|
| 371 |
+
)
|
| 372 |
+
|
| 373 |
+
with gr.Row():
|
| 374 |
+
img_seed = gr.Slider(0, MAX_SEED, value=0, label="Seed")
|
| 375 |
+
img_randomize_seed = gr.Checkbox(label="Random Seed", value=True)
|
| 376 |
+
|
| 377 |
+
with gr.Row():
|
| 378 |
+
img_width = gr.Slider(256, MAX_IMAGE_SIZE, value=512, step=64, label="Width")
|
| 379 |
+
img_height = gr.Slider(256, MAX_IMAGE_SIZE, value=512, step=64, label="Height")
|
| 380 |
+
|
| 381 |
+
with gr.Row():
|
| 382 |
+
img_guidance = gr.Slider(0.0, 20.0, value=7.5, step=0.5, label="Guidance Scale")
|
| 383 |
+
img_steps = gr.Slider(1, 50, value=20, step=1, label="Inference Steps")
|
| 384 |
+
|
| 385 |
+
img_status = gr.Markdown("Ready to generate!", elem_classes="status-success")
|
| 386 |
+
gr.Examples(examples=image_examples, inputs=[img_prompt])
|
| 387 |
+
|
| 388 |
+
# VIDEO GENERATION TAB
|
| 389 |
+
with gr.TabItem("🎬 Video Generation"):
|
| 390 |
+
with gr.Row():
|
| 391 |
+
with gr.Column(scale=3):
|
| 392 |
+
vid_prompt = gr.Text(
|
| 393 |
+
label="Prompt",
|
| 394 |
+
placeholder="Describe the video you want to generate...",
|
| 395 |
+
lines=2
|
| 396 |
+
)
|
| 397 |
+
with gr.Column(scale=1):
|
| 398 |
+
vid_generate_btn = gr.Button("🎬 Generate Video", variant="primary", size="lg")
|
| 399 |
+
|
| 400 |
+
with gr.Row():
|
| 401 |
+
with gr.Column(scale=2):
|
| 402 |
+
vid_model_dropdown = gr.Dropdown(
|
| 403 |
+
choices=list(MODEL_CONFIGS["Video Models"].keys()),
|
| 404 |
+
value="AnimateDiff (Motion)",
|
| 405 |
+
label="Model",
|
| 406 |
+
info="Video generation requires GPU"
|
| 407 |
+
)
|
| 408 |
+
|
| 409 |
+
vid_model_info = gr.Markdown("", elem_classes="model-info")
|
| 410 |
+
|
| 411 |
+
with gr.Column(scale=3):
|
| 412 |
+
vid_result = gr.Video(label="Generated Video", height=400)
|
| 413 |
+
|
| 414 |
+
with gr.Accordion("⚙️ Video Settings", open=False):
|
| 415 |
+
with gr.Row():
|
| 416 |
+
vid_negative_prompt = gr.Text(
|
| 417 |
+
label="Negative Prompt",
|
| 418 |
+
placeholder="What you don't want in the video...",
|
| 419 |
+
lines=1
|
| 420 |
+
)
|
| 421 |
+
|
| 422 |
+
with gr.Row():
|
| 423 |
+
vid_seed = gr.Slider(0, MAX_SEED, value=0, label="Seed")
|
| 424 |
+
vid_randomize_seed = gr.Checkbox(label="Random Seed", value=True)
|
| 425 |
+
|
| 426 |
+
with gr.Row():
|
| 427 |
+
vid_frames = gr.Slider(8, 64, value=16, step=8, label="Number of Frames")
|
| 428 |
+
vid_guidance = gr.Slider(1.0, 20.0, value=7.5, step=0.5, label="Guidance Scale")
|
| 429 |
+
vid_steps = gr.Slider(10, 50, value=25, step=1, label="Inference Steps")
|
| 430 |
+
|
| 431 |
+
vid_status = gr.Markdown("Ready to generate!", elem_classes="status-success")
|
| 432 |
+
gr.Examples(examples=video_examples, inputs=[vid_prompt])
|
| 433 |
+
|
| 434 |
+
# Model info update functions
|
| 435 |
+
def update_img_model_info(model_name):
|
| 436 |
+
config = MODEL_CONFIGS["Image Models"][model_name]
|
| 437 |
+
info = f"""
|
| 438 |
+
**VRAM Usage**: {config['vram_usage']} | **CPU Friendly**: {'✅' if config['cpu_friendly'] else '❌'}
|
| 439 |
+
|
| 440 |
+
**Recommended Settings**: {config['default_steps']} steps, {config['default_guidance']} guidance
|
| 441 |
+
"""
|
| 442 |
+
steps, guidance = get_model_defaults(model_name, "Image Models")
|
| 443 |
+
return info, steps, guidance
|
| 444 |
+
|
| 445 |
+
def update_vid_model_info(model_name):
|
| 446 |
+
config = MODEL_CONFIGS["Video Models"][model_name]
|
| 447 |
+
info = f"""
|
| 448 |
+
**VRAM Usage**: {config['vram_usage']} | **CPU Friendly**: {'✅' if config['cpu_friendly'] else '❌'}
|
| 449 |
+
|
| 450 |
+
**Recommended Settings**: {config['default_steps']} steps, {config['default_guidance']} guidance
|
| 451 |
+
"""
|
| 452 |
+
steps, guidance = get_model_defaults(model_name, "Video Models")
|
| 453 |
+
return info, steps, guidance
|
| 454 |
+
|
| 455 |
+
# Event handlers
|
| 456 |
+
img_model_dropdown.change(
|
| 457 |
+
update_img_model_info,
|
| 458 |
+
inputs=[img_model_dropdown],
|
| 459 |
+
outputs=[img_model_info, img_steps, img_guidance]
|
| 460 |
+
)
|
| 461 |
+
|
| 462 |
+
vid_model_dropdown.change(
|
| 463 |
+
update_vid_model_info,
|
| 464 |
+
inputs=[vid_model_dropdown],
|
| 465 |
+
outputs=[vid_model_info, vid_steps, vid_guidance]
|
| 466 |
+
)
|
| 467 |
+
|
| 468 |
+
# Generation event handlers
|
| 469 |
+
img_generate_btn.click(
|
| 470 |
+
generate_image,
|
| 471 |
+
inputs=[
|
| 472 |
+
img_model_dropdown, img_prompt, img_negative_prompt,
|
| 473 |
+
img_seed, img_randomize_seed, img_width, img_height,
|
| 474 |
+
img_guidance, img_steps
|
| 475 |
+
],
|
| 476 |
+
outputs=[img_result, img_seed, img_status]
|
| 477 |
+
)
|
| 478 |
+
|
| 479 |
+
vid_generate_btn.click(
|
| 480 |
+
generate_video,
|
| 481 |
+
inputs=[
|
| 482 |
+
vid_model_dropdown, vid_prompt, vid_negative_prompt,
|
| 483 |
+
vid_seed, vid_randomize_seed, vid_frames,
|
| 484 |
+
vid_guidance, vid_steps
|
| 485 |
+
],
|
| 486 |
+
outputs=[vid_result, vid_seed, vid_status]
|
| 487 |
+
)
|
| 488 |
|
| 489 |
if __name__ == "__main__":
|
| 490 |
+
demo.launch(
|
| 491 |
+
share=True,
|
| 492 |
+
server_name="0.0.0.0",
|
| 493 |
+
server_port=7860,
|
| 494 |
+
show_error=True
|
| 495 |
+
)
|