|
import gradio as gr |
|
from diffusers import StableDiffusionPipeline |
|
import torch |
|
|
|
|
|
model_id = "runwayml/stable-diffusion-v1-5" |
|
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16) |
|
pipe = pipe.to("cuda") |
|
|
|
|
|
def generate_image(text_description): |
|
|
|
image = pipe(text_description, num_inference_steps=50, guidance_scale=7.5).images[0] |
|
|
|
|
|
from PIL import Image, ImageDraw, ImageFont |
|
img_with_text = image.copy() |
|
draw = ImageDraw.Draw(img_with_text) |
|
try: |
|
font = ImageFont.truetype("arial.ttf", 20) |
|
except: |
|
font = ImageFont.load_default() |
|
text = f"Generated: {text_description}" |
|
draw.text((10, image.height - 100), text, font=font, fill=(255, 255, 255)) |
|
|
|
return img_with_text |
|
|
|
|
|
with gr.Blocks(title="Text-to-Image Generator") as demo: |
|
gr.Markdown("# Text-to-Image Generator") |
|
gr.Markdown("Enter a description below and generate a detailed image!") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
text_input = gr.Textbox(label="Description", placeholder="Type your image description here...", value="A serene lake surrounded by snow-capped mountains under a vibrant sunset sky with shades of orange and purple.") |
|
generate_btn = gr.Button("Generate Image") |
|
with gr.Column(): |
|
output_image = gr.Image(label="Generated Image") |
|
|
|
|
|
generate_btn.click(fn=generate_image, inputs=text_input, outputs=output_image) |
|
|
|
|
|
demo.launch() |