-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
197 lines (161 loc) · 6.71 KB
/
main.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import subprocess
import os
import argparse
import sys
import yaml
import logging
# Set up logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
project_root = os.path.dirname(os.path.abspath(__file__))
if project_root not in sys.path:
sys.path.append(project_root)
def load_characters(config_path):
try:
with open(config_path, 'r') as file:
characters = yaml.safe_load(file)
if not characters:
logger.warning(f"No characters found in {config_path}")
return characters or {}
except FileNotFoundError:
logger.error(f"Configuration file not found at {config_path}.")
return {}
except yaml.YAMLError as exc:
logger.error(f"Error parsing YAML file: {exc}")
return {}
def get_prompt_auto(characters, character_name=None):
if not characters:
logger.error("No characters available to generate a prompt.")
return None
if not character_name:
logger.error("No character name provided.")
return None
if character_name not in characters:
logger.error(f"Character '{character_name}' not found in the configuration file.")
return None
generate_prompt_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "generate", "generate_prompt.py")
if not os.path.exists(generate_prompt_path):
logger.error(f"Generate prompt script not found at: {generate_prompt_path}")
return None
try:
env = os.environ.copy()
env['PYTHONPATH'] = project_root
logger.debug(f"Running generate_prompt.py for character: {character_name}")
result = subprocess.run(
["python3", generate_prompt_path, character_name, "--mode", "auto"],
capture_output=True,
text=True,
check=True,
env=env
)
# Get the last non-empty line as the prompt
prompt_lines = [line.strip() for line in result.stdout.split('\n') if line.strip()]
if prompt_lines:
prompt = prompt_lines[-1]
logger.info(f"Generated prompt: {prompt}")
return prompt
else:
logger.error("No prompt generated (empty output)")
return None
except subprocess.CalledProcessError as e:
logger.error(f"Error running generate_prompt.py: {e.stderr.strip()}")
return None
def get_prompt_enhanced(characters, character_name=None, user_prompt=None):
if not characters or not character_name or not user_prompt:
logger.error("Missing required information for enhanced prompt generation.")
return None
if character_name not in characters:
logger.error(f"Character '{character_name}' not found in the configuration file.")
return None
generate_prompt_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "generate", "generate_prompt.py")
if not os.path.exists(generate_prompt_path):
logger.error(f"Generate prompt script not found at: {generate_prompt_path}")
return None
try:
env = os.environ.copy()
env['PYTHONPATH'] = project_root
logger.debug(f"Running enhanced prompt generation for character: {character_name}")
result = subprocess.run(
["python3", generate_prompt_path, character_name, "--mode", "enhanced"],
input=user_prompt,
capture_output=True,
text=True,
check=True,
env=env
)
prompt_lines = [line.strip() for line in result.stdout.split('\n') if line.strip()]
if prompt_lines:
prompt = prompt_lines[-1]
logger.info(f"Generated enhanced prompt: {prompt}")
return prompt
else:
logger.error("No prompt generated (empty output)")
return None
except subprocess.CalledProcessError as e:
logger.error(f"Error running generate_prompt.py: {e.stderr.strip()}")
return None
def generate_images(prompt, character_name=None):
if not prompt:
logger.error("Cannot generate images without a prompt.")
return False
if not character_name:
logger.error("Cannot generate images without a character name.")
return False
queue_script = os.path.join(os.path.dirname(os.path.abspath(__file__)), "generate", "queue_and_retrieve_images.py")
if not os.path.exists(queue_script):
logger.error(f"Queue script not found at: {queue_script}")
return False
try:
env = os.environ.copy()
env['PYTHONPATH'] = project_root
logger.debug(f"Generating images for character {character_name} with prompt: {prompt}")
result = subprocess.run(
["python3", queue_script, prompt, character_name],
capture_output=True,
text=True,
check=True,
env=env
)
logger.info("Image generation completed successfully")
return True
except subprocess.CalledProcessError as e:
logger.error(f"Error generating images: {e.stderr.strip()}")
return False
def main():
parser = argparse.ArgumentParser(description="Generate images based on prompts.")
parser.add_argument("mode", choices=["auto", "manual", "enhanced"],
help="Choose 'auto' for random generation, 'manual' for direct prompt, or 'enhanced' for combined generation.")
parser.add_argument("--character", type=str, help="Name of the character.")
args = parser.parse_args()
if not args.character:
logger.error("Character name is required.")
sys.exit(1)
config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config', 'characters.yaml')
characters = load_characters(config_path)
if not characters:
logger.error("No characters loaded from configuration.")
sys.exit(1)
if args.character not in characters:
logger.error(f"Character '{args.character}' not found in configuration.")
sys.exit(1)
if args.mode == "auto":
prompt = get_prompt_auto(characters, args.character)
elif args.mode == "enhanced":
user_prompt = sys.stdin.read().strip()
prompt = get_prompt_enhanced(characters, args.character, user_prompt)
else: # manual mode
prompt = sys.stdin.read().strip()
logger.info(f"Using manual prompt: {prompt}")
if not prompt:
logger.error("No prompt was generated or entered.")
sys.exit(1)
# Always print the prompt to stdout for capture by the calling process
print(prompt)
if not generate_images(prompt, args.character):
logger.error("Failed to generate images.")
sys.exit(1)
if __name__ == "__main__":
main()