-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp2.py
More file actions
756 lines (628 loc) Β· 25.2 KB
/
Copy pathapp2.py
File metadata and controls
756 lines (628 loc) Β· 25.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
"""
AI Visual Studio - Complete Streamlit App
Social Media Image & Banner Generator with AI-powered content
Author: AI Assistant
Date: 2025
"""
import streamlit as st
import openai
from PIL import Image, ImageDraw, ImageFont
import io
import base64
from datetime import datetime
import requests
import json
# ==================== PAGE CONFIGURATION ====================
st.set_page_config(
page_title="AI Visual Studio",
page_icon="π¨",
layout="wide",
initial_sidebar_state="expanded"
)
# ==================== CUSTOM CSS ====================
st.markdown("""
<style>
.main-header {
font-size: 3rem;
font-weight: bold;
background: linear-gradient(90deg, #f093fb 0%, #f5576c 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
text-align: center;
margin-bottom: 1rem;
}
.sub-header {
text-align: center;
color: #666;
margin-bottom: 2rem;
}
.stButton>button {
width: 100%;
background: linear-gradient(90deg, #f093fb 0%, #f5576c 100%);
color: white;
border: none;
padding: 0.75rem;
font-weight: bold;
border-radius: 8px;
}
.stButton>button:hover {
background: linear-gradient(90deg, #f5576c 0%, #f093fb 100%);
}
.preview-container {
background-color: #f8f9fa;
padding: 2rem;
border-radius: 12px;
text-align: center;
margin: 1rem 0;
}
.social-post-box {
background: white;
border: 1px solid #e0e0e0;
border-radius: 12px;
padding: 1.5rem;
margin: 1rem 0;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.cost-info {
background-color: #e8f5e9;
padding: 1rem;
border-radius: 8px;
margin-top: 1rem;
}
.footer {
text-align: center;
padding: 2rem;
color: #666;
margin-top: 3rem;
}
.platform-badge {
display: inline-block;
padding: 0.25rem 0.75rem;
border-radius: 20px;
font-size: 0.85rem;
font-weight: bold;
margin: 0.25rem;
}
.badge-instagram {
background: linear-gradient(45deg, #f09433 0%,#e6683c 25%,#dc2743 50%,#cc2366 75%,#bc1888 100%);
color: white;
}
.badge-facebook {
background: #1877f2;
color: white;
}
.badge-twitter {
background: #1da1f2;
color: white;
}
.badge-linkedin {
background: #0077b5;
color: white;
}
</style>
""", unsafe_allow_html=True)
# ==================== BANNER SIZE PRESETS ====================
BANNER_SIZES = {
"Instagram Post (Square)": (1080, 1080),
"Instagram Story": (1080, 1920),
"Facebook Post": (1200, 630),
"Facebook Cover": (820, 312),
"Twitter Post": (1200, 675),
"Twitter Header": (1500, 500),
"LinkedIn Post": (1200, 627),
"LinkedIn Banner": (1584, 396),
"YouTube Thumbnail": (1280, 720),
"Pinterest Pin": (1000, 1500),
"Web Banner (Large)": (728, 90),
"Web Banner (Medium)": (468, 60),
"Custom Size": (None, None)
}
# ==================== SOCIAL MEDIA PLATFORMS ====================
PLATFORMS = {
"Instagram": {
"emoji": "πΈ",
"color": "#E1306C",
"hashtag_limit": 30,
"char_limit": 2200
},
"Facebook": {
"emoji": "π",
"color": "#1877f2",
"hashtag_limit": 10,
"char_limit": 63206
},
"Twitter/X": {
"emoji": "π¦",
"color": "#1da1f2",
"hashtag_limit": 5,
"char_limit": 280
},
"LinkedIn": {
"emoji": "πΌ",
"color": "#0077b5",
"hashtag_limit": 10,
"char_limit": 3000
},
"Pinterest": {
"emoji": "π",
"color": "#E60023",
"hashtag_limit": 20,
"char_limit": 500
}
}
# ==================== HELPER FUNCTIONS ====================
def generate_image_dalle(api_key, prompt, size="1024x1024", quality="standard", style="vivid"):
"""Generate image using DALL-E 3"""
try:
client = openai.OpenAI(api_key=api_key)
response = client.images.generate(
model="dall-e-3",
prompt=prompt,
size=size,
quality=quality,
style=style,
n=1
)
image_url = response.data[0].url
revised_prompt = response.data[0].revised_prompt
# Download the image
img_response = requests.get(image_url)
img = Image.open(io.BytesIO(img_response.content))
return img, revised_prompt, None
except openai.AuthenticationError:
return None, None, "β Invalid API key. Please check your OpenAI API key."
except openai.BadRequestError as e:
return None, None, f"β Bad request: {str(e)}"
except Exception as e:
return None, None, f"β Error generating image: {str(e)}"
def resize_image(img, target_size):
"""Resize image to target size while maintaining aspect ratio"""
if target_size[0] is None or target_size[1] is None:
return img
# Create new image with target size and paste the generated image
new_img = Image.new('RGB', target_size, color='white')
# Calculate scaling to fit
img_ratio = img.width / img.height
target_ratio = target_size[0] / target_size[1]
if img_ratio > target_ratio:
# Image is wider than target
new_width = target_size[0]
new_height = int(target_size[0] / img_ratio)
else:
# Image is taller than target
new_height = target_size[1]
new_width = int(target_size[1] * img_ratio)
img_resized = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
# Center the image
x = (target_size[0] - new_width) // 2
y = (target_size[1] - new_height) // 2
new_img.paste(img_resized, (x, y))
return new_img
def generate_social_caption(api_key, content_idea, platform, tone, include_hashtags, include_cta):
"""Generate social media caption using GPT"""
try:
client = openai.OpenAI(api_key=api_key)
platform_info = PLATFORMS[platform]
prompt = f"""Create an engaging social media caption for {platform}.
Content Idea: {content_idea}
Tone: {tone}
Character Limit: {platform_info['char_limit']} characters
Hashtag Limit: {platform_info['hashtag_limit']} hashtags
Requirements:
- Write in a {tone.lower()} tone
- Keep it under {platform_info['char_limit']} characters
- Make it engaging and platform-appropriate
{'- Include ' + str(platform_info['hashtag_limit']) + ' relevant hashtags at the end' if include_hashtags else '- Do NOT include hashtags'}
{'- Include a clear call-to-action' if include_cta else ''}
- Use emojis strategically (2-4 emojis)
- Make it shareable and conversation-starting
Just provide the caption text, nothing else."""
response = client.chat.completions.create(
model="gpt-4-turbo",
messages=[
{"role": "system", "content": "You are an expert social media manager who creates viral, engaging content."},
{"role": "user", "content": prompt}
],
temperature=0.8,
max_tokens=500
)
caption = response.choices[0].message.content.strip()
tokens = response.usage.total_tokens
return caption, tokens, None
except Exception as e:
return None, 0, f"β Error generating caption: {str(e)}"
def create_composite_post(image, caption, platform):
"""Create a visual representation of a social media post"""
# This creates a mockup showing both image and caption together
platform_info = PLATFORMS[platform]
# Create canvas
post_width = 600
image_height = int(post_width * (image.height / image.width))
caption_height = 200
total_height = image_height + caption_height + 100
canvas = Image.new('RGB', (post_width, total_height), color='white')
# Resize and paste image
img_resized = image.resize((post_width, image_height), Image.Resampling.LANCZOS)
canvas.paste(img_resized, (0, 50))
# Add platform header
draw = ImageDraw.Draw(canvas)
# Platform name at top
header_text = f"{platform_info['emoji']} {platform}"
draw.text((20, 15), header_text, fill=platform_info['color'], font=None)
return canvas
def image_to_base64(img):
"""Convert PIL Image to base64 string"""
buffered = io.BytesIO()
img.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode()
return img_str
def calculate_dalle_cost(size, quality):
"""Calculate DALL-E 3 cost"""
costs = {
"1024x1024": {"standard": 0.040, "hd": 0.080},
"1024x1792": {"standard": 0.080, "hd": 0.120},
"1792x1024": {"standard": 0.080, "hd": 0.120}
}
return costs.get(size, {}).get(quality, 0.040)
def calculate_gpt_cost(tokens):
"""Calculate GPT-4 cost"""
# GPT-4-turbo pricing
return (tokens * 0.01) / 1000
# ==================== MAIN APP ====================
def main():
# Header
st.markdown('<h1 class="main-header">π¨ AI Visual Studio</h1>', unsafe_allow_html=True)
st.markdown('<p class="sub-header">Create Stunning Social Media Visuals & Captions with AI</p>', unsafe_allow_html=True)
# ==================== SIDEBAR ====================
with st.sidebar:
st.header("βοΈ Settings")
# API Key Input
api_key = st.text_input(
"OpenAI API Key",
type="password",
help="Enter your OpenAI API key with DALL-E 3 access"
)
if api_key:
st.success("β
API Key loaded")
else:
st.warning("β οΈ Please enter your API key")
st.divider()
# Image Settings
st.subheader("πΌοΈ Image Settings")
image_quality = st.radio(
"Image Quality",
["standard", "hd"],
help="HD costs 2x more but produces higher quality"
)
image_style = st.radio(
"Image Style",
["vivid", "natural"],
help="Vivid = more dramatic, Natural = more realistic"
)
st.divider()
# Caption Settings
st.subheader("βοΈ Caption Settings")
include_hashtags = st.checkbox("Include Hashtags", value=True)
include_cta = st.checkbox("Include Call-to-Action", value=True)
include_emojis = st.checkbox("Use Emojis", value=True)
st.divider()
# Information
st.subheader("βΉοΈ About")
st.info("""
**AI Visual Studio** creates complete social media posts with:
- AI-generated images (DALL-E 3)
- Optimized captions (GPT-4)
- Platform-specific formatting
- Ready-to-post content
""")
st.divider()
# Tips
with st.expander("π‘ Pro Tips"):
st.markdown("""
**Image Prompts:**
- Be specific and descriptive
- Mention style (realistic, cartoon, etc.)
- Include colors and mood
- Specify composition
**Caption Ideas:**
- Focus on value proposition
- Use storytelling
- Ask questions to drive engagement
- Test different CTAs
""")
# ==================== MAIN CONTENT AREA ====================
# Initialize session state
if 'generated_image' not in st.session_state:
st.session_state.generated_image = None
if 'generated_caption' not in st.session_state:
st.session_state.generated_caption = None
if 'image_cost' not in st.session_state:
st.session_state.image_cost = 0
if 'caption_tokens' not in st.session_state:
st.session_state.caption_tokens = 0
if 'revised_prompt' not in st.session_state:
st.session_state.revised_prompt = None
# Configuration Section
st.header("π Content Configuration")
col1, col2 = st.columns(2)
with col1:
st.subheader("π― Platform & Size")
platform = st.selectbox(
"Select Platform",
list(PLATFORMS.keys()),
help="Choose your target social media platform"
)
banner_size_name = st.selectbox(
"Banner Size",
list(BANNER_SIZES.keys()),
help="Select the size for your banner/image"
)
# Custom size inputs
if banner_size_name == "Custom Size":
col_w, col_h = st.columns(2)
with col_w:
custom_width = st.number_input("Width (px)", min_value=64, max_value=2048, value=1080)
with col_h:
custom_height = st.number_input("Height (px)", min_value=64, max_value=2048, value=1080)
banner_size = (custom_width, custom_height)
else:
banner_size = BANNER_SIZES[banner_size_name]
# Map to DALL-E sizes
if banner_size[0] and banner_size[1]:
aspect_ratio = banner_size[0] / banner_size[1]
if aspect_ratio == 1:
dalle_size = "1024x1024"
elif aspect_ratio > 1:
dalle_size = "1792x1024"
else:
dalle_size = "1024x1792"
else:
dalle_size = "1024x1024"
st.caption(f"DALL-E will generate: {dalle_size}")
with col2:
st.subheader("π‘ Content Details")
content_idea = st.text_area(
"Content Idea / Description",
placeholder="e.g., A modern coffee shop with morning sunlight, cozy atmosphere, perfect for promoting our new breakfast menu",
height=100,
help="Describe what you want to create"
)
tone = st.selectbox(
"Caption Tone",
["Friendly", "Professional", "Inspirational", "Humorous", "Educational", "Promotional"],
help="The tone for your social media caption"
)
st.divider()
# Image Prompt Section
st.subheader("π¨ Image Generation Prompt")
image_prompt = st.text_area(
"Detailed Image Prompt",
placeholder="e.g., A photorealistic image of a cozy modern coffee shop interior with warm morning sunlight streaming through large windows, wooden furniture, plants, people enjoying coffee, warm color palette, professional photography style",
height=120,
help="Be specific about style, colors, mood, and composition"
)
st.caption("π‘ Tip: DALL-E 3 works best with detailed, descriptive prompts mentioning style, colors, and mood")
# Generate Button
st.markdown("<br>", unsafe_allow_html=True)
col1, col2, col3 = st.columns([1, 1, 1])
with col2:
generate_button = st.button("π Generate Complete Post", use_container_width=True, type="primary")
# Generate content
if generate_button:
if not api_key:
st.error("β Please enter your OpenAI API key in the sidebar")
elif not image_prompt:
st.error("β Please provide an image generation prompt")
elif not content_idea:
st.error("β Please describe your content idea for the caption")
else:
# Progress tracking
progress_bar = st.progress(0)
status_text = st.empty()
# Step 1: Generate Image
status_text.text("π¨ Generating image with DALL-E 3...")
progress_bar.progress(25)
image, revised_prompt, error = generate_image_dalle(
api_key, image_prompt, dalle_size, image_quality, image_style
)
if error:
st.error(error)
progress_bar.empty()
status_text.empty()
else:
# Resize to target size
if banner_size != (None, None):
image = resize_image(image, banner_size)
st.session_state.generated_image = image
st.session_state.revised_prompt = revised_prompt
st.session_state.image_cost = calculate_dalle_cost(dalle_size, image_quality)
progress_bar.progress(50)
# Step 2: Generate Caption
status_text.text("βοΈ Creating social media caption with GPT-4...")
progress_bar.progress(75)
caption, tokens, error = generate_social_caption(
api_key, content_idea, platform, tone, include_hashtags, include_cta
)
if error:
st.error(error)
progress_bar.empty()
status_text.empty()
else:
st.session_state.generated_caption = caption
st.session_state.caption_tokens = tokens
progress_bar.progress(100)
status_text.text("β
Post generated successfully!")
# Clear progress after 1 second
import time
time.sleep(1)
progress_bar.empty()
status_text.empty()
st.success("π Your social media post is ready!")
st.rerun()
# Display Generated Content
if st.session_state.generated_image and st.session_state.generated_caption:
st.divider()
st.header("π± Your Social Media Post")
# Two column layout for image and caption
col_img, col_caption = st.columns([1, 1])
with col_img:
st.subheader("πΌοΈ Generated Image")
# Display image in a nice container
st.markdown('<div class="preview-container">', unsafe_allow_html=True)
st.image(st.session_state.generated_image, use_container_width=True)
st.markdown('</div>', unsafe_allow_html=True)
# Image info
img = st.session_state.generated_image
st.caption(f"π Size: {img.width}x{img.height} pixels")
# Download image button
img_byte_arr = io.BytesIO()
st.session_state.generated_image.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
st.download_button(
label="π₯ Download Image",
data=img_byte_arr,
file_name=f"social_media_image_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png",
mime="image/png",
use_container_width=True
)
# Show revised prompt
if st.session_state.revised_prompt:
with st.expander("π DALL-E Revised Prompt"):
st.info(st.session_state.revised_prompt)
with col_caption:
st.subheader(f"βοΈ {platform} Caption")
# Display caption in a social media-style box
st.markdown('<div class="social-post-box">', unsafe_allow_html=True)
platform_info = PLATFORMS[platform]
st.markdown(f"### {platform_info['emoji']} {platform}")
# Editable caption
edited_caption = st.text_area(
"Caption (editable)",
value=st.session_state.generated_caption,
height=300,
help="You can edit the caption before posting"
)
st.session_state.generated_caption = edited_caption
# Character count
char_count = len(edited_caption)
char_limit = platform_info['char_limit']
if char_count > char_limit:
st.error(f"β οΈ Caption is {char_count - char_limit} characters over the {platform} limit!")
else:
st.success(f"β
{char_count}/{char_limit} characters")
st.markdown('</div>', unsafe_allow_html=True)
# Copy to clipboard helper
st.code(edited_caption, language=None)
# Download caption
st.download_button(
label="π₯ Download Caption",
data=edited_caption,
file_name=f"caption_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt",
mime="text/plain",
use_container_width=True
)
# Action Buttons
st.markdown("<br>", unsafe_allow_html=True)
col1, col2, col3, col4 = st.columns(4)
with col1:
if st.button("π Regenerate Both", use_container_width=True):
st.session_state.generated_image = None
st.session_state.generated_caption = None
st.rerun()
with col2:
if st.button("π¨ Regenerate Image", use_container_width=True):
if image_prompt:
with st.spinner("Generating new image..."):
image, revised_prompt, error = generate_image_dalle(
api_key, image_prompt, dalle_size, image_quality, image_style
)
if not error:
if banner_size != (None, None):
image = resize_image(image, banner_size)
st.session_state.generated_image = image
st.session_state.revised_prompt = revised_prompt
st.rerun()
with col3:
if st.button("βοΈ Regenerate Caption", use_container_width=True):
if content_idea:
with st.spinner("Generating new caption..."):
caption, tokens, error = generate_social_caption(
api_key, content_idea, platform, tone, include_hashtags, include_cta
)
if not error:
st.session_state.generated_caption = caption
st.session_state.caption_tokens = tokens
st.rerun()
with col4:
if st.button("ποΈ Clear All", use_container_width=True):
st.session_state.generated_image = None
st.session_state.generated_caption = None
st.session_state.image_cost = 0
st.session_state.caption_tokens = 0
st.rerun()
# Cost Summary
st.divider()
st.subheader("π° Cost Summary")
st.markdown('<div class="cost-info">', unsafe_allow_html=True)
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Image Cost (DALL-E)", f"${st.session_state.image_cost:.4f}")
with col2:
caption_cost = calculate_gpt_cost(st.session_state.caption_tokens)
st.metric("Caption Cost (GPT-4)", f"${caption_cost:.4f}")
with col3:
total_cost = st.session_state.image_cost + caption_cost
st.metric("Total Cost", f"${total_cost:.4f}")
st.markdown('</div>', unsafe_allow_html=True)
# Platform-specific tips
with st.expander(f"π {platform} Best Practices"):
platform_info = PLATFORMS[platform]
st.markdown(f"""
**{platform} Posting Guidelines:**
- **Character Limit**: {platform_info['char_limit']:,} characters
- **Recommended Hashtags**: {platform_info['hashtag_limit']} hashtags
- **Best Times**: Varies by audience (use analytics)
- **Engagement Tips**:
- Post consistently
- Use high-quality visuals
- Engage with comments quickly
- Track performance metrics
""")
# Sample Templates Section
if not st.session_state.generated_image:
st.divider()
st.header("π‘ Example Content Ideas")
col1, col2, col3 = st.columns(3)
with col1:
st.markdown("""
**π Food & Restaurant**
- New menu item launch
- Behind-the-scenes kitchen
- Customer testimonials
- Special offers
""")
with col2:
st.markdown("""
**πΌ Business & Tech**
- Product announcements
- Company milestones
- Team highlights
- Industry insights
""")
with col3:
st.markdown("""
**π¨ Creative & Lifestyle**
- Personal stories
- Tips and tutorials
- Before/after transformations
- Event promotions
""")
# Footer
st.markdown("---")
st.markdown(
'<div class="footer">Built with β€οΈ using Streamlit, OpenAI DALL-E 3 & GPT-4 | Β© 2025 AI Visual Studio</div>',
unsafe_allow_html=True
)
# ==================== RUN APP ====================
if __name__ == "__main__":
main()