Commit
·
efbc792
1
Parent(s):
ec55776
Upload 2 files
Browse files- app.py +108 -0
- requirements.txt +7 -0
app.py
CHANGED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
30 |
+
st.session_state['image_url'] = ""
|
31 |
+
|
32 |
+
# Main content
|
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('select Number of Image to be generatd ', ('1', '2', "3", '4', '5')) # Number of images
|
37 |
+
|
38 |
+
# Pizza order input
|
39 |
+
pizza_names =["Crispy Chicken", "Chicken Pesto", "Deluxe Pepperoni", "Truffle Mushroom",
|
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", "Basil", "Broccoli", "Cheese", "Chicken", "Corn", "Ham", "Mushroom", "Olives", "Onion", "Pepperoni", "Peppers", "Pineapple", "Tomatoes" ] # rest of the list
|
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', 'Light Blue', 'Light Green', 'Light Yellow', 'Light Pink']
|
51 |
+
bg_color = st.selectbox('Select Background Color', bg_color_options)
|
52 |
+
orient =["Top View", "Side View", "lateral View"]
|
53 |
+
orientation_checkbox = st.multiselect("Orientation", orient, default=["Top View"])
|
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 descrpition in {orientation_checkbox} with back ground color as {bg_color} "
|
60 |
+
st.write(st.session_state['dynamic_prompt'])
|
61 |
+
st.session_state['generate_image'] = True
|
62 |
+
|
63 |
+
# Display the button to generate the image only if the prompt has been generated
|
64 |
+
if st.session_state['generate_image']:
|
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 |
+
# Image generation code using OpenAI's API
|
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 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
notebook
|
2 |
+
ipywidgets
|
3 |
+
tqdm
|
4 |
+
python-dotenv
|
5 |
+
openai
|
6 |
+
streamlit
|
7 |
+
rembg
|