Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from diffusers import DiffusionPipeline
|
4 |
+
from PIL import Image, ImageDraw, ImageFont
|
5 |
+
|
6 |
+
# Load the model
|
7 |
+
model = DiffusionPipeline.from_pretrained("ginipick/flux-lora-eric-cat", torch_dtype=torch.float16)
|
8 |
+
model = model.to("cuda") # Move the model to GPU if available
|
9 |
+
|
10 |
+
def generate_image(caption):
|
11 |
+
# Generate the image from the caption
|
12 |
+
image = model(caption).images[0]
|
13 |
+
|
14 |
+
# Convert to PIL Image for drawing
|
15 |
+
image = image.convert("RGBA")
|
16 |
+
|
17 |
+
# Create a draw object
|
18 |
+
draw = ImageDraw.Draw(image)
|
19 |
+
|
20 |
+
# Define font size and color
|
21 |
+
font_size = 40
|
22 |
+
font_color = "white"
|
23 |
+
|
24 |
+
# Load a font
|
25 |
+
font = ImageFont.load_default() # You can specify a TTF font file if needed
|
26 |
+
|
27 |
+
# Calculate text size and position
|
28 |
+
text_width, text_height = draw.textsize(caption, font=font)
|
29 |
+
text_position = ((image.width - text_width) // 2, 10) # Centered at the top
|
30 |
+
|
31 |
+
# Draw the text on the image
|
32 |
+
draw.text(text_position, caption, font=font, fill=font_color)
|
33 |
+
|
34 |
+
return image
|
35 |
+
|
36 |
+
# Create the Gradio interface
|
37 |
+
with gr.Blocks() as demo:
|
38 |
+
gr.Markdown("# Text to Image Generation with Meme Caption")
|
39 |
+
|
40 |
+
with gr.Row():
|
41 |
+
caption_input = gr.Textbox(label="Enter your caption", placeholder="Type your caption here...")
|
42 |
+
generate_button = gr.Button("Generate Image")
|
43 |
+
|
44 |
+
output_image = gr.Image(label="Generated Image")
|
45 |
+
|
46 |
+
generate_button.click(fn=generate_image, inputs=caption_input, outputs=output_image)
|
47 |
+
|
48 |
+
# Launch the app
|
49 |
+
demo.launch()
|