Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import subprocess
|
2 |
import shlex
|
3 |
subprocess.run(
|
@@ -10,7 +11,7 @@ from gradio_magicquill import MagicQuill
|
|
10 |
import random
|
11 |
import torch
|
12 |
import numpy as np
|
13 |
-
from PIL import Image, ImageOps
|
14 |
import base64
|
15 |
import io
|
16 |
from fastapi import FastAPI, Request
|
@@ -35,6 +36,62 @@ def tensor_to_numpy(tensor):
|
|
35 |
return (tensor.detach().cpu().numpy() * 255).astype(np.uint8)
|
36 |
return tensor
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
def tensor_to_base64(tensor):
|
39 |
tensor = tensor.squeeze(0) * 255.
|
40 |
pil_image = Image.fromarray(tensor.cpu().byte().numpy())
|
@@ -143,7 +200,8 @@ def guess_prompt_handler(original_image, add_color_image, add_edge_image):
|
|
143 |
os.remove(add_edge_mask_file.name)
|
144 |
|
145 |
return res
|
146 |
-
|
|
|
147 |
def generate(ckpt_name, total_mask, original_image, add_color_image, add_edge_image, remove_edge_image, positive_prompt, negative_prompt, grow_size, stroke_as_edge, fine_edge, edge_strength, color_strength, inpaint_strength, seed, steps, cfg, sampler_name, scheduler):
|
148 |
add_color_image, original_image, total_mask, add_edge_mask, remove_edge_mask = prepare_images_and_masks(total_mask, original_image, add_color_image, add_edge_image, remove_edge_image)
|
149 |
progress = None
|
@@ -176,7 +234,10 @@ def generate(ckpt_name, total_mask, original_image, add_color_image, add_edge_im
|
|
176 |
progress
|
177 |
)
|
178 |
|
179 |
-
|
|
|
|
|
|
|
180 |
return final_image_base64
|
181 |
|
182 |
def generate_image_handler(x, ckpt_name, negative_prompt, fine_edge, grow_size, edge_strength, color_strength, inpaint_strength, seed, steps, cfg, sampler_name, scheduler):
|
@@ -196,19 +257,15 @@ css = '''
|
|
196 |
}
|
197 |
'''
|
198 |
|
199 |
-
|
200 |
-
<meta http-equiv="Content-Security-Policy" content="frame-ancestors 'none'">
|
201 |
-
"""
|
202 |
-
|
203 |
-
with gr.Blocks(css=css, head=head) as demo:
|
204 |
with gr.Row(elem_classes="row"):
|
205 |
text = gr.Markdown(
|
206 |
"""
|
207 |
-
# Welcome to MagicQuill!
|
208 |
-
|
209 |
""")
|
210 |
with gr.Row(elem_classes="row"):
|
211 |
-
ms = MagicQuill()
|
212 |
with gr.Row(elem_classes="row"):
|
213 |
with gr.Column():
|
214 |
btn = gr.Button("Run", variant="primary")
|
@@ -325,10 +382,10 @@ async def process_background_img(request: Request):
|
|
325 |
img = await request.json()
|
326 |
resized_img_tensor = load_and_resize_image(img)
|
327 |
resized_img_base64 = "data:image/png;base64," + tensor_to_base64(resized_img_tensor)
|
|
|
328 |
return resized_img_base64
|
329 |
|
330 |
app = gr.mount_gradio_app(app, demo, "/")
|
331 |
|
332 |
if __name__ == "__main__":
|
333 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
334 |
-
# demo.launch()
|
|
|
1 |
+
import spaces
|
2 |
import subprocess
|
3 |
import shlex
|
4 |
subprocess.run(
|
|
|
11 |
import random
|
12 |
import torch
|
13 |
import numpy as np
|
14 |
+
from PIL import Image, ImageOps, ImageDraw, ImageFont
|
15 |
import base64
|
16 |
import io
|
17 |
from fastapi import FastAPI, Request
|
|
|
36 |
return (tensor.detach().cpu().numpy() * 255).astype(np.uint8)
|
37 |
return tensor
|
38 |
|
39 |
+
def add_watermark_to_image(image_tensor, watermark_text="Power By magicquill.online"):
|
40 |
+
"""
|
41 |
+
在图像右下角添加文字水印
|
42 |
+
"""
|
43 |
+
# 将tensor转换为PIL图像
|
44 |
+
if isinstance(image_tensor, torch.Tensor):
|
45 |
+
image_array = (image_tensor.squeeze(0).detach().cpu().numpy() * 255).astype(np.uint8)
|
46 |
+
else:
|
47 |
+
image_array = image_tensor
|
48 |
+
|
49 |
+
pil_image = Image.fromarray(image_array)
|
50 |
+
|
51 |
+
# 获取图像尺寸
|
52 |
+
width, height = pil_image.size
|
53 |
+
|
54 |
+
# 尝试加载字体,如果失败则使用默认字体
|
55 |
+
try:
|
56 |
+
# 根据图像大小动态调整字体大小
|
57 |
+
font_size = max(12, min(width, height) // 50)
|
58 |
+
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", font_size)
|
59 |
+
except:
|
60 |
+
try:
|
61 |
+
font_size = max(12, min(width, height) // 50)
|
62 |
+
font = ImageFont.load_default()
|
63 |
+
except:
|
64 |
+
font = ImageFont.load_default()
|
65 |
+
|
66 |
+
# 创建绘制对象
|
67 |
+
draw = ImageDraw.Draw(pil_image)
|
68 |
+
|
69 |
+
# 获取文字尺寸
|
70 |
+
bbox = draw.textbbox((0, 0), watermark_text, font=font)
|
71 |
+
text_width = bbox[2] - bbox[0]
|
72 |
+
text_height = bbox[3] - bbox[1]
|
73 |
+
|
74 |
+
# 计算水印位置(右下角,留一些边距)
|
75 |
+
margin = 10
|
76 |
+
background_padding = 3 # 减小背景padding
|
77 |
+
x = width - text_width - margin - background_padding
|
78 |
+
y = height - text_height - margin - background_padding
|
79 |
+
|
80 |
+
# 绘制紧贴文字的半透明背景矩形
|
81 |
+
draw.rectangle([
|
82 |
+
x - background_padding,
|
83 |
+
y - background_padding,
|
84 |
+
x + text_width + background_padding,
|
85 |
+
y + text_height + background_padding
|
86 |
+
], fill=(0, 0, 0, 160)) # 半透明黑色背景,稍微增加透明度
|
87 |
+
|
88 |
+
# 绘制白色文字
|
89 |
+
draw.text((x, y), watermark_text, fill=(255, 255, 255, 255), font=font)
|
90 |
+
|
91 |
+
# 转换回tensor格式
|
92 |
+
image_array = np.array(pil_image).astype(np.float32) / 255.0
|
93 |
+
return torch.from_numpy(image_array).unsqueeze(0)
|
94 |
+
|
95 |
def tensor_to_base64(tensor):
|
96 |
tensor = tensor.squeeze(0) * 255.
|
97 |
pil_image = Image.fromarray(tensor.cpu().byte().numpy())
|
|
|
200 |
os.remove(add_edge_mask_file.name)
|
201 |
|
202 |
return res
|
203 |
+
|
204 |
+
@spaces.GPU(duration=120)
|
205 |
def generate(ckpt_name, total_mask, original_image, add_color_image, add_edge_image, remove_edge_image, positive_prompt, negative_prompt, grow_size, stroke_as_edge, fine_edge, edge_strength, color_strength, inpaint_strength, seed, steps, cfg, sampler_name, scheduler):
|
206 |
add_color_image, original_image, total_mask, add_edge_mask, remove_edge_mask = prepare_images_and_masks(total_mask, original_image, add_color_image, add_edge_image, remove_edge_image)
|
207 |
progress = None
|
|
|
234 |
progress
|
235 |
)
|
236 |
|
237 |
+
# 在最终图像上添加水印
|
238 |
+
final_image_with_watermark = add_watermark_to_image(final_image, "Power By magicquill.online")
|
239 |
+
|
240 |
+
final_image_base64 = tensor_to_base64(final_image_with_watermark)
|
241 |
return final_image_base64
|
242 |
|
243 |
def generate_image_handler(x, ckpt_name, negative_prompt, fine_edge, grow_size, edge_strength, color_strength, inpaint_strength, seed, steps, cfg, sampler_name, scheduler):
|
|
|
257 |
}
|
258 |
'''
|
259 |
|
260 |
+
with gr.Blocks(css=css) as demo:
|
|
|
|
|
|
|
|
|
261 |
with gr.Row(elem_classes="row"):
|
262 |
text = gr.Markdown(
|
263 |
"""
|
264 |
+
# Welcome to MagicQuill!
|
265 |
+
Thank you to the developers for their contributions. Give a [GitHub star](https://github.com/magic-quill/magicquill)
|
266 |
""")
|
267 |
with gr.Row(elem_classes="row"):
|
268 |
+
ms = MagicQuill(theme="light")
|
269 |
with gr.Row(elem_classes="row"):
|
270 |
with gr.Column():
|
271 |
btn = gr.Button("Run", variant="primary")
|
|
|
382 |
img = await request.json()
|
383 |
resized_img_tensor = load_and_resize_image(img)
|
384 |
resized_img_base64 = "data:image/png;base64," + tensor_to_base64(resized_img_tensor)
|
385 |
+
# add more processing here
|
386 |
return resized_img_base64
|
387 |
|
388 |
app = gr.mount_gradio_app(app, demo, "/")
|
389 |
|
390 |
if __name__ == "__main__":
|
391 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|