Anustup commited on
Commit
87fcfdb
·
verified ·
1 Parent(s): 8bbd0a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +414 -414
app.py CHANGED
@@ -1,414 +1,414 @@
1
- import streamlit as st
2
- import os
3
- from prompts import prompts
4
- from constants import JSON_SCHEMA_FOR_GPT, UPDATED_MODEL_ONLY_SCHEMA, JSON_SCHEMA_FOR_LOC_ONLY
5
- from gpt import runAssistant, checkRunStatus, retrieveThread, createAssistant, saveFileOpenAI, startAssistantThread, \
6
- create_chat_completion_request_open_ai_for_summary, addMessageToThread, create_image_completion_request_gpt
7
- from summarizer import create_brand_html, create_langchain_openai_query
8
- from theme import flux_generated_image, flux_generated_image_seed
9
- import time
10
- from PIL import Image
11
- import io
12
-
13
-
14
- def process_run(st, thread_id, assistant_id):
15
- run_id = runAssistant(thread_id, assistant_id)
16
- status = 'running'
17
- while status != 'completed':
18
- with st.spinner('. . .'):
19
- time.sleep(20)
20
- status = checkRunStatus(thread_id, run_id)
21
- thread_messages = retrieveThread(thread_id)
22
- for message in thread_messages:
23
- if not message['role'] == 'user':
24
- return message["content"]
25
- else:
26
- pass
27
-
28
-
29
- def page1():
30
- st.title("Upload Product")
31
- st.markdown("<h2 style='color:#FF5733; font-weight:bold;'>Add a Product</h2>", unsafe_allow_html=True)
32
- st.markdown("<p style='color:#444;'>Upload your product images, more images you upload better the AI learns</p>",
33
- unsafe_allow_html=True)
34
- uploaded_files = st.file_uploader("Upload Images", accept_multiple_files=True, key="uploaded_files_key")
35
- product_description = st.text_area("Describe the product", value=st.session_state.get("product_description", ""))
36
- col1, col2 = st.columns([1, 2])
37
- with col1:
38
- if st.button("Save"):
39
- st.session_state['uploaded_files'] = uploaded_files
40
- st.session_state['product_description'] = product_description
41
- st.success("Product information saved!")
42
- with col2:
43
- if st.button("Add product and move to next page"):
44
- if not uploaded_files:
45
- st.warning("Please upload at least one image.")
46
- elif not product_description:
47
- st.warning("Please provide a description for the product.")
48
- else:
49
- st.session_state['uploaded_files'] = uploaded_files
50
- st.session_state['product_description'] = product_description
51
- st.session_state['page'] = "Page 2"
52
-
53
-
54
- def page2():
55
- st.title("Tell us about your shoot preference")
56
- st.markdown("<h3 style='color:#444;'>What are you shooting today?</h3>", unsafe_allow_html=True)
57
- shoot_type = st.radio("Select your shoot type:", ["Editorial", "Catalogue"], index=0)
58
- st.session_state['shoot_type'] = shoot_type
59
- brand_link = st.text_input("Add your brand link:", value=st.session_state.get("brand_link", ""))
60
- st.session_state['brand_link'] = brand_link
61
- if st.button("Get Brand Summary"):
62
- if brand_link:
63
- brand_summary_html = create_brand_html(brand_link)
64
- brand_summary = create_langchain_openai_query(brand_summary_html)
65
- st.session_state['brand_summary'] = brand_summary
66
- st.success("Brand summary fetched!")
67
- else:
68
- st.warning("Please add a brand link.")
69
- brand_summary_value = st.session_state.get('brand_summary', "")
70
- editable_summary = st.text_area("Brand Summary:", value=brand_summary_value, height=100)
71
- st.session_state['brand_summary'] = editable_summary
72
- product_info = st.text_area("Tell us something about your product:", value=st.session_state.get("product_info", ""))
73
- st.session_state['product_info'] = product_info
74
- reference_images = st.file_uploader("Upload Reference Images", accept_multiple_files=True,
75
- key="reference_images_key")
76
- st.session_state['reference_images'] = reference_images
77
- if st.button("Give Me Ideas"):
78
- st.session_state['page'] = "Page 3"
79
-
80
-
81
- def page3():
82
- st.title("Scene Suggestions")
83
- st.write("Based on your uploaded product and references!")
84
- feedback = st.chat_input("Provide feedback:")
85
- if not st.session_state.get("assistant_initialized", False):
86
- assistant_id = createAssistant("You are a helpful assistant who is an expert in Fashion Shoots.")
87
- updated_prompt = prompts["IDEA_GENERATION_PROMPT"].format(
88
- brand_details=st.session_state["brand_summary"],
89
- product_details=st.session_state["product_info"],
90
- type_of_shoot=st.session_state["shoot_type"],
91
- json_schema=JSON_SCHEMA_FOR_GPT,
92
- product_name=st.session_state["product_description"]
93
- )
94
- file_locations = []
95
- for uploaded_file in st.session_state['uploaded_files']:
96
- bytes_data = uploaded_file.getvalue()
97
- image = Image.open(io.BytesIO(bytes_data))
98
- image.verify()
99
- location = f"temp_image_{uploaded_file.name}"
100
- with open(location, "wb") as f:
101
- f.write(bytes_data)
102
- file_locations.append(location)
103
- image.close()
104
- for uploaded_file in st.session_state['reference_images']:
105
- bytes_data = uploaded_file.getvalue()
106
- image = Image.open(io.BytesIO(bytes_data))
107
- image.verify()
108
- location = f"temp2_image_{uploaded_file.name}"
109
- with open(location, "wb") as f:
110
- f.write(bytes_data)
111
- file_locations.append(location)
112
- image.close()
113
- file_ids = [saveFileOpenAI(location) for location in file_locations]
114
- thread_id = startAssistantThread(file_ids, updated_prompt, "yes", "yes")
115
- st.session_state.assistant_id = assistant_id
116
- st.session_state.thread_id = thread_id
117
- st.session_state.assistant_initialized = True
118
- regenerate_images(thread_id, assistant_id)
119
- if feedback:
120
- if 'images' in st.session_state and 'descriptions' in st.session_state:
121
- for image_path in st.session_state['images']:
122
- os.remove(image_path)
123
- del st.session_state['images']
124
- del st.session_state['descriptions']
125
- del st.session_state["json_descriptions"]
126
- addMessageToThread(st.session_state.thread_id, feedback)
127
- regenerate_images(st.session_state.thread_id, st.session_state.assistant_id)
128
- selected_image_index = None
129
- cols = st.columns(1)
130
- for i in range(len(st.session_state["images"])):
131
- with cols[i]:
132
- st.image(st.session_state.images[i], caption=st.session_state.descriptions[i], use_column_width=True)
133
- if st.radio(f"Select {i + 1}", [f"Select Image {i + 1}"], key=f"radio_{i}"):
134
- selected_image_index = i
135
- if selected_image_index is not None and st.button("Refine"):
136
- st.session_state.selected_image_index = selected_image_index
137
- st.session_state.selected_image = st.session_state.images[selected_image_index]
138
- st.session_state.selected_text = st.session_state.descriptions[selected_image_index]
139
- st.session_state['page'] = "Page 4"
140
- if st.button("Go Back!"):
141
- st.session_state.page = "Page 2"
142
-
143
-
144
- def regenerate_images(thread_id, assistant_id):
145
- """Helper function to generate images and descriptions."""
146
- response_from_process_list = []
147
- for _ in range(1): # Assuming you generate 1 set of image/description
148
- response_from_process = process_run(st, thread_id, assistant_id)
149
- response_from_process_list.append(response_from_process)
150
-
151
- summary_list = []
152
- for final_response in response_from_process_list:
153
- prompt_for_idea_summary = prompts["IDEA_SUMMARY_PROMPT"].format(
154
- json_schema=str(final_response)
155
- )
156
- summary = create_chat_completion_request_open_ai_for_summary(prompt_for_idea_summary, "No")
157
- summary_list.append(summary)
158
-
159
- # Generate images based on the summaries
160
- flux_generated_theme_image = []
161
- for summary in summary_list:
162
- theme_image = flux_generated_image(summary)
163
- flux_generated_theme_image.append(theme_image["file_name"])
164
-
165
- # Save the new images and descriptions in session state
166
- st.session_state["images"] = flux_generated_theme_image
167
- st.session_state["descriptions"] = summary_list
168
- st.session_state["json_descriptions"] = response_from_process_list
169
-
170
-
171
- def page4():
172
- import json
173
- selected_theme_text_by_user = st.session_state.json_descriptions[st.session_state.selected_image_index]
174
- print(selected_theme_text_by_user)
175
- schema_for_model_bg = {"type": "object",
176
- "properties": {
177
- "Model": {
178
- "type": "string",
179
- "description": "The model name or identifier."
180
- },
181
- "Background": {
182
- "type": "string",
183
- "description": "Description or type of the background."
184
- }},
185
- "required": ["Model", "Background"],
186
- "additionalProperties": False
187
- }
188
- prompt_to_get_details = (f"You are provided with a brief of a Fashion Shoot : "
189
- f"{st.session_state["json_descriptions"]}).\n Now provide me a JSON which will"
190
- f"have two keys ```Model``` and ```Background```. Provide all detail's"
191
- f"present about model and background in the brief provided by you. Just provide a "
192
- f"natural langauge description. I will use it as description of model and "
193
- f"background needed by the brand Output JSON following the schema")
194
- response_from_open_ai = create_chat_completion_request_open_ai_for_summary(prompt_to_get_details,
195
- schema_name="model_bg",
196
- json_schema=schema_for_model_bg,
197
- json_mode="yes")
198
- json_response_from_open_ai = json.loads(response_from_open_ai)
199
- with (st.sidebar):
200
- st.title(st.session_state["product_info"])
201
- st.write("Product Image")
202
- st.image(st.session_state['uploaded_files'])
203
- st.text("Scene Suggestion:")
204
- st.image(st.session_state.selected_image)
205
- dimensions = st.text_input("Enter Dimensions e.g 3:4, 1:2", key="Dimensions")
206
- seed = st.selectbox(
207
- "Seed Preference",
208
- ("Fixed", "Random"),
209
- )
210
- if seed == "Fixed":
211
- seed_number = st.number_input("Enter an integer:", min_value=1, max_value=100000, value=10, step=1)
212
- else:
213
- seed_number = 0
214
- st.text("Thanks will take care")
215
- model_preference = st.selectbox(
216
- "Model Preference",
217
- ("Create Own/Edit Pre-filled", "Ideas", "Upload Reference"),
218
- )
219
- if model_preference == "Create Own/Edit Pre-filled":
220
- pre_filled_model_details = st.text_area("Model Idea", value=json_response_from_open_ai["Model"],
221
- key="Model Idea")
222
- elif model_preference == "Ideas":
223
- prompt_to_generate_idea = ("Your task is to create model ideas for shoot of a product of a brand. "
224
- "The details about the brand: ```{brand_details}.\n The product: {product_name},"
225
- "which is: ```{product_details}```.\n Reference images for the product and "
226
- "brands shoot idea is already provided with you. Additionally brand wants to "
227
- "have a ```{type_of_shoot}``` of the model. Now based on all provided details, "
228
- "think step by step and provide your ideas about what type of model the brand"
229
- "should need based on mentioned JSON format. Also provide a combined prompt "
230
- "which the brand will use to create a shoot image. While creating the "
231
- "combined prompt as mentioned in the JSON schema, do not miss any details you"
232
- " mentioned in the JSON.")
233
- updated_model_idea_gen_prompt = prompt_to_generate_idea.format(
234
- brand_details=st.session_state["brand_summary"],
235
- product_details=st.session_state["product_info"],
236
- type_of_shoot=st.session_state["shoot_type"],
237
- product_name=st.session_state["product_description"]
238
-
239
- )
240
- response_for_only_model = create_chat_completion_request_open_ai_for_summary(updated_model_idea_gen_prompt
241
- , schema_name="model_only",
242
- json_schema=
243
- UPDATED_MODEL_ONLY_SCHEMA,
244
- json_mode="yes")
245
- pre_filled_model_details = st.text_area("Model Idea", value=response_for_only_model,
246
- key="Model Idea")
247
- else:
248
- uploaded_files = st.file_uploader("Upload one Model Reference Image here",
249
- accept_multiple_files=False, key="uploader")
250
- bytes_data = uploaded_files.getvalue()
251
- image = Image.open(io.BytesIO(bytes_data))
252
- image.verify()
253
- location = f"temp_image_{uploaded_files.name}"
254
- with open(location, "wb") as f:
255
- f.write(bytes_data)
256
- image.close()
257
- prompt_to_generate_idea = ("Follow this JSON Schema : {json_schema_model_only}."
258
- "Your task is to create model ideas for shoot of a product of a brand. "
259
- "The details about the brand: ```{brand_details}.\n The product: {product_name},"
260
- "which is: ```{product_details}```.\n Reference images for the product and "
261
- "brands shoot idea is already provided with you. Additionally brand wants to "
262
- "have a ```{type_of_shoot}``` of the model. Now based on all provided details, "
263
- "think step by step and provide your ideas about what type of model the brand"
264
- "should need based on mentioned JSON format. Also provide a combined prompt "
265
- "which the brand will use to create a shoot image. While creating the "
266
- "combined prompt as mentioned in the JSON schema, do not miss any details you"
267
- " mentioned in the JSON.")
268
- updated_model_idea_gen_prompt = prompt_to_generate_idea.format(
269
- json_schema_model_only=UPDATED_MODEL_ONLY_SCHEMA,
270
- brand_details=st.session_state["brand_summary"],
271
- product_details=st.session_state["product_info"],
272
- type_of_shoot=st.session_state["shoot_type"],
273
- product_name=st.session_state["product_description"]
274
-
275
- )
276
- json_response = create_image_completion_request_gpt(location, updated_model_idea_gen_prompt)
277
- pre_filled_model_details = st.text_area("Model Idea", value=json_response,
278
- key="Model Idea")
279
- background_preference = st.selectbox(
280
- "Background Preference",
281
- ("Create Own/Edit Pre-filled", "Ideas", "Upload Reference"),
282
- )
283
- if background_preference == "Create Own/Edit Pre-filled":
284
- pre_filled_background_details = st.text_area("Background Idea",
285
- value=json_response_from_open_ai["Background"],
286
- key="Background Idea")
287
- elif background_preference == "Ideas":
288
- prompt_to_generate_idea = ("Follow this JSON Schema : {json_schema_background_only}."
289
- "Your task is to create location/background ideas for shoot of a "
290
- "product of a brand. "
291
- "The details about the brand: ```{brand_details}.\n The product: {product_name},"
292
- "which is: ```{product_details}```.\n Reference images for the product and "
293
- "brands shoot idea is already provided with you. Additionally brand wants to "
294
- "have a ```{type_of_shoot}``` of the model. Now based on all provided details, "
295
- "think step by step and provide your ideas about what type of location the brand"
296
- "should need based on mentioned JSON format. Also provide a combined prompt "
297
- "which the brand will use to create a shoot image. While creating the "
298
- "combined prompt as mentioned in the JSON schema, do not miss any details you"
299
- " mentioned in the JSON.")
300
- updated_bg_idea_gen_prompt = prompt_to_generate_idea.format(
301
- json_schema_background_only=JSON_SCHEMA_FOR_LOC_ONLY,
302
- brand_details=st.session_state["brand_summary"],
303
- product_details=st.session_state["product_info"],
304
- type_of_shoot=st.session_state["shoot_type"],
305
- product_name=st.session_state["product_description"]
306
-
307
- )
308
- response_for_only_bg = create_chat_completion_request_open_ai_for_summary(updated_bg_idea_gen_prompt,
309
- schema_name="bg_o",
310
- json_schema=JSON_SCHEMA_FOR_LOC_ONLY,
311
- json_mode="yes")
312
- pre_filled_background_details = st.text_area("Background Idea", value=response_for_only_bg,
313
- key="Background Idea")
314
- else:
315
- uploaded_files = st.file_uploader("Upload one Background Reference Image here",
316
- accept_multiple_files=False, key="uploader")
317
- bytes_data = uploaded_files.getvalue()
318
- image = Image.open(io.BytesIO(bytes_data))
319
- image.verify()
320
- location = f"temp2_image_{uploaded_files.name}"
321
- with open(location, "wb") as f:
322
- f.write(bytes_data)
323
- image.close()
324
- prompt_to_generate_idea = ("Follow this JSON Schema : {json_schema_bg_only}."
325
- "Your task is to create Background/Location ideas for shoot of a "
326
- "product of a brand. "
327
- "The details about the brand: ```{brand_details}.\n The product: {product_name},"
328
- "which is: ```{product_details}```.\n Reference images for the product and "
329
- "brands shoot idea is already provided with you. Additionally brand wants to "
330
- "have a ```{type_of_shoot}``` of the model. Now based on all provided details, "
331
- "think step by step and provide your ideas about what type of location the brand"
332
- "should need based on mentioned JSON format. Also provide a combined prompt "
333
- "which the brand will use to create a shoot image. While creating the "
334
- "combined prompt as mentioned in the JSON schema, do not miss any details you"
335
- " mentioned in the JSON.")
336
- updated_bg_idea_gen_prompt = prompt_to_generate_idea.format(
337
- json_schema_bg_only=JSON_SCHEMA_FOR_LOC_ONLY,
338
- brand_details=st.session_state["brand_summary"],
339
- product_details=st.session_state["product_info"],
340
- type_of_shoot=st.session_state["shoot_type"],
341
- product_name=st.session_state["product_description"]
342
-
343
- )
344
- json_response = create_image_completion_request_gpt(location, updated_bg_idea_gen_prompt)
345
- pre_filled_background_details = st.text_area("Background Idea", value=json_response,
346
- key="Background Idea")
347
- start_chat = st.button("Start Chat")
348
- if "mood_chat_messages" not in st.session_state:
349
- st.session_state["mood_chat_messages"] = []
350
- if seed and dimensions and model_preference and background_preference:
351
- if start_chat:
352
- final_mood_board_image_prompt = prompts["FINAL_PROMPT_GENERATION"].format(
353
- brand_details=st.session_state["brand_summary"],
354
- product_details=st.session_state["product_info"],
355
- type_of_shoot=st.session_state["shoot_type"],
356
- product_name=st.session_state["product_description"],
357
- model_details=pre_filled_model_details,
358
- location_details=pre_filled_background_details,
359
- theme_details=str(selected_theme_text_by_user),
360
- chat_history=str(st.session_state["mood_chat_messages"])
361
- )
362
- prompt_for_flux_mood_board = create_chat_completion_request_open_ai_for_summary(
363
- final_mood_board_image_prompt, "No", system_message=prompts["SYSTEM_PROMPT_FOR_MOOD_BOARD"])
364
- if seed == "Fixed":
365
- generated_flux_image = flux_generated_image_seed(prompt_for_flux_mood_board, seed_number, dimensions)
366
- else:
367
- generated_flux_image = flux_generated_image(prompt_for_flux_mood_board)
368
- st.session_state["mood_chat_messages"].append({
369
- "role": "AI",
370
- "message": prompt_for_flux_mood_board,
371
- "image": generated_flux_image["file_name"]
372
- })
373
- # for message in st.session_state["mood_chat_messages"]:
374
- # if message["role"] == "AI":
375
- # st.write(f"Caimera AI: {message['message']}")
376
- # st.image(message['image'])
377
- #else:
378
- # st.write(f"**You**: {message['message']}")
379
- user_input = st.chat_input("Type your message here...")
380
- if user_input:
381
- st.session_state["mood_chat_messages"].append({"role": "User", "message": user_input})
382
- prompt_for_flux_mood_board_n = create_chat_completion_request_open_ai_for_summary(
383
- user_input, "No", system_message=prompts["SYSTEM_PROMPT_FOR_MOOD_BOARD"])
384
- if seed == "Fixed":
385
- generated_flux_image_n = flux_generated_image_seed(prompt_for_flux_mood_board_n, seed_number,
386
- dimensions)
387
- else:
388
- generated_flux_image_n = flux_generated_image(prompt_for_flux_mood_board_n)
389
- st.session_state["mood_chat_messages"].append({
390
- "role": "AI",
391
- "message": prompt_for_flux_mood_board_n,
392
- "image": generated_flux_image_n["file_name"]
393
- })
394
- for message in st.session_state["mood_chat_messages"]:
395
- if message["role"] == "AI":
396
- st.write(f"**AI**: {message['message']}")
397
- st.image(message['image'])
398
- else:
399
- st.write(f"**You**: {message['message']}")
400
- print(seed_number)
401
-
402
-
403
- if 'page' not in st.session_state:
404
- st.session_state.page = "Page 1"
405
-
406
- # Routing between pages
407
- if st.session_state.page == "Page 1":
408
- page1()
409
- elif st.session_state.page == "Page 2":
410
- page2()
411
- elif st.session_state.page == "Page 3":
412
- page3()
413
- elif st.session_state.page == "Page 4":
414
- page4()
 
