text2image_1 / generate_prompts.py
RanM's picture
Rename generate_propmts.py to generate_prompts.py
20e0b1b verified
raw
history blame
2.86 kB
import base64
import re
def insert_description(sentence, character, description):
"""
Integrates the character and its description at the beginning of the sentence if the character is mentioned.
Parameters:
- sentence (str): The original sentence.
- character (str): The character to be described.
- description (str): The description of the character.
Returns:
str: The sentence modified to include the character and description at the beginning.
"""
# Inserts character and description at the beginning of the sentence if the character is found.
character = character.lower()
# Remove everything after the newline character
cleaned_description = re.sub(r'\n.*', '', description)
# Remove non-alphabetic characters and quotes from the description
cleaned_description = re.sub(r'[^a-zA-Z\s,]', '', cleaned_description).replace("'", '').replace('"', '')
# Check if the character appears in the sentence
if character in sentence.lower():
# Insert the character and its description at the beginning of the sentence
modified_sentence = f"{character}: {cleaned_description.strip()}. {sentence}"
return modified_sentence
else:
return sentence
def process_text(sentence, character_dict):
"""
Enhances the given sentence by incorporating descriptions for each mentioned character.
Parameters:
- sentence (str): The original sentence.
- character_dict (dict): Dictionary mapping characters to their descriptions.
Returns:
str: The sentence with integrated character descriptions.
"""
# Modifies sentences in the given text based on character descriptions.
modified_sentence = sentence # Initialize with the original sentence
# Iterate through each character in the dictionary
for character, descriptions in character_dict.items():
for description in descriptions:
# Update the sentence with the character and its description
modified_sentence = insert_description(modified_sentence, character, description)
return modified_sentence
def generate_prompt(text, sentence_mapping, character_dict, selected_style):
"""
Generates a prompt for image generation based on the selected style and input text.
Parameters:
- style (str): The chosen illustration style.
- text (str): The input text for the illustration.
Returns:
tuple: A prompt string.
"""
# Retrieve the enhanced sentence associated with the original text
enhanced_sentence = sentence_mapping.get(text, text)
image_descriptions = process_text(enhanced_sentence, character_dict)
# Define prompts and other parameters
prompt = f"Make an illustration in {selected_style} style from: {image_descriptions}"
return prompt