Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
import torch
|
2 |
-
import gradio as gr
|
3 |
from diffusers import StableDiffusionPipeline
|
4 |
from PIL import Image, ImageDraw, ImageFont
|
5 |
import os
|
|
|
6 |
|
7 |
# Function to Generate Image
|
8 |
def generate_image(prompt, height=1024, width=1024):
|
@@ -23,8 +23,7 @@ def generate_image(prompt, height=1024, width=1024):
|
|
23 |
raise ValueError("Hugging Face API key is not set. Export it as HUGGINGFACE_API_KEY.")
|
24 |
|
25 |
# Load the Stable Diffusion model
|
26 |
-
|
27 |
-
model_id = "CompVis/stable-diffusion-v1-4" # You can change to v2 models if needed
|
28 |
pipeline = StableDiffusionPipeline.from_pretrained(
|
29 |
model_id,
|
30 |
use_auth_token=HUGGINGFACE_API_KEY,
|
@@ -36,84 +35,47 @@ def generate_image(prompt, height=1024, width=1024):
|
|
36 |
pipeline = pipeline.to(device)
|
37 |
|
38 |
# Generate the image
|
39 |
-
print(f"Generating image for prompt: '{prompt}'...")
|
40 |
image = pipeline(prompt, height=height, width=width).images[0]
|
41 |
|
42 |
return image
|
43 |
|
44 |
-
# Function to Add
|
45 |
def add_text_to_image(image, product_name, tagline, cta_text, font_size=50):
|
46 |
"""
|
47 |
-
Add clean and sharp text
|
48 |
-
|
49 |
-
Args:
|
50 |
-
image (PIL Image): Generated image to add text to.
|
51 |
-
product_name (str): Product name to be emphasized.
|
52 |
-
tagline (str): Tagline to be emphasized.
|
53 |
-
cta_text (str): Call to action text to be emphasized.
|
54 |
-
font_size (int): The font size.
|
55 |
-
|
56 |
-
Returns:
|
57 |
-
PIL Image: Image with added text.
|
58 |
"""
|
59 |
draw = ImageDraw.Draw(image)
|
60 |
-
|
61 |
try:
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
cta_font = ImageFont.truetype("arial.ttf", font_size - 10) # Smaller for CTA
|
66 |
except IOError:
|
67 |
-
|
68 |
-
product_font = ImageFont.load_default()
|
69 |
-
tagline_font = ImageFont.load_default()
|
70 |
-
cta_font = ImageFont.load_default()
|
71 |
|
72 |
-
#
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
# Add product name in large font
|
78 |
-
draw.text(product_name_position, product_name, font=product_font, fill="white")
|
79 |
-
|
80 |
-
# Add tagline below the product name
|
81 |
-
draw.text(tagline_position, tagline, font=tagline_font, fill="white")
|
82 |
-
|
83 |
-
# Add CTA below tagline
|
84 |
-
draw.text(cta_position, cta_text, font=cta_font, fill="gold") # CTA in gold to stand out
|
85 |
|
86 |
return image
|
87 |
|
88 |
# Main function to generate advertisement
|
89 |
-
def
|
90 |
"""
|
91 |
-
Generate
|
92 |
-
|
93 |
-
Args:
|
94 |
-
brand_title (str): Brand title for the advertisement.
|
95 |
-
tagline (str): Tagline for the advertisement.
|
96 |
-
cta (str): Call to action text.
|
97 |
-
brand_logo (file, optional): Brand logo image (optional).
|
98 |
-
product_image (file, optional): Product image (optional).
|
99 |
-
custom_prompt (str, optional): Custom prompt for image generation.
|
100 |
-
|
101 |
-
Returns:
|
102 |
-
PIL Image: Final advertisement image.
|
103 |
"""
|
104 |
-
# Prepare the final prompt
|
105 |
prompt = custom_prompt if custom_prompt else (
|
106 |
f"An elegant advertisement for {brand_title}, featuring gold and white tones, "
|
107 |
f"with a radiant and premium look. Product focus and beautiful typography for '{tagline}'."
|
108 |
)
|
109 |
|
110 |
-
# Generate the image
|
111 |
generated_image = generate_image(prompt)
|
112 |
|
113 |
-
#
|
114 |
final_image = add_text_to_image(generated_image, brand_title, tagline, cta)
|
115 |
|
116 |
-
# Optionally
|
117 |
if brand_logo:
|
118 |
logo = Image.open(brand_logo).resize((150, 150))
|
119 |
final_image.paste(logo, (50, 350), logo.convert('RGBA'))
|
@@ -125,18 +87,37 @@ def generate_ad(brand_title, tagline, cta, brand_logo=None, product_image=None,
|
|
125 |
return final_image
|
126 |
|
127 |
# Gradio Interface
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
inputs=[
|
131 |
-
gr.Textbox(label="Brand Title", placeholder="
|
132 |
-
gr.Textbox(label="Tagline", placeholder="
|
133 |
-
gr.Textbox(label="CTA", placeholder="
|
134 |
-
gr.
|
135 |
-
gr.
|
136 |
-
gr.
|
137 |
],
|
138 |
-
outputs=gr.Image(label="Generated Advertisement"),
|
|
|
|
|
139 |
)
|
140 |
|
141 |
-
# Launch the
|
142 |
-
|
|
|
|
1 |
import torch
|
|
|
2 |
from diffusers import StableDiffusionPipeline
|
3 |
from PIL import Image, ImageDraw, ImageFont
|
4 |
import os
|
5 |
+
import gradio as gr # Import Gradio for the interface
|
6 |
|
7 |
# Function to Generate Image
|
8 |
def generate_image(prompt, height=1024, width=1024):
|
|
|
23 |
raise ValueError("Hugging Face API key is not set. Export it as HUGGINGFACE_API_KEY.")
|
24 |
|
25 |
# Load the Stable Diffusion model
|
26 |
+
model_id = "stabilityai/stable-diffusion-2-1"
|
|
|
27 |
pipeline = StableDiffusionPipeline.from_pretrained(
|
28 |
model_id,
|
29 |
use_auth_token=HUGGINGFACE_API_KEY,
|
|
|
35 |
pipeline = pipeline.to(device)
|
36 |
|
37 |
# Generate the image
|
|
|
38 |
image = pipeline(prompt, height=height, width=width).images[0]
|
39 |
|
40 |
return image
|
41 |
|
42 |
+
# Function to Add Text to Image
|
43 |
def add_text_to_image(image, product_name, tagline, cta_text, font_size=50):
|
44 |
"""
|
45 |
+
Add clean and sharp text to the generated image.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
"""
|
47 |
draw = ImageDraw.Draw(image)
|
|
|
48 |
try:
|
49 |
+
product_font = ImageFont.truetype("arial.ttf", font_size + 20)
|
50 |
+
tagline_font = ImageFont.truetype("arial.ttf", font_size)
|
51 |
+
cta_font = ImageFont.truetype("arial.ttf", font_size - 10)
|
|
|
52 |
except IOError:
|
53 |
+
product_font = tagline_font = cta_font = ImageFont.load_default()
|
|
|
|
|
|
|
54 |
|
55 |
+
# Add product name, tagline, and CTA to the image
|
56 |
+
draw.text((50, 50), product_name, font=product_font, fill="white")
|
57 |
+
draw.text((50, 150), tagline, font=tagline_font, fill="white")
|
58 |
+
draw.text((50, 250), cta_text, font=cta_font, fill="gold")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
return image
|
61 |
|
62 |
# Main function to generate advertisement
|
63 |
+
def generate_advertisement(brand_title, tagline, cta, custom_prompt=None, brand_logo=None, product_image=None):
|
64 |
"""
|
65 |
+
Generate advertisement image with text overlay and optional logo/product image.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
"""
|
|
|
67 |
prompt = custom_prompt if custom_prompt else (
|
68 |
f"An elegant advertisement for {brand_title}, featuring gold and white tones, "
|
69 |
f"with a radiant and premium look. Product focus and beautiful typography for '{tagline}'."
|
70 |
)
|
71 |
|
72 |
+
# Generate the base image using Stable Diffusion
|
73 |
generated_image = generate_image(prompt)
|
74 |
|
75 |
+
# Overlay text (brand title, tagline, and CTA)
|
76 |
final_image = add_text_to_image(generated_image, brand_title, tagline, cta)
|
77 |
|
78 |
+
# Optionally add logo and product images
|
79 |
if brand_logo:
|
80 |
logo = Image.open(brand_logo).resize((150, 150))
|
81 |
final_image.paste(logo, (50, 350), logo.convert('RGBA'))
|
|
|
87 |
return final_image
|
88 |
|
89 |
# Gradio Interface
|
90 |
+
def gradio_interface(brand_title, tagline, cta, custom_prompt, brand_logo, product_image):
|
91 |
+
"""
|
92 |
+
Gradio interface wrapper to call the advertisement generation function.
|
93 |
+
"""
|
94 |
+
# Generate the ad
|
95 |
+
ad_image = generate_advertisement(
|
96 |
+
brand_title=brand_title,
|
97 |
+
tagline=tagline,
|
98 |
+
cta=cta,
|
99 |
+
custom_prompt=custom_prompt,
|
100 |
+
brand_logo=brand_logo.name if brand_logo else None,
|
101 |
+
product_image=product_image.name if product_image else None
|
102 |
+
)
|
103 |
+
return ad_image
|
104 |
+
|
105 |
+
# Gradio UI Layout
|
106 |
+
interface = gr.Interface(
|
107 |
+
fn=gradio_interface,
|
108 |
inputs=[
|
109 |
+
gr.Textbox(label="Brand Title", placeholder="e.g., GlowWell Skin Serum"),
|
110 |
+
gr.Textbox(label="Tagline", placeholder="e.g., Radiance Redefined"),
|
111 |
+
gr.Textbox(label="Call to Action (CTA)", placeholder="e.g., Shop Now"),
|
112 |
+
gr.Textbox(label="Custom Prompt (Optional)", placeholder="Describe your ad style..."),
|
113 |
+
gr.File(label="Brand Logo (Optional)"),
|
114 |
+
gr.File(label="Product Image (Optional)")
|
115 |
],
|
116 |
+
outputs=gr.Image(type="pil", label="Generated Advertisement"),
|
117 |
+
title="AI-Powered Advertisement Generator",
|
118 |
+
description="Generate stunning advertisements using Stable Diffusion. Provide brand details, and optionally upload images or add custom descriptions to create your perfect ad."
|
119 |
)
|
120 |
|
121 |
+
# Launch the interface
|
122 |
+
if __name__ == "__main__":
|
123 |
+
interface.launch()
|