Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,146 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
import
|
4 |
-
from
|
5 |
-
import torch
|
6 |
|
7 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
12 |
-
pipe.enable_xformers_memory_efficient_attention()
|
13 |
-
pipe = pipe.to(device)
|
14 |
-
else:
|
15 |
-
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
|
16 |
-
pipe = pipe.to(device)
|
17 |
|
18 |
-
MAX_SEED = np.iinfo(np.int32).max
|
19 |
-
MAX_IMAGE_SIZE = 1024
|
20 |
|
21 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
if randomize_seed:
|
24 |
-
seed = random.randint(0, MAX_SEED)
|
25 |
-
|
26 |
-
generator = torch.Generator().manual_seed(seed)
|
27 |
-
|
28 |
-
image = pipe(
|
29 |
-
prompt = prompt,
|
30 |
-
negative_prompt = negative_prompt,
|
31 |
-
guidance_scale = guidance_scale,
|
32 |
-
num_inference_steps = num_inference_steps,
|
33 |
-
width = width,
|
34 |
-
height = height,
|
35 |
-
generator = generator
|
36 |
-
).images[0]
|
37 |
-
|
38 |
-
return image
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
]
|
45 |
|
46 |
-
css="""
|
47 |
-
#col-container {
|
48 |
-
margin: 0 auto;
|
49 |
-
max-width: 520px;
|
50 |
-
}
|
51 |
-
"""
|
52 |
|
53 |
-
if torch.cuda.is_available():
|
54 |
-
power_device = "GPU"
|
55 |
-
else:
|
56 |
-
power_device = "CPU"
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
placeholder="Enter your prompt",
|
73 |
-
container=False,
|
74 |
-
)
|
75 |
-
|
76 |
-
run_button = gr.Button("Run", scale=0)
|
77 |
-
|
78 |
-
result = gr.Image(label="Result", show_label=False)
|
79 |
|
80 |
-
with gr.Accordion("Advanced Settings", open=False):
|
81 |
-
|
82 |
-
negative_prompt = gr.Text(
|
83 |
-
label="Negative prompt",
|
84 |
-
max_lines=1,
|
85 |
-
placeholder="Enter a negative prompt",
|
86 |
-
visible=False,
|
87 |
-
)
|
88 |
-
|
89 |
-
seed = gr.Slider(
|
90 |
-
label="Seed",
|
91 |
-
minimum=0,
|
92 |
-
maximum=MAX_SEED,
|
93 |
-
step=1,
|
94 |
-
value=0,
|
95 |
-
)
|
96 |
-
|
97 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
98 |
-
|
99 |
-
with gr.Row():
|
100 |
-
|
101 |
-
width = gr.Slider(
|
102 |
-
label="Width",
|
103 |
-
minimum=256,
|
104 |
-
maximum=MAX_IMAGE_SIZE,
|
105 |
-
step=32,
|
106 |
-
value=512,
|
107 |
-
)
|
108 |
-
|
109 |
-
height = gr.Slider(
|
110 |
-
label="Height",
|
111 |
-
minimum=256,
|
112 |
-
maximum=MAX_IMAGE_SIZE,
|
113 |
-
step=32,
|
114 |
-
value=512,
|
115 |
-
)
|
116 |
-
|
117 |
-
with gr.Row():
|
118 |
-
|
119 |
-
guidance_scale = gr.Slider(
|
120 |
-
label="Guidance scale",
|
121 |
-
minimum=0.0,
|
122 |
-
maximum=10.0,
|
123 |
-
step=0.1,
|
124 |
-
value=0.0,
|
125 |
-
)
|
126 |
-
|
127 |
-
num_inference_steps = gr.Slider(
|
128 |
-
label="Number of inference steps",
|
129 |
-
minimum=1,
|
130 |
-
maximum=12,
|
131 |
-
step=1,
|
132 |
-
value=2,
|
133 |
-
)
|
134 |
-
|
135 |
-
gr.Examples(
|
136 |
-
examples = examples,
|
137 |
-
inputs = [prompt]
|
138 |
-
)
|
139 |
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
-
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
# @时间 : 2024/5/15 18:03
|
3 |
+
# @作者 : caishilong
|
4 |
+
# @文件名 : main.py
|
5 |
+
# @项目名 : ai-platform
|
6 |
+
# @Software : PyCharm
|
7 |
+
import io
|
8 |
import gradio as gr
|
9 |
import numpy as np
|
10 |
+
import requests
|
11 |
+
from PIL import Image
|
|
|
12 |
|
|
|
13 |
|
14 |
+
API_BASE_URL = "https://api.cloudflare.com/client/v4/accounts/0c76db6b77f9629c9e3f19d037c937c0/ai/run/"
|
15 |
+
headers = {"Authorization": "Bearer qOn7C354fZtjY7SiOFWmwN9g0rtbtLqflUPMS5JU"}
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
|
|
|
|
17 |
|
18 |
+
def run(model, inputs):
|
19 |
+
input = inputs
|
20 |
+
response = requests.post(f"{API_BASE_URL}{model}", headers=headers, json=input)
|
21 |
+
# 返回的是"content-type": "image/png"
|
22 |
+
try:
|
23 |
+
print(response.json())
|
24 |
+
except:
|
25 |
+
pass
|
26 |
+
image = Image.open(io.BytesIO(response.content))
|
27 |
+
image_array = np.array(image)
|
28 |
+
return image_array
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
# output = run("@cf/stabilityai/stable-diffusion-xl-base-1.0", inputs)
|
32 |
+
#
|
33 |
+
# with open("output.png", "wb") as f:
|
34 |
+
# f.write(output)
|
|
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
def fake_diffusion(model, prompt, guidance, image: np.ndarray):
|
39 |
+
# image = Image.fromarray(image)
|
40 |
+
#
|
41 |
+
# image = np.array(image)
|
42 |
+
# print(image)
|
43 |
+
# plt.plot(121)
|
44 |
+
# plt.imshow(image)
|
45 |
+
# plt.show()
|
46 |
+
data = {"prompt": prompt,
|
47 |
+
"guidance": guidance,
|
48 |
+
# "image": image.tolist()
|
49 |
+
}
|
50 |
+
output = run(model, data)
|
51 |
+
yield output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
demo = gr.Interface(fake_diffusion,
|
55 |
+
[
|
56 |
+
gr.Dropdown(["@cf/stabilityai/stable-diffusion-xl-base-1.0",
|
57 |
+
"@cf/lykon/dreamshaper-8-lcm",
|
58 |
+
'@cf/bytedance/stable-diffusion-xl-lightning',
|
59 |
+
'@cf/runwayml/stable-diffusion-v1-5-img2img'],
|
60 |
+
label="Model", value='@cf/bytedance/stable-diffusion-xl-lightning'),
|
61 |
+
gr.Textbox(label='prompt', value='human playing with a dog'),
|
62 |
+
gr.Slider(1, 20, 20, label='guidance'),
|
63 |
+
# gr.Image()
|
64 |
+
],
|
65 |
+
outputs="image")
|
66 |
|
67 |
+
if __name__ == "__main__":
|
68 |
+
demo.launch(server_name='0.0.0.0',auth= ("admin", r'qwe[]\123'),)
|