Spaces:
Sleeping
Sleeping
v1
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from diffusers import AutoPipelineForText2Image
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
import threading
|
5 |
+
import time;
|
6 |
+
from queue import Queue
|
7 |
+
import spaces
|
8 |
+
|
9 |
+
|
10 |
+
@spaces.GPU(duration=120)
|
11 |
+
def GenerateImage(prompt,steps,progress,model):
|
12 |
+
|
13 |
+
data = []
|
14 |
+
|
15 |
+
queue = Queue();
|
16 |
+
|
17 |
+
def StartThread():
|
18 |
+
|
19 |
+
pipe_txt2img = AutoPipelineForText2Image.from_pretrained(
|
20 |
+
model, torch_dtype=torch.float16, use_safetensors=True
|
21 |
+
).to("cuda")
|
22 |
+
|
23 |
+
vae = pipe_txt2img.vae
|
24 |
+
|
25 |
+
def latents_callback(i, t, latents):
|
26 |
+
print("Latents...");
|
27 |
+
latents = 1 / 0.18215 * latents
|
28 |
+
image = vae.decode(latents).sample[0]
|
29 |
+
image = (image / 2 + 0.5).clamp(0, 1)
|
30 |
+
image = image.cpu().permute(1, 2, 0).numpy()
|
31 |
+
FinalImage = pipe_txt2img.numpy_to_pil(image)
|
32 |
+
queue.put({'type':'image', 'image':FinalImage[0], 'step': i})
|
33 |
+
|
34 |
+
generator = torch.Generator(device="cpu").manual_seed(37)
|
35 |
+
FinalImage = pipe_txt2img(prompt, generator=generator, num_inference_steps=steps,callback=latents_callback, callback_steps=progress).images[0]
|
36 |
+
queue.put({'type':'image', 'image':FinalImage, 'step': steps+1})
|
37 |
+
queue.put({'type':'end'})
|
38 |
+
|
39 |
+
t = threading.Thread(target=StartThread)
|
40 |
+
t.start();
|
41 |
+
|
42 |
+
while True:
|
43 |
+
print("Waiting next item");
|
44 |
+
nextItem = queue.get()
|
45 |
+
|
46 |
+
print(f"NextItem: {nextItem}");
|
47 |
+
|
48 |
+
if nextItem['type'] == 'end':
|
49 |
+
break;
|
50 |
+
|
51 |
+
Image = nextItem['image']
|
52 |
+
Step = nextItem['step']
|
53 |
+
print(f"Image: {Image}")
|
54 |
+
yield [Image,Step];
|
55 |
+
|
56 |
+
print("Waiting thread finish...");
|
57 |
+
t.join()
|
58 |
+
|
59 |
+
print("Finished!");
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
with gr.Blocks() as demo:
|
64 |
+
with gr.Row():
|
65 |
+
prompt = gr.Text(label="Imagem prompt");
|
66 |
+
TotalSteps = gr.Slider(label="Imagem prompt", minimum=1,maximum=150,value=10);
|
67 |
+
ProgressSteps = gr.Number(label="Progress steps", value = 2);
|
68 |
+
model = gr.Text(label="Model", value="dreamlike-art/dreamlike-photoreal-2.0")
|
69 |
+
|
70 |
+
with gr.Row():
|
71 |
+
with gr.Column():
|
72 |
+
btnRun = gr.Button(value="Run!");
|
73 |
+
btnStop = gr.Button(value="Stop!");
|
74 |
+
status = gr.Text(label="Current Step");
|
75 |
+
|
76 |
+
|
77 |
+
image = gr.Image();
|
78 |
+
|
79 |
+
|
80 |
+
GenerateEvent = btnRun.click( GenerateImage, [prompt,TotalSteps,ProgressSteps,model], [image,status] );
|
81 |
+
btnStop.click( None,None,None, cancels=[GenerateEvent] )
|
82 |
+
|
83 |
+
if __name__ == "__main__":
|
84 |
+
demo.launch(show_api=True)
|
85 |
+
|
86 |
+
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
|