File size: 22,293 Bytes
eb8806e
 
b9f4ccd
 
 
eb8806e
 
b9f4ccd
 
 
 
 
 
 
 
 
 
 
 
eb8806e
 
 
b9f4ccd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9cb71c2
b9f4ccd
9cb71c2
b9f4ccd
417aa68
b9f4ccd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb8806e
b9f4ccd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4eb8efe
b9f4ccd
5a1d31c
b9f4ccd
 
 
 
 
 
 
eb8806e
b9f4ccd
 
 
 
eb8806e
 
 
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import os
import gradio as gr
import random
import time
import logging
import google.generativeai as genai

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler("api_debug.log"),
        logging.StreamHandler()
    ]
)
logger = logging.getLogger("idea_generator")

# Get Gemini API Key from environment variable
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
genai.configure(api_key=GEMINI_API_KEY)

# Helper function to select one option from a transformation string with a slash
def choose_alternative(transformation):
    if "/" not in transformation:
        return transformation
    parts = transformation.split("/")
    if len(parts) != 2:
        return random.choice([part.strip() for part in parts])
    left = parts[0].strip()
    right = parts[1].strip()
    if " " in left:
        tokens = left.split(" ", 1)
        prefix = tokens[0]
        if not right.startswith(prefix):
            option1 = left
            option2 = prefix + " " + right
        else:
            option1 = left
            option2 = right
        return random.choice([option1, option2])
    else:
        return random.choice([left, right])

