-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdesigner.py
70 lines (59 loc) · 2.47 KB
/
mdesigner.py
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
# Cell 1: Setup
import streamlit as st
from openai import OpenAI
import os
import requests
from io import BytesIO
# Get your OpenAI API key from environment variables
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
st.error("OpenAI API key is not set. Please set it in your environment variables.")
# Used in production
client = OpenAI(api_key=api_key)
# Cell 2: Title & Description
st.title(':rainbow[Merch AI Designer: Revolutionizing Merchandise Creation]')
st.subheader('I create new merch concepts based on your ideas!')
# Cell 3: Sidebar Title and Design Elements
st.sidebar.title("Make Merch!🧵")
st.sidebar.subheader("Simply complete the form below and tap MAKE IT!")
description_input = st.sidebar.text_area("Describe what you want it to look like🤔:")
merch_type = st.sidebar.selectbox("What kind of merch do you want?",
("T-Shirt👚", "Album Cover💿", "Mug☕", "Tote Bag👜"))
st.sidebar.write("You selected:", merch_type)
make_it_button = st.sidebar.button('MAKE IT!')
# Cell 4: Function to generate the image
def generate_image(description, merch_type):
if not api_key:
st.error("OpenAI API key is not set. Please set it in your environment variables.")
return None
prompt = f"Create a design for a {merch_type} with the following description: {description}"
response = client.images.generate(
model="dall-e-3",
prompt=prompt,
size="1024x1024",
quality="standard",
n=1,
)
# Accessing the URL correctly from the response object
image_url = response.data[0].url
return image_url
# Handle button click
if make_it_button:
if description_input.strip() == "":
st.error("Please provide a description for your merch idea.")
else:
thumbnail_url = generate_image(description_input, merch_type)
if thumbnail_url:
st.image(thumbnail_url, caption=f'🤩 Your Custom {merch_type} Idea!')
# Download the image
response = requests.get(thumbnail_url)
img_data = BytesIO(response.content)
# Add a download button
st.download_button(
label="Download Image",
data=img_data,
file_name="custom_merch_image.png",
mime="image/png"
)
else:
st.error("Failed to generate image. Please try again.")