File size: 3,179 Bytes
a340531
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Import necessary libraries
from transformers import pipeline
from PIL import Image, ImageDraw, ImageFont
from gradio import Interface, Textbox, Radio, Dataframe, File, Label

# Set up Hugging Face pipeline for text-to-image
text_to_image = pipeline('text2image')

# Define UI elements
textarea = Textbox(lines=5, placeholder="Enter your message here...")
radio_button = Radio([("Single", "single"), ("Married", "married"), ("Divorced", "divorced")])
dataframe = Dataframe(columns=["Item", "Cost"], datatype=["str", "number"])
file_upload = File(types=["image"])
label = Label()

# Define the interface
interface = Interface(
    fn=generate_image,
    inputs=[textarea, radio_button, dataframe, file_upload],
    outputs=label,
    capture_session=True,
    interpretation="default",
    examples=[
        ["Hello, world!", "Single", [[("Suit", 5000), ("Laptop", 800), ("Car", 1800)]], None],
        ["I love you.", "Married", [[("Suit", 800), ("Watch", 1800), ("Car", 800)]], None],
    ],
    title="Text-to-Image Generator",
    description="Generate an image based on text input",
)

# Generate image function
def generate_image(textarea, radio_button, dataframe, file_upload):
    # Convert text to image file
    text_file = textarea.value
    font_name = text_file.split(".")
    if len(font_name) > 1:
        font_name = font_name[1]
    else:
        font_name = "default"
    font = ImageFont.truetype(TEXT_FONT, 20)
    line_width = calculate_line_width(textarea.value, font)
    line_height = font.getsize(textarea.value)[1] * 2
    text_color = (0, 0, 0)
    text_image = Image.new("RGB", (line_width, line_height), (255, 255, 255))
    text_draw = ImageDraw.Draw(text_image)
    text_draw.text((0, 0), textarea.value, font=font, fill=text_color)
    text_image = text_image.convert("L")
    text_image = text_image.point(lambda x: 255 - x)

    # Merge text image with user image
    try:
        background_image = Image.open(file_upload.value)
    except Exception:
        background_image = None
    if background_image is not None:
        background_image = background_image.convert("L")
        text_image = text_image.resize(background_image.size)
        text_image = text_image.convert("RGB")
        text_image.paste(background_image, box=(0, 0), mask=text_image)

    # Add radio button selection to label
    if radio_button.selected_value == "married":
        label_text = "Your personalized wedding invitation is almost here!"
    elif radio_button.selected_value == "single":
        label_text = "This is the image of your special message"
    else:
        label_text = "You are getting divorced?"

    # Add assets to label
    label_text += " 😂"
    for item in dataframe.value:
        cost = item[1]
        label_text += f" {item[0]}-\${cost:,}"

    # Display the generated image
    generated_image.show(label_text, text_image)

# Define calculate_line_width function
def calculate_line_width(text, font):
    max_width = 0
    for line in text.split("\n"):
        line_width = font.getsize(line)[0]
        if line_width > max_width:
            max_width = line_width
    return max_width

# Run the interface
interface.launch()