-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt_loader.py
More file actions
133 lines (113 loc) · 4.85 KB
/
Copy pathprompt_loader.py
File metadata and controls
133 lines (113 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
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
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
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
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
# coding=utf-8
# Copyright 2024 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import csv
import json
class PromptLoader:
def __init__(
self,
prompt_file: str,
prompt_file_type: str,
batch_size: int = 1,
num_images_per_prompt: int = 1,
max_num_prompts: int = 0
):
self.check_input_isvalid(batch_size, num_images_per_prompt, max_num_prompts)
self.prompts = []
self.catagories = ['Not_specified']
self.batch_size = batch_size
self.num_images_per_prompt = num_images_per_prompt
if prompt_file_type == 'plain':
self.load_prompts_plain(prompt_file, max_num_prompts)
elif prompt_file_type == 'parti':
self.load_prompts_parti(prompt_file, max_num_prompts)
elif prompt_file_type == 'hpsv2':
self.load_prompts_hpsv2(prompt_file, max_num_prompts)
else:
print("This operation is not supported!")
self.current_id = 0
self.inner_id = 0
def __len__(self):
return len(self.prompts) * self.num_images_per_prompt
def __iter__(self):
return self
def __next__(self):
if self.current_id == len(self.prompts):
raise StopIteration
ret = {
'prompts': [],
'catagories': [],
'save_names': [],
'n_prompts': self.batch_size,
}
for _ in range(self.batch_size):
if self.current_id == len(self.prompts):
ret['prompts'].append('')
ret['save_names'].append('')
ret['catagories'].append('')
ret['n_prompts'] -= 1
else:
prompt, catagory_id = self.prompts[self.current_id]
ret['prompts'].append(prompt)
ret['catagories'].append(self.catagories[catagory_id])
ret['save_names'].append(f'{self.current_id}_{self.inner_id}')
self.inner_id += 1
if self.inner_id == self.num_images_per_prompt:
self.inner_id = 0
self.current_id += 1
return ret
def load_prompts_plain(self, file_path: str, max_num_prompts: int):
with os.fdopen(os.open(file_path, os.O_RDONLY), "r") as f:
for i, line in enumerate(f):
if max_num_prompts and i == max_num_prompts:
break
prompt = line.strip()
self.prompts.append((prompt, 0))
def load_prompts_parti(self, file_path: str, max_num_prompts: int):
with os.fdopen(os.open(file_path, os.O_RDONLY), "r") as f:
# Skip the first line
next(f)
tsv_file = csv.reader(f, delimiter="\t")
for i, line in enumerate(tsv_file):
if max_num_prompts and i == max_num_prompts:
break
prompt = line[0]
catagory = line[1]
if catagory not in self.catagories:
self.catagories.append(catagory)
catagory_id = self.catagories.index(catagory)
self.prompts.append((prompt, catagory_id))
def load_prompts_hpsv2(self, file_path: str, max_num_prompts: int):
with open(file_path, 'r') as file:
all_prompts = json.load(file)
count = 0
for style, prompts in all_prompts.items():
for prompt in prompts:
count += 1
if max_num_prompts and count >= max_num_prompts:
break
if style not in self.catagories:
self.catagories.append(style)
catagory_id = self.catagories.index(style)
self.prompts.append((prompt, catagory_id))
def check_input_isvalid(self, batch_size, num_images_per_prompt, max_num_prompts):
if batch_size <= 0:
raise ValueError(f"Param batch_size invalid, expected positive value, but get {batch_size}")
if num_images_per_prompt <= 0:
raise ValueError(f"Param num_images_per_prompt invalid, expected positive value, but get {num_images_per_prompt}")
if max_num_prompts < 0:
raise ValueError(f"Param max_num_prompts invalid, expected greater than or equal to 0, \
but get {max_num_prompts}")