Spaces:
Runtime error
Runtime error
Update generate_prompts.py
Browse files- 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
|
7 |
|
8 |
Parameters:
|
9 |
-
- sentence (str): The original sentence.
|
10 |
-
- character (str): The character to be
|
11 |
-
- description (
|
12 |
|
13 |
Returns:
|
14 |
-
str: The sentence
|
15 |
"""
|
16 |
-
|
17 |
-
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
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):
|
37 |
|
38 |
Returns:
|
39 |
-
str: The sentence
|
40 |
"""
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
|
52 |
-
def generate_prompt(
|
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 |
-
|
65 |
-
image_descriptions = process_text(enhanced_sentence, character_dict)
|
66 |
# Define prompts and other parameters
|
67 |
-
prompt = f"
|
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 |
|