# Physical transformation categories and related changes
physical_transformation_categories = {
    "๊ณต๊ฐ„์  ๋ณ€ํ™”": [
        "์ „์ง„/ํ›„์ง„ ์ด๋™", "์ขŒ/์šฐ ์ด๋™", "์ƒ์Šน/ํ•˜๊ฐ•", "ํ”ผ์น˜ ํšŒ์ „", "์š” ํšŒ์ „", "๋กค ํšŒ์ „",
        "๊ถค๋„ ์šด๋™", "๋‚˜์„ ํ˜• ์ด๋™", "๊ด€์„ฑ ๋“œ๋ฆฌํ”„ํŠธ", "์ž์ด๋กœ์Šค์ฝ”ํ”ฝ ์„ธ์ฐจ ์šด๋™", "๋น„๋Œ€์นญ ํšŒ์ „",
        "์ง„์ž ์šด๋™", "ํƒ„๋„ํ•™์  ์ด๋™", "๋ฌด์ค‘๋ ฅ ์ƒํƒœ์—์„œ์˜ ๋ถ€์œ "
    ],
    "๋ฌผ๋ฆฌ์  ํ˜•์ƒ ๋ณ€ํ™”": [
        "๋ถ€ํ”ผ ์ฆ๊ฐ€/๊ฐ์†Œ", "๊ธธ์ด ์‹ ์žฅ/์ˆ˜์ถ•", "๋„ˆ๋น„ ํ™•์žฅ/์ถ•์†Œ", "๋†’์ด ์ฆ๊ฐ€/๊ฐ์†Œ",
        "๋ฐ€๋„ ๋ณ€ํ™”", "๋ฌด๊ฒŒ ์ฆ๊ฐ€/๊ฐ์†Œ", "ํ˜•ํƒœ ๋ณ€ํ˜•", "์œ„์ƒ ๋ณ€ํ™”", "๋น„๋“ฑ๋ฐฉ์„ฑ ๋ณ€ํ˜•",
        "๋น„์„ ํ˜• ๋ณ€ํ˜•", "๋’คํ‹€๋ฆผ/๋น„ํ‹€๋ฆผ", "์ˆ˜์ถ•/ํŒฝ์ฐฝ ๋น„์œจ ๋ณ€ํ™”", "๋ชจ์„œ๋ฆฌ ๋ผ์šด๋”ฉ/์ƒคํ”„๋‹",
        "ํŒŒ๋‹จ/๊ท ์—ด ์ง„ํ–‰", "์„ธ๊ทธ๋จผํŠธํ™”"
    ],
    "ํ‘œ๋ฉด/์™ธ๊ด€ ๋ณ€ํ™”": [
        "์ƒ‰์ƒ ๋ณ€ํ™”", "ํ…์Šค์ฒ˜ ๋ณ€ํ™”", "ํˆฌ๋ช…๋„/๋ถˆํˆฌ๋ช…๋„ ๋ณ€ํ™”", "๊ด‘ํƒ/๋ฌด๊ด‘ ๋ณ€ํ™”",
        "๋ฐ˜์‚ฌ์œจ ๋ณ€ํ™”", "ํŒจํ„ด ๋ณ€ํ™”", "์ด์ƒ‰์„ฑ ํšจ๊ณผ", "๊ด‘๋ณ€์ƒ‰ ํšจ๊ณผ", "์—ด๋ณ€์ƒ‰ ํšจ๊ณผ",
        "ํ™€๋กœ๊ทธ๋ž˜ํ”ฝ ํŒจํ„ด ๋ณ€ํ™”", "ํ”„๋ ˆ๋„ฌ ๋ฐ˜์‚ฌ", "ํ‘œ๋ฉด ๋ชจํ•‘", "๋‚˜๋…ธ๊ตฌ์กฐ ํŒจํ„ด ๋ณ€ํ™”",
        "์ž๊ฐ€ ์ฒญ์†Œ ํšจ๊ณผ"
    ],
    "๋ฌผ์งˆ ์ƒํƒœ ๋ณ€ํ™”": [
        "๊ณ ์ฒดํ™”/์•กํ™”/๊ธฐํ™”", "๊ฒฐ์ •ํ™”/์šฉํ•ด", "์‚ฐํ™”/๋ถ€์‹", "๊ฒฝํ™”/์—ฐํ™”", "์ดˆ์ž„๊ณ„ ์ƒํƒœ ์ „ํ™˜",
        "๋น„์ •์งˆํ™”/๊ฒฐ์ •ํ™”", "์ƒ๋ถ„๋ฆฌ", "์ฝœ๋กœ์ด๋“œ ํ˜•์„ฑ/๋ถ„ํ•ด", "๊ฒ”ํ™”/์กธํ™”", "์ค€์•ˆ์ • ์ƒํƒœ ์ „์ด",
        "์ž๊ธฐ์กฐ๋ฆฝ/๋ถ„ํ•ด", "์ƒ์ „์ด ํžˆ์Šคํ…Œ๋ฆฌ์‹œ์Šค", "์œตํ•ด", "์‘๊ณ "
    ],
    "์—ด์—ญํ•™์  ๋ณ€ํ™”": [
        "์˜จ๋„ ์ƒ์Šน/๋ƒ‰๊ฐ", "์—ดํŒฝ์ฐฝ/์ˆ˜์ถ•", "์—ด ์ „๋„/๋‹จ์—ด", "์••๋ ฅ ์ฆ๊ฐ€/๊ฐ์†Œ", "๋‹จ์—ด ์žํ™”/ํƒˆ์žํ™”",
        "์—”ํŠธ๋กœํ”ผ ๋ณ€ํ™”", "์—ด์ „๊ธฐ ํšจ๊ณผ", "์ž๊ธฐ์—ด๋Ÿ‰ ํšจ๊ณผ", "์ƒ๋ณ€ํ™” ์ถ•์—ด/๋ฐฉ์—ด", "์—ด ์ŠคํŠธ๋ ˆ์Šค ๋ฐœ์ƒ/์™„ํ™”",
        "์—ด์ถฉ๊ฒฉ ํšจ๊ณผ", "๋ณต์‚ฌ ๋ƒ‰๊ฐ/๊ฐ€์—ด"
    ],
    "์šด๋™ํ•™์  ๋ณ€ํ™”": [
        "๊ฐ€์†/๊ฐ์†", "๋“ฑ์† ์šด๋™", "์ง„๋™/์ง„๋™ ๊ฐ์‡ ", "์ถฉ๋Œ/๋ฐ˜๋ฐœ", "ํšŒ์ „ ์†๋„ ์ฆ๊ฐ€/๊ฐ์†Œ",
        "๊ฐ์†๋„ ๋ณ€ํ™”", "์นด์˜ค์Šค์  ์šด๋™", "์Šคํ‹ฑ-์Šฌ๋ฆฝ ํ˜„์ƒ", "๊ณต์ง„/๋ฐ˜๊ณต์ง„", "์œ ์ฒด์—ญํ•™์  ์–‘๋ ฅ/ํ•ญ๋ ฅ ๋ณ€ํ™”",
        "๋Œํ•‘ ๊ณ„์ˆ˜ ๋ณ€ํ™”", "ํ•˜๋ชจ๋‹‰ ๋ชจ์…˜ ํ•ฉ์„ฑ", "๋น„๋‰ดํ„ด ์œ ์ฒด ๊ฑฐ๋™", "ํšŒ์ „-๋ณ‘์ง„ ์šด๋™ ์ปคํ”Œ๋ง"
    ],
    "๊ตฌ์กฐ์  ๋ณ€ํ™”": [
        "๋ถ€ํ’ˆ ์ถ”๊ฐ€/์ œ๊ฑฐ", "์กฐ๋ฆฝ/๋ถ„ํ•ด", "์ ‘์ด/ํŽผ์นจ", "๋ณ€ํ˜•/๋ณต์›", "์œ„์ƒ์ตœ์ ํ™” ํ˜•์ƒ ๋ณ€ํ™”",
        "์ž๊ฐ€ ์žฌ๊ตฌ์„ฑ", "ํ”„๋ž™ํƒˆ ํŒจํ„ด ํ˜•์„ฑ/์†Œ๋ฉธ", "์„ธํฌ์ž๋™์ž ํŒจํ„ด ๋ณ€ํ™”", "๋ชจ๋“ˆ์‹ ๋ณ€ํ˜•",
        "๋ฐœํ˜„์  ๊ตฌ์กฐ ํ˜•์„ฑ", "ํ˜•์ƒ ๊ธฐ์–ต ํšจ๊ณผ", "4D ํ”„๋ฆฐํŒ… ๊ตฌ์กฐ ๋ณ€ํ™”", "๋ถ€๋ถ„ ์ œ๊ฑฐ", "๋ถ€๋ถ„ ๊ตํ™˜",
        "๊ฒฐํ•ฉ", "๋ถ„๋ฆฌ"
    ],
    "์ „๊ธฐ/์ž๊ธฐ์  ๋ณ€ํ™”": [
        "์žํ™”/ํƒˆ์žํ™”", "์ „ํ•˜ ์ฆ๊ฐ€/๊ฐ์†Œ", "์ „๊ธฐ์žฅ ์ƒ์„ฑ/์†Œ๋ฉธ", "์ž๊ธฐ์žฅ ์ƒ์„ฑ/์†Œ๋ฉธ", "์ดˆ์ „๋„ ์ƒํƒœ ์ „์ด",
        "๊ฐ•์œ ์ „/๋ฐ˜๊ฐ•์œ ์ „ ์ „์ด", "์–‘์ž ์Šคํ•€ ์ƒํƒœ ๋ณ€ํ™”", "ํ”Œ๋ผ์ฆˆ๋งˆ ์ƒํƒœ ํ˜•์„ฑ/์†Œ๋ฉธ", "์Šคํ•€ํŒŒ ์ „ํŒŒ",
        "๊ด‘์ „ ํšจ๊ณผ", "์••์ „ ํšจ๊ณผ", "ํ™€ ํšจ๊ณผ"
    ],
    "ํ™”ํ•™์  ๋ณ€ํ™”": [
        "ํ‘œ๋ฉด ์ฝ”ํŒ… ๋ณ€ํ™”", "๋ฌผ์งˆ ์กฐ์„ฑ ๋ณ€ํ™”", "ํ™”ํ•™ ๋ฐ˜์‘์— ์˜ํ•œ ๋ณ€ํ™”", "์ด‰๋งค ํ™œ์„ฑํ™”/๋น„ํ™œ์„ฑํ™”",
        "๊ด‘ํ™”ํ•™ ๋ฐ˜์‘", "์ „๊ธฐํ™”ํ•™ ๋ฐ˜์‘", "์ž๊ธฐ์กฐ๋ฆฝ ๋‹จ๋ถ„์ž๋ง‰ ํ˜•์„ฑ", "๋ถ„์ž์ปดํ“จํŒ… ๋ณ€ํ™”",
        "์ƒ์ฒด๋ชจ๋ฐฉ ํ‘œ๋ฉด ๋ณ€ํ™”", "์Šค๋งˆํŠธ ํด๋ฆฌ๋จธ ๋ฐ˜์‘", "ํ™”ํ•™์  ์ง„๋™ ๋ฐ˜์‘", "์‚ฐํ™”", "ํ™˜์›",
        "์ค‘ํ•ฉ", "๊ฐ€์ˆ˜๋ถ„ํ•ด", "์œตํ•ฉ", "๋ฐฉ์‚ฌํ™”"
    ],
    "์‹œ๊ฐ„ ๊ด€๋ จ ๋ณ€ํ™”": [
        "๋…ธํ™”/ํ’ํ™”", "๋งˆ๋ชจ/๋ถ€์‹", "ํ‡ด์ƒ‰/๋ณ€์ƒ‰", "์†์ƒ/๋ณต๊ตฌ", "์ˆ˜๋ช… ์ฃผ๊ธฐ ๋‹จ๊ณ„ ๋ณ€ํ™”",
        "์‚ฌ์šฉ์ž ์ธํ„ฐ๋ž™์…˜ ๊ธฐ๋ฐ˜ ์ ์‘", "ํ•™์Šต๊ธฐ๋ฐ˜ ํ˜•์ƒ ์ตœ์ ํ™”", "์‹œ๊ฐ„์  ๋ฌผ์„ฑ ๋ณ€ํ™”", "์ง‘๋‹จ ๊ธฐ์–ต ํšจ๊ณผ",
        "๋ฌธํ™”์  ์˜๋ฏธ ๋ณ€ํ™”", "์‹œ๊ฐ„ ์ง€์—ฐ ์‘๋‹ต", "์ด๋ ฅ ์˜์กด์  ๋ณ€ํ™”", "ํผ์ง€ ์‹œ๊ฐ„ ๊ฑฐ๋™", "์ง„ํ™”์  ๋ณ€ํ™”",
        "์ฃผ๊ธฐ์  ์žฌ์ƒ/์žฌ์ƒ์„ฑ"
    ]
}

