2024-12-13 10:46:31 +00:00
|
|
|
import argparse
|
2024-12-13 09:35:47 +00:00
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import json
|
2024-12-13 10:46:31 +00:00
|
|
|
import configparser
|
2024-12-13 09:35:47 +00:00
|
|
|
import pycountry
|
|
|
|
import inflect
|
2024-12-13 10:46:31 +00:00
|
|
|
from lib.rtf_parser import RTF_Parser
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
from comfy_api_simplified import ComfyApiWrapper, ComfyWorkflowWrapper
|
|
|
|
|
|
|
|
# logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
|
|
|
|
2024-12-13 09:35:47 +00:00
|
|
|
|
2024-12-13 10:46:31 +00:00
|
|
|
# Load user configurations
|
2024-12-13 09:35:47 +00:00
|
|
|
user_config = configparser.ConfigParser()
|
2024-12-13 10:46:31 +00:00
|
|
|
user_config.read("config.cfg")
|
2024-12-13 09:35:47 +00:00
|
|
|
|
|
|
|
rtf = RTF_Parser()
|
|
|
|
p = inflect.engine()
|
|
|
|
|
|
|
|
|
2024-12-13 10:46:31 +00:00
|
|
|
def generate_image(uid,comfy_prompt,app_config,model):
|
|
|
|
"""Generate an image using the Comfy API."""
|
2024-12-13 09:35:47 +00:00
|
|
|
url = user_config["general"]["url"]
|
|
|
|
|
2024-12-13 10:46:31 +00:00
|
|
|
# Initialize API and workflow
|
|
|
|
api = ComfyApiWrapper(url)
|
|
|
|
wf = ComfyWorkflowWrapper("./workflow_api.json")
|
|
|
|
|
|
|
|
# Set workflow parameters
|
|
|
|
wf.set_node_param("KSampler", "seed", random.getrandbits(32))
|
|
|
|
wf.set_node_param("KSampler", "steps", 6)
|
|
|
|
wf.set_node_param("positive", "text", comfy_prompt)
|
|
|
|
wf.set_node_param("Save Image", "filename_prefix", uid)
|
|
|
|
wf.set_node_param("Load Checkpoint", "ckpt_name", model)
|
|
|
|
|
|
|
|
# queue your workflow for completion
|
|
|
|
results = api.queue_and_wait_images(wf, "Save Image")
|
|
|
|
for filename, image_data in results.items():
|
|
|
|
with open(f"./generated_images/{uid}.png", "wb+") as f:
|
|
|
|
f.write(image_data)
|
2024-12-13 09:35:47 +00:00
|
|
|
|
|
|
|
def get_country_name_from_code(code):
|
2024-12-13 10:46:31 +00:00
|
|
|
"""Get country name from 3-letter ISO code."""
|
2024-12-13 09:35:47 +00:00
|
|
|
try:
|
|
|
|
country = pycountry.countries.get(alpha_3=code.upper())
|
|
|
|
return country.name if country else "Unknown country code"
|
|
|
|
except KeyError:
|
|
|
|
return "Invalid country code"
|
|
|
|
|
2024-12-13 10:46:31 +00:00
|
|
|
|
|
|
|
def generate_prompts_for_players(players, app_config):
|
|
|
|
"""Generate images for a specific player and configuration."""
|
|
|
|
prompts = []
|
|
|
|
for player in players[:100]:
|
|
|
|
print(f"\nGenerating image for {player[0]} - {player[8]}")
|
|
|
|
folder_name = f"generated_images/"
|
|
|
|
os.makedirs(folder_name, exist_ok=True)
|
|
|
|
|
2024-12-13 09:35:47 +00:00
|
|
|
country = get_country_name_from_code(player[1])
|
2024-12-13 10:46:31 +00:00
|
|
|
facial_characteristics = random.choice(app_config["facial_characteristics"])
|
|
|
|
hair_length = app_config["hair_length"][player[5]]
|
|
|
|
hair_colour = app_config["hair_color"][player[6]]
|
|
|
|
skin_tone = app_config["skin_tone_map"][player[7]]
|
2024-12-13 09:35:47 +00:00
|
|
|
player_age = p.number_to_words(player[3])
|
2024-12-13 10:46:31 +00:00
|
|
|
hair = random.choice(app_config["hair"])
|
2024-12-13 09:35:47 +00:00
|
|
|
|
2024-12-13 10:46:31 +00:00
|
|
|
# Format the prompt
|
|
|
|
prompt = app_config["prompt"].format(
|
2024-12-13 09:35:47 +00:00
|
|
|
skin_tone=skin_tone,
|
|
|
|
age=player_age,
|
|
|
|
country=country,
|
2024-12-13 10:46:31 +00:00
|
|
|
facial_characteristics=facial_characteristics or "no facial hair",
|
2024-12-13 09:35:47 +00:00
|
|
|
hair=f"{hair_length} {hair_colour} {hair}",
|
|
|
|
)
|
|
|
|
print(f"Generated prompt: {prompt}")
|
2024-12-13 10:46:31 +00:00
|
|
|
prompt = f"{player[0]}:{prompt}"
|
|
|
|
prompts.append(prompt)
|
|
|
|
return prompts
|
2024-12-13 09:35:47 +00:00
|
|
|
|
2024-12-13 10:46:31 +00:00
|
|
|
def main():
|
|
|
|
"""Main function for generating images."""
|
2024-12-13 09:35:47 +00:00
|
|
|
parser = argparse.ArgumentParser(description="Generate images for country groups")
|
2024-12-13 10:46:31 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--rtf_file",
|
|
|
|
type=str,
|
|
|
|
default=None,
|
|
|
|
help="Path to the RTF file to be processed",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--num_inference_steps",
|
|
|
|
type=int,
|
|
|
|
default=6,
|
|
|
|
help="Number of inference steps. Defaults to 6",
|
|
|
|
)
|
2024-12-13 09:35:47 +00:00
|
|
|
args = parser.parse_args()
|
2024-12-13 10:46:31 +00:00
|
|
|
|
2024-12-13 09:35:47 +00:00
|
|
|
if not args.rtf_file:
|
|
|
|
raise Exception("Please pass in a RTF file as --rtf_file")
|
2024-12-13 10:46:31 +00:00
|
|
|
|
|
|
|
# Parse the RTF file
|
2024-12-13 09:35:47 +00:00
|
|
|
rtf_file = rtf.parse_rtf(args.rtf_file)
|
|
|
|
|
2024-12-13 10:46:31 +00:00
|
|
|
# Extract unique values
|
|
|
|
hair_length = list(set(item[5] for item in rtf_file))
|
|
|
|
hair_colour = list(set(item[6] for item in rtf_file))
|
|
|
|
skin_tone = list(set(item[7] for item in rtf_file))
|
|
|
|
|
|
|
|
# Load configurations
|
2024-12-13 09:35:47 +00:00
|
|
|
with open("config.json", "r") as f:
|
|
|
|
app_config = json.load(f)
|
|
|
|
|
2024-12-13 10:46:31 +00:00
|
|
|
prompts = generate_prompts_for_players(rtf_file, app_config)
|
|
|
|
for prompt in prompts:
|
|
|
|
uid = prompt.split(":")[0]
|
|
|
|
comfy_prompt = prompt.split(":")[1]
|
|
|
|
generate_image(
|
|
|
|
uid,
|
|
|
|
comfy_prompt,
|
|
|
|
app_config,
|
|
|
|
user_config["general"]["model"],
|
|
|
|
)
|
2024-12-13 09:35:47 +00:00
|
|
|
|
|
|
|
print("\nImage generation complete for all country groups.")
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-12-13 10:46:31 +00:00
|
|
|
main()
|