Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from diffusers import DiffusionPipeline
|
3 |
import dask
|
4 |
-
from dask import delayed
|
5 |
-
from concurrent.futures import ThreadPoolExecutor
|
6 |
-
import os
|
7 |
-
os.environ['HF_HOME'] = '/blabla/cache/'
|
8 |
-
|
9 |
|
10 |
# Load model
|
11 |
pipe = DiffusionPipeline.from_pretrained("prompthero/openjourney-v4")
|
@@ -19,27 +15,23 @@ def generate_image(prompt, num_inference_steps=50):
|
|
19 |
image = pipe(prompt, num_inference_steps=num_inference_steps).images[0]
|
20 |
return image
|
21 |
|
|
|
22 |
@delayed
|
23 |
def dask_generate(prompt):
|
24 |
return generate_image(prompt)
|
25 |
|
26 |
def parallel_generate(prompt):
|
27 |
-
# Use multithreading to speed up the computation by processing multiple images simultaneously
|
28 |
-
with ThreadPoolExecutor(max_workers=4) as executor:
|
29 |
-
futures = [executor.submit(dask_generate, prompt) for _ in range(4)] # Example with 4 threads
|
30 |
-
results = [future.result() for future in futures]
|
31 |
-
|
32 |
# Execute the generation using Dask to potentially improve processing speed
|
33 |
-
|
34 |
-
return
|
35 |
|
36 |
# Gradio interface
|
37 |
iface = gr.Interface(
|
38 |
fn=parallel_generate,
|
39 |
inputs=gr.Textbox(label="Prompt", placeholder="Enter your prompt here"),
|
40 |
outputs=gr.Image(type="pil"),
|
41 |
-
title="
|
42 |
-
description="Enter a prompt to generate an image efficiently using CPU optimization
|
43 |
)
|
44 |
|
45 |
# Launch the Gradio app
|
|
|
1 |
import gradio as gr
|
2 |
from diffusers import DiffusionPipeline
|
3 |
import dask
|
4 |
+
from dask import delayed
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Load model
|
7 |
pipe = DiffusionPipeline.from_pretrained("prompthero/openjourney-v4")
|
|
|
15 |
image = pipe(prompt, num_inference_steps=num_inference_steps).images[0]
|
16 |
return image
|
17 |
|
18 |
+
# Dask-delayed function to utilize multi-core CPU processing
|
19 |
@delayed
|
20 |
def dask_generate(prompt):
|
21 |
return generate_image(prompt)
|
22 |
|
23 |
def parallel_generate(prompt):
|
|
|
|
|
|
|
|
|
|
|
24 |
# Execute the generation using Dask to potentially improve processing speed
|
25 |
+
image = dask.compute(dask_generate(prompt))[0]
|
26 |
+
return image
|
27 |
|
28 |
# Gradio interface
|
29 |
iface = gr.Interface(
|
30 |
fn=parallel_generate,
|
31 |
inputs=gr.Textbox(label="Prompt", placeholder="Enter your prompt here"),
|
32 |
outputs=gr.Image(type="pil"),
|
33 |
+
title="CPU Optimized Image Generation",
|
34 |
+
description="Enter a prompt to generate an image efficiently using CPU optimization."
|
35 |
)
|
36 |
|
37 |
# Launch the Gradio app
|