# Marketing transformation categories
marketing_transformation_categories = {
    "๋ธŒ๋žœ๋“œ/์•„์ด๋ดํ‹ฐํ‹ฐ ๋ณ€ํ™”": [
        "๋ธŒ๋žœ๋“œ๋ช… ๋ณ€๊ฒฝ/๋ฆฌ๋ธŒ๋žœ๋”ฉ", "๋กœ๊ณ  ์žฌ์„ค๊ณ„/ํ˜„๋Œ€ํ™”", "์Šฌ๋กœ๊ฑด ์—…๋ฐ์ดํŠธ/๋ณ€๊ฒฝ",
        "๋ธŒ๋žœ๋“œ ์ปฌ๋Ÿฌ ํŒ”๋ ˆํŠธ ๋ณ€๊ฒฝ", "๋ธŒ๋žœ๋“œ ๋ณด์ด์Šค/ํ†ค ์กฐ์ •", "๋ธŒ๋žœ๋“œ ํฌ์ง€์…”๋‹ ์žฌ์ •๋ฆฝ",
        "๋ธŒ๋žœ๋“œ ์Šคํ† ๋ฆฌ ์žฌ๊ตฌ์„ฑ", "๋ธŒ๋žœ๋“œ ์•„ํ‚คํ…์ฒ˜ ์žฌ๊ตฌ์„ฑ", "๋ธŒ๋žœ๋“œ ํผ์Šค๋„๋ฆฌํ‹ฐ ์ง„ํ™”",
        "๋ธŒ๋žœ๋“œ ์ด๋ฏธ์ง€ ์žฌ์ •๋ฆฝ"
    ],
    "์ œํ’ˆ/์„œ๋น„์Šค ๋ณ€ํ™”": [
        "๊ธฐ๋Šฅ์  ์—…๊ทธ๋ ˆ์ด๋“œ/๋‹ค์šด๊ทธ๋ ˆ์ด๋“œ", "ํฌ์žฅ/ํŒจํ‚ค์ง• ์žฌ์„ค๊ณ„", "์ œํ’ˆ ์‚ฌ์ด์ฆˆ/์šฉ๋Ÿ‰ ์กฐ์ •",
        "SKU ํ™•์žฅ/์ถ•์†Œ", "๋งž์ถคํ˜•/๊ฐœ์ธํ™” ์˜ต์…˜ ์ถ”๊ฐ€", "ํ”„๋ฆฌ๋ฏธ์—„/๋ฒ ์ด์ง ๋ผ์ธ ์ถœ์‹œ",
        "ํ•œ์ •ํŒ/์‹œ์ฆŒ ์—๋””์…˜ ์ถœ์‹œ", "์ฝœ๋ผ๋ณด๋ ˆ์ด์…˜/ํฌ๋กœ์Šค์˜ค๋ฒ„ ์ œํ’ˆ", "์ง€์†๊ฐ€๋Šฅ์„ฑ ๊ฐœ์„ ",
        "๋ฒˆ๋“ค๋ง/์–ธ๋ฒˆ๋“ค๋ง ์ „๋žต ๋ณ€๊ฒฝ"
    ],
    "๊ฐ€๊ฒฉ ์ „๋žต ๋ณ€ํ™”": [
        "ํ”„๋ฆฌ๋ฏธ์—„/๋Ÿญ์…”๋ฆฌ ๊ฐ€๊ฒฉ ํฌ์ง€์…”๋‹", "๊ฐ€๊ฒฉ ํ•˜๋ฝ/๊ฒฝ์ œ์  ํฌ์ง€์…”๋‹", "๊ฐ€์น˜ ๊ธฐ๋ฐ˜ ๊ฐ€๊ฒฉ ์ฑ…์ •",
        "๋™์  ๊ฐ€๊ฒฉ ์ •์ฑ… ๋„์ž…", "๋ฉค๋ฒ„์‹ญ/๊ตฌ๋… ๋ชจ๋ธ ์ „ํ™˜", "๊ฐ€๊ฒฉ ์„ธ๋ถ„ํ™” ์ „๋žต",
        "ํ”„๋กœ๋ชจ์…˜ ๊ฐ€๊ฒฉ ์ „๋žต ๋ณ€๊ฒฝ", "์‹ฌ๋ฆฌ์  ๊ฐ€๊ฒฉ ์ฑ…์ • ์ ์šฉ", "๋ฒˆ๋“ค ๊ฐ€๊ฒฉ ์ „๋žต ๋„์ž…",
        "ํ”„๋ฆฌ๋ฏธ์—„/์ด์ฝ”๋…ธ๋ฏธ ์ด์ค‘ ์ „๋žต"
    ],
    "ํ”„๋กœ๋ชจ์…˜/์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ๋ณ€ํ™”": [
        "๋ฉ”์‹œ์ง€ ํ”„๋ ˆ์ด๋ฐ ๋ณ€๊ฒฝ", "ํƒ€๊ฒŸ ์˜ค๋””์–ธ์Šค ํ™•์žฅ/์žฌ์ •์˜", "๊ฐ์„ฑ์ /์ด์„ฑ์  ์†Œ๊ตฌ์  ์ „ํ™˜",
        "์Šคํ† ๋ฆฌํ…”๋ง ๋ฐฉ์‹ ๋ณ€ํ™”", "์ฝ˜ํ…์ธ  ๋งˆ์ผ€ํŒ… ์ „๋žต ์กฐ์ •", "ํฌ๋ฆฌ์—์ดํ‹ฐ๋ธŒ ๋ฐฉํ–ฅ์„ฑ ๋ณ€๊ฒฝ",
        "๊ด‘๊ณ  ์บ ํŽ˜์ธ ์žฌ์„ค๊ณ„", "๋ธŒ๋žœ๋“œ ์•ฐ๋ฐฐ์„œ๋”/์ธํ”Œ๋ฃจ์–ธ์„œ ๊ต์ฒด", "์–ธ์–ด/ํŠธ๋ก  ์กฐ์ •",
        "๋น„์ฃผ์–ผ ์•„์ด๋ดํ‹ฐํ‹ฐ ์—…๋ฐ์ดํŠธ"
    ],
    "์ฑ„๋„/์œ ํ†ต ๋ณ€ํ™”": [
        "์˜จ๋ผ์ธ/์˜คํ”„๋ผ์ธ ์ „๋žต ์ „ํ™˜", "์˜ด๋‹ˆ์ฑ„๋„ ํ†ตํ•ฉ/๋ถ„๋ฆฌ", "์œ ํ†ต ํŒŒํŠธ๋„ˆ์‹ญ ํ™•์žฅ/์ถ•์†Œ",
        "D2C(Direct-to-Consumer) ๋ชจ๋ธ ๋„์ž…", "๋ฆฌํ…Œ์ผ ๊ฒฝํ—˜ ์žฌ์„ค๊ณ„", "์„œ๋น„์Šค ๋”œ๋ฆฌ๋ฒ„๋ฆฌ ๋ฐฉ์‹ ๋ณ€๊ฒฝ",
        "ํŒ์—…/์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜ ์œ ํ†ต ์ „๋žต", "์ง€์—ญ์  ํ™•์žฅ/์ถ•์†Œ", "ํŒŒํŠธ๋„ˆ ์ƒํƒœ๊ณ„ ์žฌ๊ตฌ์„ฑ",
        "๋””์ง€ํ„ธ ๋งˆ์ผ“ํ”Œ๋ ˆ์ด์Šค ์ „๋žต ๋ณ€ํ™”"
    ],
    "๊ณ ๊ฐ ๊ฒฝํ—˜ ๋ณ€ํ™”": [
        "๊ณ ๊ฐ ์—ฌ์ • ์žฌ์„ค๊ณ„", "ํผ์Šค๋„๋ผ์ด์ œ์ด์…˜/๋งž์ถคํ™” ๊ฐ•ํ™”", "์„œ๋น„์Šค ๋ ˆ๋ฒจ ์กฐ์ •",
        "๋กœ์—ดํ‹ฐ ํ”„๋กœ๊ทธ๋žจ ์žฌ๊ตฌ์„ฑ", "๊ณ ๊ฐ ์ฐธ์—ฌ ๋ฉ”์ปค๋‹ˆ์ฆ˜ ๋ณ€ํ™”", "์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค/UX ๊ฐœ์„ ",
        "๊ณ ๊ฐ ์„œ๋น„์Šค ํ”„๋กœํ† ์ฝœ ๋ณ€๊ฒฝ", "ํ”ผ๋“œ๋ฐฑ ๋ฃจํ”„ ์žฌ์„ค๊ณ„", "์ž๊ฐ€ ์„œ๋น„์Šค ์˜ต์…˜ ํ™•์žฅ",
        "์ฒดํ—˜ํ˜• ๋งˆ์ผ€ํŒ… ์š”์†Œ ๊ฐ•ํ™”"
    ],
    "๋””์ง€ํ„ธ ๋งˆ์ผ€ํŒ… ๋ณ€ํ™”": [
        "๊ฒ€์ƒ‰ ์ตœ์ ํ™”(SEO) ์ „๋žต ๋ณ€๊ฒฝ", "์†Œ์…œ ๋ฏธ๋””์–ด ํ”Œ๋žซํผ ํฌํŠธํด๋ฆฌ์˜ค ์กฐ์ •", "์ฝ˜ํ…์ธ  ํƒ€์ž…/ํฌ๋งท ๋‹ค์–‘ํ™”",
        "๋ฐ์ดํ„ฐ ๊ธฐ๋ฐ˜ ํƒ€๊ฒŸํŒ… ์ •๊ตํ™”", "๋งˆ์ผ€ํŒ… ์ž๋™ํ™” ํ™•์žฅ/์กฐ์ •", "๋””์ง€ํ„ธ ๊ด‘๊ณ  ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ตœ์ ํ™”",
        "์ด๋ฉ”์ผ ๋งˆ์ผ€ํŒ… ์ „๋žต ๋ณ€ํ™”", "๋ชจ๋ฐ”์ผ ์ตœ์ ํ™” ์ „๋žต ๊ฐ•ํ™”", "AR/VR ๋งˆ์ผ€ํŒ… ์š”์†Œ ํ†ตํ•ฉ",
        "์ธ๊ณต์ง€๋Šฅ ๊ธฐ๋ฐ˜ ๋งˆ์ผ€ํŒ… ๋„์ž…"
    ],
    "์‹œ์žฅ ํฌ์ง€์…”๋‹ ๋ณ€ํ™”": [
        "์‹œ์žฅ ์„ธ๊ทธ๋จผํŠธ ์žฌ์ •์˜", "๊ฒฝ์Ÿ์‚ฌ ๋Œ€๋น„ ํฌ์ง€์…”๋‹ ๋ณ€๊ฒฝ", "ํ‹ˆ์ƒˆ์‹œ์žฅ/๋ฉ”์ธ์ŠคํŠธ๋ฆผ ์ „๋žต ์ „ํ™˜",
        "์‚ฐ์—…/์นดํ…Œ๊ณ ๋ฆฌ ํšก๋‹จ ์žฌํฌ์ง€์…”๋‹", "์ง€๋ฆฌ์  ํƒ€๊ฒŸ ์‹œ์žฅ ํ™•์žฅ/์ถ•์†Œ", "๊ธ€๋กœ๋ฒŒ/๋กœ์ปฌ ํฌ์ง€์…”๋‹ ์กฐ์ •",
        "๋ฌธํ™”์  ๋งฅ๋ฝ ์ ์‘", "์ฃผ์š” ์‹œ์žฅ ๋ฉ”์‹œ์ง€ ์žฌ์ •์˜", "ํŠน์„ฑ ์ฐจ๋ณ„ํ™” ๊ฐ•ํ™”/๋ณ€๊ฒฝ",
        "๊ฐ€์น˜ ์ œ์•ˆ ์žฌ๊ตฌ์„ฑ"
    ],
    "ํ˜์‹ /ํŠธ๋ Œ๋“œ ๋ฐ˜์‘": [
        "์ฒจ๋‹จ ๊ธฐ์ˆ  ํ†ตํ•ฉ", "ํŠธ๋ Œ๋“œ ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ/ํŒ”๋กœ์›Œ ํฌ์ง€์…”๋‹", "์ง€์†๊ฐ€๋Šฅ์„ฑ/์นœํ™˜๊ฒฝ ์ „๋žต ๊ฐ•ํ™”",
        "์‚ฌํšŒ์  ์ฑ…์ž„ ์ด๋‹ˆ์…”ํ‹ฐ๋ธŒ ๋„์ž…", "๋””์ง€ํ„ธ ํŠธ๋žœ์Šคํฌ๋ฉ”์ด์…˜ ๊ฐ€์†ํ™”", "ํ—ฌ์Šค/์›ฐ๋น™ ์š”์†Œ ๊ฐ•ํ™”",
        "์„ธ๋Œ€๋ณ„ ํŠธ๋ Œ๋“œ ๋ฐ˜์‘ ์ ์šฉ", "๋ฏธ๋ž˜์ง€ํ–ฅ์  ํ˜์‹  ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜", "๋ฌธํ™”์  ํ˜„์ƒ ์—ฐ๊ณ„ ๋งˆ์ผ€ํŒ…",
        "ํฌ๋กœ์Šค ์ธ๋”์ŠคํŠธ๋ฆฌ ํ˜์‹  ์ ์šฉ"
    ],
    "๋ฐ์ดํ„ฐ/๋ถ„์„ ๊ธฐ๋ฐ˜ ๋ณ€ํ™”": [
        "๊ณ ๊ฐ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ๋ฐฉ์‹ ๋ณ€๊ฒฝ", "์˜ˆ์ธก ๋ถ„์„ ๋ชจ๋ธ ๋„์ž…/๊ฐ•ํ™”", "A/B ํ…Œ์ŠคํŒ… ์ฒด๊ณ„ ๊ตฌ์ถ•/ํ™•์žฅ",
        "์†Œ๋น„์ž ์ธ์‚ฌ์ดํŠธ ๋ฐœ๊ตด ๋ฐฉ์‹ ๋ณ€ํ™”", "๋งˆ์ผ€ํŒ… ROI ์ธก์ • ํ”„๋ ˆ์ž„์›Œํฌ ๋ณ€๊ฒฝ", "์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ํ™œ์šฉ ์ฒด๊ณ„ ๊ตฌ์ถ•",
        "ํ”„๋ผ์ด๋ฒ„์‹œ ์ค‘์‹ฌ ๋ฐ์ดํ„ฐ ์ „๋žต", "์„ธ๊ทธ๋จผํ…Œ์ด์…˜ ๋ชจ๋ธ ๊ณ ๋„ํ™”", "๋””์ง€ํ„ธ ํ–‰๋™ ๋ถ„์„ ์ฒด๊ณ„ ๊ฐ•ํ™”",
        "ํ†ตํ•ฉ ๋งˆ์ผ€ํŒ… ๋ฐ์ดํ„ฐ ๋Œ€์‹œ๋ณด๋“œ ๊ตฌ์ถ•"
    ]
}

