Spaces:
azhan77168
/
Running on Zero

azhan77168 commited on
Commit
37505a7
·
verified ·
1 Parent(s): 9a290c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -4
app.py CHANGED
@@ -11,7 +11,7 @@ from gradio_magicquill import MagicQuill
11
  import random
12
  import torch
13
  import numpy as np
14
- from PIL import Image, ImageOps
15
  import base64
16
  import io
17
  from fastapi import FastAPI, Request
@@ -25,10 +25,19 @@ import cv2
25
  import os
26
  import requests
27
 
 
 
 
 
 
 
 
 
28
  snapshot_download(repo_id="LiuZichen/MagicQuill-models", repo_type="model", local_dir="models")
29
  # HF_TOKEN = os.environ.get("HF_TOKEN")
30
  # The client has been made public. Welcome to duplicate our repo.
31
  client = Client("LiuZichen/DrawNGuess")
 
32
  scribbleColorEditModel = ScribbleColorEditModel()
33
 
34
  def tensor_to_numpy(tensor):
@@ -36,6 +45,62 @@ def tensor_to_numpy(tensor):
36
  return (tensor.detach().cpu().numpy() * 255).astype(np.uint8)
37
  return tensor
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  def tensor_to_base64(tensor):
40
  tensor = tensor.squeeze(0) * 255.
41
  pil_image = Image.fromarray(tensor.cpu().byte().numpy())
@@ -145,7 +210,7 @@ def guess_prompt_handler(original_image, add_color_image, add_edge_image):
145
 
146
  return res
147
 
148
- @spaces.GPU
149
  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):
150
  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)
151
  progress = None
@@ -178,7 +243,10 @@ def generate(ckpt_name, total_mask, original_image, add_color_image, add_edge_im
178
  progress
179
  )
180
 
181
- final_image_base64 = tensor_to_base64(final_image)
 
 
 
182
  return final_image_base64
183
 
184
  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):
@@ -330,4 +398,3 @@ app = gr.mount_gradio_app(app, demo, "/")
330
 
331
  if __name__ == "__main__":
332
  uvicorn.run(app, host="0.0.0.0", port=7860)
333
- # demo.launch()
 
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
 
25
  import os
26
  import requests
27
 
28
+ @spaces.GPU
29
+ def warmup_gpu():
30
+ import torch
31
+ if torch.cuda.is_available():
32
+ torch.cuda.empty_cache()
33
+ return True
34
+
35
+
36
  snapshot_download(repo_id="LiuZichen/MagicQuill-models", repo_type="model", local_dir="models")
37
  # HF_TOKEN = os.environ.get("HF_TOKEN")
38
  # The client has been made public. Welcome to duplicate our repo.
39
  client = Client("LiuZichen/DrawNGuess")
40
+ warmup_gpu()
41
  scribbleColorEditModel = ScribbleColorEditModel()
42
 
43
  def tensor_to_numpy(tensor):
 
45
  return (tensor.detach().cpu().numpy() * 255).astype(np.uint8)
46
  return tensor
47
 
48
+ def add_watermark_to_image(image_tensor, watermark_text="Power By magicquill.online"):
49
+ """
50
+ 在图像右下角添加文字水印
51
+ """
52
+ # 将tensor转换为PIL图像
53
+ if isinstance(image_tensor, torch.Tensor):
54
+ image_array = (image_tensor.squeeze(0).detach().cpu().numpy() * 255).astype(np.uint8)
55
+ else:
56
+ image_array = image_tensor
57
+
58
+ pil_image = Image.fromarray(image_array)
59
+
60
+ # 获取图像尺寸
61
+ width, height = pil_image.size
62
+
63
+ # 尝试加载字体,如果失败则使用默认字体
64
+ try:
65
+ # 根据图像大小动态调整字体大小
66
+ font_size = max(12, min(width, height) // 50)
67
+ font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", font_size)
68
+ except:
69
+ try:
70
+ font_size = max(12, min(width, height) // 50)
71
+ font = ImageFont.load_default()
72
+ except:
73
+ font = ImageFont.load_default()
74
+
75
+ # 创建绘制对象
76
+ draw = ImageDraw.Draw(pil_image)
77
+
78
+ # 获取文字尺寸
79
+ bbox = draw.textbbox((0, 0), watermark_text, font=font)
80
+ text_width = bbox[2] - bbox[0]
81
+ text_height = bbox[3] - bbox[1]
82
+
83
+ # 计算水印位置(右下角,留一些边距)
84
+ margin = 10
85
+ background_padding = 3 # 减小背景padding
86
+ x = width - text_width - margin - background_padding
87
+ y = height - text_height - margin - background_padding
88
+
89
+ # 绘制紧贴文字的半透明背景矩形
90
+ draw.rectangle([
91
+ x - background_padding,
92
+ y - background_padding,
93
+ x + text_width + background_padding,
94
+ y + text_height + background_padding
95
+ ], fill=(0, 0, 0, 160)) # 半透明黑色背景,稍微增加透明度
96
+
97
+ # 绘制白色文字
98
+ draw.text((x, y), watermark_text, fill=(255, 255, 255, 255), font=font)
99
+
100
+ # 转换回tensor格式
101
+ image_array = np.array(pil_image).astype(np.float32) / 255.0
102
+ return torch.from_numpy(image_array).unsqueeze(0)
103
+
104
  def tensor_to_base64(tensor):
105
  tensor = tensor.squeeze(0) * 255.
106
  pil_image = Image.fromarray(tensor.cpu().byte().numpy())
 
210
 
211
  return res
212
 
213
+ @spaces.GPU(duration=120)
214
  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):
215
  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)
216
  progress = None
 
243
  progress
244
  )
245
 
246
+ # 在最终图像上添加水印
247
+ final_image_with_watermark = add_watermark_to_image(final_image, "Power By magicquill.online")
248
+
249
+ final_image_base64 = tensor_to_base64(final_image_with_watermark)
250
  return final_image_base64
251
 
252
  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):
 
398
 
399
  if __name__ == "__main__":
400
  uvicorn.run(app, host="0.0.0.0", port=7860)