|
import streamlit as st |
|
|
|
|
|
import tempfile |
|
|
|
import requests |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.set_page_config(page_title='Image Generation', page_icon=':money_with_wings:', layout='wide') |
|
|
|
st.title('Image Generation') |
|
|
|
|
|
|
|
|
|
if 'dynamic_prompt' not in st.session_state: |
|
st.session_state['dynamic_prompt'] = "" |
|
if 'generate_image' not in st.session_state: |
|
st.session_state['generate_image'] = False |
|
if 'image_generated' not in st.session_state: |
|
st.session_state['image_generated'] = False |
|
if 'image_url' not in st.session_state: |
|
st.session_state['image_url'] = "" |
|
|
|
|
|
with st.form(key='my_form'): |
|
|
|
size = st.selectbox('Select size of the images', ('256x256', '512x512', '1024x1024')) |
|
num_images = st.selectbox('select Number of Image to be generatd ', ('1', '2', "3", '4', '5')) |
|
|
|
|
|
pizza_names =["Crispy Chicken", "Chicken Pesto", "Deluxe Pepperoni", "Truffle Mushroom", |
|
"Ultimate Cheese", "Spicy Veggie Ranch", "Classic Chicken Ranch", |
|
"BBQ Chicken Ranch", "Spicy Chicken Ranch", "Very Veggie", |
|
"Super Supreme", "Classic Pepperoni", "Margherita", "Cheeky Chicken", |
|
"Chicken Super Supreme", "Chicken BBQ Supreme", "Chicken Fajita", |
|
"Chicken Shawerma", "Other"] |
|
name = st.selectbox("Name of Pizza", pizza_names) |
|
size_pizza = st.selectbox("Size of Pizza", ["Small", "Medium", "Large", "Extra Large"]) |
|
toppings_options = ["Arugula", "Bacon", "Basil", "Broccoli", "Cheese", "Chicken", "Corn", "Ham", "Mushroom", "Olives", "Onion", "Pepperoni", "Peppers", "Pineapple", "Tomatoes" ] |
|
toppings = st.multiselect("Toppings", toppings_options, default=["Mushroom", "Tomatoes", "Onion"]) |
|
prompt = st.text_input(label='Enter additional Descriptions ') |
|
bg_color_options = ['White', 'Black', 'Light Blue', 'Light Green', 'Light Yellow', 'Light Pink'] |
|
bg_color = st.selectbox('Select Background Color', bg_color_options) |
|
orient =["Top View", "Side View", "lateral View"] |
|
orientation_checkbox = st.multiselect("Orientation", orient, default=["Top View"]) |
|
|
|
generate_prompt_button = st.form_submit_button(label='Generate Prompt') |
|
|
|
|
|
if generate_prompt_button: |
|
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 descrpition in {orientation_checkbox} with back ground color as {bg_color} " |
|
st.write(st.session_state['dynamic_prompt']) |
|
st.session_state['generate_image'] = True |
|
|
|
|
|
if st.session_state['generate_image']: |
|
with st.form(key='image_form'): |
|
generate_image_button = st.form_submit_button(label='Generate Image') |
|
|
|
if generate_image_button: |
|
|
|
|
|
if st.session_state['dynamic_prompt']: |
|
response = openai.Image.create( |
|
prompt=st.session_state['dynamic_prompt'], |
|
n=num_images, |
|
size=size, |
|
) |
|
st.session_state['image_url'] = response["data"][0]["url"] |
|
st.image(st.session_state['image_url'], caption="Generated image", use_column_width=True) |
|
|
|
|
|
st.session_state['image_generated'] = True |
|
|
|
submitted = st.form_submit_button("Submit") |
|
|
|
|
|
|
|
|
|
if st.session_state['image_generated']: |
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file: |
|
response = requests.get(st.session_state['image_url']) |
|
with open(tmp_file.name, "wb") as f: |
|
f.write(response.content) |
|
st.download_button( |
|
label="Download image", |
|
data=tmp_file, |
|
file_name="generated_image.png", |
|
mime="image/png" |
|
) |
|
|
|
combined_info = f""" |
|
Image Generation Details: |
|
- Text Prompt: {st.session_state['dynamic_prompt']} |
|
- Image Size: {size} |
|
- Number of Images: {num_images} |
|
""" |
|
st.write(combined_info) |
|
|
|
|
|
|