Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -15,7 +15,7 @@ import pytesseract
|
|
15 |
from fontTools.ttLib import TTFont
|
16 |
from langchain_groq import ChatGroq
|
17 |
import logging
|
18 |
-
from
|
19 |
|
20 |
|
21 |
# Load environment variables
|
@@ -100,7 +100,7 @@ def advertisement_generator():
|
|
100 |
logging.basicConfig(level=logging.DEBUG)
|
101 |
logger = logging.getLogger(__name__)
|
102 |
|
103 |
-
client =
|
104 |
|
105 |
def generate_poster():
|
106 |
#st.header("Generate Social Media Post")
|
@@ -128,11 +128,11 @@ def generate_poster():
|
|
128 |
if aspect_ratio == "1:1":
|
129 |
dimensions = st.selectbox("Select Image Dimensions", ["1024x1024", "1200x1200", "1504x1504"])
|
130 |
elif aspect_ratio == "16:9":
|
131 |
-
dimensions = st.selectbox("Select Image Dimensions", ["
|
132 |
elif aspect_ratio == "4:5":
|
133 |
-
dimensions = st.selectbox("Select Image Dimensions", ["
|
134 |
elif aspect_ratio == "9:16":
|
135 |
-
dimensions = st.selectbox("Select Image Dimensions", ["
|
136 |
|
137 |
design_style = st.selectbox("Select Design Style", [
|
138 |
"Minimalistic", "Bold/Graphic", "Elegant", "Playful/Fun",
|
@@ -174,8 +174,11 @@ def generate_poster():
|
|
174 |
|
175 |
if generate_button:
|
176 |
# Generate 4 different variations of the prompt with enhanced realism and attention-grabbing elements
|
|
|
|
|
|
|
177 |
prompt_variations = [
|
178 |
-
f"Create a
|
179 |
for i in range(4)
|
180 |
]
|
181 |
|
@@ -184,11 +187,36 @@ def generate_poster():
|
|
184 |
with st.spinner(f"Generating Graphic {i+1}..."):
|
185 |
logger.info(f"Generating Graphic {i+1} with prompt: {prompt}")
|
186 |
|
187 |
-
#
|
188 |
-
|
189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
|
191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
# Add logo if provided
|
193 |
if logo:
|
194 |
logo_image = Image.open(logo)
|
@@ -274,9 +302,21 @@ def generate_image_prompt(image):
|
|
274 |
return response.text
|
275 |
|
276 |
def generate_new_image(prompt):
|
277 |
-
# Generate image using
|
278 |
-
|
279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
280 |
|
281 |
# Part 2: Image Editing
|
282 |
|
|
|
15 |
from fontTools.ttLib import TTFont
|
16 |
from langchain_groq import ChatGroq
|
17 |
import logging
|
18 |
+
from together import Together
|
19 |
|
20 |
|
21 |
# Load environment variables
|
|
|
100 |
logging.basicConfig(level=logging.DEBUG)
|
101 |
logger = logging.getLogger(__name__)
|
102 |
|
103 |
+
client = Together()
|
104 |
|
105 |
def generate_poster():
|
106 |
#st.header("Generate Social Media Post")
|
|
|
128 |
if aspect_ratio == "1:1":
|
129 |
dimensions = st.selectbox("Select Image Dimensions", ["1024x1024", "1200x1200", "1504x1504"])
|
130 |
elif aspect_ratio == "16:9":
|
131 |
+
dimensions = st.selectbox("Select Image Dimensions", ["1024x576", "1280x720", "1792x1008"])
|
132 |
elif aspect_ratio == "4:5":
|
133 |
+
dimensions = st.selectbox("Select Image Dimensions", ["1024x1280", "1200x1500", "1600x2000"])
|
134 |
elif aspect_ratio == "9:16":
|
135 |
+
dimensions = st.selectbox("Select Image Dimensions", ["576x1024", "720x1280", "1008x1792"])
|
136 |
|
137 |
design_style = st.selectbox("Select Design Style", [
|
138 |
"Minimalistic", "Bold/Graphic", "Elegant", "Playful/Fun",
|
|
|
174 |
|
175 |
if generate_button:
|
176 |
# Generate 4 different variations of the prompt with enhanced realism and attention-grabbing elements
|
177 |
+
lighting_options = ['golden hour lighting', 'studio lighting', 'natural daylight', 'dramatic spotlights']
|
178 |
+
visual_elements = ['3D elements', 'metallic accents', 'glass effects', 'neon highlights']
|
179 |
+
|
180 |
prompt_variations = [
|
181 |
+
f"Create a professional and eye-catching {post_type.lower()} advertisement. The image should feature impactful {selected_color.lower() if selected_color != 'None' else 'vibrant'} colors that align with brand identity. Header: \"{header}\". Sub-header: \"{sub_header}\". Implement a {design_style.lower()} design style with clean, commercial-grade visuals. Main focus: {description}. Compose in {aspect_ratio} aspect ratio at {width}x{height}. The design should incorporate modern {random.choice(visual_elements)} to enhance visual appeal. Ensure high resolution with perfect clarity and legibility. Text should be bold, clear and strategically placed for maximum impact. Create a compelling visual hierarchy that drives attention to key messaging and call-to-action elements. Make it look like a premium advertisement created by a professional design agency. Variation {i+1}/4."
|
182 |
for i in range(4)
|
183 |
]
|
184 |
|
|
|
187 |
with st.spinner(f"Generating Graphic {i+1}..."):
|
188 |
logger.info(f"Generating Graphic {i+1} with prompt: {prompt}")
|
189 |
|
190 |
+
# Adjust dimensions if needed to stay within API limits
|
191 |
+
adjusted_width = min(1792, max(64, width))
|
192 |
+
adjusted_height = min(1792, max(64, height))
|
193 |
+
|
194 |
+
# Maintain aspect ratio while adjusting dimensions
|
195 |
+
if width > 1792 or height > 1792:
|
196 |
+
ratio = min(1792/width, 1792/height)
|
197 |
+
adjusted_width = int(width * ratio)
|
198 |
+
adjusted_height = int(height * ratio)
|
199 |
|
200 |
+
# Generate image using Together API
|
201 |
+
response = client.images.generate(
|
202 |
+
prompt=prompt,
|
203 |
+
model="black-forest-labs/FLUX.1-schnell-Free",
|
204 |
+
width=adjusted_width,
|
205 |
+
height=adjusted_height,
|
206 |
+
steps=4,
|
207 |
+
n=1,
|
208 |
+
response_format="b64_json"
|
209 |
+
)
|
210 |
+
|
211 |
+
if response.data:
|
212 |
+
# Convert base64 to image
|
213 |
+
image_data = base64.b64decode(response.data[0].b64_json)
|
214 |
+
image = Image.open(io.BytesIO(image_data))
|
215 |
+
|
216 |
+
# Resize back to original dimensions if needed
|
217 |
+
if adjusted_width != width or adjusted_height != height:
|
218 |
+
image = image.resize((width, height), Image.LANCZOS)
|
219 |
+
|
220 |
# Add logo if provided
|
221 |
if logo:
|
222 |
logo_image = Image.open(logo)
|
|
|
302 |
return response.text
|
303 |
|
304 |
def generate_new_image(prompt):
|
305 |
+
# Generate image using Together API
|
306 |
+
response = client.images.generate(
|
307 |
+
prompt=prompt,
|
308 |
+
model="black-forest-labs/FLUX.1-schnell-Free",
|
309 |
+
width=1024,
|
310 |
+
height=768,
|
311 |
+
steps=4,
|
312 |
+
n=1,
|
313 |
+
response_format="b64_json"
|
314 |
+
)
|
315 |
+
|
316 |
+
if response.data:
|
317 |
+
image_data = base64.b64decode(response.data[0].b64_json)
|
318 |
+
return Image.open(io.BytesIO(image_data))
|
319 |
+
return None
|
320 |
|
321 |
# Part 2: Image Editing
|
322 |
|