def query_gemini_api(prompt):
    try:
        # Use a stable model
        model = genai.GenerativeModel('gemini-2.0-flash-thinking-exp-01-21')
        
        # Generate content with a simple approach
        response = model.generate_content(prompt)
        
        # Check the response - defensive handling
        try:
            # First try .text attribute
            if hasattr(response, 'text'):
                return response.text
            
            # Try accessing candidates (safely)
            if hasattr(response, 'candidates') and response.candidates:
                if len(response.candidates) > 0:
                    candidate = response.candidates[0]
                    
                    # Try accessing content and parts
                    if hasattr(candidate, 'content'):
                        content = candidate.content
                        
                        if hasattr(content, 'parts') and content.parts:
                            if len(content.parts) > 0:
                                return content.parts[0].text
            
            # Direct parts access attempt
            if hasattr(response, 'parts') and response.parts:
                if len(response.parts) > 0:
                    return response.parts[0].text
            
            # Fallback if all attempts fail
            return "Unable to generate a response. API response structure is different than expected."
            
        except Exception as inner_e:
            logger.error(f"Error processing response: {inner_e}")
            return f"An error occurred while processing the response: {str(inner_e)}"
            
    except Exception as e:
        logger.error(f"Error calling Gemini API: {e}")
        
        # Check for API key validation error
        if "API key not valid" in str(e):
            return "API key is not valid. Please check your GEMINI_API_KEY environment variable."
        
        return f"An error occurred while calling the API: {str(e)}"

