Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import
|
| 4 |
-
import textwrap
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def generate_image(text_description):
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
draw = ImageDraw.Draw(img)
|
| 11 |
-
|
| 12 |
-
# Create a simple gradient for sunset (orange to purple)
|
| 13 |
-
for y in range(512):
|
| 14 |
-
r = int(255 * (1 - y / 512) * 0.8) # Fade red/orange
|
| 15 |
-
g = int(100 * (1 - y / 512)) # Minimal green
|
| 16 |
-
b = int(150 + 105 * (y / 512)) # Increase blue/purple
|
| 17 |
-
draw.line([(0, y), (512, y)], fill=(r, g, b))
|
| 18 |
|
| 19 |
-
#
|
|
|
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
-
font = ImageFont.truetype("arial.ttf",
|
| 22 |
except:
|
| 23 |
font = ImageFont.load_default()
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
wrapper = textwrap.TextWrapper(width=20) # Adjust width for better wrapping
|
| 27 |
-
wrapped_text = wrapper.fill(text=text_description)
|
| 28 |
-
lines = wrapped_text.split('\n')
|
| 29 |
-
|
| 30 |
-
# Draw each line of text at the bottom
|
| 31 |
-
y_text = 400
|
| 32 |
-
for line in lines:
|
| 33 |
-
bbox = draw.textbbox((0, 0), line, font=font)
|
| 34 |
-
line_width = bbox[2] - bbox[0]
|
| 35 |
-
line_height = bbox[3] - bbox[1]
|
| 36 |
-
draw.text((20, y_text), line, font=font, fill=(255, 255, 255))
|
| 37 |
-
y_text += line_height + 5
|
| 38 |
-
|
| 39 |
-
# Add graphical elements based on description
|
| 40 |
-
if "lake" in text_description.lower():
|
| 41 |
-
draw.rectangle([150, 300, 350, 400], fill=(0, 191, 255)) # Blue lake
|
| 42 |
-
if "mountains" in text_description.lower():
|
| 43 |
-
draw.polygon([(200, 250), (250, 200), (300, 250)], fill=(255, 255, 255)) # White mountain
|
| 44 |
-
draw.polygon([(250, 200), (300, 150), (350, 200)], fill=(255, 255, 255)) # Second mountain
|
| 45 |
-
|
| 46 |
-
return img
|
| 47 |
|
| 48 |
# Gradio interface
|
| 49 |
with gr.Blocks(title="Text-to-Image Generator") as demo:
|
| 50 |
gr.Markdown("# Text-to-Image Generator")
|
| 51 |
-
gr.Markdown("Enter a description below and generate
|
| 52 |
|
| 53 |
with gr.Row():
|
| 54 |
with gr.Column():
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import torch
|
|
|
|
| 4 |
|
| 5 |
+
# Load the Stable Diffusion model (use a lightweight version for Spaces)
|
| 6 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 7 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 8 |
+
pipe = pipe.to("cuda") # Use GPU if available
|
| 9 |
+
|
| 10 |
+
# Function to generate image from description
|
| 11 |
def generate_image(text_description):
|
| 12 |
+
# Generate image using Stable Diffusion
|
| 13 |
+
image = pipe(text_description, num_inference_steps=50, guidance_scale=7.5).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Optional: Add text to the image
|
| 16 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 17 |
+
img_with_text = image.copy()
|
| 18 |
+
draw = ImageDraw.Draw(img_with_text)
|
| 19 |
try:
|
| 20 |
+
font = ImageFont.truetype("arial.ttf", 20)
|
| 21 |
except:
|
| 22 |
font = ImageFont.load_default()
|
| 23 |
+
text = f"Generated: {text_description}"
|
| 24 |
+
draw.text((10, image.height - 100), text, font=font, fill=(255, 255, 255))
|
| 25 |
|
| 26 |
+
return img_with_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Gradio interface
|
| 29 |
with gr.Blocks(title="Text-to-Image Generator") as demo:
|
| 30 |
gr.Markdown("# Text-to-Image Generator")
|
| 31 |
+
gr.Markdown("Enter a description below and generate a detailed image!")
|
| 32 |
|
| 33 |
with gr.Row():
|
| 34 |
with gr.Column():
|