File size: 1,932 Bytes
2f082de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import base64
import streamlit as st
import openai
import os

# openai.api_key = ""
openai.api_key = os.environ.get("OPENAI_API_KEY")

st.set_page_config(
    page_title="DALL·E Gerador de Imagens 🖼️",
    page_icon="🎨",
    layout="wide",
)
# Custom CSS styles
st.markdown(
    """
    <style>
    .download-button {
        background-color: #221e5b;
        color: #ffffff;
        padding: 10px 15px;
        border: 25px;
        border-radius: 5px;
        cursor: pointer;
        text-decoration: none;
        font-weight: bold;
    }

    .download-button:hover {
        background-color: #ff5588;
        color: #ffffff;
    }
    </style>
    """,
    unsafe_allow_html=True
)

st.title("DALL·E Gerador de Imagens 🖼️")

# Prompt input
prompt = st.text_area("Entre o prompt:👇", height=5)

# Size selection
size_options = ["256x256", "512x512", "1024x1024"]
selected_size = st.selectbox("Selecione o tamanho da imagem:", size_options)
# href = f'<a class="download-button" href="data:image/png;base64,{"Hello"}" download="generated_image.png">Download</a>'
# st.markdown(href, unsafe_allow_html=True)


if st.button("Veja a mágica 🪄"):
    # Generate image
    try:
        response = openai.Image.create(
            prompt=prompt,
            n=1,
            size=selected_size,
            response_format="b64_json",
        )

        # Display image

        if response["data"]:
            image_data = base64.b64decode(response["data"][0]["b64_json"])
            st.image(image_data)

            # Download button
            b64_image = base64.b64encode(image_data).decode()
            href = f'<a class="download-button" href="data:image/png;base64,{b64_image}" download="generated_image.png">Download</a>'
            st.markdown(href, unsafe_allow_html=True)
        else:
            st.warning("No image generated.")
    except Exception as e:
        st.error(e)
        print(e)