Pratap2002 commited on
Commit
0b1f1ae
·
verified ·
1 Parent(s): 11dab30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +180 -136
app.py CHANGED
@@ -83,7 +83,7 @@ def generate_advertisement_prompt(description):
83
  return response.content
84
 
85
  def advertisement_generator():
86
- st.title("Advertisement Post Generator")
87
 
88
  post_description = st.text_input("Enter a brief description for your advertisement post:")
89
 
@@ -121,144 +121,162 @@ def increase_image_quality(image, scale_factor):
121
  return image.resize(new_size, Image.LANCZOS)
122
 
123
  def generate_poster():
124
- st.header("Generate Social Media Post")
125
- post_type = st.selectbox("Select Post Type", ["Instagram advertisement post", "Facebook advertisement post", "Twitter advertisement post", "Other"])
126
- aspect_ratio = st.selectbox("Select Image Aspect Ratio", ["1:1", "16:9", "4:5", "9:16"])
127
-
128
- # Add dimension selection based on aspect ratio
129
- if aspect_ratio == "1:1":
130
- dimensions = st.selectbox("Select Image Dimensions", ["1080x1080", "1200x1200", "1500x1500"])
131
- elif aspect_ratio == "16:9":
132
- dimensions = st.selectbox("Select Image Dimensions", ["1920x1080", "1280x720", "2560x1440"])
133
- elif aspect_ratio == "4:5":
134
- dimensions = st.selectbox("Select Image Dimensions", ["1080x1350", "1200x1500", "1600x2000"])
135
- elif aspect_ratio == "9:16":
136
- dimensions = st.selectbox("Select Image Dimensions", ["1080x1920", "1350x2400", "1600x2844"])
137
-
138
- # Extract width and height from the selected dimensions
139
- width, height = map(int, dimensions.split('x'))
140
-
141
- design_style = st.selectbox("Select Design Style", [
142
- "Minimalistic", "Bold/Graphic", "Elegant", "Playful/Fun",
143
- "Corporate/Professional", "Retro/Vintage", "Modern/Contemporary", "Illustrative/Artistic"
144
- ])
145
-
146
- # Add color selection with predefined options
147
- color_options = ["None", "Black", "White", "Red", "Blue", "Green", "Yellow", "Purple"]
148
- selected_color = st.selectbox("Choose a dominant color for the image", color_options)
149
-
150
- description = st.text_area("Enter prompt for Advertisement:")
151
- if st.button("Use Advertisement Generator", key="ad_generator_button"):
152
  st.session_state.show_ad_generator = True
153
 
154
  if st.session_state.get('show_ad_generator', False):
155
  with st.expander("Advertisement Generator :arrow_right:", expanded=True):
156
  advertisement_generator()
