Bexiiii's picture
Update app.py
87dc0e0 verified
import gradio as gr
import numpy as np
import random
import torch
from diffusers import DiffusionPipeline, AutoencoderKL
device = "cuda" if torch.cuda.is_available() else "cpu"
# Базовая модель (обязательно та же, на которой ты обучал LoRA)
base_model_id = "stabilityai/stable-diffusion-xl-base-1.0"
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
pipe = DiffusionPipeline.from_pretrained(
base_model_id,
vae=vae,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
use_safetensors=True,
variant="fp16"
)
# Загружаем твой LoRA
pipe.load_lora_weights("Bexiiii/Inspira_v_1")
pipe = pipe.to(device)
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 1024
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
if randomize_seed:
seed = random.randint(0, MAX_SEED)
generator = torch.Generator(device=device).manual_seed(seed)
image = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps,
width=width,
height=height,
generator=generator,
).images[0]
return image, seed
examples = [
"A creative Instagram ad for brand TOK, featuring a cute dog wearing stylish accessories",
"Ultra-detailed vibrant cinematic photo of a corgi mascot for Inspira",
]
with gr.Blocks() as demo:
with gr.Column():
gr.Markdown("# Inspira_v_1 — SDXL + LoRA")
with gr.Row():
prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt")
run_button = gr.Button("Generate")
result = gr.Image(label="Result")
seed = gr.Number(label="Seed", value=0)
with gr.Accordion("Advanced Settings", open=False):
negative_prompt = gr.Textbox(label="Negative prompt")
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
width = gr.Slider(256, MAX_IMAGE_SIZE, step=32, value=1024, label="Width")
height = gr.Slider(256, MAX_IMAGE_SIZE, step=32, value=1024, label="Height")
guidance_scale = gr.Slider(0, 10, step=0.1, value=7.5, label="Guidance scale")
num_inference_steps = gr.Slider(1, 50, step=1, value=30, label="Inference steps")
gr.Examples(examples=examples, inputs=[prompt])
run_button.click(
fn=infer,
inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
outputs=[result, seed]
)
if __name__ == "__main__":
demo.launch()