1
+ import streamlit as st
2
+ import os
3
+ from prompts import prompts
4
+ from constants import JSON_SCHEMA_FOR_GPT, UPDATED_MODEL_ONLY_SCHEMA, JSON_SCHEMA_FOR_LOC_ONLY
5
+ from gpt import runAssistant, checkRunStatus, retrieveThread, createAssistant, saveFileOpenAI, startAssistantThread, \
6
+ create_chat_completion_request_open_ai_for_summary, addMessageToThread, create_image_completion_request_gpt
7
+ from summarizer import create_brand_html, create_langchain_openai_query
8
+ from theme import flux_generated_image, flux_generated_image_seed
9
+ import time
10
+ from PIL import Image
11
+ import io
12
+
13
+
14
+ def process_run(st, thread_id, assistant_id):
15
+ run_id = runAssistant(thread_id, assistant_id)
16
+ status = 'running'
17
+ while status != 'completed':
18
+ with st.spinner('. . .'):
19
+ time.sleep(20)
20
+ status = checkRunStatus(thread_id, run_id)
21
+ thread_messages = retrieveThread(thread_id)
22
+ for message in thread_messages:
23
+ if not message['role'] == 'user':
24
+ return message["content"]
25
+ else:
26
+ pass
27
+
28
+
29
+ def page1():
30
+ st.title("Upload Product")
31
+ st.markdown("<h2 style='color:#FF5733; font-weight:bold;'>Add a Product</h2>", unsafe_allow_html=True)
32
+ st.markdown("<p style='color:#444;'>Upload your product images, more images you upload better the AI learns</p>",
33
+ unsafe_allow_html=True)
34
+ uploaded_files = st.file_uploader("Upload Images", accept_multiple_files=True, key="uploaded_files_key")
35
+ product_description = st.text_area("Describe the product", value=st.session_state.get("product_description", ""))
36
+ col1, col2 = st.columns([1, 2])
37
+ with col1:
38
+ if st.button("Save"):
39
+ st.session_state['uploaded_files'] = uploaded_files
40
+ st.session_state['product_description'] = product_description
41
+ st.success("Product information saved!")
42
+ with col2:
43
+ if st.button("Add product and move to next page"):
44
+ if not uploaded_files:
45
+ st.warning("Please upload at least one image.")
46
+ elif not product_description:
47
+ st.warning("Please provide a description for the product.")
48
+ else:
49
+ st.session_state['uploaded_files'] = uploaded_files
50
+ st.session_state['product_description'] = product_description
51
+ st.session_state['page'] = "Page 2"
52
+
53
+
54
+ def page2():
55
+ st.title("Tell us about your shoot preference")
56
+ st.markdown("<h3 style='color:#444;'>What are you shooting today?</h3>", unsafe_allow_html=True)
57
+ shoot_type = st.radio("Select your shoot type:", ["Editorial", "Catalogue"], index=0)
58
+ st.session_state['shoot_type'] = shoot_type
59
+ brand_link = st.text_input("Add your brand link:", value=st.session_state.get("brand_link", ""))
60
+ st.session_state['brand_link'] = brand_link
61
+ if st.button("Get Brand Summary"):
62
+ if brand_link:
63
+ brand_summary_html = create_brand_html(brand_link)
64
+ brand_summary = create_langchain_openai_query(brand_summary_html)
65
+ st.session_state['brand_summary'] = brand_summary
66
+ st.success("Brand summary fetched!")
67
+ else:
68
+ st.warning("Please add a brand link.")
69
+ brand_summary_value = st.session_state.get('brand_summary', "")
70
+ editable_summary = st.text_area("Brand Summary:", value=brand_summary_value, height=100)
71
+ st.session_state['brand_summary'] = editable_summary
72
+ product_info = st.text_area("Tell us something about your product:", value=st.session_state.get("product_info", ""))
73
+ st.session_state['product_info'] = product_info
74
+ reference_images = st.file_uploader("Upload Reference Images", accept_multiple_files=True,
75
+ key="reference_images_key")
76
+ st.session_state['reference_images'] = reference_images
77
+ if st.button("Give Me Ideas"):
78
+ st.session_state['page'] = "Page 3"
79
+
80
+
81
+ def page3():
82
+ st.title("Scene Suggestions")
83
+ st.write("Based on your uploaded product and references!")
84
+ feedback = st.chat_input("Provide feedback:")
85
+ if not st.session_state.get("assistant_initialized", False):
86
+ assistant_id = createAssistant("You are a helpful assistant who is an expert in Fashion Shoots.")
87
+ updated_prompt = prompts["IDEA_GENERATION_PROMPT"].format(
88
+ brand_details=st.session_state["brand_summary"],
89
+ product_details=st.session_state["product_info"],
90
+ type_of_shoot=st.session_state["shoot_type"],
91
+ json_schema=JSON_SCHEMA_FOR_GPT,
92
+ product_name=st.session_state["product_description"]
93
+ )
94
+ file_locations = []
95
+ for uploaded_file in st.session_state['uploaded_files']:
96
+ bytes_data = uploaded_file.getvalue()
97
+ image = Image.open(io.BytesIO(bytes_data))
98
+ image.verify()
99
+ location = f"temp_image_{uploaded_file.name}"
100
+ with open(location, "wb") as f:
101
+ f.write(bytes_data)
102
+ file_locations.append(location)
103
+ image.close()
104
+ for uploaded_file in st.session_state['reference_images']:
105
+ bytes_data = uploaded_file.getvalue()
106
+ image = Image.open(io.BytesIO(bytes_data))
107
+ image.verify()
108
+ location = f"temp2_image_{uploaded_file.name}"
109
+ with open(location, "wb") as f:
110
+ f.write(bytes_data)
111
+ file_locations.append(location)
112
+ image.close()
113
+ file_ids = [saveFileOpenAI(location) for location in file_locations]
114
+ thread_id = startAssistantThread(file_ids, updated_prompt, "yes", "yes")
115
+ st.session_state.assistant_id = assistant_id
116
+ st.session_state.thread_id = thread_id
117
+ st.session_state.assistant_initialized = True
118
+ regenerate_images(thread_id, assistant_id)
119
+ if feedback:
120
+ if 'images' in st.session_state and 'descriptions' in st.session_state:
121
+ for image_path in st.session_state['images']:
122
+ os.remove(image_path)
123
+ del st.session_state['images']
124
+ del st.session_state['descriptions']
125
+ del st.session_state["json_descriptions"]
126
+ addMessageToThread(st.session_state.thread_id, feedback)
127
+ regenerate_images(st.session_state.thread_id, st.session_state.assistant_id)
128
+ selected_image_index = None
129
+ cols = st.columns(1)
130
+ for i in range(len(st.session_state["images"])):
131
+ with cols[i]:
132
+ st.image(st.session_state.images[i], caption=st.session_state.descriptions[i], use_column_width=True)
133
+ if st.radio(f"Select {i + 1}", [f"Select Image {i + 1}"], key=f"radio_{i}"):
134
+ selected_image_index = i
135
+ if selected_image_index is not None and st.button("Refine"):
136
+ st.session_state.selected_image_index = selected_image_index
137
+ st.session_state.selected_image = st.session_state.images[selected_image_index]
138
+ st.session_state.selected_text = st.session_state.descriptions[selected_image_index]
139
+ st.session_state['page'] = "Page 4"
140
+ if st.button("Go Back!"):
141
+ st.session_state.page = "Page 2"
142
+
143
+
144
+ def regenerate_images(thread_id, assistant_id):
145
+ """Helper function to generate images and descriptions."""
146
+ response_from_process_list = []
147
+ for _ in range(1): # Assuming you generate 1 set of image/description
148
+ response_from_process = process_run(st, thread_id, assistant_id)
149
+ response_from_process_list.append(response_from_process)
150
+
151
+ summary_list = []
152
+ for final_response in response_from_process_list:
153
+ prompt_for_idea_summary = prompts["IDEA_SUMMARY_PROMPT"].format(
154
+ json_schema=str(final_response)
155
+ )
156
+ summary = create_chat_completion_request_open_ai_for_summary(prompt_for_idea_summary, "No")
157
+ summary_list.append(summary)
158
+
159
+ # Generate images based on the summaries
160
+ flux_generated_theme_image = []
161
+ for summary in summary_list:
162
+ theme_image = flux_generated_image(summary)
163
+ flux_generated_theme_image.append(theme_image["file_name"])
164
+
165
+ # Save the new images and descriptions in session state
166
+ st.session_state["images"] = flux_generated_theme_image
167
+ st.session_state["descriptions"] = summary_list
168
+ st.session_state["json_descriptions"] = response_from_process_list
169
+
170
+
171
+ def page4():
172
+ import json
173
+ selected_theme_text_by_user = st.session_state.json_descriptions[st.session_state.selected_image_index]
174
+ print(selected_theme_text_by_user)
175
+ schema_for_model_bg = {"type": "object",
176
+ "properties": {
177
+ "Model": {
178
+ "type": "string",
179
+ "description": "The model name or identifier."
180
+ },
181
+ "Background": {
182
+ "type": "string",
183
+ "description": "Description or type of the background."
184
+ }},
185
+ "required": ["Model", "Background"],
186
+ "additionalProperties": False
187
+ }
188
+ prompt_to_get_details = (f"You are provided with a brief of a Fashion Shoot : "
189
+ f"{st.session_state[\"json_descriptions\"]}.\n Now provide me a JSON which will"
190
+ f"have two keys ```Model``` and ```Background```. Provide all detail's"
191
+ f"present about model and background in the brief provided by you. Just provide a "
192
+ f"natural langauge description. I will use it as description of model and "
193
+ f"background needed by the brand Output JSON following the schema")
194
+ response_from_open_ai = create_chat_completion_request_open_ai_for_summary(prompt_to_get_details,
195
+ schema_name="model_bg",
196
+ json_schema=schema_for_model_bg,
197
+ json_mode="yes")
198
+ json_response_from_open_ai = json.loads(response_from_open_ai)
199
+ with (st.sidebar):
200
+ st.title(st.session_state["product_info"])
201
+ st.write("Product Image")
202
+ st.image(st.session_state['uploaded_files'])
203
+ st.text("Scene Suggestion:")
204
+ st.image(st.session_state.selected_image)
205
+ dimensions = st.text_input("Enter Dimensions e.g 3:4, 1:2", key="Dimensions")
206
+ seed = st.selectbox(
207
+ "Seed Preference",
208
+ ("Fixed", "Random"),
209
+ )
210
+ if seed == "Fixed":
211
+ seed_number = st.number_input("Enter an integer:", min_value=1, max_value=100000, value=10, step=1)
212
+ else:
213
+ seed_number = 0
214
+ st.text("Thanks will take care")
215
+ model_preference = st.selectbox(
216
+ "Model Preference",
217
+ ("Create Own/Edit Pre-filled", "Ideas", "Upload Reference"),
218
+ )
219
+ if model_preference == "Create Own/Edit Pre-filled":
220
+ pre_filled_model_details = st.text_area("Model Idea", value=json_response_from_open_ai["Model"],
221
+ key="Model Idea")
222
+ elif model_preference == "Ideas":
223
+ prompt_to_generate_idea = ("Your task is to create model ideas for shoot of a product of a brand. "
224
+ "The details about the brand: ```{brand_details}.\n The product: {product_name},"
225
+ "which is: ```{product_details}```.\n Reference images for the product and "
226
+ "brands shoot idea is already provided with you. Additionally brand wants to "
227
+ "have a ```{type_of_shoot}``` of the model. Now based on all provided details, "
228
+ "think step by step and provide your ideas about what type of model the brand"
229
+ "should need based on mentioned JSON format. Also provide a combined prompt "
230
+ "which the brand will use to create a shoot image. While creating the "
231
+ "combined prompt as mentioned in the JSON schema, do not miss any details you"
232
+ " mentioned in the JSON.")
233
+ updated_model_idea_gen_prompt = prompt_to_generate_idea.format(
234
+ brand_details=st.session_state["brand_summary"],
235
+ product_details=st.session_state["product_info"],
236
+ type_of_shoot=st.session_state["shoot_type"],
237
+ product_name=st.session_state["product_description"]
238
+
239
+ )
240
+ response_for_only_model = create_chat_completion_request_open_ai_for_summary(updated_model_idea_gen_prompt
241
+ , schema_name="model_only",
242
+ json_schema=
243
+ UPDATED_MODEL_ONLY_SCHEMA,
244
+ json_mode="yes")
245
+ pre_filled_model_details = st.text_area("Model Idea", value=response_for_only_model,
246
+ key="Model Idea")
247
+ else:
248
+ uploaded_files = st.file_uploader("Upload one Model Reference Image here",
249
+ accept_multiple_files=False, key="uploader")
250
+ bytes_data = uploaded_files.getvalue()
251
+ image = Image.open(io.BytesIO(bytes_data))
252
+ image.verify()
253
+ location = f"temp_image_{uploaded_files.name}"
254
+ with open(location, "wb") as f:
255
+ f.write(bytes_data)
256
+ image.close()
257
+ prompt_to_generate_idea = ("Follow this JSON Schema : {json_schema_model_only}."
258
+ "Your task is to create model ideas for shoot of a product of a brand. "
259
+ "The details about the brand: ```{brand_details}.\n The product: {product_name},"
260
+ "which is: ```{product_details}```.\n Reference images for the product and "
261
+ "brands shoot idea is already provided with you. Additionally brand wants to "
262
+ "have a ```{type_of_shoot}``` of the model. Now based on all provided details, "
263
+ "think step by step and provide your ideas about what type of model the brand"
264
+ "should need based on mentioned JSON format. Also provide a combined prompt "
265
+ "which the brand will use to create a shoot image. While creating the "
266
+ "combined prompt as mentioned in the JSON schema, do not miss any details you"
267
+ " mentioned in the JSON.")
268
+ updated_model_idea_gen_prompt = prompt_to_generate_idea.format(
269
+ json_schema_model_only=UPDATED_MODEL_ONLY_SCHEMA,
270
+ brand_details=st.session_state["brand_summary"],
271
+ product_details=st.session_state["product_info"],
272
+ type_of_shoot=st.session_state["shoot_type"],
273
+ product_name=st.session_state["product_description"]
274
+
275
+ )
276
+ json_response = create_image_completion_request_gpt(location, updated_model_idea_gen_prompt)
277
+ pre_filled_model_details = st.text_area("Model Idea", value=json_response,
278
+ key="Model Idea")
279
+ background_preference = st.selectbox(
280
+ "Background Preference",
281
+ ("Create Own/Edit Pre-filled", "Ideas", "Upload Reference"),
282
+ )
283
+ if background_preference == "Create Own/Edit Pre-filled":
284
+ pre_filled_background_details = st.text_area("Background Idea",
285
+ value=json_response_from_open_ai["Background"],
286
+ key="Background Idea")
287
+ elif background_preference == "Ideas":
288
+ prompt_to_generate_idea = ("Follow this JSON Schema : {json_schema_background_only}."
289
+ "Your task is to create location/background ideas for shoot of a "
290
+ "product of a brand. "
291
+ "The details about the brand: ```{brand_details}.\n The product: {product_name},"
292
+ "which is: ```{product_details}```.\n Reference images for the product and "
293
+ "brands shoot idea is already provided with you. Additionally brand wants to "
294
+ "have a ```{type_of_shoot}``` of the model. Now based on all provided details, "
295
+ "think step by step and provide your ideas about what type of location the brand"
296
+ "should need based on mentioned JSON format. Also provide a combined prompt "
297
+ "which the brand will use to create a shoot image. While creating the "
298
+ "combined prompt as mentioned in the JSON schema, do not miss any details you"
299
+ " mentioned in the JSON.")
300
+ updated_bg_idea_gen_prompt = prompt_to_generate_idea.format(
301
+ json_schema_background_only=JSON_SCHEMA_FOR_LOC_ONLY,
302
+ brand_details=st.session_state["brand_summary"],
303
+ product_details=st.session_state["product_info"],
304
+ type_of_shoot=st.session_state["shoot_type"],
305
+ product_name=st.session_state["product_description"]
306
+
307
+ )
308
+ response_for_only_bg = create_chat_completion_request_open_ai_for_summary(updated_bg_idea_gen_prompt,
309
+ schema_name="bg_o",
310
+ json_schema=JSON_SCHEMA_FOR_LOC_ONLY,
311
+ json_mode="yes")
312
+ pre_filled_background_details = st.text_area("Background Idea", value=response_for_only_bg,
313
+ key="Background Idea")
314
+ else:
315
+ uploaded_files = st.file_uploader("Upload one Background Reference Image here",
316
+ accept_multiple_files=False, key="uploader")
317
+ bytes_data = uploaded_files.getvalue()
318
+ image = Image.open(io.BytesIO(bytes_data))
319
+ image.verify()
320
+ location = f"temp2_image_{uploaded_files.name}"
321
+ with open(location, "wb") as f:
322
+ f.write(bytes_data)
323
+ image.close()
324
+ prompt_to_generate_idea = ("Follow this JSON Schema : {json_schema_bg_only}."
325
+ "Your task is to create Background/Location ideas for shoot of a "
326
+ "product of a brand. "
327
+ "The details about the brand: ```{brand_details}.\n The product: {product_name},"
328
+ "which is: ```{product_details}```.\n Reference images for the product and "
329
+ "brands shoot idea is already provided with you. Additionally brand wants to "
330
+ "have a ```{type_of_shoot}``` of the model. Now based on all provided details, "
331
+ "think step by step and provide your ideas about what type of location the brand"
332
+ "should need based on mentioned JSON format. Also provide a combined prompt "
333
+ "which the brand will use to create a shoot image. While creating the "
334
+ "combined prompt as mentioned in the JSON schema, do not miss any details you"
335
+ " mentioned in the JSON.")
336
+ updated_bg_idea_gen_prompt = prompt_to_generate_idea.format(
337
+ json_schema_bg_only=JSON_SCHEMA_FOR_LOC_ONLY,
338
+ brand_details=st.session_state["brand_summary"],
339
+ product_details=st.session_state["product_info"],
340
+ type_of_shoot=st.session_state["shoot_type"],
341
+ product_name=st.session_state["product_description"]
342
+
343
+ )
344
+ json_response = create_image_completion_request_gpt(location, updated_bg_idea_gen_prompt)
345
+ pre_filled_background_details = st.text_area("Background Idea", value=json_response,
346
+ key="Background Idea")
347
+ start_chat = st.button("Start Chat")
348
+ if "mood_chat_messages" not in st.session_state:
349
+ st.session_state["mood_chat_messages"] = []
350
+ if seed and dimensions and model_preference and background_preference:
351
+ if start_chat:
352
+ final_mood_board_image_prompt = prompts["FINAL_PROMPT_GENERATION"].format(
353
+ brand_details=st.session_state["brand_summary"],
354
+ product_details=st.session_state["product_info"],
355
+ type_of_shoot=st.session_state["shoot_type"],
356
+ product_name=st.session_state["product_description"],
357
+ model_details=pre_filled_model_details,
358
+ location_details=pre_filled_background_details,
359
+ theme_details=str(selected_theme_text_by_user),
360
+ chat_history=str(st.session_state["mood_chat_messages"])
361
+ )
362
+ prompt_for_flux_mood_board = create_chat_completion_request_open_ai_for_summary(
363
+ final_mood_board_image_prompt, "No", system_message=prompts["SYSTEM_PROMPT_FOR_MOOD_BOARD"])
364
+ if seed == "Fixed":
365
+ generated_flux_image = flux_generated_image_seed(prompt_for_flux_mood_board, seed_number, dimensions)
366
+ else:
367
+ generated_flux_image = flux_generated_image(prompt_for_flux_mood_board)
368
+ st.session_state["mood_chat_messages"].append({
369
+ "role": "AI",
370
+ "message": prompt_for_flux_mood_board,
371
+ "image": generated_flux_image["file_name"]
372
+ })
373
+ # for message in st.session_state["mood_chat_messages"]:
374
+ # if message["role"] == "AI":
375
+ # st.write(f"Caimera AI: {message['message']}")
376
+ # st.image(message['image'])
377
+ #else:
378
+ # st.write(f"**You**: {message['message']}")
379
+ user_input = st.chat_input("Type your message here...")
380
+ if user_input:
381
+ st.session_state["mood_chat_messages"].append({"role": "User", "message": user_input})
382
+ prompt_for_flux_mood_board_n = create_chat_completion_request_open_ai_for_summary(
383
+ user_input, "No", system_message=prompts["SYSTEM_PROMPT_FOR_MOOD_BOARD"])
384
+ if seed == "Fixed":
385
+ generated_flux_image_n = flux_generated_image_seed(prompt_for_flux_mood_board_n, seed_number,
386
+ dimensions)
387
+ else:
388
+ generated_flux_image_n = flux_generated_image(prompt_for_flux_mood_board_n)
389
+ st.session_state["mood_chat_messages"].append({
390
+ "role": "AI",
391
+ "message": prompt_for_flux_mood_board_n,
392
+ "image": generated_flux_image_n["file_name"]
393
+ })
394
+ for message in st.session_state["mood_chat_messages"]:
395
+ if message["role"] == "AI":
396
+ st.write(f"**AI**: {message['message']}")
397
+ st.image(message['image'])
398
+ else:
399
+ st.write(f"**You**: {message['message']}")
400
+ print(seed_number)
401
+
402
+
403
+ if 'page' not in st.session_state:
404
+ st.session_state.page = "Page 1"
405
+
406
+ # Routing between pages
407
+ if st.session_state.page == "Page 1":
408
+ page1()
409
+ elif st.session_state.page == "Page 2":
410
+ page2()
411
+ elif st.session_state.page == "Page 3":
412
+ page3()
413
+ elif st.session_state.page == "Page 4":
414
+ page4()