# Generate enhanced descriptions without unnecessary questions or explanations
def enhance_with_llm(base_description, obj_name, category, is_marketing=False):
    domain = "๋งˆ์ผ€ํŒ…" if is_marketing else "๋ฌผ๋ฆฌ์ "
    prompt = f"""๋‹ค์Œ์€ '{obj_name}'์˜ '{domain} {category}' ๊ด€๋ จ ๊ฐ„๋‹จํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค:
"{base_description}"
ํ•ด๋‹น ์„ค๋ช…์„ 3-4๋ฌธ์žฅ์˜ ์ฐฝ์˜์ ์ธ ๋‹ต๋ณ€์œผ๋กœ ํ™•์žฅํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋ผ."""
    return query_gemini_api(prompt)

# Generate physical transformations for a single object
def generate_single_object_physical_transformations(obj):
    results = {}
    for category, transformations in physical_transformation_categories.items():
        transformation = choose_alternative(random.choice(transformations))
        base_description = f"{obj}์ด(๊ฐ€) {transformation} ํ˜„์ƒ์„ ๋ณด์ธ๋‹ค"
        results[category] = {"base": base_description, "enhanced": None}
    return results

# Generate marketing transformations for a single object
def generate_single_object_marketing_transformations(obj):
    results = {}
    for category, transformations in marketing_transformation_categories.items():
        transformation = choose_alternative(random.choice(transformations))
        base_description = f"{obj}์ด(๊ฐ€) {transformation} ์ „๋žต์„ ์‹คํ–‰ํ–ˆ๋‹ค"
        results[category] = {"base": base_description, "enhanced": None}
    return results

