Saarthak2002 commited on
Commit
9462d72
·
verified ·
1 Parent(s): 4e63d55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -66
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
- 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,
@@ -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 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
  try:
62
- # Load font for product name, tagline, and CTA
63
- product_font = ImageFont.truetype("arial.ttf", font_size + 20) # Larger for product name
64
- tagline_font = ImageFont.truetype("arial.ttf", font_size) # Slightly smaller for tagline
65
- cta_font = ImageFont.truetype("arial.ttf", font_size - 10) # Smaller for CTA
66
  except IOError:
67
- # Fallback font in case "arial.ttf" is not available
68
- product_font = ImageFont.load_default()
69
- tagline_font = ImageFont.load_default()
70
- cta_font = ImageFont.load_default()
71
 
72
- # Define positions for text
73
- product_name_position = (50, 50)
74
- tagline_position = (50, 150)
75
- cta_position = (50, 250)
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 generate_ad(brand_title, tagline, cta, brand_logo=None, product_image=None, custom_prompt=None):
90
  """
91
- Generate an advertisement image with the provided details.
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
- # Add product and CTA text to the image
114
  final_image = add_text_to_image(generated_image, brand_title, tagline, cta)
115
 
116
- # Optionally, add brand logo and product image if provided
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
- iface = gr.Interface(
129
- fn=generate_ad,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  inputs=[
131
- gr.Textbox(label="Brand Title", placeholder="Enter brand title"),
132
- gr.Textbox(label="Tagline", placeholder="Enter tagline"),
133
- gr.Textbox(label="CTA", placeholder="Enter Call to Action"),
134
- gr.Image(type="file", label="Brand Logo (optional)", optional=True),
135
- gr.Image(type="file", label="Product Image (optional)", optional=True),
136
- gr.Textbox(label="Custom Prompt (optional)", placeholder="Enter a custom prompt (or leave empty)"),
137
  ],
138
- outputs=gr.Image(label="Generated Advertisement"),
 
 
139
  )
140
 
141
- # Launch the Gradio Interface
142
- iface.launch()
 
 
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()