RanM commited on
Commit
1d34edc
·
verified ·
1 Parent(s): b8a509c

Create generate_prompts.py

Browse files
Files changed (1) hide show
  1. generate_prompts.py +62 -0
generate_prompts.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ 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