File size: 4,643 Bytes
32dda0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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 and negative 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 tuple containing the prompt and negative prompt strings.
    """
    # 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}"
    negative_prompt = "lowres, bad anatomy, bad hands, text, chat box, words, error, missing fingers, extra digit, " \
                      "fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, " \
                      "watermark, username, blurry "
    return prompt, negative_prompt

def get_image_from_space(text, sentence_mapping, character_dict, selected_style, client):
    """
    Requests an image from a Hugging Face space based on the provided prompt.

    Parameters:
        - prompt (str): The text prompt for image generation.
        - negative_prompt (str): Text specifying what to avoid in the image.

    Returns:
        bytes: The generated image data in bytes format, or None if the request fails.
    """
    image_bytes = None  # Initialize image bytes to None for error handling
    try:
        with st.spinner("注讜讚 讻诪讛 专讙注讬诐 讜讛讗讬讜专 讬讜驻讬注"):
            # Define the payload with the text prompt
            prompt,_ = generate_prompt(text, sentence_mapping, character_dict, selected_style)
            payload = {
                "inputs": prompt
            }
            result = client.predict(
                prompt=payload,
                api_name="/predict"
            )

            # Check if the result is a base64 encoded string
            if isinstance(result, str):
                image_bytes = base64.b64decode(result)
                return image_bytes
            else:
                st.error("讗讜讬 诇讗 谞讬转谉 诇讬讬爪专 转诪讜谞讛, 讬砖 诇谞住讜转 砖讜讘 讘讛诪砖讱")
    except Exception as e:
        print(f"讗讜讬 诇讗 谞讬转谉 诇讬讬爪专 转诪讜谞讛, 讬砖 诇谞住讜转 砖讜讘 讘讛诪砖讱: {e}")
        return image_bytes