Commit
·
acb8c94
1
Parent(s):
77d5b22
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,15 @@
|
|
1 |
import streamlit as st
|
2 |
-
#import openai
|
3 |
-
#import os
|
4 |
-
import tempfile
|
5 |
-
#from dotenv import load_dotenv
|
6 |
import requests # For downloading images
|
7 |
-
|
8 |
-
# Load environment variables
|
9 |
-
#load_dotenv()
|
10 |
-
#openai_api_key = os.getenv("OPENAI_API_KEY")
|
11 |
-
|
12 |
-
# Initialize OpenAI API key
|
13 |
-
#openai.api_key = openai_api_key
|
14 |
|
15 |
# Set page config
|
16 |
st.set_page_config(page_title='Image Generation', page_icon=':money_with_wings:', layout='wide')
|
17 |
|
18 |
st.title('Image Generation')
|
19 |
|
20 |
-
|
21 |
-
|
22 |
# Initialize session state
|
23 |
if 'dynamic_prompt' not in st.session_state:
|
24 |
st.session_state['dynamic_prompt'] = ""
|
25 |
-
if 'generate_image' not in st.session_state:
|
26 |
-
st.session_state['generate_image'] = False
|
27 |
if 'image_generated' not in st.session_state:
|
28 |
st.session_state['image_generated'] = False
|
29 |
if 'image_url' not in st.session_state:
|
@@ -33,76 +19,56 @@ if 'image_url' not in st.session_state:
|
|
33 |
with st.form(key='my_form'):
|
34 |
# Image generation input
|
35 |
size = st.selectbox('Select size of the images', ('256x256', '512x512', '1024x1024'))
|
36 |
-
num_images = st.selectbox('
|
37 |
|
38 |
# Pizza order input
|
39 |
-
pizza_names =["Crispy Chicken", "Chicken Pesto",
|
40 |
-
"Ultimate Cheese", "Spicy Veggie Ranch", "Classic Chicken Ranch",
|
41 |
-
"BBQ Chicken Ranch", "Spicy Chicken Ranch", "Very Veggie",
|
42 |
-
"Super Supreme", "Classic Pepperoni", "Margherita", "Cheeky Chicken",
|
43 |
-
"Chicken Super Supreme", "Chicken BBQ Supreme", "Chicken Fajita",
|
44 |
-
"Chicken Shawerma", "Other"]
|
45 |
name = st.selectbox("Name of Pizza", pizza_names)
|
46 |
size_pizza = st.selectbox("Size of Pizza", ["Small", "Medium", "Large", "Extra Large"])
|
47 |
-
toppings_options = ["Arugula", "Bacon",
|
48 |
toppings = st.multiselect("Toppings", toppings_options, default=["Mushroom", "Tomatoes", "Onion"])
|
49 |
prompt = st.text_input(label='Enter additional Descriptions ')
|
50 |
-
bg_color_options = ['White', 'Black',
|
51 |
bg_color = st.selectbox('Select Background Color', bg_color_options)
|
52 |
-
|
53 |
-
orientation_checkbox = st.multiselect("Orientation",
|
54 |
|
55 |
generate_prompt_button = st.form_submit_button(label='Generate Prompt')
|
56 |
|
57 |
# Action when the generate prompt button is clicked
|
58 |
if generate_prompt_button:
|
59 |
-
st.session_state['dynamic_prompt'] = f"Imagine you are a marketer who wants to post an image on social media for a {size_pizza} {name} Pizza with following toppings on it {', '.join(toppings)}. I want to generate image for the given
|
60 |
st.write(st.session_state['dynamic_prompt'])
|
61 |
-
st.session_state['
|
|
|
62 |
|
63 |
# Display the button to generate the image only if the prompt has been generated
|
64 |
-
if st.session_state['
|
65 |
with st.form(key='image_form'):
|
66 |
generate_image_button = st.form_submit_button(label='Generate Image')
|
67 |
|
68 |
if generate_image_button:
|
69 |
-
|
70 |
-
|
71 |
-
if st.session_state['dynamic_prompt']:
|
72 |
-
response = openai.Image.create(
|
73 |
-
prompt=st.session_state['dynamic_prompt'],
|
74 |
-
n=num_images,
|
75 |
-
size=size,
|
76 |
-
)
|
77 |
-
st.session_state['image_url'] = response["data"][0]["url"]
|
78 |
st.image(st.session_state['image_url'], caption="Generated image", use_column_width=True)
|
79 |
-
|
80 |
-
# Update the session state to reflect that the image has been generated
|
81 |
-
st.session_state['image_generated'] = True
|
82 |
-
|
83 |
-
submitted = st.form_submit_button("Submit")
|
84 |
-
#st.write("Outside the form")
|
85 |
-
|
86 |
-
|
87 |
-
# Download button for the generated image
|
88 |
-
if st.session_state['image_generated']:
|
89 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
|
90 |
-
response = requests.get(st.session_state['image_url'])
|
91 |
-
with open(tmp_file.name, "wb") as f:
|
92 |
-
f.write(response.content) # Saving the image data
|
93 |
-
st.download_button(
|
94 |
-
label="Download image",
|
95 |
-
data=tmp_file,
|
96 |
-
file_name="generated_image.png",
|
97 |
-
mime="image/png"
|
98 |
-
)
|
99 |
-
|
100 |
-
combined_info = f"""
|
101 |
-
Image Generation Details:
|
102 |
-
- Text Prompt: {st.session_state['dynamic_prompt']}
|
103 |
-
- Image Size: {size}
|
104 |
-
- Number of Images: {num_images}
|
105 |
-
"""
|
106 |
-
st.write(combined_info)
|
107 |
-
|
108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
2 |
import requests # For downloading images
|
3 |
+
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# Set page config
|
6 |
st.set_page_config(page_title='Image Generation', page_icon=':money_with_wings:', layout='wide')
|
7 |
|
8 |
st.title('Image Generation')
|
9 |
|
|
|
|
|
10 |
# Initialize session state
|
11 |
if 'dynamic_prompt' not in st.session_state:
|
12 |
st.session_state['dynamic_prompt'] = ""
|
|
|
|
|
13 |
if 'image_generated' not in st.session_state:
|
14 |
st.session_state['image_generated'] = False
|
15 |
if 'image_url' not in st.session_state:
|
|
|
19 |
with st.form(key='my_form'):
|
20 |
# Image generation input
|
21 |
size = st.selectbox('Select size of the images', ('256x256', '512x512', '1024x1024'))
|
22 |
+
num_images = st.selectbox('Select Number of Images to be Generated', ('1', '2', '3', '4', '5'))
|
23 |
|
24 |
# Pizza order input
|
25 |
+
pizza_names = ["Crispy Chicken", "Chicken Pesto", ...] # truncated for brevity
|
|
|
|
|
|
|
|
|
|
|
26 |
name = st.selectbox("Name of Pizza", pizza_names)
|
27 |
size_pizza = st.selectbox("Size of Pizza", ["Small", "Medium", "Large", "Extra Large"])
|
28 |
+
toppings_options = ["Arugula", "Bacon", ...] # truncated for brevity
|
29 |
toppings = st.multiselect("Toppings", toppings_options, default=["Mushroom", "Tomatoes", "Onion"])
|
30 |
prompt = st.text_input(label='Enter additional Descriptions ')
|
31 |
+
bg_color_options = ['White', 'Black', ...] # truncated for brevity
|
32 |
bg_color = st.selectbox('Select Background Color', bg_color_options)
|
33 |
+
orientation = ["Top View", "Side View", "Lateral View"]
|
34 |
+
orientation_checkbox = st.multiselect("Orientation", orientation, default=["Top View"])
|
35 |
|
36 |
generate_prompt_button = st.form_submit_button(label='Generate Prompt')
|
37 |
|
38 |
# Action when the generate prompt button is clicked
|
39 |
if generate_prompt_button:
|
40 |
+
st.session_state['dynamic_prompt'] = f"Imagine you are a marketer who wants to post an image on social media for a {size_pizza} {name} Pizza with following toppings on it {', '.join(toppings)}. I want to generate image for the given description in {orientation_checkbox} with back ground color as {bg_color} "
|
41 |
st.write(st.session_state['dynamic_prompt'])
|
42 |
+
st.session_state['image_url'] = "url_of_your_generated_image" # Here, integrate with your custom model
|
43 |
+
st.session_state['image_generated'] = True
|
44 |
|
45 |
# Display the button to generate the image only if the prompt has been generated
|
46 |
+
if st.session_state['dynamic_prompt']:
|
47 |
with st.form(key='image_form'):
|
48 |
generate_image_button = st.form_submit_button(label='Generate Image')
|
49 |
|
50 |
if generate_image_button:
|
51 |
+
# Display the image
|
52 |
+
if st.session_state['image_url']:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
st.image(st.session_state['image_url'], caption="Generated image", use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
# Download button for the generated image
|
56 |
+
if st.session_state['image_generated']:
|
57 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
|
58 |
+
response = requests.get(st.session_state['image_url'])
|
59 |
+
with open(tmp_file.name, "wb") as f:
|
60 |
+
f.write(response.content) # Saving the image data
|
61 |
+
st.download_button(
|
62 |
+
label="Download image",
|
63 |
+
data=tmp_file,
|
64 |
+
file_name="generated_image.png",
|
65 |
+
mime="image/png"
|
66 |
+
)
|
67 |
+
|
68 |
+
combined_info = f"""
|
69 |
+
Image Generation Details:
|
70 |
+
- Text Prompt: {st.session_state['dynamic_prompt']}
|
71 |
+
- Image Size: {size}
|
72 |
+
- Number of Images: {num_images}
|
73 |
+
"""
|
74 |
+
st.write(combined_info)
|