Spaces:
Sleeping
Sleeping
updated main code to dynamically detect device
Browse files
media.py
CHANGED
|
@@ -1,152 +1,152 @@
|
|
| 1 |
-
# --- LIBRARIES ---
|
| 2 |
-
import torch
|
| 3 |
-
import gradio as gr
|
| 4 |
-
import random
|
| 5 |
-
import time
|
| 6 |
-
from diffusers import AutoPipelineForText2Image, TextToVideoSDPipeline
|
| 7 |
-
import gc
|
| 8 |
-
import os
|
| 9 |
-
import imageio
|
| 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 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
print(
|
| 92 |
-
yield {output_image:
|
| 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 |
demo.launch()
|
|
|
|
| 1 |
+
# --- LIBRARIES ---
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import random
|
| 5 |
+
import time
|
| 6 |
+
from diffusers import AutoPipelineForText2Image, TextToVideoSDPipeline
|
| 7 |
+
import gc
|
| 8 |
+
import os
|
| 9 |
+
import imageio
|
| 10 |
+
|
| 11 |
+
# --- DYNAMIC HARDWARE DETECTION (THE FIX) ---
|
| 12 |
+
# Check if a CUDA-enabled GPU is available, otherwise use the CPU
|
| 13 |
+
if torch.cuda.is_available():
|
| 14 |
+
device = "cuda"
|
| 15 |
+
torch_dtype = torch.float16 # Use float16 for GPU
|
| 16 |
+
print("✅ GPU detected. Using CUDA.")
|
| 17 |
+
else:
|
| 18 |
+
device = "cpu"
|
| 19 |
+
torch_dtype = torch.float32 # Use float32 for CPU
|
| 20 |
+
print("⚠️ No GPU detected. Using CPU. Performance will be slower.")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# --- AUTHENTICATION FOR HUGGING FACE SPACES ---
|
| 24 |
+
try:
|
| 25 |
+
from huggingface_hub import login
|
| 26 |
+
HF_TOKEN = os.environ.get('HF_TOKEN')
|
| 27 |
+
if HF_TOKEN:
|
| 28 |
+
login(token=HF_TOKEN)
|
| 29 |
+
print("✅ Hugging Face Authentication successful.")
|
| 30 |
+
else:
|
| 31 |
+
print("⚠️ Hugging Face token not found in Space Secrets. Gated models may not be available.")
|
| 32 |
+
except ImportError:
|
| 33 |
+
print("Could not import huggingface_hub. Please ensure it's in requirements.txt")
|
| 34 |
+
|
| 35 |
+
# --- CONFIGURATION & STATE ---
|
| 36 |
+
available_models = {
|
| 37 |
+
"Fast Image (SDXL Turbo)": "stabilityai/sdxl-turbo",
|
| 38 |
+
"Quality Image (SDXL)": "stabilityai/stable-diffusion-xl-base-1.0",
|
| 39 |
+
"Video (Zeroscope)": "cerspense/zeroscope-v2-576w"
|
| 40 |
+
}
|
| 41 |
+
model_state = { "current_pipe": None, "loaded_model_name": None }
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# --- CORE GENERATION FUNCTION ---
|
| 45 |
+
def generate_media(model_key, prompt, negative_prompt, steps, cfg_scale, width, height, seed, num_frames):
|
| 46 |
+
if model_state.get("loaded_model_name") != model_key:
|
| 47 |
+
print(f"Switching to {model_key}. Unloading previous model...")
|
| 48 |
+
yield {status_textbox: f"Unloading previous model..."}
|
| 49 |
+
if model_state.get("current_pipe"):
|
| 50 |
+
del model_state["current_pipe"]
|
| 51 |
+
gc.collect()
|
| 52 |
+
if device == "cuda":
|
| 53 |
+
torch.cuda.empty_cache()
|
| 54 |
+
|
| 55 |
+
model_id = available_models[model_key]
|
| 56 |
+
print(f"Loading {model_id}...")
|
| 57 |
+
yield {status_textbox: f"Loading {model_id}... This can take a minute."}
|
| 58 |
+
|
| 59 |
+
# Adapt model loading based on hardware
|
| 60 |
+
if "Image" in model_key:
|
| 61 |
+
pipe = AutoPipelineForText2Image.from_pretrained(model_id, torch_dtype=torch_dtype, variant="fp16" if device == "cuda" else "fp32")
|
| 62 |
+
elif "Video" in model_key:
|
| 63 |
+
pipe = TextToVideoSDPipeline.from_pretrained(model_id, torch_dtype=torch_dtype)
|
| 64 |
+
|
| 65 |
+
# Move pipe to the detected device
|
| 66 |
+
pipe.to(device)
|
| 67 |
+
|
| 68 |
+
# CPU offloading only makes sense on a GPU setup
|
| 69 |
+
if device == "cuda" and "Turbo" not in model_key and "Video" not in model_key:
|
| 70 |
+
pipe.enable_model_cpu_offload()
|
| 71 |
+
|
| 72 |
+
model_state["current_pipe"] = pipe
|
| 73 |
+
model_state["loaded_model_name"] = model_key
|
| 74 |
+
print(f"✅ Model loaded successfully on {device.upper()}.")
|
| 75 |
+
|
| 76 |
+
pipe = model_state["current_pipe"]
|
| 77 |
+
generator = torch.Generator(device).manual_seed(seed)
|
| 78 |
+
yield {status_textbox: f"Generating with {model_key} on {device.upper()}..."}
|
| 79 |
+
|
| 80 |
+
if "Image" in model_key:
|
| 81 |
+
print("Generating image...")
|
| 82 |
+
if "Turbo" in model_key:
|
| 83 |
+
num_steps, guidance_scale = 1, 0.0
|
| 84 |
+
else:
|
| 85 |
+
num_steps, guidance_scale = int(steps), float(cfg_scale)
|
| 86 |
+
|
| 87 |
+
image = pipe(
|
| 88 |
+
prompt=prompt, negative_prompt=negative_prompt, num_inference_steps=num_steps,
|
| 89 |
+
guidance_scale=guidance_scale, width=int(width), height=int(height), generator=generator
|
| 90 |
+
).images[0]
|
| 91 |
+
print("✅ Image generation complete.")
|
| 92 |
+
yield {output_image: image, output_video: None, status_textbox: f"Seed used: {seed}"}
|
| 93 |
+
|
| 94 |
+
elif "Video" in model_key:
|
| 95 |
+
print("Generating video...")
|
| 96 |
+
video_frames = pipe(prompt=prompt, num_inference_steps=int(steps), height=320, width=576, num_frames=int(num_frames), generator=generator).frames
|
| 97 |
+
|
| 98 |
+
video_path = f"/tmp/video_{seed}.mp4"
|
| 99 |
+
imageio.mimsave(video_path, video_frames, fps=12)
|
| 100 |
+
print(f"✅ Video saved to {video_path}")
|
| 101 |
+
yield {output_image: None, output_video: video_path, status_textbox: f"Seed used: {seed}"}
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
# --- GRADIO USER INTERFACE (No changes needed here) ---
|
| 105 |
+
with gr.Blocks(theme='gradio/soft') as demo:
|
| 106 |
+
gr.Markdown("# The Generative Media Suite")
|
| 107 |
+
# ... (rest of the UI code is identical to before)
|
| 108 |
+
gr.Markdown("Create fast images, high-quality images, or short videos. Created by cheeseman182.")
|
| 109 |
+
seed_state = gr.State(-1)
|
| 110 |
+
with gr.Row():
|
| 111 |
+
with gr.Column(scale=2):
|
| 112 |
+
model_selector = gr.Radio(label="Select Model", choices=list(available_models.keys()), value=list(available_models.keys())[0])
|
| 113 |
+
prompt_input = gr.Textbox(label="Prompt", lines=4, placeholder="An astronaut riding a horse on Mars, cinematic...")
|
| 114 |
+
negative_prompt_input = gr.Textbox(label="Negative Prompt", lines=2, value="ugly, blurry, deformed, watermark, text")
|
| 115 |
+
with gr.Accordion("Settings", open=True):
|
| 116 |
+
steps_slider = gr.Slider(1, 100, 30, step=1, label="Inference Steps")
|
| 117 |
+
cfg_slider = gr.Slider(0.0, 15.0, 7.5, step=0.5, label="Guidance Scale (CFG)")
|
| 118 |
+
with gr.Row():
|
| 119 |
+
width_slider = gr.Slider(256, 1024, 768, step=64, label="Width")
|
| 120 |
+
height_slider = gr.Slider(256, 1024, 768, step=64, label="Height")
|
| 121 |
+
num_frames_slider = gr.Slider(12, 48, 24, step=4, label="Video Frames", visible=False)
|
| 122 |
+
seed_input = gr.Number(-1, label="Seed (-1 for random)")
|
| 123 |
+
generate_button = gr.Button("Generate", variant="primary")
|
| 124 |
+
with gr.Column(scale=3):
|
| 125 |
+
output_image = gr.Image(label="Image Result", interactive=False, height="60vh", visible=True)
|
| 126 |
+
output_video = gr.Video(label="Video Result", interactive=False, height="60vh", visible=False)
|
| 127 |
+
status_textbox = gr.Textbox(label="Status", interactive=False)
|
| 128 |
+
def update_ui_on_model_change(model_key):
|
| 129 |
+
is_video = "Video" in model_key
|
| 130 |
+
is_turbo = "Turbo" in model_key
|
| 131 |
+
return {
|
| 132 |
+
steps_slider: gr.update(interactive=not is_turbo, value=1 if is_turbo else 30),
|
| 133 |
+
cfg_slider: gr.update(interactive=not is_turbo, value=0.0 if is_turbo else 7.5),
|
| 134 |
+
width_slider: gr.update(visible=not is_video),
|
| 135 |
+
height_slider: gr.update(visible=not is_video),
|
| 136 |
+
num_frames_slider: gr.update(visible=is_video),
|
| 137 |
+
output_image: gr.update(visible=not is_video),
|
| 138 |
+
output_video: gr.update(visible=is_video)
|
| 139 |
+
}
|
| 140 |
+
model_selector.change(update_ui_on_model_change, model_selector, [steps_slider, cfg_slider, width_slider, height_slider, num_frames_slider, output_image, output_video])
|
| 141 |
+
click_event = generate_button.click(
|
| 142 |
+
fn=lambda s: (s if s != -1 else random.randint(0, 2**32 - 1)),
|
| 143 |
+
inputs=seed_input,
|
| 144 |
+
outputs=seed_state,
|
| 145 |
+
queue=False
|
| 146 |
+
).then(
|
| 147 |
+
fn=generate_media,
|
| 148 |
+
inputs=[model_selector, prompt_input, negative_prompt_input, steps_slider, cfg_slider, width_slider, height_slider, seed_state, num_frames_slider],
|
| 149 |
+
outputs=[output_image, output_video, status_textbox]
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
demo.launch()
|