Gokce commited on
Commit
c566077
·
1 Parent(s): 5fad756

Upload 5 files

Browse files
Files changed (5) hide show
  1. Procfile +1 -0
  2. ad_Template_generator.py +79 -0
  3. home.py +28 -0
  4. requirements.txt +4 -0
  5. stable_diffusion.py +31 -0
Procfile ADDED
@@ -0,0 +1 @@
 
 
1
+ web: streamlit run home.py
ad_Template_generator.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image, ImageDraw, ImageFont
3
+
4
+ def create_ad_Template(punchline, button_text, logo_image, main_image, punchline_font_size, button_font_size):
5
+ ad_Template_width, ad_Template_height = 800, 1000
6
+ ad_Template = Image.new("RGB", (ad_Template_width, ad_Template_height), "white")
7
+
8
+ main_image = main_image.resize((300, 300))
9
+ ad_Template.paste(main_image, (250, 300))
10
+
11
+
12
+ logo_size = (200, 150)
13
+ logo_image = logo_image.resize(logo_size)
14
+
15
+ if logo_image.mode != 'RGBA':
16
+ logo_image = logo_image.convert('RGBA')
17
+
18
+ logo_position = (300, 50)
19
+ ad_Template.paste(logo_image, logo_position, logo_image)
20
+
21
+ draw = ImageDraw.Draw(ad_Template)
22
+
23
+ font_path = 'fonts/arial.ttf'
24
+ punchline_font = ImageFont.truetype(font_path, punchline_font_size)
25
+ button_font = ImageFont.truetype(font_path, button_font_size)
26
+
27
+ draw.text((400, 650), punchline, fill="black", font=punchline_font, anchor="mm", spacing=10, align="center", width=400)
28
+
29
+ #button size based on text length and font
30
+ button_text_size = button_font.getmask(button_text).getbbox()[2:]
31
+ button_width = button_text_size[0] + 40
32
+ button_height = button_text_size[1] + 20
33
+
34
+ button_y_position = 800
35
+ button_x_position = 400 - button_width // 2
36
+
37
+ button_position = (button_x_position, button_y_position)
38
+ corner_radius = 10
39
+ draw.rounded_rectangle([button_position, (button_position[0] + button_width, button_position[1] + button_height)],
40
+ fill=(144, 238, 144), outline="black", radius=corner_radius)
41
+
42
+ text_position = (button_x_position + button_width // 2, button_y_position + button_height // 2)
43
+ draw.text(text_position, button_text, fill="black", font=button_font, anchor="mm")
44
+
45
+ return ad_Template
46
+
47
+
48
+ def show_ad_Template_generator_page():
49
+ st.title("Ad Template Generator")
50
+
51
+ punchline = st.text_input("Enter the punchline text for the Ad Template:")
52
+ punchline_font_size = st.select_slider("Select the font size for the punchline:", options=[i for i in range(1, 101)])
53
+
54
+
55
+ button_text = st.text_input("Enter the button text:")
56
+ button_font_size = st.select_slider("Select the font size for the button text:", options=[i for i in range(1, 101)])
57
+
58
+ # Option to use the generated image or upload a new one
59
+ use_generated_image = st.checkbox("Use the image generated by Stable Diffusion")
60
+
61
+ logo_image = st.file_uploader("Choose a logo image...", type=["jpg", "jpeg", "png"])
62
+ if logo_image is not None:
63
+ logo_image = Image.open(logo_image)
64
+
65
+ main_image = None
66
+ if use_generated_image and 'generated_image' in st.session_state:
67
+ main_image = st.session_state['generated_image']
68
+ st.image(main_image, caption="Generated Image", use_column_width=True)
69
+ else:
70
+ uploaded_main_image = st.file_uploader("Choose the main image...", type=["jpg", "jpeg", "png"])
71
+ if uploaded_main_image is not None:
72
+ main_image = Image.open(uploaded_main_image)
73
+
74
+ generate_button = st.button("Generate Ad Template")
75
+
76
+ if generate_button and punchline and button_text and logo_image and main_image:
77
+ ad_Template = create_ad_Template(punchline, button_text, logo_image, main_image, punchline_font_size, button_font_size)
78
+
79
+ st.image(ad_Template, caption="Generated Ad Template", use_column_width=True)
home.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from stable_diffusion import show_stable_diffusion_page
3
+ from ad_Template_generator import show_ad_Template_generator_page
4
+
5
+ st.set_page_config(page_title="Multi-Page App", layout="wide")
6
+
7
+ #the navigation structure
8
+ PAGES = {
9
+ "Home": lambda: st.markdown("""
10
+ # Welcome to the Ad Teamplate Generator App 👋 !
11
+
12
+ This application allows you to generate ad templates with customizable elements such as headlines, button texts, and images.
13
+ You can create your ad template with an image created with stable diffusion or with an image you upload yourself.
14
+
15
+ To get started,my suggestion is to first go to the Stable Diffusion page and create your image. Afterward, navigate to the Ad Template Generator page to prepare your template.
16
+
17
+ Enjoy creating your own ad templates!
18
+ """),
19
+ "Stable Diffusion": show_stable_diffusion_page,
20
+ "Ad Template Generator": show_ad_Template_generator_page
21
+ }
22
+
23
+
24
+ st.sidebar.title("Navigation")
25
+ selection = st.sidebar.radio("Go to", list(PAGES.keys()))
26
+
27
+ page = PAGES[selection]
28
+ page()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ diffusers
3
+ torch
4
+ PIL
stable_diffusion.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from PIL import Image
4
+
5
+ from diffusers import StableDiffusionImg2ImgPipeline
6
+
7
+ def show_stable_diffusion_page():
8
+ st.title("Generate Image Using Stable Diffusion Img2Img")
9
+
10
+ # Initialize the stable diffusion pipeline
11
+ model_id_or_path = "runwayml/stable-diffusion-v1-5"
12
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id_or_path, torch_dtype=torch.float32)
14
+ pipe.to(device)
15
+
16
+
17
+ uploaded_main_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
18
+
19
+ if uploaded_main_image is not None:
20
+ main_image = Image.open(uploaded_main_image).convert("RGB")
21
+ main_image = main_image.resize((768, 512))
22
+
23
+
24
+ prompt = st.text_input("Enter a prompt:")
25
+ #st.write(f"User entered prompt: {prompt}")
26
+
27
+
28
+ if st.button("Generate Image"):
29
+ result_images = pipe(prompt=prompt, image=main_image, strength=0.75, guidance_scale=7.5).images
30
+ st.session_state['generated_image'] = result_images[0]
31
+ st.image(result_images[0], caption="Generated Image", use_column_width=True)