# Generate physical transformations for two objects
def generate_two_objects_physical_interaction(obj1, obj2):
    results = {}
    for category, transformations in physical_transformation_categories.items():
        transformation = choose_alternative(random.choice(transformations))
        template = random.choice([
            "{obj1}์ด(๊ฐ€) {obj2}์— ๊ฒฐํ•ฉํ•˜์—ฌ {change}๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค",
            "{obj1}๊ณผ(์™€) {obj2}์ด(๊ฐ€) ์ถฉ๋Œํ•˜๋ฉด์„œ {change}๊ฐ€ ์ผ์–ด๋‚ฌ๋‹ค"
        ])
        base_description = template.format(obj1=obj1, obj2=obj2, change=transformation)
        results[category] = {"base": base_description, "enhanced": None}
    return results

# Generate marketing transformations for two objects
def generate_two_objects_marketing_interaction(obj1, obj2):
    results = {}
    for category, transformations in marketing_transformation_categories.items():
        transformation = choose_alternative(random.choice(transformations))
        template = random.choice([
            "{obj1}์ด(๊ฐ€) {obj2}์˜ ๋ธŒ๋žœ๋“œ ํŠน์„ฑ์„ ํ™œ์šฉํ•˜์—ฌ {change}๋ฅผ ์ง„ํ–‰ํ–ˆ๋‹ค",
            "{obj1}๊ณผ(์™€) {obj2}์˜ ํ˜‘์—…์œผ๋กœ {change}๊ฐ€ ์‹œ์žฅ์— ๋„์ž…๋˜์—ˆ๋‹ค"
        ])
        base_description = template.format(obj1=obj1, obj2=obj2, change=transformation)
        results[category] = {"base": base_description, "enhanced": None}
    return results

