Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
# Placeholder function for image generation (replace with actual model inference if desired)
|
| 6 |
+
def generate_image(text_description):
|
| 7 |
+
# Create a blank image
|
| 8 |
+
img = Image.new('RGB', (512, 512), color=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
|
| 9 |
+
draw = ImageDraw.Draw(img)
|
| 10 |
+
|
| 11 |
+
# Add text to the image (simulating a generated image based on description)
|
| 12 |
+
try:
|
| 13 |
+
font = ImageFont.truetype("arial.ttf", 40)
|
| 14 |
+
except:
|
| 15 |
+
font = ImageFont.load_default()
|
| 16 |
+
|
| 17 |
+
# Wrap text description if too long
|
| 18 |
+
text = text_description if len(text_description) < 30 else text_description[:27] + "..."
|
| 19 |
+
draw.text((20, 20), f"Generated: {text}", font=font, fill=(255, 255, 255))
|
| 20 |
+
|
| 21 |
+
# Here you could integrate a real model like Stable Diffusion
|
| 22 |
+
# Example (commented out):
|
| 23 |
+
# from diffusers import StableDiffusionPipeline
|
| 24 |
+
# pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
|
| 25 |
+
# img = pipe(text_description).images[0]
|
| 26 |
+
|
| 27 |
+
return img
|
| 28 |
+
|
| 29 |
+
# Gradio interface
|
| 30 |
+
with gr.Blocks(title="Text-to-Image Generator") as demo:
|
| 31 |
+
gr.Markdown("# Text-to-Image Generator")
|
| 32 |
+
gr.Markdown("Enter a description below and generate an image!")
|
| 33 |
+
|
| 34 |
+
with gr.Row():
|
| 35 |
+
with gr.Column():
|
| 36 |
+
text_input = gr.Textbox(label="Description", placeholder="Type your image description here...")
|
| 37 |
+
generate_btn = gr.Button("Generate Image")
|
| 38 |
+
with gr.Column():
|
| 39 |
+
output_image = gr.Image(label="Generated Image")
|
| 40 |
+
|
| 41 |
+
# Connect the button to the function
|
| 42 |
+
generate_btn.click(fn=generate_image, inputs=text_input, outputs=output_image)
|
| 43 |
+
|
| 44 |
+
# Launch the app
|
| 45 |
+
demo.launch()
|