Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,18 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
|
|
3 |
import torch
|
4 |
from diffusers import DiffusionPipeline
|
5 |
|
6 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
7 |
-
model_repo_id = "stabilityai/sdxl-turbo" #
|
|
|
8 |
if torch.cuda.is_available():
|
9 |
torch_dtype = torch.float16
|
10 |
else:
|
11 |
torch_dtype = torch.float32
|
12 |
|
13 |
-
# Изначально загружаем модель по умолчанию
|
14 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
15 |
pipe = pipe.to(device)
|
16 |
|
@@ -30,17 +32,15 @@ def infer(
|
|
30 |
):
|
31 |
global model_repo_id, pipe
|
32 |
|
33 |
-
#
|
34 |
if model != model_repo_id:
|
35 |
try:
|
36 |
-
# Пробуем загрузить новую модель
|
37 |
new_pipe = DiffusionPipeline.from_pretrained(model, torch_dtype=torch_dtype)
|
38 |
new_pipe = new_pipe.to(device)
|
39 |
-
# Если успешно, то обновляем pipe и модель
|
40 |
pipe = new_pipe
|
41 |
model_repo_id = model
|
42 |
except Exception as e:
|
43 |
-
raise gr.Error(f"Не удалось загрузить модель {model}
|
44 |
|
45 |
generator = torch.Generator(device=device).manual_seed(seed)
|
46 |
|
@@ -69,17 +69,17 @@ css = """
|
|
69 |
}
|
70 |
"""
|
71 |
|
|
|
72 |
with gr.Blocks(css=css) as demo:
|
73 |
with gr.Column(elem_id="col-container"):
|
74 |
gr.Markdown(" # Text-to-Image App")
|
75 |
|
76 |
-
# Вместо выпадающего списка — текстовое поле для ввода модели
|
77 |
model = gr.Textbox(
|
78 |
-
label="Model
|
79 |
value="stabilityai/sdxl-turbo", # Значение по умолчанию
|
80 |
interactive=True
|
81 |
)
|
82 |
-
|
83 |
prompt = gr.Text(
|
84 |
label="Prompt",
|
85 |
show_label=False,
|
@@ -102,7 +102,7 @@ with gr.Blocks(css=css) as demo:
|
|
102 |
step=1,
|
103 |
value=42,
|
104 |
)
|
105 |
-
|
106 |
guidance_scale = gr.Slider(
|
107 |
label="Guidance scale",
|
108 |
minimum=0.0,
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
+
import random
|
4 |
import torch
|
5 |
from diffusers import DiffusionPipeline
|
6 |
|
7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
8 |
+
model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
|
9 |
+
|
10 |
if torch.cuda.is_available():
|
11 |
torch_dtype = torch.float16
|
12 |
else:
|
13 |
torch_dtype = torch.float32
|
14 |
|
15 |
+
# Изначально загружаем модель по умолчанию (как в исходном коде)
|
16 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
17 |
pipe = pipe.to(device)
|
18 |
|
|
|
32 |
):
|
33 |
global model_repo_id, pipe
|
34 |
|
35 |
+
# Если пользователь ввёл новую модель, пробуем загрузить
|
36 |
if model != model_repo_id:
|
37 |
try:
|
|
|
38 |
new_pipe = DiffusionPipeline.from_pretrained(model, torch_dtype=torch_dtype)
|
39 |
new_pipe = new_pipe.to(device)
|
|
|
40 |
pipe = new_pipe
|
41 |
model_repo_id = model
|
42 |
except Exception as e:
|
43 |
+
raise gr.Error(f"Не удалось загрузить модель '{model}'.\nОшибка: {e}")
|
44 |
|
45 |
generator = torch.Generator(device=device).manual_seed(seed)
|
46 |
|
|
|
69 |
}
|
70 |
"""
|
71 |
|
72 |
+
# Убрали выпадающее меню, заменили на текстовое поле
|
73 |
with gr.Blocks(css=css) as demo:
|
74 |
with gr.Column(elem_id="col-container"):
|
75 |
gr.Markdown(" # Text-to-Image App")
|
76 |
|
|
|
77 |
model = gr.Textbox(
|
78 |
+
label="Model (repo or local path)",
|
79 |
value="stabilityai/sdxl-turbo", # Значение по умолчанию
|
80 |
interactive=True
|
81 |
)
|
82 |
+
|
83 |
prompt = gr.Text(
|
84 |
label="Prompt",
|
85 |
show_label=False,
|
|
|
102 |
step=1,
|
103 |
value=42,
|
104 |
)
|
105 |
+
|
106 |
guidance_scale = gr.Slider(
|
107 |
label="Guidance scale",
|
108 |
minimum=0.0,
|