# Generate physical transformations for three objects
def generate_three_objects_physical_interaction(obj1, obj2, obj3):
    results = {}
    for category, transformations in physical_transformation_categories.items():
        transformation = choose_alternative(random.choice(transformations))
        template = random.choice([
            "{obj1}, {obj2}, {obj3}์ด(๊ฐ€) ์‚ผ๊ฐํ˜• ๊ตฌ์กฐ๋กœ ๊ฒฐํ•ฉํ•˜์—ฌ {change}๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค",
            "{obj1}์ด(๊ฐ€) {obj2}์™€(๊ณผ) {obj3} ์‚ฌ์ด์—์„œ ๋งค๊ฐœ์ฒด ์—ญํ• ์„ ํ•˜๋ฉฐ {change}๋ฅผ ์ด‰์ง„ํ–ˆ๋‹ค"
        ])
        base_description = template.format(obj1=obj1, obj2=obj2, obj3=obj3, change=transformation)
        results[category] = {"base": base_description, "enhanced": None}
    return results

# Generate marketing transformations for three objects
def generate_three_objects_marketing_interaction(obj1, obj2, obj3):
    results = {}
    for category, transformations in marketing_transformation_categories.items():
        transformation = choose_alternative(random.choice(transformations))
        template = random.choice([
            "{obj1}, {obj2}, {obj3}์ด(๊ฐ€) ๊ณต๋™ ๋งˆ์ผ€ํŒ…์œผ๋กœ {change}๊ฐ€ ์‹œ๋„ˆ์ง€๋ฅผ ์ฐฝ์ถœํ–ˆ๋‹ค",
            "{obj1}์ด(๊ฐ€) {obj2}์™€(๊ณผ) {obj3}์˜ ๊ณ ๊ฐ์ธต์„ ํ†ตํ•ฉํ•˜์—ฌ {change}๋กœ ์ƒˆ๋กœ์šด ์‹œ์žฅ์„ ์ฐฝ์ถœํ–ˆ๋‹ค"
        ])
        base_description = template.format(obj1=obj1, obj2=obj2, obj3=obj3, change=transformation)
        results[category] = {"base": base_description, "enhanced": None}
    return results

# Enhance descriptions using the LLM
def enhance_descriptions(results, objects, is_marketing=False):
    obj_name = " ๋ฐ ".join([obj for obj in objects if obj])
    for category, result in results.items():
        result["enhanced"] = enhance_with_llm(result["base"], obj_name, category, is_marketing)
    return results

# Generate physical transformations based on number of objects
def generate_physical_transformations(text1, text2=None, text3=None):
    if text2 and text3:
        results = generate_three_objects_physical_interaction(text1, text2, text3)
        objects = [text1, text2, text3]
    elif text2:
        results = generate_two_objects_physical_interaction(text1, text2)
        objects = [text1, text2]
    else:
        results = generate_single_object_physical_transformations(text1)
        objects = [text1]
    return enhance_descriptions(results, objects, is_marketing=False)

# Generate marketing transformations based on number of objects
def generate_marketing_transformations(text1, text2=None, text3=None):
    if text2 and text3:
        results = generate_three_objects_marketing_interaction(text1, text2, text3)
        objects = [text1, text2, text3]
    elif text2:
        results = generate_two_objects_marketing_interaction(text1, text2)
        objects = [text1, text2]
    else:
        results = generate_single_object_marketing_transformations(text1)
        objects = [text1]
    return enhance_descriptions(results, objects, is_marketing=True)

# Format results for display
def format_results(results):
    formatted = ""
    for category, result in results.items():
        formatted += f"## {category}\n**๊ธฐ๋ณธ ๋ณ€ํ™”**: {result['base']}\n\n**์ž์„ธํ•œ ์„ค๋ช…**: {result['enhanced']}\n\n---\n\n"
    return formatted

