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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -98
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,7 +121,32 @@ 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
  description = st.text_area("Enter prompt for Advertisement:")
126
  if st.button("Use Advertisement Generator", key="ad_generator_button"):
127
  st.session_state.show_ad_generator = True
@@ -129,60 +154,25 @@ def generate_poster():
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
  if st.button("Generate Images"):
188
  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."
@@ -197,10 +187,9 @@ def generate_poster():
197
  if user_prompt:
198
  full_prompt += f" Include the following text in the image (Prompt {i+1}): '{user_prompt}'."
199
 
200
- generated_images = []
201
  for i in range(num_images):
202
- with st.spinner(f"Generating Graphic {i+1}..."):
203
- logger.info(f"Generating Graphic {i+1} with prompt: {full_prompt}")
204
  # Add a random seed to ensure different images
205
  seed = random.randint(1, 1000000)
206
  image_bytes = query({"inputs": full_prompt, "parameters": {"seed": seed, "width": width, "height": height}})
@@ -255,8 +244,6 @@ def generate_poster():
255
  # Convert back to RGB for compatibility
256
  image = combined_image.convert('RGB')
257
 
258
- generated_images.append(image)
259
-
260
  # Display generated image
261
  st.image(image, caption=f"Generated Poster {i+1}", use_column_width=True)
262
 
@@ -265,13 +252,13 @@ def generate_poster():
265
  image.save(buf, format="PNG")
266
  byte_im = buf.getvalue()
267
  st.download_button(
268
- label=f"Download generated Graphic {i+1}",
269
  data=byte_im,
270
- file_name=f"generated_Graphic_{i+1}.png",
271
  mime="image/png"
272
  )
273
  else:
274
- st.error(f"Failed to generate Graphic {i+1}")
275
 
276
  # Content from image_to_image.py
277
  def encode_image(image):
@@ -541,49 +528,23 @@ def remove_background(image):
541
  return Image.fromarray(result)
542
 
543
  # Main Streamlit App
544
- def main():
545
- # Add logo to the middle of the sidebar
546
- logo = Image.open("Mark8 AI.png") # Replace with your logo path
547
- col1, col2, col3 = st.sidebar.columns([1, 2, 1])
548
- with col2:
549
- st.image(logo, width=150)
550
-
551
- # Initialize session state for page
552
- if 'page' not in st.session_state:
553
- st.session_state.page = "poster_generation"
554
-
555
- # Function to display title and description
556
- def display_title_and_description(title, description):
557
- st.title(title)
558
- st.write(description)
559
-
560
- if st.sidebar.button("Poster Generation"):
561
- st.session_state.page = "poster_generation"
562
-
563
- if st.sidebar.button("Image to Image Generation"):
564
- st.session_state.page = "text_to_image"
565
-
566
- if st.sidebar.button("Image Editing"):
567
- st.session_state.page = "image_editing"
568
-
569
- if st.sidebar.button("Advertisement Generator"):
570
- st.session_state.page = "advertisement_generator"
571
 
572
- if st.session_state.page == "text_to_image":
573
- display_title_and_description("Graphic Generator", "Transform your ideas into stunning visuals.")
574
  text_to_image_generation()
575
- elif st.session_state.page == "image_editing":
576
- display_title_and_description("Graphic Generator", "Enhance and modify your images with powerful tools.")
577
  image_editing()
578
- elif st.session_state.page == "poster_generation":
579
- display_title_and_description("Graphic Generator", "Create eye-catching posters for various platforms.")
580
  generate_poster()
581
- elif st.session_state.page == "advertisement_generator":
582
- display_title_and_description("Graphic Generator", "Create compelling advertisements with AI assistance.")
583
  advertisement_generator()
584
 
585
  def text_to_image_generation():
586
- # st.header("Text to Image Generation")
587
 
588
  # Image to Image Generation
589
  st.subheader("Image to Image Generation")
@@ -633,7 +594,7 @@ def text_to_image_generation():
633
  st.image(new_image, caption=f"Generated Image {i+1}", use_column_width=True)
634
 
635
  def image_editing():
636
- #st.header("Image Editing")
637
 
638
  # Background Removal
639
  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
+ 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
 
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."
 
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}})
 
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
 
 
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
  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
  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")