Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,26 +28,61 @@ openai.api_key = st.secrets["openai_api_key"]
|
|
| 28 |
# # Set the OpenAI API key
|
| 29 |
# openai.api_key = openai_api_key
|
| 30 |
|
| 31 |
-
def generate_images(prompt): #def generate_images(image_description, num_images):
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
prompt = st.text_area("Enter a description for the image you want to generate")
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
#create a button
|
| 46 |
-
if st.button("Generate Images"):
|
| 47 |
-
with st.spinner("Analyzing the image ..."):
|
| 48 |
-
generate_image=generate_images(prompt) #generate_image=generate_images(prompt, num_of_images)
|
| 49 |
-
st.image(generate_image)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
|
|
|
|
| 28 |
# # Set the OpenAI API key
|
| 29 |
# openai.api_key = openai_api_key
|
| 30 |
|
| 31 |
+
# def generate_images(prompt): #def generate_images(image_description, num_images):
|
| 32 |
+
# response = openai.images.generate(
|
| 33 |
+
# model="dall-e-3",
|
| 34 |
+
# prompt = prompt,
|
| 35 |
+
# size="1024x1024",
|
| 36 |
+
# quality="standard",
|
| 37 |
+
# n = 1,
|
| 38 |
+
# )
|
| 39 |
+
# image_url = response.data[0].url
|
| 40 |
+
# return image_url
|
| 41 |
+
|
| 42 |
+
# prompt = st.text_area("Enter a description for the image you want to generate")
|
| 43 |
|
|
|
|
| 44 |
|
| 45 |
+
# #create a button
|
| 46 |
+
# if st.button("Generate Images"):
|
| 47 |
+
# with st.spinner("Analyzing the image ..."):
|
| 48 |
+
# generate_image=generate_images(prompt) #generate_image=generate_images(prompt, num_of_images)
|
| 49 |
+
# st.image(generate_image)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
def generate_images(prompt): #def generate_images(image_description, num_images):
|
| 53 |
+
response = openai.images.generate(
|
| 54 |
+
model="dall-e-3",
|
| 55 |
+
prompt = prompt,
|
| 56 |
+
size="1024x1024",
|
| 57 |
+
quality="standard",
|
| 58 |
+
n = 1,
|
| 59 |
+
)
|
| 60 |
+
image_url = response.data[0].url
|
| 61 |
+
return image_url
|
| 62 |
+
|
| 63 |
+
# Use a unique key for the text area to avoid conflicts with other session_state usages
|
| 64 |
+
image_description_prompt = st.text_area("Enter a description for the image you want to generate", key="image_description")
|
| 65 |
+
|
| 66 |
+
if st.button("Generate Images"):
|
| 67 |
+
with st.spinner("Analyzing the image ..."):
|
| 68 |
+
# Generate the image and store its URL in session_state
|
| 69 |
+
st.session_state['generated_image_url'] = generate_images(image_description_prompt)
|
| 70 |
+
|
| 71 |
+
# Check if the 'generated_image_url' key exists in session_state and display the image and download button
|
| 72 |
+
if 'generated_image_url' in st.session_state and st.session_state['generated_image_url']:
|
| 73 |
+
st.image(st.session_state['generated_image_url'], caption="Generated Image")
|
| 74 |
+
|
| 75 |
+
# Fetch the image from the URL to enable downloading
|
| 76 |
+
response = requests.get(st.session_state['generated_image_url'])
|
| 77 |
+
if response.status_code == 200:
|
| 78 |
+
img_bytes = BytesIO(response.content)
|
| 79 |
+
st.download_button(
|
| 80 |
+
label="Download Image",
|
| 81 |
+
data=img_bytes,
|
| 82 |
+
file_name="generated_image.png",
|
| 83 |
+
mime="image/png"
|
| 84 |
+
)
|
| 85 |
+
st.success('Powered by MTSS GPT. AI can make mistakes. Consider checking important information.')
|
| 86 |
|
| 87 |
|
| 88 |
|