# Process physical inputs and generate transformations
def process_physical_inputs(text1, text2, text3):
    messages = []
    messages.append("์ž…๋ ฅ๊ฐ’ ํ™•์ธ ์ค‘...")
    time.sleep(0.3)
    text1 = text1.strip() if text1 else None
    text2 = text2.strip() if text2 else None
    text3 = text3.strip() if text3 else None
    if not text1:
        messages.append("์˜ค๋ฅ˜: ์ตœ์†Œ ํ•˜๋‚˜์˜ ํ‚ค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
        return "\n\n".join(messages)
    messages.append("๋ฌผ๋ฆฌ์  ๋ณ€ํ™” ์•„์ด๋””์–ด ์ƒ์„ฑ ์ค‘...")
    time.sleep(0.3)
    results = generate_physical_transformations(text1, text2, text3)
    messages.append("๊ฒฐ๊ณผ ํฌ๋งทํŒ… ์ค‘...")
    time.sleep(0.3)
    formatted = format_results(results)
    messages.append("์™„๋ฃŒ!")
    messages.append(formatted)
    return "\n\n".join(messages)

# Process marketing inputs and generate transformations
def process_marketing_inputs(text1, text2, text3):
    messages = []
    messages.append("์ž…๋ ฅ๊ฐ’ ํ™•์ธ ์ค‘...")
    time.sleep(0.3)
    text1 = text1.strip() if text1 else None
    text2 = text2.strip() if text2 else None
    text3 = text3.strip() if text3 else None
    if not text1:
        messages.append("์˜ค๋ฅ˜: ์ตœ์†Œ ํ•˜๋‚˜์˜ ํ‚ค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
        return "\n\n".join(messages)
    messages.append("๋งˆ์ผ€ํŒ… ๋ณ€ํ™” ์•„์ด๋””์–ด ์ƒ์„ฑ ์ค‘...")
    time.sleep(0.3)
    results = generate_marketing_transformations(text1, text2, text3)
    messages.append("๊ฒฐ๊ณผ ํฌ๋งทํŒ… ์ค‘...")
    time.sleep(0.3)
    formatted = format_results(results)
    messages.append("์™„๋ฃŒ!")
    messages.append(formatted)
    return "\n\n".join(messages)

# Check for API key and return warning message if needed
def get_warning_message():
    if not GEMINI_API_KEY:
        return "โš ๏ธ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ GEMINI_API_KEY๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. Gemini API ํ‚ค๋ฅผ ์„ค์ •ํ•˜์„ธ์š”."
    return ""

# Create the Gradio UI
with gr.Blocks(title="ํ‚ค์›Œ๋“œ ๊ธฐ๋ฐ˜ ์ฐฝ์˜์  ์•„์ด๋””์–ด ์ƒ์„ฑ๊ธฐ", theme=gr.themes.Soft(primary_hue="teal", secondary_hue="slate", neutral_hue="neutral")) as demo:
    gr.HTML("""
    <style>
    body { background: linear-gradient(135deg, #e0eafc, #cfdef3); font-family: 'Arial', sans-serif; }
    .gradio-container { padding: 20px; }
    h1, h2 { text-align: center; }
    h1 { color: #333; }
    h2 { color: #555; }
    .output { background-color: #ffffff; padding: 15px; border-radius: 8px; }
    .gr-button { background-color: #4CAF50; color: white; border: none; border-radius: 4px; padding: 8px 16px; }
    </style>
    """)
    gr.Markdown("# ๐Ÿš€ ํ‚ค์›Œ๋“œ ๊ธฐ๋ฐ˜ ์ฐฝ์˜์  ์•„์ด๋””์–ด ์ƒ์„ฑ๊ธฐ")
    gr.Markdown("์ž…๋ ฅํ•œ ํ‚ค์›Œ๋“œ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ฐฝ์˜์ ์ธ ๋ฌผ๋ฆฌ์ /๋งˆ์ผ€ํŒ… ๋ณ€ํ™” ์•„์ด๋””์–ด๋ฅผ ๋„์ถœํ•ฉ๋‹ˆ๋‹ค. ์ตœ๋Œ€ 3๊ฐœ์˜ ํ‚ค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.")
    warning = gr.Markdown(get_warning_message())
    with gr.Row():
        with gr.Column(scale=1):
            text_input1 = gr.Textbox(label="ํ‚ค์›Œ๋“œ 1 (ํ•„์ˆ˜)", placeholder="์˜ˆ: ์Šค๋งˆํŠธํฐ")
            text_input2 = gr.Textbox(label="ํ‚ค์›Œ๋“œ 2 (์„ ํƒ)", placeholder="์˜ˆ: ์ธ๊ณต์ง€๋Šฅ")
            text_input3 = gr.Textbox(label="ํ‚ค์›Œ๋“œ 3 (์„ ํƒ)", placeholder="์˜ˆ: ํ—ฌ์Šค์ผ€์–ด")
            submit_button = gr.Button("์•„์ด๋””์–ด ์ƒ์„ฑํ•˜๊ธฐ")
        with gr.Column(scale=2):
            with gr.Tabs():
                with gr.TabItem("๋ฌผ๋ฆฌ์  ๋ณ€ํ™” ์•„์ด๋””์–ด", id="physical_tab"):
                    physical_output = gr.Markdown(label="๋ฌผ๋ฆฌ์  ๋ณ€ํ™” ์•„์ด๋””์–ด ๊ฒฐ๊ณผ")
                with gr.TabItem("๋งˆ์ผ€ํŒ… ๋ณ€ํ™” ์•„์ด๋””์–ด", id="marketing_tab"):
                    marketing_output = gr.Markdown(label="๋งˆ์ผ€ํŒ… ๋ณ€ํ™” ์•„์ด๋””์–ด ๊ฒฐ๊ณผ")
    
    # Add some example combinations
    gr.Examples(
        examples=[
            ["์Šค๋งˆํŠธํฐ", "", ""],
            ["์ปคํ”ผ", "์ฑ…", ""],
            ["์ž๋™์ฐจ", "๋กœ๋ด‡", "์ธ๊ณต์ง€๋Šฅ"],
            ["์šด๋™ํ™”", "์›จ์–ด๋Ÿฌ๋ธ”", "๊ฑด๊ฐ•"],
        ],
        inputs=[text_input1, text_input2, text_input3],
    )
    
    # Connect the button click events to functions
    submit_button.click(fn=process_physical_inputs, inputs=[text_input1, text_input2, text_input3], outputs=physical_output)
    submit_button.click(fn=process_marketing_inputs, inputs=[text_input1, text_input2, text_input3], outputs=marketing_output)

if __name__ == "__main__":
    demo.launch(debug=True)