157
- header = st.text_input("Enter Header for Advertisement:")
158
- sub_header = st.text_input("Enter Sub-header for Advertisement:")
159
- # Allow multiple user prompts
160
- user_prompts = []
161
- num_prompts = st.number_input("Number of Text Descriptions", min_value=1, max_value=80, value=1)
162
- for i in range(num_prompts):
163
- user_prompt = st.text_area(f"Enter Descriptions to display in the image (Descriptions {i+1}):")
164
- user_prompts.append(user_prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
 
166
- logo = st.file_uploader("Upload Logo (optional)", type=['png', 'jpg', 'jpeg'])
 
 
 
 
 
 
 
 
 
 
167
 
168
- # Add logo position selection
169
- logo_position = st.selectbox("Select Logo Position", [
170
- "None", "Top Left", "Top Middle", "Top Right",
171
- "Left Middle", "Right Middle",
172
- "Bottom Left", "Bottom Middle", "Bottom Right"
173
- ])
 
 
 
 
 
 
 
174
 
175
- num_images = st.number_input("Number of Images", min_value=4, max_value=8, value=4)
176
-
177
- if st.button("Generate Images"):
178
- base_prompt = f"Create a minimalistic and realistic poster image with the following text as the header: '{header}'. Use '{sub_header}' as the sub-header. Use this design style and go with this {design_style.lower()} style for the post. Include the following text as the description for the advertisement: {description}. The image should have an aspect ratio of {aspect_ratio} and dimensions of {width}x{height}. Use {selected_color.lower()} as the dominant color in the image. Ensure the image is in 4k high resolution. And do not include any extra text in the image only instruction text included.And try to fill the vacant part with releveng images. Make the design clean and elegant, focusing on simplicity and realism."
179
-
180
- if post_type == "Other":
181
- full_prompt = f"{base_prompt} The image should be suitable for general use across various platforms."
182
- else:
183
- full_prompt = f"{base_prompt} This image is specifically for a {post_type.lower()}."
184
-
185
- # Add user prompts to full_prompt if provided
186
- for i, user_prompt in enumerate(user_prompts):
187
- if user_prompt:
188
- full_prompt += f" Include the following text in the image (Prompt {i+1}): '{user_prompt}'."
189
-
190
- for i in range(num_images):
191
- with st.spinner(f"Generating image {i+1}..."):
192
- logger.info(f"Generating image {i+1} with prompt: {full_prompt}")
193
- # Add a random seed to ensure different images
194
- seed = random.randint(1, 1000000)
195
- image_bytes = query({"inputs": full_prompt, "parameters": {"seed": seed, "width": width, "height": height}})
196
-
197
- if image_bytes:
198
- image = Image.open(io.BytesIO(image_bytes))
 
 
 
199
 
200
- # Add logo if provided
201
- if logo:
202
- logo_image = Image.open(logo)
203
- logo_width = int(image.width * 0.15) # 15% of the image width
204
- logo_height = int(logo_image.height * (logo_width / logo_image.width))
205
- logo_image = logo_image.resize((logo_width, logo_height), Image.LANCZOS)
206
 
207
- padding = int(image.width * 0.02) # Fixed 2% padding
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
- if logo_position == "None":
210
- # Randomly choose a corner for logo placement
211
- corner = random.choice(["Top Left", "Top Right", "Bottom Left", "Bottom Right"])
212
- if corner == "Top Left":
213
- position = (padding, padding)
214
- elif corner == "Top Right":
215
- position = (image.width - logo_width - padding, padding)
216
- elif corner == "Bottom Left":
217
- position = (padding, image.height - logo_height - padding)
218
- else: # Bottom Right
219
- position = (image.width - logo_width - padding, image.height - logo_height - padding)
220
- else:
221
- if logo_position == "Top Left":
222
- position = (padding, padding)
223
- elif logo_position == "Top Middle":
224
- position = ((image.width - logo_width) // 2, padding)
225
- elif logo_position == "Top Right":
226
- position = (image.width - logo_width - padding, padding)
227
- elif logo_position == "Bottom Left":
228
- position = (padding, image.height - logo_height - padding)
229
- elif logo_position == "Bottom Middle":
230
- position = ((image.width - logo_width) // 2, image.height - logo_height - padding)
231
- else: # Bottom Right
232
- position = (image.width - logo_width - padding, image.height - logo_height - padding)
233
-
234
- # Create a new image with an alpha channel
235
- combined_image = Image.new('RGBA', image.size, (0, 0, 0, 0))
236
- combined_image.paste(image, (0, 0))
237
 
238
- # Convert logo to RGBA if it's not already
239
- if logo_image.mode != 'RGBA':
240
- logo_image = logo_image.convert('RGBA')
241
 
242
- combined_image.paste(logo_image, position, logo_image)
 
243
 
244
- # Convert back to RGB for compatibility
245
- image = combined_image.convert('RGB')
246
-
247
- # Display generated image
248
- st.image(image, caption=f"Generated Poster {i+1}", use_column_width=True)
249
-
250
- # Provide download option for the generated image
251
- buf = io.BytesIO()
252
- image.save(buf, format="PNG")
253
- byte_im = buf.getvalue()
254
- st.download_button(
255
- label=f"Download generated poster {i+1}",
256
- data=byte_im,
257
- file_name=f"generated_poster_{i+1}.png",
258
- mime="image/png"
259
- )
260
- else:
261
- st.error(f"Failed to generate image {i+1}")
262
 
263
  # Content from image_to_image.py
264
  def encode_image(image):
@@ -528,23 +546,49 @@ def remove_background(image):
528
  return Image.fromarray(result)
529
 
530
  # Main Streamlit App
531
- def main():
532
- st.title("Image Generation and Editing App")
533
-
534
- # Sidebar for navigation
535
- page = st.sidebar.selectbox("Choose a function", ["Text to Image Generation", "Image Editing", "Poster Generation", "Advertisement Generator"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
536
 
537
- if page == "Text to Image Generation":
 
538
  text_to_image_generation()
539
- elif page == "Image Editing":
 
540
  image_editing()
541
- elif page == "Poster Generation":
 
542
  generate_poster()
543
- elif page == "Advertisement Generator":
 
544
  advertisement_generator()
545
 
546
  def text_to_image_generation():
547
- st.header("Text to Image Generation")
548
 
549
  # Image to Image Generation
550
  st.subheader("Image to Image Generation")
@@ -594,7 +638,7 @@ def text_to_image_generation():
594
  st.image(new_image, caption=f"Generated Image {i+1}", use_column_width=True)
595
 
596
  def image_editing():
597
- st.header("Image Editing")
598
 
599
  # Background Removal
600
  st.subheader("Background Removal")
 
83
  return response.content
84
 
85
  def advertisement_generator():
86
+ #st.title("Advertisement Post Generator")
87
 
88
  post_description = st.text_input("Enter a brief description for your advertisement post:")
89
 
 
121
  return image.resize(new_size, Image.LANCZOS)
122
 
123
  def generate_poster():
124
+ #st.header("Generate Social Media Post")
125
+ description = st.text_input("Enter prompt for Advertisement:")
126
+ if st.button("", key="ad_generator_button"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  st.session_state.show_ad_generator = True
128
 
129
  if st.session_state.get('show_ad_generator', False):
130
  with st.expander("Advertisement Generator :arrow_right:", expanded=True):
131
  advertisement_generator()
132
+
133
+ st.header("Graphic Type")
134
+ col1, col2 = st.columns(2)
135
+ with col1:
136
+ post_type = st.selectbox("Select Post Type", ["Instagram advertisement post", "Facebook advertisement post", "Twitter advertisement post", "Other"])
137
+ aspect_ratio = st.selectbox("Select Image Aspect Ratio", ["1:1", "16:9", "4:5", "9:16"])
138
+ with col2:
139
+ # Add dimension selection based on aspect ratio
140
+ if aspect_ratio == "1:1":
141
+ dimensions = st.selectbox("Select Image Dimensions", ["1080x1080", "1200x1200", "1500x1500"])
142
+ elif aspect_ratio == "16:9":
143
+ dimensions = st.selectbox("Select Image Dimensions", ["1920x1080", "1280x720", "2560x1440"])
144
+ elif aspect_ratio == "4:5":
145
+ dimensions = st.selectbox("Select Image Dimensions", ["1080x1350", "1200x1500", "1600x2000"])
146
+ elif aspect_ratio == "9:16":
147
+ dimensions = st.selectbox("Select Image Dimensions", ["1080x1920", "1350x2400", "1600x2844"])
148
+
149
+ design_style = st.selectbox("Select Design Style", [
150
+ "Minimalistic", "Bold/Graphic", "Elegant", "Playful/Fun",
151
+ "Corporate/Professional", "Retro/Vintage", "Modern/Contemporary", "Illustrative/Artistic"
152
+ ])
153
+
154
+ # Extract width and height from the selected dimensions
155
+ width, height = map(int, dimensions.split('x'))
156
+
157
+ st.header("Content")
158
 
159
+ with st.expander("Content Details", expanded=False):
160
+ header = st.text_input("Enter Header for Advertisement:")
161
+ sub_header = st.text_input("Enter Sub-header for Advertisement:")
162
+ # Allow multiple user prompts
163
+ user_prompts = []
164
+ num_prompts = st.number_input("Number of Text Descriptions", min_value=1, max_value=80, value=1)
165
+ for i in range(num_prompts):
166
+ user_prompt = st.text_area(f"Enter Descriptions to display in the image (Descriptions {i+1}):")
167
+ user_prompts.append(user_prompt)
168
+
169
+ st.header("Branding")
170
 
171
+ with st.expander("Branding Details", expanded=False):
172
+ # Add color selection with predefined options
173
+ color_options = ["None", "Black", "White", "Red", "Blue", "Green", "Yellow", "Purple"]
174
+ selected_color = st.selectbox("Choose a dominant color for the image", color_options)
175
+
176
+ logo = st.file_uploader("Upload Logo (optional)", type=['png', 'jpg', 'jpeg'])
177
+
178
+ # Add logo position selection
179
+ logo_position = st.selectbox("Select Logo Position", [
180
+ "None", "Top Left", "Top Middle", "Top Right",
181
+ "Left Middle", "Right Middle",
182
+ "Bottom Left", "Bottom Middle", "Bottom Right"
183
+ ])
184
 
185
+ num_images = st.number_input("Number of Images", min_value=4, max_value=100, value=4)
186
+
187
+ # Create a column layout to center the button
188
+ col1, col2, col3 = st.columns([1, 2, 1])
189
+
190
+ # Place the button in the middle column and increase its width
191
+ with col2:
192
+ if st.button("Generate Graphics", use_container_width=True):
193
+ base_prompt = f"Create a minimalistic and realistic poster image with the following text as the header: '{header}'. Use '{sub_header}' as the sub-header. Use this design style and go with this {design_style.lower()} style for the post. Include the following text as the description for the advertisement: {description}. The image should have an aspect ratio of {aspect_ratio} and dimensions of {width}x{height}. Use {selected_color.lower()} as the dominant color in the image. Ensure the image is in 4k high resolution. And do not include any extra text in the image only instruction text included.And try to fill the vacant part with releveng images. Make the design clean and elegant, focusing on simplicity and realism."
194
+
195
+ if post_type == "Other":
196
+ full_prompt = f"{base_prompt} The image should be suitable for general use across various platforms."
197
+ else:
198
+ full_prompt = f"{base_prompt} This image is specifically for a {post_type.lower()}."
199
+
200
+ # Add user prompts to full_prompt if provided
201
+ for i, user_prompt in enumerate(user_prompts):
202
+ if user_prompt:
203
+ full_prompt += f" Include the following text in the image (Prompt {i+1}): '{user_prompt}'."
204
+
205
+ generated_images = []
206
+ for i in range(num_images):
207
+ with st.spinner(f"Generating Graphic {i+1}..."):
208
+ logger.info(f"Generating Graphic {i+1} with prompt: {full_prompt}")
209
+ # Add a random seed to ensure different images
210
+ seed = random.randint(1, 1000000)
211
+ image_bytes = query({"inputs": full_prompt, "parameters": {"seed": seed, "width": width, "height": height}})
212
 
213
+ if image_bytes:
214
+ image = Image.open(io.BytesIO(image_bytes))
 
 
 
 
215
 
216
+ # Add logo if provided
217
+ if logo:
218
+ logo_image = Image.open(logo)
219
+ logo_width = int(image.width * 0.15) # 15% of the image width
220
+ logo_height = int(logo_image.height * (logo_width / logo_image.width))
221
+ logo_image = logo_image.resize((logo_width, logo_height), Image.LANCZOS)
222
+
223
+ padding = int(image.width * 0.02) # Fixed 2% padding
224
+
225
+ if logo_position == "None":
226
+ # Randomly choose a corner for logo placement
227
+ corner = random.choice(["Top Left", "Top Right", "Bottom Left", "Bottom Right"])
228
+ if corner == "Top Left":
229
+ position = (padding, padding)
230
+ elif corner == "Top Right":
231
+ position = (image.width - logo_width - padding, padding)
232
+ elif corner == "Bottom Left":
233
+ position = (padding, image.height - logo_height - padding)
234
+ else: # Bottom Right
235
+ position = (image.width - logo_width - padding, image.height - logo_height - padding)
236
+ else:
237
+ if logo_position == "Top Left":
238
+ position = (padding, padding)
239
+ elif logo_position == "Top Middle":
240
+ position = ((image.width - logo_width) // 2, padding)
241
+ elif logo_position == "Top Right":
242
+ position = (image.width - logo_width - padding, padding)
243
+ elif logo_position == "Bottom Left":
244
+ position = (padding, image.height - logo_height - padding)
245
+ elif logo_position == "Bottom Middle":
246
+ position = ((image.width - logo_width) // 2, image.height - logo_height - padding)
247
+ else: # Bottom Right
248
+ position = (image.width - logo_width - padding, image.height - logo_height - padding)
249
 
250
+ # Create a new image with an alpha channel
251
+ combined_image = Image.new('RGBA', image.size, (0, 0, 0, 0))
252
+ combined_image.paste(image, (0, 0))
253
+
254
+ # Convert logo to RGBA if it's not already
255
+ if logo_image.mode != 'RGBA':
256
+ logo_image = logo_image.convert('RGBA')
257
+
258
+ combined_image.paste(logo_image, position, logo_image)
259
+
260
+ # Convert back to RGB for compatibility
261
+ image = combined_image.convert('RGB')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
262
 
263
+ generated_images.append(image)
 
 
264
 
265
+ # Display generated image
266
+ st.image(image, caption=f"Generated Poster {i+1}", use_column_width=True)
267
 
268
+ # Provide download option for the generated image
269
+ buf = io.BytesIO()
270
+ image.save(buf, format="PNG")
271
+ byte_im = buf.getvalue()
272
+ st.download_button(
273
+ label=f"Download generated Graphic {i+1}",
274
+ data=byte_im,
275
+ file_name=f"generated_Graphic_{i+1}.png",
276
+ mime="image/png"
277
+ )
278
+ else:
279
+ st.error(f"Failed to generate Graphic {i+1}")
 
 
 
 
 
 
280
 
281
  # Content from image_to_image.py
282
  def encode_image(image):
 
546
  return Image.fromarray(result)
547
 
548
  # Main Streamlit App
549
+ def main():
550
+ # Add logo to the middle of the sidebar
551
+ logo = Image.open("Mark8 AI.png") # Replace with your logo path
552
+ col1, col2, col3 = st.sidebar.columns([1, 2, 1])
553
+ with col2:
554
+ st.image(logo, width=150)
555
+
556
+ # Initialize session state for page
557
+ if 'page' not in st.session_state:
558
+ st.session_state.page = "poster_generation"
559
+
560
+ # Function to display title and description
561
+ def display_title_and_description(title, description):
562
+ st.title(title)
563
+ st.write(description)
564
+
565
+ if st.sidebar.button("Poster Generation"):
566
+ st.session_state.page = "poster_generation"
567
+
568
+ if st.sidebar.button("Image to Image Generation"):
569
+ st.session_state.page = "text_to_image"
570
+
571
+ if st.sidebar.button("Image Editing"):
572
+ st.session_state.page = "image_editing"
573
+
574
+ if st.sidebar.button("Advertisement Generator"):
575
+ st.session_state.page = "advertisement_generator"
576
 
577
+ if st.session_state.page == "text_to_image":
578
+ display_title_and_description("Graphic Generator", "Transform your ideas into stunning visuals.")
579
  text_to_image_generation()
580
+ elif st.session_state.page == "image_editing":
581
+ display_title_and_description("Graphic Generator", "Enhance and modify your images with powerful tools.")
582
  image_editing()
583
+ elif st.session_state.page == "poster_generation":
584
+ display_title_and_description("Graphic Generator", "Create eye-catching posters for various platforms.")
585
  generate_poster()
586
+ elif st.session_state.page == "advertisement_generator":
587
+ display_title_and_description("Graphic Generator", "Create compelling advertisements with AI assistance.")
588
  advertisement_generator()
589
 
590
  def text_to_image_generation():
591
+ # st.header("Text to Image Generation")
592
 
593
  # Image to Image Generation
594
  st.subheader("Image to Image Generation")
 
638
  st.image(new_image, caption=f"Generated Image {i+1}", use_column_width=True)
639
 
640
  def image_editing():
641
+ #st.header("Image Editing")
642
 
643
  # Background Removal
644
  st.subheader("Background Removal")