Spaces:
Running
on
Zero
Running
on
Zero
Update feifeilib/feifeitexttoimg.py
Browse files- feifeilib/feifeitexttoimg.py +124 -121
feifeilib/feifeitexttoimg.py
CHANGED
@@ -1,121 +1,124 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from PIL import Image
|
3 |
-
import numpy as np
|
4 |
-
import random
|
5 |
-
import spaces
|
6 |
-
import torch
|
7 |
-
import torch.nn.functional as F
|
8 |
-
import config
|
9 |
-
from extras.expansion import FooocusExpansion
|
10 |
-
from feifeilib.feifeimodload import feifeimodload
|
11 |
-
import re
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
if
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
#
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
# 将NumPy
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
#
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
import random
|
5 |
+
import spaces
|
6 |
+
import torch
|
7 |
+
import torch.nn.functional as F
|
8 |
+
import config
|
9 |
+
from extras.expansion import FooocusExpansion
|
10 |
+
from feifeilib.feifeimodload import feifeimodload
|
11 |
+
import re
|
12 |
+
from diffusers.callbacks import SDXLCFGCutoffCallback
|
13 |
+
|
14 |
+
callback = SDXLCFGCutoffCallback(cutoff_step_ratio=0.4)
|
15 |
+
expansion = FooocusExpansion()
|
16 |
+
pipe = feifeimodload()
|
17 |
+
with open("artist.txt", "r") as file:
|
18 |
+
artists = file.readlines()
|
19 |
+
|
20 |
+
MAX_SEED = np.iinfo(np.int32).max
|
21 |
+
|
22 |
+
# 去除每行末尾的换行符
|
23 |
+
artists = [artist.strip() for artist in artists]
|
24 |
+
|
25 |
+
@spaces.GPU()
|
26 |
+
def feifeitexttoimg(
|
27 |
+
prompt,
|
28 |
+
quality_select=False,
|
29 |
+
sharpened_select=False,
|
30 |
+
styles_Radio=["(None)"],
|
31 |
+
FooocusExpansion_select=False,
|
32 |
+
seed=random.randint(0, MAX_SEED),
|
33 |
+
randomize_seed=False,
|
34 |
+
width=896,
|
35 |
+
height=1152,
|
36 |
+
num_inference_steps=4,
|
37 |
+
guidance_scale=3.5,
|
38 |
+
num_strength=0.35,
|
39 |
+
progress=gr.Progress(track_tqdm=True),
|
40 |
+
):
|
41 |
+
# 处理随机种子
|
42 |
+
if randomize_seed:
|
43 |
+
seed = random.randint(0, MAX_SEED)
|
44 |
+
generator = torch.Generator().manual_seed(seed)
|
45 |
+
|
46 |
+
if not prompt:
|
47 |
+
prompt = "the photo is a 18 yo jpop girl is looking absolutely adorable and gorgeous, with a playful and mischievous grin, her eyes twinkling with joy. art by __artist__ and __artist__"
|
48 |
+
if "__artist__" in prompt:
|
49 |
+
# 随机选择艺术家
|
50 |
+
selected_artists = random.sample(artists, len(artists))
|
51 |
+
|
52 |
+
# 使用正则表达式替换所有的 __artist__
|
53 |
+
def replace_artists(match):
|
54 |
+
return selected_artists.pop(0)
|
55 |
+
|
56 |
+
prompt = re.sub(r"__artist__", replace_artists, prompt)
|
57 |
+
# print("__artist__ " + prompt)
|
58 |
+
if quality_select:
|
59 |
+
prompt += ", masterpiece, best quality, very aesthetic, absurdres"
|
60 |
+
# print("111 " + prompt)
|
61 |
+
|
62 |
+
if styles_Radio:
|
63 |
+
for style_name in styles_Radio:
|
64 |
+
for style in config.style_list:
|
65 |
+
if style["name"] == style_name:
|
66 |
+
prompt += style["prompt"].replace("{prompt}", "the ")
|
67 |
+
# print("222 " + prompt)
|
68 |
+
if FooocusExpansion_select:
|
69 |
+
prompt = expansion(prompt, seed)
|
70 |
+
# print("333 " + prompt)
|
71 |
+
# print("000 " + prompt)
|
72 |
+
|
73 |
+
# 生成图像
|
74 |
+
image = pipe(
|
75 |
+
prompt="",
|
76 |
+
prompt_2=prompt,
|
77 |
+
width=width,
|
78 |
+
height=height,
|
79 |
+
num_inference_steps=num_inference_steps,
|
80 |
+
generator=generator,
|
81 |
+
guidance_scale=guidance_scale,
|
82 |
+
callback_on_step_end=callback,
|
83 |
+
output_type="pil",
|
84 |
+
).images[0]
|
85 |
+
|
86 |
+
if sharpened_select:
|
87 |
+
# 将PIL图像转换为NumPy数组
|
88 |
+
image_np = np.array(image)
|
89 |
+
|
90 |
+
# 将NumPy数组转换为PyTorch张量
|
91 |
+
image_tensor = (torch.tensor(image_np).permute(
|
92 |
+
2, 0, 1).unsqueeze(0).float().to("cuda"))
|
93 |
+
|
94 |
+
# 定义锐化滤镜,并调整中心值
|
95 |
+
strength = num_strength
|
96 |
+
sharpen_kernel = (torch.tensor(
|
97 |
+
[
|
98 |
+
[0, -1 * strength, 0],
|
99 |
+
[-1 * strength, 1 + 4 * strength, -1 * strength],
|
100 |
+
[0, -1 * strength, 0],
|
101 |
+
],
|
102 |
+
dtype=torch.float32,
|
103 |
+
).unsqueeze(0).unsqueeze(0).to("cuda"))
|
104 |
+
|
105 |
+
# 分别对每个通道应用卷积核
|
106 |
+
sharpened_channels = []
|
107 |
+
for i in range(3):
|
108 |
+
channel_tensor = image_tensor[:, i:i + 1, :, :]
|
109 |
+
sharpened_channel = F.conv2d(channel_tensor,
|
110 |
+
sharpen_kernel,
|
111 |
+
padding=1)
|
112 |
+
sharpened_channels.append(sharpened_channel)
|
113 |
+
|
114 |
+
# 合并通道
|
115 |
+
sharpened_image_tensor = torch.cat(sharpened_channels, dim=1)
|
116 |
+
|
117 |
+
# 将增强后的图像转换回PIL格式
|
118 |
+
sharpened_image_np = (sharpened_image_tensor.squeeze(0).permute(
|
119 |
+
1, 2, 0).cpu().numpy())
|
120 |
+
sharpened_image_np = np.clip(sharpened_image_np, 0,
|
121 |
+
255).astype(np.uint8)
|
122 |
+
image = Image.fromarray(sharpened_image_np)
|
123 |
+
|
124 |
+
return image, seed
|