aifeifei798 commited on
Commit
31ef714
·
verified ·
1 Parent(s): dc6f4e4

Upload 5 files

Browse files
feifeilib/feifeifluxapi.py CHANGED
@@ -1,13 +1,19 @@
1
  import os
2
  from huggingface_hub import InferenceClient
 
 
 
3
  client = InferenceClient("black-forest-labs/FLUX.1-dev", token=os.getenv('HF_TOKEN'))
4
  client.headers["x-use-cache"] = "0"
5
 
6
- def feifeifluxapi(prompt="the photo is a jpop girl",height=1152,width=896,guidance_scale=3.5):
 
7
  image = client.text_to_image(
8
  prompt=prompt,
9
  height=height,
10
  width=width,
11
  guidance_scale=guidance_scale
12
  )
 
 
13
  return image
 
1
  import os
2
  from huggingface_hub import InferenceClient
3
+ from feifeilib.feifeiprompt import feifeiprompt
4
+ from feifeilib.feifeisharpened import feifeisharpened
5
+
6
  client = InferenceClient("black-forest-labs/FLUX.1-dev", token=os.getenv('HF_TOKEN'))
7
  client.headers["x-use-cache"] = "0"
8
 
9
+ def feifeifluxapi(seed,prompt,quality_select,sharpened_select, styles_Radio,FooocusExpansion_select,height=1152,width=896,guidance_scale=3.5,num_strength):
10
+ prompt,generator = feifeiprompt(seed,prompt,quality_select,styles_Radio,FooocusExpansion_select)
11
  image = client.text_to_image(
12
  prompt=prompt,
13
  height=height,
14
  width=width,
15
  guidance_scale=guidance_scale
16
  )
17
+ if sharpened_select:
18
+ feifeisharpened(image,num_strength)
19
  return image
feifeilib/feifeiprompt.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import random
3
+ from extras.expansion import FooocusExpansion
4
+ import re
5
+
6
+ expansion = FooocusExpansion()
7
+ with open("artist.txt", "r") as file:
8
+ artists = file.readlines()
9
+
10
+ MAX_SEED = np.iinfo(np.int32).max
11
+
12
+ # 去除每行末尾的换行符
13
+ artists = [artist.strip() for artist in artists]
14
+
15
+ def feifeiprompt(randomize_seed,prompt,quality_select,styles_Radio,FooocusExpansion_select):
16
+ # 处理随机种子
17
+ if randomize_seed:
18
+ seed = random.randint(0, MAX_SEED)
19
+ generator = torch.Generator().manual_seed(seed)
20
+
21
+ if not prompt:
22
+ 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__"
23
+ if "__artist__" in prompt:
24
+ # 随机选择艺术家
25
+ selected_artists = random.sample(artists, len(artists))
26
+
27
+ # 使用正则表达式替换所有的 __artist__
28
+ def replace_artists(match):
29
+ return selected_artists.pop(0)
30
+
31
+ prompt = re.sub(r"__artist__", replace_artists, prompt)
32
+
33
+ if quality_select:
34
+ prompt += ", masterpiece, best quality, very aesthetic, absurdres"
35
+
36
+
37
+ if styles_Radio:
38
+ for style_name in styles_Radio:
39
+ for style in config.style_list:
40
+ if style["name"] == style_name:
41
+ prompt += style["prompt"].replace("{prompt}", "the ")
42
+
43
+ if FooocusExpansion_select:
44
+ prompt = expansion(prompt, seed)
45
+ return prompt,generator
feifeilib/feifeisharpened.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import torch
3
+ import torch.nn.functional as F
4
+
5
+ def feifeisharpened(image,num_strength):
6
+ # 将PIL图像转换为NumPy数组
7
+ image_np = np.array(image)
8
+
9
+ # 将NumPy数组转换为PyTorch张量
10
+ image_tensor = (torch.tensor(image_np).permute(
11
+ 2, 0, 1).unsqueeze(0).float().to("cuda"))
12
+
13
+ # 定义锐化滤镜,并调整中心值
14
+ strength = num_strength
15
+ sharpen_kernel = (torch.tensor(
16
+ [
17
+ [0, -1 * strength, 0],
18
+ [-1 * strength, 1 + 4 * strength, -1 * strength],
19
+ [0, -1 * strength, 0],
20
+ ],
21
+ dtype=torch.float32,
22
+ ).unsqueeze(0).unsqueeze(0).to("cuda"))
23
+
24
+ # 分别对每个通道应用卷积核
25
+ sharpened_channels = []
26
+ for i in range(3):
27
+ channel_tensor = image_tensor[:, i:i + 1, :, :]
28
+ sharpened_channel = F.conv2d(channel_tensor,
29
+ sharpen_kernel,
30
+ padding=1)
31
+ sharpened_channels.append(sharpened_channel)
32
+
33
+ # 合并通道
34
+ sharpened_image_tensor = torch.cat(sharpened_channels, dim=1)
35
+
36
+ # 将增强后的图像转换回PIL格式
37
+ sharpened_image_np = (sharpened_image_tensor.squeeze(0).permute(
38
+ 1, 2, 0).cpu().numpy())
39
+ sharpened_image_np = np.clip(sharpened_image_np, 0,
40
+ 255).astype(np.uint8)
41
+ image = Image.fromarray(sharpened_image_np)
42
+ return image
feifeilib/feifeitexttoimg.py CHANGED
@@ -1,24 +1,11 @@
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(
@@ -36,39 +23,8 @@ def feifeitexttoimg(
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,
@@ -81,41 +37,5 @@ def feifeitexttoimg(
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
  import spaces
 
 
 
3
  from extras.expansion import FooocusExpansion
4
  from feifeilib.feifeimodload import feifeimodload
5
+ from feifeilib.feifeiprompt import feifeiprompt
6
+ from feifeilib.feifeisharpened import feifeisharpened
7
 
 
8
  pipe = feifeimodload()
 
 
 
 
 
 
 
9
 
10
  @spaces.GPU()
11
  def feifeitexttoimg(
 
23
  num_strength=0.35,
24
  progress=gr.Progress(track_tqdm=True),
25
  ):
26
+ prompt,generator = feifeiprompt(seed,prompt,quality_select,styles_Radio,FooocusExpansion_select)
27
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  image = pipe(
29
  prompt="",
30
  prompt_2=prompt,
 
37
  ).images[0]
38
 
39
  if sharpened_select:
40
+ feifeisharpened(image,num_strength)
41
+ return image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
feifeiui/feifeiui.py CHANGED
@@ -222,15 +222,21 @@ def create_ui():
222
  guidancescale,
223
  num_strength,
224
  ],
225
- outputs=[result, seed],
226
  )
227
  flux_button.click(
228
  fn=feifeifluxapi, # Function to run for this button
229
  inputs=[
 
230
  flux_prompt,
 
 
 
 
231
  flux_height,
232
  flux_width,
233
- flux_guidancescale
 
234
  ],
235
  outputs=[flux_result],
236
  )
 
222
  guidancescale,
223
  num_strength,
224
  ],
225
+ outputs=[result],
226
  )
227
  flux_button.click(
228
  fn=feifeifluxapi, # Function to run for this button
229
  inputs=[
230
+ flux_seed,
231
  flux_prompt,
232
+ quality_select,
233
+ sharpened_select,
234
+ styles_Radio,
235
+ FooocusExpansion_select,
236
  flux_height,
237
  flux_width,
238
+ flux_guidancescale,
239
+ num_strength
240
  ],
241
  outputs=[flux_result],
242
  )