Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,47 @@
|
|
1 |
import gradio as gr
|
2 |
from diffusers import DiffusionPipeline
|
|
|
|
|
|
|
3 |
import os
|
4 |
os.environ['HF_HOME'] = '/blabla/cache/'
|
5 |
|
6 |
-
|
|
|
7 |
pipe = DiffusionPipeline.from_pretrained("prompthero/openjourney-v4")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
|
|
|
|
|
|
10 |
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
return image
|
16 |
|
17 |
-
#
|
18 |
iface = gr.Interface(
|
19 |
-
fn=
|
20 |
-
inputs=gr.Textbox(label="
|
21 |
-
outputs=gr.Image(type="pil"
|
22 |
-
title="Image Generation
|
23 |
-
description="Enter a prompt to generate an image using
|
24 |
)
|
25 |
|
26 |
-
# Launch the Gradio
|
27 |
if __name__ == "__main__":
|
28 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from diffusers import DiffusionPipeline
|
3 |
+
import dask
|
4 |
+
from dask import delayed, compute
|
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")
|
12 |
|
13 |
+
def generate_image(prompt, num_inference_steps=50):
|
14 |
+
"""
|
15 |
+
Generate an image based on a text prompt using diffusion with optimizations.
|
16 |
+
The number of inference steps is reduced for faster generation.
|
17 |
+
"""
|
18 |
+
# Reduce steps for faster processing
|
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 |
+
images = compute(*results)
|
34 |
+
return images[0] # Return the first image generated for simplicity
|
|
|
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="Multithreaded CPU Optimized Image Generation",
|
42 |
+
description="Enter a prompt to generate an image efficiently using CPU optimization and multithreading."
|
43 |
)
|
44 |
|
45 |
+
# Launch the Gradio app
|
46 |
if __name__ == "__main__":
|
47 |
iface.launch()
|