Spaces:
Sleeping
Sleeping
Update clothGen.py
Browse files- clothGen.py +39 -21
clothGen.py
CHANGED
@@ -8,52 +8,55 @@ nv_prompt_file = pd.read_excel('汉服-女词库.xlsx')
|
|
8 |
na_prompt_file = pd.read_excel('汉服-男词库.xlsx')
|
9 |
nv_prompt = nv_prompt_file.to_string(index=False)
|
10 |
na_prompt = na_prompt_file.to_string(index=False)
|
|
|
11 |
|
12 |
|
13 |
-
def
|
14 |
-
lora_path = "https://huggingface.co/PPSharks/PPSharksModels/resolve/main/NV.safetensors"
|
15 |
-
if gender == "男":
|
16 |
-
lora_path = "https://huggingface.co/PPSharks/PPSharksModels/resolve/main/NA.safetensors"
|
17 |
-
elif gender == "女":
|
18 |
-
lora_path = "https://huggingface.co/PPSharks/PPSharksModels/resolve/main/NV.safetensors"
|
19 |
-
|
20 |
prompt = prompt_gen(advice, gender)
|
21 |
start_index = prompt.find("Begin")
|
22 |
if start_index == -1:
|
23 |
start_index = prompt.find("begin")
|
24 |
intro_index = prompt.find("服饰风格介绍")
|
25 |
cloth_intro = ""
|
26 |
-
|
27 |
if start_index != -1:
|
28 |
start_index += len("Begin\n")
|
29 |
-
end_index = prompt.find("End"
|
30 |
if end_index != -1:
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
34 |
else:
|
35 |
-
print("No '
|
36 |
else:
|
37 |
-
print("No '
|
38 |
if intro_index != -1:
|
39 |
intro_index += len("服饰风格介绍\n")
|
40 |
cloth_intro = ("汉服,是汉民族的传统服饰。又称衣冠、衣裳、汉装。汉服是中国“衣冠上国”“礼仪之邦”“锦绣中华”的体现,承载了中国的染织绣等杰出"
|
41 |
"工艺和美学,传承了30多项中国非物质文化遗产以及受保护的中国工艺美术。\n") + prompt[intro_index:]
|
|
|
|
|
|
|
42 |
print(cloth_intro)
|
43 |
else:
|
44 |
print("No '服饰风格介绍' found.")
|
|
|
|
|
45 |
|
|
|
46 |
handler = fal_client.submit(
|
47 |
"fal-ai/fast-sdxl",
|
48 |
arguments={
|
49 |
-
"prompt":
|
50 |
-
"negative_prompt": "
|
51 |
"feet, hair, portrait, worst quality, low quality, normal quality, lowres, signature, "
|
52 |
"watermark, jpeg artifacts, logo, monochrome, grayscale, ugly",
|
53 |
"image_size": "portrait_4_3",
|
54 |
"num_inference_steps": 28,
|
55 |
"guidance_scale": 7.5,
|
56 |
-
"num_images":
|
57 |
"loras": [{"path": lora_path, "scale": 0.7}],
|
58 |
"embeddings": [],
|
59 |
"safety_checker_version": "v1",
|
@@ -63,17 +66,32 @@ def cloth_gen(advice, gender):
|
|
63 |
|
64 |
request_id = handler.request_id
|
65 |
result = fal_client.result("fal-ai/fast-sdxl", request_id)
|
66 |
-
|
67 |
-
save_directory = "downloads"
|
68 |
-
image_index = 1
|
69 |
for image in result['images']:
|
70 |
response = requests.get(image['url'])
|
71 |
if response.status_code == 200:
|
72 |
filename = os.path.join(save_directory, f"gen_cloth_{image_index}.jpeg")
|
73 |
-
cloth_image.append(filename)
|
74 |
with open(filename, 'wb') as f:
|
75 |
f.write(response.content)
|
76 |
image_index += 1
|
77 |
else:
|
78 |
print(f"Failed to download image from {image['url']}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
return cloth_image, cloth_image[0], cloth_intro
|
|
|
8 |
na_prompt_file = pd.read_excel('汉服-男词库.xlsx')
|
9 |
nv_prompt = nv_prompt_file.to_string(index=False)
|
10 |
na_prompt = na_prompt_file.to_string(index=False)
|
11 |
+
save_directory = "downloads"
|
12 |
|
13 |
|
14 |
+
def pro_gen(advice, gender, index):
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
prompt = prompt_gen(advice, gender)
|
16 |
start_index = prompt.find("Begin")
|
17 |
if start_index == -1:
|
18 |
start_index = prompt.find("begin")
|
19 |
intro_index = prompt.find("服饰风格介绍")
|
20 |
cloth_intro = ""
|
21 |
+
prompt__gen = ""
|
22 |
if start_index != -1:
|
23 |
start_index += len("Begin\n")
|
24 |
+
end_index = prompt.find("End")
|
25 |
if end_index != -1:
|
26 |
+
prompt__gen = prompt[start_index:end_index]
|
27 |
+
filename = os.path.join(save_directory, f"prompt_{index}.txt")
|
28 |
+
with open(filename, "w") as file:
|
29 |
+
file.write(prompt__gen)
|
30 |
+
print(prompt__gen)
|
31 |
else:
|
32 |
+
print("No 'promptEnd' found after 'prompt'.")
|
33 |
else:
|
34 |
+
print("No 'prompt' found in the text.")
|
35 |
if intro_index != -1:
|
36 |
intro_index += len("服饰风格介绍\n")
|
37 |
cloth_intro = ("汉服,是汉民族的传统服饰。又称衣冠、衣裳、汉装。汉服是中国“衣冠上国”“礼仪之邦”“锦绣中华”的体现,承载了中国的染织绣等杰出"
|
38 |
"工艺和美学,传承了30多项中国非物质文化遗产以及受保护的中国工艺美术。\n") + prompt[intro_index:]
|
39 |
+
filename = os.path.join(save_directory, f"cloth_intro_{index}.txt")
|
40 |
+
with open(filename, "w") as file:
|
41 |
+
file.write(cloth_intro)
|
42 |
print(cloth_intro)
|
43 |
else:
|
44 |
print("No '服饰风格介绍' found.")
|
45 |
+
return prompt__gen
|
46 |
+
|
47 |
|
48 |
+
def generate(lora_path, prompt__gen, index):
|
49 |
handler = fal_client.submit(
|
50 |
"fal-ai/fast-sdxl",
|
51 |
arguments={
|
52 |
+
"prompt": prompt__gen,
|
53 |
+
"negative_prompt": "human, people, person, man, woman, child, model, face, head, eyes, hands, arms, legs, "
|
54 |
"feet, hair, portrait, worst quality, low quality, normal quality, lowres, signature, "
|
55 |
"watermark, jpeg artifacts, logo, monochrome, grayscale, ugly",
|
56 |
"image_size": "portrait_4_3",
|
57 |
"num_inference_steps": 28,
|
58 |
"guidance_scale": 7.5,
|
59 |
+
"num_images": 2,
|
60 |
"loras": [{"path": lora_path, "scale": 0.7}],
|
61 |
"embeddings": [],
|
62 |
"safety_checker_version": "v1",
|
|
|
66 |
|
67 |
request_id = handler.request_id
|
68 |
result = fal_client.result("fal-ai/fast-sdxl", request_id)
|
69 |
+
image_index = index * 2 - 1
|
|
|
|
|
70 |
for image in result['images']:
|
71 |
response = requests.get(image['url'])
|
72 |
if response.status_code == 200:
|
73 |
filename = os.path.join(save_directory, f"gen_cloth_{image_index}.jpeg")
|
|
|
74 |
with open(filename, 'wb') as f:
|
75 |
f.write(response.content)
|
76 |
image_index += 1
|
77 |
else:
|
78 |
print(f"Failed to download image from {image['url']}")
|
79 |
+
|
80 |
+
|
81 |
+
def cloth_gen(gender):
|
82 |
+
lora_path = "https://huggingface.co/PPSharks/PPSharksModels/resolve/main/NV.safetensors"
|
83 |
+
if gender == "男":
|
84 |
+
lora_path = "https://huggingface.co/PPSharks/PPSharksModels/resolve/main/NA.safetensors"
|
85 |
+
else:
|
86 |
+
lora_path = "https://huggingface.co/PPSharks/PPSharksModels/resolve/main/NV.safetensors"
|
87 |
+
|
88 |
+
cloth_image = []
|
89 |
+
for i in range(1, 4):
|
90 |
+
with open(os.path.join(save_directory, f"prompt_{i}.txt"), "r") as file:
|
91 |
+
prompt__gen = file.read()
|
92 |
+
generate(lora_path, prompt__gen, i)
|
93 |
+
cloth_image.append(os.path.join(save_directory, f"gen_cloth_{i*2-1}.jpeg"))
|
94 |
+
cloth_image.append(os.path.join(save_directory, f"gen_cloth_{i*2}.jpeg"))
|
95 |
+
with open(os.path.join(save_directory, f"cloth_intro_1.txt"), "r") as file:
|
96 |
+
cloth_intro = file.read()
|
97 |
return cloth_image, cloth_image[0], cloth_intro
|