RanM commited on
Commit
0d6192e
1 Parent(s): 11d38a0

Update generate_prompts.py

Browse files
Files changed (1) hide show
  1. generate_prompts.py +39 -32
generate_prompts.py CHANGED
@@ -3,53 +3,61 @@ import re
3
 
4
  def insert_description(sentence, character, description):
5
  """
6
- Integrates the character and its description at the beginning of the sentence if the character is mentioned.
7
 
8
  Parameters:
9
- - sentence (str): The original sentence.
10
- - character (str): The character to be described.
11
- - description (str): The description of the character.
12
 
13
  Returns:
14
- str: The sentence modified to include the character and description at the beginning.
15
  """
16
- # Inserts character and description at the beginning of the sentence if the character is found.
17
- character = character.lower()
18
- # Remove everything after the newline character
19
- cleaned_description = re.sub(r'\n.*', '', description)
20
- # Remove non-alphabetic characters and quotes from the description
21
- cleaned_description = re.sub(r'[^a-zA-Z\s,]', '', cleaned_description).replace("'", '').replace('"', '')
22
- # Check if the character appears in the sentence
23
- if character in sentence.lower():
24
- # Insert the character and its description at the beginning of the sentence
25
- modified_sentence = f"{character}: {cleaned_description.strip()}. {sentence}"
26
- return modified_sentence
27
- else:
28
- return sentence
29
 
30
  def process_text(sentence, character_dict):
31
  """
32
  Enhances the given sentence by incorporating descriptions for each mentioned character.
 
33
 
34
  Parameters:
35
- - sentence (str): The original sentence.
36
- - character_dict (dict): Dictionary mapping characters to their descriptions.
37
 
38
  Returns:
39
- str: The sentence with integrated character descriptions.
40
  """
41
- # Modifies sentences in the given text based on character descriptions.
42
- modified_sentence = sentence # Initialize with the original sentence
 
 
 
 
 
 
43
 
44
- # Iterate through each character in the dictionary
45
- for character, descriptions in character_dict.items():
46
- for description in descriptions:
47
- # Update the sentence with the character and its description
48
  modified_sentence = insert_description(modified_sentence, character, description)
49
- return modified_sentence
 
 
 
 
 
 
50
 
51
 
52
- def generate_prompt(text, sentence_mapping, character_dict, selected_style):
53
  """
54
  Generates a prompt for image generation based on the selected style and input text.
55
 
@@ -61,9 +69,8 @@ def generate_prompt(text, sentence_mapping, character_dict, selected_style):
61
  tuple: A prompt string.
62
  """
63
  # Retrieve the enhanced sentence associated with the original text
64
- enhanced_sentence = sentence_mapping.get(text, text)
65
- image_descriptions = process_text(enhanced_sentence, character_dict)
66
  # Define prompts and other parameters
67
- prompt = f"Make an illustration in {selected_style} style from: {image_descriptions}"
68
  return prompt
69
 
 
3
 
4
  def insert_description(sentence, character, description):
5
  """
6
+ Integrates the character, its type, and its description within the sentence right after the character's name is mentioned.
7
 
8
  Parameters:
9
+ - sentence (str): The original sentence where the description is to be inserted.
10
+ - character (str): The character whose description is to be added.
11
+ - description (dict): The dictionary containing the character's type and descriptive words.
12
 
13
  Returns:
14
+ str: The modified sentence with the character's description if the character is present; otherwise, returns the original sentence.
15
  """
16
+ character_lower = character.lower()
17
+
18
+ # Use regex to find and replace the character's name with the name plus the description
19
+ modified_sentence = re.sub(
20
+ fr"\b{character}\b",
21
+ fr"{character.capitalize()}{description}",
22
+ sentence,
23
+ flags=re.IGNORECASE
24
+ )
25
+ return modified_sentence
 
 
 
26
 
27
  def process_text(sentence, character_dict):
28
  """
29
  Enhances the given sentence by incorporating descriptions for each mentioned character.
30
+ Falls back to the original sentence if `character_dict` is empty.
31
 
32
  Parameters:
33
+ - sentence (str): The original sentence to be processed.
34
+ - character_dict (dict): A dictionary mapping characters to their respective descriptions.
35
 
36
  Returns:
37
+ str: The sentence modified to include character descriptions where applicable, or the original sentence.
38
  """
39
+ try:
40
+ # If character_dict is empty, return the original sentence
41
+ if not character_dict:
42
+ print("Character descriptions are empty, returning the original sentence.")
43
+ return sentence
44
+
45
+ # Start with the original sentence
46
+ modified_sentence = sentence
47
 
48
+ for character, description in character_dict.items():
49
+ # Insert description into the sentence where the character is mentioned
 
 
50
  modified_sentence = insert_description(modified_sentence, character, description)
51
+
52
+ print(f'modified_sentence: {modified_sentence}')
53
+ return modified_sentence
54
+
55
+ except Exception as e:
56
+ print(f"Error processing text: {e}. Returning original sentence.")
57
+ return sentence
58
 
59
 
60
+ def generate_prompt(sentence, character_dict, selected_style):
61
  """
62
  Generates a prompt for image generation based on the selected style and input text.
63
 
 
69
  tuple: A prompt string.
70
  """
71
  # Retrieve the enhanced sentence associated with the original text
72
+ image_descriptions = process_text(sentence, character_dict)
 
73
  # Define prompts and other parameters
74
+ prompt = f"Create an illustration in {selected_style} style from: {image_descriptions}"
75
  return prompt
76