Spaces:
Sleeping
Sleeping
Commit
·
2f082de
1
Parent(s):
71ac5b9
Upload App.py
Browse files
App.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import openai
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# openai.api_key = ""
|
| 7 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
| 8 |
+
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="DALL·E Gerador de Imagens 🖼️",
|
| 11 |
+
page_icon="🎨",
|
| 12 |
+
layout="wide",
|
| 13 |
+
)
|
| 14 |
+
# Custom CSS styles
|
| 15 |
+
st.markdown(
|
| 16 |
+
"""
|
| 17 |
+
<style>
|
| 18 |
+
.download-button {
|
| 19 |
+
background-color: #221e5b;
|
| 20 |
+
color: #ffffff;
|
| 21 |
+
padding: 10px 15px;
|
| 22 |
+
border: 25px;
|
| 23 |
+
border-radius: 5px;
|
| 24 |
+
cursor: pointer;
|
| 25 |
+
text-decoration: none;
|
| 26 |
+
font-weight: bold;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
.download-button:hover {
|
| 30 |
+
background-color: #ff5588;
|
| 31 |
+
color: #ffffff;
|
| 32 |
+
}
|
| 33 |
+
</style>
|
| 34 |
+
""",
|
| 35 |
+
unsafe_allow_html=True
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
st.title("DALL·E Gerador de Imagens 🖼️")
|
| 39 |
+
|
| 40 |
+
# Prompt input
|
| 41 |
+
prompt = st.text_area("Entre o prompt:👇", height=5)
|
| 42 |
+
|
| 43 |
+
# Size selection
|
| 44 |
+
size_options = ["256x256", "512x512", "1024x1024"]
|
| 45 |
+
selected_size = st.selectbox("Selecione o tamanho da imagem:", size_options)
|
| 46 |
+
# href = f'<a class="download-button" href="data:image/png;base64,{"Hello"}" download="generated_image.png">Download</a>'
|
| 47 |
+
# st.markdown(href, unsafe_allow_html=True)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
if st.button("Veja a mágica 🪄"):
|
| 51 |
+
# Generate image
|
| 52 |
+
try:
|
| 53 |
+
response = openai.Image.create(
|
| 54 |
+
prompt=prompt,
|
| 55 |
+
n=1,
|
| 56 |
+
size=selected_size,
|
| 57 |
+
response_format="b64_json",
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Display image
|
| 61 |
+
|
| 62 |
+
if response["data"]:
|
| 63 |
+
image_data = base64.b64decode(response["data"][0]["b64_json"])
|
| 64 |
+
st.image(image_data)
|
| 65 |
+
|
| 66 |
+
# Download button
|
| 67 |
+
b64_image = base64.b64encode(image_data).decode()
|
| 68 |
+
href = f'<a class="download-button" href="data:image/png;base64,{b64_image}" download="generated_image.png">Download</a>'
|
| 69 |
+
st.markdown(href, unsafe_allow_html=True)
|
| 70 |
+
else:
|
| 71 |
+
st.warning("No image generated.")
|
| 72 |
+
except Exception as e:
|
| 73 |
+
st.error(e)
|
| 74 |
+
print(e)
|