Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,51 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
4 |
-
from diffusers import DiffusionPipeline
|
5 |
import torch
|
6 |
-
import time
|
7 |
|
8 |
-
# Device
|
9 |
DEVICE = "cpu"
|
|
|
10 |
|
11 |
-
# Model Options (optimized for CPU)
|
12 |
MODEL_OPTIONS = {
|
13 |
"Medium Quality (Faster)": "stabilityai/stable-diffusion-2-base",
|
14 |
"Fastest (Draft Quality)": "hf-internal-testing/tiny-stable-diffusion-pipe",
|
15 |
}
|
16 |
|
17 |
-
# Default to fastest model
|
18 |
DEFAULT_MODEL_ID = MODEL_OPTIONS["Fastest (Draft Quality)"]
|
|
|
19 |
|
20 |
-
# Cache models to avoid reloading
|
21 |
PIPELINES = {}
|
22 |
|
23 |
def load_pipeline(model_id):
|
24 |
if model_id in PIPELINES:
|
25 |
return PIPELINES[model_id]
|
26 |
else:
|
27 |
-
pipe = DiffusionPipeline.from_pretrained(
|
28 |
-
|
29 |
-
)
|
30 |
-
pipe.scheduler = LMSDiscreteScheduler.from_config(pipe.scheduler.config)
|
31 |
-
pipe = pipe.to(DEVICE)
|
32 |
PIPELINES[model_id] = pipe
|
33 |
return pipe
|
34 |
|
35 |
def generate_image(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, num_images, model_choice):
|
36 |
if not prompt:
|
37 |
raise gr.Error("Будь ласка, введіть опис для зображення.")
|
|
|
38 |
|
39 |
pipe = load_pipeline(MODEL_OPTIONS[model_choice])
|
40 |
|
|
|
|
|
|
|
41 |
generator = torch.Generator(device=DEVICE)
|
42 |
if not randomize_seed:
|
43 |
generator = generator.manual_seed(seed)
|
44 |
|
45 |
-
start_time = time.time()
|
46 |
images = pipe(
|
47 |
prompt,
|
48 |
negative_prompt=negative_prompt,
|
@@ -50,17 +53,15 @@ def generate_image(prompt, negative_prompt, seed, randomize_seed, width, height,
|
|
50 |
height=height,
|
51 |
guidance_scale=guidance_scale,
|
52 |
num_inference_steps=num_inference_steps,
|
53 |
-
num_images_per_prompt=num_images,
|
|
|
54 |
generator=generator,
|
55 |
).images
|
56 |
|
57 |
end_time = time.time()
|
58 |
generation_time = end_time - start_time
|
59 |
|
60 |
-
return images, f"Час генерації: {generation_time:.2f} секунд"
|
61 |
-
|
62 |
-
|
63 |
-
# ... (Gradio UI remains largely the same, with an added status text output)
|
64 |
|
65 |
run_button = gr.Button("Згенерувати")
|
66 |
gallery = gr.Gallery(label="Згенеровані зображення")
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
4 |
+
from diffusers import DiffusionPipeline
|
5 |
import torch
|
6 |
+
import time
|
7 |
|
8 |
+
# Device and hardware configuration
|
9 |
DEVICE = "cpu"
|
10 |
+
NUM_CPU_CORES = 2
|
11 |
|
12 |
+
# Model Options (optimized for CPU and memory constraints)
|
13 |
MODEL_OPTIONS = {
|
14 |
"Medium Quality (Faster)": "stabilityai/stable-diffusion-2-base",
|
15 |
"Fastest (Draft Quality)": "hf-internal-testing/tiny-stable-diffusion-pipe",
|
16 |
}
|
17 |
|
18 |
+
# Default to fastest model and lower image size for limited resources
|
19 |
DEFAULT_MODEL_ID = MODEL_OPTIONS["Fastest (Draft Quality)"]
|
20 |
+
DEFAULT_IMAGE_SIZE = 512 # Lower default resolution
|
21 |
|
22 |
+
# Cache models to avoid reloading
|
23 |
PIPELINES = {}
|
24 |
|
25 |
def load_pipeline(model_id):
|
26 |
if model_id in PIPELINES:
|
27 |
return PIPELINES[model_id]
|
28 |
else:
|
29 |
+
pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
|
30 |
+
pipe.to(DEVICE)
|
|
|
|
|
|
|
31 |
PIPELINES[model_id] = pipe
|
32 |
return pipe
|
33 |
|
34 |
def generate_image(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, num_images, model_choice):
|
35 |
if not prompt:
|
36 |
raise gr.Error("Будь ласка, введіть опис для зображення.")
|
37 |
+
torch.set_num_threads(NUM_CPU_CORES) # Set PyTorch thread count
|
38 |
|
39 |
pipe = load_pipeline(MODEL_OPTIONS[model_choice])
|
40 |
|
41 |
+
# Adjust memory usage based on available RAM
|
42 |
+
torch.cuda.empty_cache() # Not strictly necessary on CPU, but good practice
|
43 |
+
|
44 |
generator = torch.Generator(device=DEVICE)
|
45 |
if not randomize_seed:
|
46 |
generator = generator.manual_seed(seed)
|
47 |
|
48 |
+
start_time = time.time()
|
49 |
images = pipe(
|
50 |
prompt,
|
51 |
negative_prompt=negative_prompt,
|
|
|
53 |
height=height,
|
54 |
guidance_scale=guidance_scale,
|
55 |
num_inference_steps=num_inference_steps,
|
56 |
+
num_images_per_prompt=num_images,
|
57 |
+
|
58 |
generator=generator,
|
59 |
).images
|
60 |
|
61 |
end_time = time.time()
|
62 |
generation_time = end_time - start_time
|
63 |
|
64 |
+
return images, f"Час генерації: {generation_time:.2f} секунд"
|
|
|
|
|
|
|
65 |
|
66 |
run_button = gr.Button("Згенерувати")
|
67 |
gallery = gr.Gallery(label="Згенеровані зображення")
|