Spaces:
Runtime error
Runtime error
#!/usr/bin/env python | |
import os | |
import random | |
import uuid | |
import gradio as gr | |
import numpy as np | |
from PIL import Image | |
import spaces | |
import torch | |
from diffusers import StableDiffusionXLPipeline, KDPM2AncestralDiscreteScheduler, AutoencoderKL | |
DESCRIPTION = """ | |
# Proteus ```V0.1``` | |
Model by [dataautogpt3](https://huggingface.co/dataautogpt3) | |
Demo by [ehristoforu](https://huggingface.co/ehristoforu) | |
""" | |
if not torch.cuda.is_available(): | |
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>" | |
MAX_SEED = np.iinfo(np.int32).max | |
USE_TORCH_COMPILE = 0 | |
ENABLE_CPU_OFFLOAD = 0 | |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") | |
# Lista de modelos disponíveis | |
available_models = { | |
"animagine-xl-3.0": "cagliostrolab/animagine-xl-3.0", | |
# Adicione mais modelos conforme necessário | |
} | |
# Função para carregar o modelo escolhido | |
def load_model(model_name): | |
if model_name == "animagine-xl-3.0": | |
return StableDiffusionXLPipeline.from_pretrained(model_name, use_safetensors=True) | |
# Adicione mais modelos conforme necessário | |
else: | |
raise ValueError(f"Model '{model_name}' not recognized.") | |
# Parâmetros iniciais | |
selected_model_name = "animagine-xl-3.0" | |
pipe = load_model(selected_model_name) | |
# Dropdown para selecionar o modelo | |
model_dropdown = gr.Dropdown( | |
label="Select Model", | |
choices=list(available_models.keys()), | |
default=selected_model_name, | |
) | |
# Adicione o dropdown à UI | |
with gr.Blocks(title="Proteus V0.1", css=css) as demo: | |
gr.Markdown(DESCRIPTION) | |
gr.DuplicateButton( | |
value="Duplicate Space for private use", | |
elem_id="duplicate-button", | |
visible=False, | |
) | |
with gr.Group(): | |
with gr.Row(): | |
prompt = gr.Text( | |
label="Prompt", | |
show_label=False, | |
max_lines=1, | |
placeholder="Enter your prompt", | |
container=False, | |
) | |
run_button = gr.Button("Run", scale=0) | |
result = gr.Gallery(label="Result", columns=1, preview=True, show_label=False) | |
with gr.Accordion("Advanced options", open=False): | |
with gr.Row(): | |
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False) | |
negative_prompt = gr.Text( | |
label="Negative prompt", | |
max_lines=1, | |
placeholder="Enter a negative prompt", | |
visible=False, | |
) | |
seed = gr.Slider( | |
label="Seed", | |
minimum=0, | |
maximum=MAX_SEED, | |
step=1, | |
value=0, | |
visible=True | |
) | |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True) | |
with gr.Row(visible=True): | |
width = gr.Slider( | |
label="Width", | |
minimum=512, | |
maximum=1536, | |
step=8, | |
value=768, | |
) | |
height = gr.Slider( | |
label="Height", | |
minimum=512, | |
maximum=1536, | |
step=8, | |
value=768, | |
) | |
with gr.Row(): | |
guidance_scale = gr.Slider( | |
label="Guidance Scale", | |
minimum=0.1, | |
maximum=20, | |
step=0.1, | |
value=7.0, | |
) | |
# Adicione o dropdown ao início da seção avançada | |
with gr.Row(): | |
model_dropdown | |
# Restante do código... | |
# Função para gerar imagens | |
def generate( | |
prompt: str, | |
negative_prompt: str = "", | |
use_negative_prompt: bool = False, | |
seed: int = 0, | |
width: int = 1024, | |
height: int = 1024, | |
guidance_scale: float = 3, | |
randomize_seed: bool = False, | |
model_name: str = selected_model_name, # Novo parâmetro para o modelo | |
progress=gr.Progress(track_tqdm=True), | |
): | |
global pipe | |
pipe = load_model(model_name) | |
# Restante do código... | |
# Restante do código... | |
# Adicione a seleção do modelo à função de execução | |
gr.on( | |
triggers=[ | |
prompt.submit, | |
negative_prompt.submit, | |
run_button.click, | |
model_dropdown.change, # Adicione o dropdown de modelo como um trigger | |
], | |
fn=generate, | |
inputs=[ | |
prompt, | |
negative_prompt, | |
use_negative_prompt, | |
seed, | |
width, | |
height, | |
guidance_scale, | |
randomize_seed, | |
model_dropdown, # Adicione o dropdown de modelo como uma entrada | |
], | |
outputs=[result, seed], | |
api_name="run", | |
) | |
# ... Restante do código ... | |
# Lançamento da interface | |
if __name__ == "__main__": | |
demo.queue(max_size=20).launch(show_api=False, debug=False) |