aifeifei798 commited on
Commit
19020c2
·
verified ·
1 Parent(s): 3422e0c

Update feifeilib/feifeitexttoimg.py

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