Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +112 -0
- requirements.txt +8 -0
app.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
8 |
+
#Function to generate image
|
9 |
+
def generate_image(prompt, height=512, width=512):
|
10 |
+
|
11 |
+
model_id = "CompVis/stable-diffusion-v1-4"
|
12 |
+
|
13 |
+
HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
|
14 |
+
|
15 |
+
if not HUGGINGFACE_API_KEY:
|
16 |
+
raise ValueError("Hugging Face API key is not set. Export it as HUGGINGFACE_API_KEY.")
|
17 |
+
|
18 |
+
# Use half-precision and reduce model load time
|
19 |
+
pipeline = StableDiffusionPipeline.from_pretrained(
|
20 |
+
model_id,
|
21 |
+
use_auth_token=HUGGINGFACE_API_KEY,
|
22 |
+
torch_dtype=torch.float16,
|
23 |
+
revision="fp16"
|
24 |
+
)
|
25 |
+
|
26 |
+
pipeline = pipeline.to("cuda" if torch.cuda.is_available() else "cpu")
|
27 |
+
image = pipeline(prompt, height=height, width=width).images[0]
|
28 |
+
return image
|
29 |
+
|
30 |
+
|
31 |
+
# Function to Add Text to Image
|
32 |
+
def add_text_to_image(image, product_name, tagline, cta_text, font_size=50):
|
33 |
+
"""
|
34 |
+
Add clean and sharp text to the generated image.
|
35 |
+
"""
|
36 |
+
draw = ImageDraw.Draw(image)
|
37 |
+
try:
|
38 |
+
product_font = ImageFont.truetype("arial.ttf", font_size + 20)
|
39 |
+
tagline_font = ImageFont.truetype("arial.ttf", font_size)
|
40 |
+
cta_font = ImageFont.truetype("arial.ttf", font_size - 10)
|
41 |
+
except IOError:
|
42 |
+
product_font = tagline_font = cta_font = ImageFont.load_default()
|
43 |
+
|
44 |
+
# Add product name, tagline, and CTA to the image
|
45 |
+
draw.text((50, 50), product_name, font=product_font, fill="white")
|
46 |
+
draw.text((50, 150), tagline, font=tagline_font, fill="white")
|
47 |
+
draw.text((50, 250), cta_text, font=cta_font, fill="gold")
|
48 |
+
|
49 |
+
return image
|
50 |
+
|
51 |
+
# Main function to generate advertisement
|
52 |
+
def generate_advertisement(brand_title, tagline, cta, custom_prompt=None, brand_logo=None, product_image=None):
|
53 |
+
"""
|
54 |
+
Generate advertisement image with text overlay and optional logo/product image.
|
55 |
+
"""
|
56 |
+
prompt = custom_prompt if custom_prompt else (
|
57 |
+
f"An elegant advertisement for {brand_title}, featuring gold and white tones, "
|
58 |
+
f"with a radiant and premium look. Product focus and beautiful typography for '{tagline}'."
|
59 |
+
)
|
60 |
+
|
61 |
+
# Generate the base image using Stable Diffusion
|
62 |
+
generated_image = generate_image(prompt)
|
63 |
+
|
64 |
+
# Overlay text (brand title, tagline, and CTA)
|
65 |
+
final_image = add_text_to_image(generated_image, brand_title, tagline, cta)
|
66 |
+
|
67 |
+
# Optionally add logo and product images
|
68 |
+
if brand_logo:
|
69 |
+
logo = Image.open(brand_logo).resize((150, 150))
|
70 |
+
final_image.paste(logo, (50, 350), logo.convert('RGBA'))
|
71 |
+
|
72 |
+
if product_image:
|
73 |
+
product = Image.open(product_image).resize((300, 300))
|
74 |
+
final_image.paste(product, (250, 350), product.convert('RGBA'))
|
75 |
+
|
76 |
+
return final_image
|
77 |
+
|
78 |
+
# Gradio Interface
|
79 |
+
def gradio_interface(brand_title, tagline, cta, custom_prompt, brand_logo, product_image):
|
80 |
+
"""
|
81 |
+
Gradio interface wrapper to call the advertisement generation function.
|
82 |
+
"""
|
83 |
+
# Generate the ad
|
84 |
+
ad_image = generate_advertisement(
|
85 |
+
brand_title=brand_title,
|
86 |
+
tagline=tagline,
|
87 |
+
cta=cta,
|
88 |
+
custom_prompt=custom_prompt,
|
89 |
+
brand_logo=brand_logo.name if brand_logo else None,
|
90 |
+
product_image=product_image.name if product_image else None
|
91 |
+
)
|
92 |
+
return ad_image
|
93 |
+
|
94 |
+
# Gradio UI Layout
|
95 |
+
interface = gr.Interface(
|
96 |
+
fn=gradio_interface,
|
97 |
+
inputs=[
|
98 |
+
gr.Textbox(label="Brand Title", placeholder="e.g., GlowWell Skin Serum"),
|
99 |
+
gr.Textbox(label="Tagline", placeholder="e.g., Radiance Redefined"),
|
100 |
+
gr.Textbox(label="Call to Action (CTA)", placeholder="e.g., Shop Now"),
|
101 |
+
gr.Textbox(label="Custom Prompt (Optional)", placeholder="Describe your ad style..."),
|
102 |
+
gr.File(label="Brand Logo (Optional)"),
|
103 |
+
gr.File(label="Product Image (Optional)")
|
104 |
+
],
|
105 |
+
outputs=gr.Image(type="pil", label="Generated Advertisement"),
|
106 |
+
title="AI-Powered Advertisement Generator",
|
107 |
+
description="Generate stunning advertisements using Stable Diffusion. Provide brand details, and optionally upload images or add custom descriptions to create your perfect ad."
|
108 |
+
)
|
109 |
+
|
110 |
+
# Launch the interface
|
111 |
+
if __name__ == "__main__":
|
112 |
+
interface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
diffusers
|
3 |
+
gradio
|
4 |
+
transformers
|
5 |
+
accelerate
|
6 |
+
pillow
|
7 |
+
|
8 |
+
|