RanM commited on
Commit
2b6ca8f
·
verified ·
1 Parent(s): 5a982ce

Update generate_prompts.py

Browse files
Files changed (1) hide show
  1. generate_prompts.py +46 -32
generate_prompts.py CHANGED
@@ -3,60 +3,74 @@ 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
  Parameters:
8
- - sentence (str): The original sentence.
9
- - character (str): The character to be described.
10
- - description (str): The description of the character.
 
11
  Returns:
12
- str: The sentence modified to include the character and description at the beginning.
13
  """
14
- # Inserts character and description at the beginning of the sentence if the character is found.
15
- character = character.lower()
16
- # Remove everything after the newline character
17
- cleaned_description = re.sub(r'\n.*', '', description)
18
- # Remove non-alphabetic characters and quotes from the description
19
- cleaned_description = re.sub(r'[^a-zA-Z\s,]', '', cleaned_description).replace("'", '').replace('"', '')
20
- # Check if the character appears in the sentence
21
- if character in sentence.lower():
22
- # Insert the character and its description at the beginning of the sentence
23
- modified_sentence = f"{character}: {cleaned_description.strip()}. {sentence}"
24
- return modified_sentence
25
- else:
26
- return sentence
27
 
28
  def process_text(sentence, character_dict):
29
  """
30
  Enhances the given sentence by incorporating descriptions for each mentioned character.
 
 
31
  Parameters:
32
- - sentence (str): The original sentence.
33
- - character_dict (dict): Dictionary mapping characters to their descriptions.
 
34
  Returns:
35
- str: The sentence with integrated character descriptions.
36
  """
37
- # Modifies sentences in the given text based on character descriptions.
38
- modified_sentence = sentence # Initialize with the original sentence
 
 
 
 
 
 
39
 
40
- # Iterate through each character in the dictionary
41
- for character, descriptions in character_dict.items():
42
- for description in descriptions:
43
- # Update the sentence with the character and its description
44
  modified_sentence = insert_description(modified_sentence, character, description)
45
- return modified_sentence
 
 
 
 
 
 
46
 
47
 
48
- def generate_prompt(text, sentence_mapping, character_dict, selected_style):
49
  """
50
  Generates a prompt for image generation based on the selected style and input text.
 
51
  Parameters:
52
  - style (str): The chosen illustration style.
53
  - text (str): The input text for the illustration.
 
54
  Returns:
55
  tuple: A prompt string.
56
  """
57
  # Retrieve the enhanced sentence associated with the original text
58
- enhanced_sentence = sentence_mapping.get(text, text)
59
- image_descriptions = process_text(enhanced_sentence, character_dict)
60
  # Define prompts and other parameters
61
- prompt = f"Make an illustration in {selected_style} style from: {image_descriptions}"
62
  return prompt
 
 
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
+
64
  Parameters:
65
  - style (str): The chosen illustration style.
66
  - text (str): The input text for the illustration.
67
+
68
  Returns:
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
+