File size: 1,807 Bytes
99e2a15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
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
import gradio as gr
from diffusers import StableDiffusionPipeline
import torch

# Список моделей для вибору
IMAGE_MODELS = {
    "Stable Diffusion v1.5": "runwayml/stable-diffusion-v1-5",
    "Stable Diffusion XL Base": "stabilityai/stable-diffusion-xl-base-1.0",
    "Kandinsky 2.2": "kandinsky-community/kandinsky-2-2-decoder",
    "DreamShaper XL": "Lykon/DreamShaper"
}

# Кеш для завантажених пайплайнів
loaded_pipes = {}

def get_pipe(model_name):
    if model_name not in loaded_pipes:
        pipe = StableDiffusionPipeline.from_pretrained(
            IMAGE_MODELS[model_name],
            torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
        )
        pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
        loaded_pipes[model_name] = pipe
    return loaded_pipes[model_name]

def generate(prompt, model_name, steps, guidance):
    pipe = get_pipe(model_name)
    result = pipe(prompt, num_inference_steps=steps, guidance_scale=guidance)
    return result.images[0]

# Інтерфейс Gradio з вибором моделі та налаштуваннями якості
demo = gr.Interface(
    fn=generate,
    inputs=[
        gr.Textbox(label="Prompt", placeholder="Напиши опис сюди"),
        gr.Dropdown(choices=list(IMAGE_MODELS.keys()), value="Stable Diffusion XL Base", label="Модель"),
        gr.Slider(minimum=10, maximum=50, value=30, step=1, label="Кількість кроків (steps)"),
        gr.Slider(minimum=1.0, maximum=15.0, value=7.5, step=0.5, label="Guidance scale")
    ],
    outputs=gr.Image(label="Result"),
    title="AI Image Generator (Multi-Models + Quality Settings)"
)

demo.launch(ssr_mode=False)