File size: 1,826 Bytes
f930eeb
 
 
 
 
e29b927
86b9624
 
f930eeb
 
 
86b9624
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f930eeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from diffusers import DiffusionPipeline
from PIL import Image, ImageDraw, ImageFont

# Load the model (make sure to use a model that exists on Hugging Face)
device = "cuda" if torch.cuda.is_available() else "cpu"
model = DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float32).to(device)

def generate_image(caption):
    # Generate the image from the caption
    try:
        with torch.no_grad():
            image = model(caption).images[0]
        
        # Convert to PIL Image for drawing
        image = image.convert("RGBA")
        
        # Create a draw object
        draw = ImageDraw.Draw(image)
        
        # Define font size and color
        font_size = 40
        font_color = "white"
        
        # Load a font
        font = ImageFont.load_default()  # You can specify a TTF font file if needed
        
        # Calculate text size and position
        text_width, text_height = draw.textsize(caption, font=font)
        text_position = ((image.width - text_width) // 2, 10)  # Centered at the top
        
        # Draw the text on the image
        draw.text(text_position, caption, font=font, fill=font_color)
        
        return image
    except Exception as e:
        print(f"Error generating image: {e}")
        return None

# Create the Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# Text to Image Generation with Meme Caption")
    
    with gr.Row():
        caption_input = gr.Textbox(label="Enter your caption", placeholder="Type your caption here...")
        generate_button = gr.Button("Generate Image")
    
    output_image = gr.Image(label="Generated Image")
    
    generate_button.click(fn=generate_image, inputs=caption_input, outputs=output_image)

# Launch the app
demo.launch()