Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
9 |
+
"""
|
10 |
+
Generate an image using Stable Diffusion from Hugging Face.
|
11 |
+
|
12 |
+
Args:
|
13 |
+
prompt (str): The text prompt to guide image generation.
|
14 |
+
height (int): Height of the generated image.
|
15 |
+
width (int): Width of the generated image.
|
16 |
+
|
17 |
+
Returns:
|
18 |
+
PIL Image: Generated image.
|
19 |
+
"""
|
20 |
+
# Load Hugging Face access token from environment variables
|
21 |
+
HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
|
22 |
+
if not HUGGINGFACE_API_KEY:
|
23 |
+
raise ValueError("Hugging Face API key is not set. Export it as HUGGINGFACE_API_KEY.")
|
24 |
+
|
25 |
+
# Load the Stable Diffusion model
|
26 |
+
print("Loading Stable Diffusion model...")
|
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,
|
31 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
32 |
+
)
|
33 |
+
|
34 |
+
# Move to GPU if available
|
35 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
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 Emphasized Text to the Image (Product Name, Tagline, CTA)
|
45 |
+
def add_text_to_image(image, product_name, tagline, cta_text, font_size=50):
|
46 |
+
"""
|
47 |
+
Add clean and sharp text (product name, tagline, and CTA) to the generated image with emphasis.
|
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 |
+
# Load font for product name, tagline, and CTA
|
62 |
+
product_font = ImageFont.truetype("arial.ttf", font_size + 20) # Larger for product name
|
63 |
+
tagline_font = ImageFont.truetype("arial.ttf", font_size) # Slightly smaller for tagline
|
64 |
+
cta_font = ImageFont.truetype("arial.ttf", font_size - 10) # Smaller for CTA
|
65 |
+
|
66 |
+
# Define positions for text
|
67 |
+
product_name_position = (50, 50)
|
68 |
+
tagline_position = (50, 150)
|
69 |
+
cta_position = (50, 250)
|
70 |
+
|
71 |
+
# Add product name in large font
|
72 |
+
draw.text(product_name_position, product_name, font=product_font, fill="white")
|
73 |
+
|
74 |
+
# Add tagline below the product name
|
75 |
+
draw.text(tagline_position, tagline, font=tagline_font, fill="white")
|
76 |
+
|
77 |
+
# Add CTA below tagline
|
78 |
+
draw.text(cta_position, cta_text, font=cta_font, fill="gold") # CTA in gold to stand out
|
79 |
+
|
80 |
+
return image
|
81 |
+
|
82 |
+
# Gradio Interface Function
|
83 |
+
def generate_ad(brand_title, tagline, cta, brand_logo, product_image, custom_prompt):
|
84 |
+
"""
|
85 |
+
Generate an advertisement image with the provided details.
|
86 |
+
|
87 |
+
Args:
|
88 |
+
brand_title (str): Brand title for the advertisement.
|
89 |
+
tagline (str): Tagline for the advertisement.
|
90 |
+
cta (str): Call to action text.
|
91 |
+
brand_logo (file): Brand logo image (optional).
|
92 |
+
product_image (file): Product image (optional).
|
93 |
+
custom_prompt (str): Custom prompt for image generation.
|
94 |
+
|
95 |
+
Returns:
|
96 |
+
PIL Image: Final advertisement image.
|
97 |
+
"""
|
98 |
+
# Prepare the final prompt
|
99 |
+
prompt = custom_prompt if custom_prompt else (
|
100 |
+
f"An elegant advertisement for {brand_title}, featuring gold and white tones, "
|
101 |
+
f"with a radiant and premium look. Product focus and beautiful typography for '{tagline}'."
|
102 |
+
)
|
103 |
+
|
104 |
+
# Generate the image
|
105 |
+
generated_image = generate_image(prompt)
|
106 |
+
|
107 |
+
# Add product and CTA text to the image
|
108 |
+
final_image = add_text_to_image(generated_image, brand_title, tagline, cta)
|
109 |
+
|
110 |
+
# Optionally, add brand logo and product image if provided
|
111 |
+
if brand_logo:
|
112 |
+
logo = Image.open(brand_logo).resize((150, 150))
|
113 |
+
final_image.paste(logo, (50, 350), logo.convert('RGBA'))
|
114 |
+
|
115 |
+
if product_image:
|
116 |
+
product = Image.open(product_image).resize((300, 300))
|
117 |
+
final_image.paste(product, (250, 350), product.convert('RGBA'))
|
118 |
+
|
119 |
+
return final_image
|
120 |
+
|
121 |
+
# Create Gradio Interface
|
122 |
+
iface = gr.Interface(
|
123 |
+
fn=generate_ad,
|
124 |
+
inputs=[
|
125 |
+
gr.Textbox(label="Brand Title", placeholder="Enter brand title"),
|
126 |
+
gr.Textbox(label="Tagline", placeholder="Enter tagline"),
|
127 |
+
gr.Textbox(label="CTA", placeholder="Enter Call to Action"),
|
128 |
+
gr.Image(type="file", label="Brand Logo (optional)", optional=True),
|
129 |
+
gr.Image(type="file", label="Product Image (optional)", optional=True),
|
130 |
+
gr.Textbox(label="Custom Prompt (optional)", placeholder="Enter a custom prompt (or leave empty)"),
|
131 |
+
],
|
132 |
+
outputs=gr.Image(label="Generated Advertisement"),
|
133 |
+
)
|
134 |
+
|
135 |
+
# Launch Gradio Interface
|
136 |
+
iface.launch()
|