-
Notifications
You must be signed in to change notification settings - Fork 0
/
89.py
193 lines (164 loc) · 8.3 KB
/
89.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os
import streamlit as st
import tempfile
from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate)
st.title('Design Genius Pro')
# Get openai_api_key
st.sidebar.markdown("""# How to use
1. Enter your [OpenAI API key](https://platform.openai.com/account/api-keys) below🔑
2. Upload a design image by clicking on the "Upload Design Image" button.
3. Select the desired color scheme from the dropdown menu.
4. Select the desired font pairing from the dropdown menu.
5. Provide feedback on the design in the text area.
6. Click the "Submit" button.
7. Wait for the results to be generated by DemoGPT.
8. View the design inspiration, color palette suggestions, font pairing suggestions, and constructive feedback in the respective sections.""")
openai_api_key = st.sidebar.text_input(
"OpenAI API Key",
placeholder="sk-...",
value=os.getenv("OPENAI_API_KEY", ""),
type="password",
)
st.sidebar.markdown("# About")
st.sidebar.markdown("Design Genius Pro is a creative design assistant app that provides designers with valuable suggestions and feedback. With the help of LLM technology, it offers design inspiration, generates color palettes, suggests font pairings, and provides constructive critiques on user-uploaded designs. Whether you need fresh ideas or want to improve your existing designs, Design Genius Pro is here to assist you in creating stunning and visually appealing designs.")
# Copy and paste all the functions as is
def designInspirationGenerator(design_image):
chat = ChatOpenAI(
model="gpt-3.5-turbo-16k",
openai_api_key=openai_api_key,
temperature=0.5
)
system_template = """You are an AI assistant specialized in design. Your task is to generate design inspiration and suggestions based on the given design image: {design_image}."""
system_message_prompt = SystemMessagePromptTemplate.from_template(
system_template)
human_template = """Please analyze the design image: {design_image}, and provide design inspiration and suggestions."""
human_message_prompt = HumanMessagePromptTemplate.from_template(
human_template)
chat_prompt = ChatPromptTemplate.from_messages(
[system_message_prompt, human_message_prompt]
)
chain = LLMChain(llm=chat, prompt=chat_prompt)
result = chain.run(design_image=design_image)
return result # returns string
def colorPaletteGenerator(color_scheme):
chat = ChatOpenAI(
model="gpt-3.5-turbo-16k",
openai_api_key=openai_api_key,
temperature=0.5
)
system_template = """You are a color palette generator. Your task is to suggest color palettes based on the desired color scheme: {color_scheme}."""
system_message_prompt = SystemMessagePromptTemplate.from_template(
system_template)
human_template = """Please generate color palette suggestions for the following color scheme: {color_scheme}."""
human_message_prompt = HumanMessagePromptTemplate.from_template(
human_template)
chat_prompt = ChatPromptTemplate.from_messages(
[system_message_prompt, human_message_prompt]
)
chain = LLMChain(llm=chat, prompt=chat_prompt)
result = chain.run(color_scheme=color_scheme)
return result # returns string
def fontPairingGenerator(font_pairing):
chat = ChatOpenAI(
model="gpt-3.5-turbo-16k",
openai_api_key=openai_api_key,
temperature=0.5
)
system_template = """You are a font expert. Your task is to generate font pairing suggestions based on the desired font pairing: {font_pairing}."""
system_message_prompt = SystemMessagePromptTemplate.from_template(
system_template)
human_template = """Please provide font pairing suggestions for the desired font pairing: {font_pairing}."""
human_message_prompt = HumanMessagePromptTemplate.from_template(
human_template)
chat_prompt = ChatPromptTemplate.from_messages(
[system_message_prompt, human_message_prompt]
)
chain = LLMChain(llm=chat, prompt=chat_prompt)
result = chain.run(font_pairing=font_pairing)
return result # returns string
def feedbackGenerator(feedback):
chat = ChatOpenAI(
model="gpt-3.5-turbo-16k",
openai_api_key=openai_api_key,
temperature=0.5
)
system_template = """You are an AI assistant providing constructive feedback based on user feedback."""
system_message_prompt = SystemMessagePromptTemplate.from_template(
system_template)
human_template = """Thank you for your feedback. Based on your input, here is some constructive feedback: {feedback}."""
human_message_prompt = HumanMessagePromptTemplate.from_template(
human_template)
chat_prompt = ChatPromptTemplate.from_messages(
[system_message_prompt, human_message_prompt]
)
chain = LLMChain(llm=chat, prompt=chat_prompt)
result = chain.run(feedback=feedback)
return result # returns string
# Create a form
with st.form(key='design_genius_pro'):
# Under the form, take all the user inputs
uploaded_file = st.file_uploader("Upload Design Image", type=[
'jpg', 'jpeg', 'png'], key='design_image')
if uploaded_file is not None:
# Create a temporary file to store the uploaded content
extension = uploaded_file.name.split(".")[-1]
with tempfile.NamedTemporaryFile(delete=False, suffix=f'.{extension}') as temp_file:
temp_file.write(uploaded_file.read())
design_image = temp_file.name # it shows the file path
else:
design_image = ''
color_scheme = st.selectbox("Select the color scheme", ["Light", "Dark"])
font_pairing = st.selectbox("Select the desired font pairing", [
"Arial and Times New Roman", "Helvetica and Courier", "Verdana and Georgia", "Roboto and Open Sans", "Lato and Montserrat"])
feedback = st.text_area("Provide feedback on the design")
submit_button = st.form_submit_button(label='Submit')
# If form is submitted by st.form_submit_button run the logic
if submit_button:
# Call the functions
if not openai_api_key.startswith('sk-'):
st.warning('Please enter your OpenAI API key!', icon='⚠')
design_inspiration = ""
elif design_image:
with st.spinner('DemoGPT is working on it. It takes less than 10 seconds...'):
design_inspiration = designInspirationGenerator(design_image)
else:
design_inspiration = ""
if not openai_api_key.startswith('sk-'):
st.warning('Please enter your OpenAI API key!', icon='⚠')
color_palette = ""
elif color_scheme:
with st.spinner('DemoGPT is working on it. It takes less than 10 seconds...'):
color_palette = colorPaletteGenerator(color_scheme)
else:
color_palette = ""
if not openai_api_key.startswith('sk-'):
st.warning('Please enter your OpenAI API key!', icon='⚠')
font_suggestions = ""
elif font_pairing:
with st.spinner('DemoGPT is working on it. It takes less than 10 seconds...'):
font_suggestions = fontPairingGenerator(font_pairing)
else:
font_suggestions = ""
if not openai_api_key.startswith('sk-'):
st.warning('Please enter your OpenAI API key!', icon='⚠')
constructive_feedback = ""
elif feedback:
with st.spinner('DemoGPT is working on it. It takes less than 10 seconds...'):
constructive_feedback = feedbackGenerator(feedback)
else:
constructive_feedback = ""
# Show the results
if design_inspiration is not None and len(str(design_inspiration)) > 0:
st.markdown(design_inspiration)
if color_palette is not None and len(str(color_palette)) > 0:
st.subheader("Color Palette Suggestions")
st.write(color_palette)
if font_suggestions is not None and len(str(font_suggestions)) > 0:
st.subheader("Font Pairing Suggestions")
st.write(font_suggestions)
if constructive_feedback is not None and len(str(constructive_feedback)) > 0:
st.info(constructive_feedback)
# END OF THE CODE