File size: 20,771 Bytes
19b0461
 
 
 
42b7751
19b0461
 
 
 
 
 
100a0f3
19b0461
ef570d2
19b0461
42b7751
19b0461
 
ef570d2
19b0461
49cb6a7
ef570d2
19b0461
c733ec4
19b0461
47a9209
19b0461
 
 
 
83bfb9a
19b0461
c733ec4
19b0461
 
 
 
 
 
 
ca4cae7
19b0461
 
 
 
 
 
ca4cae7
 
42b7751
19b0461
 
 
 
 
47a9209
ef570d2
19b0461
 
 
 
 
 
 
ca4cae7
 
19b0461
 
24fdf22
19b0461
 
 
185c35f
 
19b0461
 
 
 
 
47a9209
19b0461
 
ca4cae7
19b0461
 
47a9209
19b0461
 
4556f8e
 
19b0461
6a13746
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24fdf22
c733ec4
574bcf9
 
fbac076
a101c78
c733ec4
 
6a13746
 
c733ec4
574bcf9
6a13746
c733ec4
ca4cae7
 
 
 
 
a101c78
 
 
 
 
 
 
 
 
 
 
 
 
c733ec4
 
c237e9a
c733ec4
24fdf22
c733ec4
 
 
185c35f
c733ec4
 
 
 
 
 
 
ca4cae7
fb4628b
e18e74c
fb4628b
 
c733ec4
fb4628b
 
 
 
 
 
 
 
 
 
c733ec4
 
 
 
 
 
 
fb4628b
 
100a0f3
c733ec4
 
 
 
 
 
 
 
e18e74c
 
c733ec4
f316c14
 
c733ec4
ca4cae7
ae540d4
a5168ba
5b3eaed
127ba33
59b4146
127ba33
51d2a0e
127ba33
51d2a0e
9b67263
 
d16af35
9b67263
 
127ba33
 
06ed903
9b67263
 
 
06ed903
127ba33
858008a
 
 
 
 
127ba33
06ed903
f2dce6c
 
 
 
 
6ffa42f
b2ce3c0
858008a
9d5c88a
47fd3c4
 
127ba33
06ed903
f2dce6c
 
 
 
127ba33
06ed903
f2dce6c
 
 
 
127ba33
ecc44ed
 
 
 
b1fb6d6
ecc44ed
 
 
f2dce6c
d71287a
 
 
 
9badad0
d71287a
c83fca1
b2ce3c0
858008a
 
d71287a
d374694
d71287a
 
858008a
 
6b78349
b2ce3c0
858008a
b2ce3c0
858008a
 
 
 
b2ce3c0
858008a
 
 
cd51d46
 
 
 
b5b7997
cd51d46
858008a
 
 
b2ce3c0
858008a
 
 
06ed903
9b67263
5495dd4
b2ce3c0
127ba33
9b67263
 
127ba33
06ed903
9b67263
aa19b16
9b67263
 
127ba33
06ed903
9b67263
 
 
d374694
9b67263
127ba33
469bddf
 
 
 
 
31e3fe4
469bddf
 
 
 
 
 
9abf2a6
59b4146
b2ce3c0
9abf2a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf9ba92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5853519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9abf2a6
46010ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b78349
 
b2ce3c0
6b78349
 
 
 
 
 
 
 
 
 
 
 
 
d0e48a0
9abf2a6
6b78349
9abf2a6
858008a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
469bddf
6b78349
 
 
 
 
 
59b4146
 
 
 
 
 
 
 
 
 
 
6b78349
59b4146
 
 
 
 
 
 
 
6b78349
59b4146
89905ab
6b78349
5853519
6b78349
 
f86bb41
6b78349
 
 
 
 
 
 
 
469bddf
127ba33
469bddf
2f70846
ae540d4
9abf2a6
858008a
 
 
ca4cae7
cf9ba92
c733ec4
5b3eaed
cd51d46
858008a
 
cd51d46
858008a
f86bb41
c733ec4
cd51d46
858008a
 
e306d8d
5853519
cd51d46
 
5853519
 
cd51d46
5853519
 
cd51d46
5853519
f316c14
ee13591
89905ab
d0e48a0
89905ab
6111113
cd51d46
a1941fd
 
5bd504f
f86bb41
a1941fd
5853519
cd51d46
a1941fd
 
574bcf9
9b67263
ca4cae7
 
 
6b92104
 
19b0461
47a9209
574bcf9
 
 
fbac076
a101c78
 
f316c14
 
 
e18e74c
c237e9a
f316c14
 
 
a101c78
f316c14
6a13746
f316c14
 
fbac076
a101c78
6a13746
574bcf9
 
 
 
c237e9a
f316c14
 
a101c78
6a13746
4556f8e
e18e74c
 
fb4628b
c733ec4
e18e74c
 
574bcf9
 
185c35f
ca4cae7
185c35f
 
4556f8e
 
 
 
 
 
 
 
 
 
 
 
24fdf22
19b0461
574bcf9
 
 
 
 
b5158fe
e1c4ed0
574bcf9
 
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
import os
import gradio as gr
from PIL import Image
from io import BytesIO
from dotenv import load_dotenv
from gtts import gTTS
import tempfile
import traceback
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as RLImage
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
import re
import google.generativeai as genai


# Load API keys
load_dotenv()
api_key = os.getenv("GOOGLE_API_KEY")


if not api_key:
    raise EnvironmentError("Missing API keys. Check GOOGLE_API_KEY in .env.")

# Configure models
genai.configure(api_key=api_key)
text_model = genai.GenerativeModel("gemini-1.5-flash")
image_model = genai.GenerativeModel(
    model_name="gemini-2.0-flash-preview-image-generation",
    generation_config={"response_modalities": ["TEXT", "IMAGE"]}
)


# -------------------- Utility Functions --------------------
def generate_image_from_text(prompt):
    try:
        response = image_model.generate_content(prompt)
        for part in response.candidates[0].content.parts:
            if hasattr(part, 'inline_data') and part.inline_data.mime_type.startswith("image/"):
                return Image.open(BytesIO(part.inline_data.data))
    except Exception as e:
        print("Image generation error:", e)
    return None

def summarize_scene(scene_text):
    try:
        response = text_model.generate_content(f"Summarize this scene in one sentence: {scene_text}")
        return response.text.strip()
    except Exception as e:
        print("Summary error:", e)
        return "Summary unavailable."

def explain_scene(image):
    try:
        response = text_model.generate_content([image, "Explain this image scene in detail."])
        return response.text.strip()
    except:
        return "Explanation unavailable."

def text_to_speech(text):
    try:
        tts = gTTS(text)
        tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
        tts.save(tmp.name)
        return tmp.name
    except Exception as e:
        print("TTS error:", e)
        return None

def generate_pdf(images, explanations, title="AI-Generated Story Scenes"):
    with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
        doc = SimpleDocTemplate(tmp.name, pagesize=letter)
        styles = getSampleStyleSheet()
        story = [Paragraph(title, styles["Title"]), Spacer(1, 12)]


        for i, img in enumerate(images):
            if img:
                with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as img_tmp:
                    img.save(img_tmp.name)
                    story += [
                        RLImage(img_tmp.name, width=400, height=300),
                        Spacer(1, 12),
                        Paragraph(f"Scene {i + 1} Explanation", styles["Heading3"]),
                        Paragraph(explanations[i], styles["BodyText"]),
                        Spacer(1, 24),
                    ]
        doc.build(story)
        return tmp.name
def reset_fields():
    return "", "", "", "", "", "Fantasy"

def recreate_scene_handler(
    num_scenes, theme, char_count, character_names, dialogue,
    dialogue_speaker, char_styles, char_moods, bg_style,
    images, summaries, explanations, scene_number_to_recreate):

    index = int(scene_number_to_recreate) - 1
    if index < 0 or index >= len(images):
        return (
            None, "", "❌ Invalid input. You have not generated Scene {} yet.".format(scene_number_to_recreate),
            images, summaries, explanations,
            f"⚠️ Scene {scene_number_to_recreate} is not available. Generate it first.", gr.update(visible=False)
        )

    return generate_scene(
        num_scenes, theme, char_count, character_names, dialogue,
        dialogue_speaker, char_styles, char_moods, bg_style,
        images, summaries, explanations,
        recreate_mode=True, current_index=index
    )



# -------------------- Core Logic --------------------

def generate_scene(num_scenes, theme, char_count, character_names, dialogue,
    dialogue_speaker, char_styles, char_moods, bg_style,
    images, summaries, explanations, recreate_mode=False, current_index=0):

    prompt = (
        f"A {bg_style}-style illustration for Scene {current_index + 1} with {char_count} characters in a '{theme}' setting. "
        f"Characters: {character_names}. They are dressed as: {char_styles}. Current mood: {char_moods}. "
    )
    if dialogue:
        prompt += f'The character "{dialogue_speaker}" says: "{dialogue}". Display this in a speech bubble.'
        prompt += f' Please visualize this as a speech bubble above {dialogue_speaker}, like in a cartoon.'

    image = generate_image_from_text(prompt)
    summary = summarize_scene(prompt)
    explanation = explain_scene(image) if image else "Explanation unavailable."

    if recreate_mode:
        images[current_index] = image
        summaries[current_index] = summary
        explanations[current_index] = explanation
    else:
        if len(images) >= int(num_scenes):
            return gr.update(), gr.update(), gr.update(), images, summaries, explanations, \
                   f"βœ… All {num_scenes} scenes have been generated.", gr.update(visible=True)
        images.append(image)
        summaries.append(summary)
        explanations.append(explanation)

    status = f"βœ… Scene {current_index + 1} {'recreated' if recreate_mode else 'generated'}."
    done_visible = len(images) == int(num_scenes)

    return image, summary, explanation, images, summaries, explanations, status, gr.update(visible=done_visible)

def finalize_story(images, explanations, title):
    if not images or not explanations:
        return None, None

    pdf = generate_pdf(images, explanations,title)

    with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8") as txt:
        for i, exp in enumerate(explanations):
            txt.write(f"Scene {i+1}:\n{exp}\n\n")
        txt_path = txt.name

    return txt_path, pdf

def ai_write_scene(theme, total_scenes, scene_summaries):
    try:
        scene_index = len(scene_summaries) + 1
        if scene_index > int(total_scenes):
            return "", "", "", "", "", "Fantasy", f"βœ… All {total_scenes} scenes completed."

        story_so_far = "\n".join([f"Scene {i+1}: {s}" for i, s in enumerate(scene_summaries)]) if scene_summaries else ""
        prompt = f"""
You are co-writing a story set in this theme: "{theme}".
Generate Scene {scene_index} of {total_scenes}.
Continue the story logically based on previous scenes (if any):

{story_so_far}

Return:
1. Character names (existing or new),
2. A single dialogue line,
3. The speaker of that dialogue,
4. Outfits worn,
5. Characters' emotional moods,
6. Background style (choose from: Realistic, Cartoon, Fantasy, Dark Fantasy).
Only provide the raw values, no headers.
"""
        response = text_model.generate_content(prompt).text.strip()
        parts = [re.sub(r"^\d+\.\s*", "", line.strip()) for line in response.split("\n") if line.strip()]
        char_names = parts[0] if len(parts) > 0 else ""
        dialogue = parts[1] if len(parts) > 1 else ""
        speaker = parts[2] if len(parts) > 2 else ""
        outfits = parts[3] if len(parts) > 3 else ""
        moods = parts[4] if len(parts) > 4 else ""
        bg_style = parts[5] if len(parts) > 5 else "Fantasy"

        return char_names, dialogue, speaker, outfits, moods, bg_style, f"πŸ“ Scene {scene_index} ready to generate."
    except Exception as e:
        print("AI write error:", e)
        return "", "", "", "", "", "Fantasy", "⚠️ AI scene generation failed."


# -------------------- UI --------------------


with gr.Blocks(
    title="Comic Creator", css="""
    body {
        background-color: #ffffff;  
    }

    .gradio-container {
        max-width: 1000px;
        margin: 2rem auto;
        padding: 32px;
        background: #d5f2ee;
        border-radius: 20px;
        box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
        font-family: 'Segoe UI', sans-serif;
    }
    
    .gr-markdown h2, .gr-markdown h3 {
        color: #2e4053;
    }
    
    .gr-button {
        border-radius: 10px;
        font-weight: bold;
        padding: 12px 24px;
        transition: all 0.4s ease;
        box-shadow: 0 4px 10px rgba(0,0,0,0.15);
    }
    
    #generate-btn,
    #ai-write-btn,
    #recreate-btn,
    #reset-btn,
    #done-btn,
    #recreate-scene-index {
        background-color: #66b2b2;
        color: white;
        border: 1px solid #000000;
        border-radius:10px;
        margin:12px;
    }
    
    #generate-btn, #ai-write-btn{
        display: flex;
        justify-content: center;
        gap: 10px;
    }
    
    #recreate-btn,#reset-btn, #done-btn {
        display: flex;
        justify-content: center;
        gap: 10px;
    }
    #tts-btn{
        background-color: #66b2b2;
        color: white;
        border: 1px solid #000000;
        border-radius:10px;
        width:700px;
        height:80px;
    }


    #generate-btn:hover,
    #ai-write-btn:hover,
    #recreate-btn:hover,
    #reset-btn:hover,
    #done-btn:hover,
    #tts-btn:hover{
        background: #008080 ;
        transform: scale(1.05);
        color: #000;
        cursor: pointer;
        border-radius:10px;
    }


    input, textarea, select {
        border-radius: 8px ;
        border: 1px solid #004c4c;
        padding: 10px !important;
        background-color: #d5f2ee;
        box-shadow: inset 0 1px 3px rgba(0,0,0,0.05);
        transition: all 0.2s ease-in-out;
    }
    input:focus, textarea:focus, select:focus {
        border-color: #004c4c !important;
        box-shadow: 0 0 6px rgba(255, 105, 180, 0.3);
        outline: none;
    }

    .custom-rows{
        background: #e1f5f5;
        padding: 10px;               
        border-radius: 8px;           
        }
    
    /* πŸŽ† Scene Image Border */
    .gr-image img {
        border: 4px dashed #004c4c;
        border-radius: 16px;
        padding: 4px;
    }
    
    .gr-accordion {
        background-color: #d4a373;
        border: 1px solid #004c4c;
        border-radius: 12px;
        padding: 12px;
        margin-bottom: 12px;
    }
    
    .gr-accordion .gr-box {
        background-color: #d4a373;
        border-radius: 10px;
        padding: 12px;
    }
    
    .gr-image, .gr-audio, .gr-file {
        border: 1px solid #d6eaf8;
        border-radius: 12px;
        background-color: #d1e8e8;
        padding: 12px;
    }

    .animated-title {
        font-size: 3rem;
        font-weight: bold;
        text-align: center;
        color: #147d5a;
        animation: float 3s ease-in-out infinite;
        text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
        margin-bottom: 1.5rem;
        font-family: 'Comic Sans MS', cursive, sans-serif;
    }
    
    #accordion {
        background: linear-gradient(to right, #ffffff, #66b2b2);
        border: 2px solid #004c4c;
        border-radius: 12px;
        padding: 16px;
        box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        font-family: 'Segoe UI', sans-serif;
        transition: all 0.3s ease-in-out;
    }
    
    #accordion:hover {
        box-shadow: 0 6px 20px rgba(0,0,0,0.2);
        transform: scale(1.01);
    }
    
    #accordion h2 {
        color: #343a40;
        font-weight: 600;
        font-size: 1.2rem;
        font-color: black;
    }
    #accordion1{
        background: linear-gradient(to right, #ffffff, #66b2b2);
        border: 2px solid #004c4c;
        border-radius: 12px;
        padding: 16px;
        box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        font-family: 'Segoe UI', sans-serif;
        transition: all 0.3s ease-in-out;
    }
    
    #accordion1:hover {
        box-shadow: 0 6px 20px rgba(0,0,0,0.2);
        transform: scale(1.01);
    }
    
    #accordion1 h2 {
        color: #343a40;
        font-weight: 600;
        font-size: 1.2rem;
        
    }
    #accordion2{
        background: linear-gradient(to right, #ffffff, #66b2b2);
        border: 2px solid #004c4c;
        border-radius: 12px;
        padding: 16px;
        box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        font-family: 'Segoe UI', sans-serif;
        transition: all 0.3s ease-in-out;
    }
    
    #accordion2:hover {
        box-shadow: 0 6px 20px rgba(0,0,0,0.2);
        transform: scale(1.01);
    }
    
    #accordion2 h2 {
        color: #343a40;
        font-weight: 600;
        font-size: 1.2rem;
    }
    #custom-dropdown select {
        background-color: #d5f2ee;
        border: 2px solid #008080;
        border-radius: 10px;
        padding: 10px 12px;
        font-size: 1rem;
        color: #004c4c;
        font-weight: 600;
        transition: all 0.3s ease-in-out;
        box-shadow: inset 0 1px 3px rgba(0,0,0,0.08);
    }
        
    #custom-dropdown select:focus {
        outline: none;
        border-color: #00a3a3;
        box-shadow: 0 0 8px rgba(0, 163, 163, 0.3);
        background-color: #d5f2ee;
    }
    
    #custom-dropdown label {
        font-weight: bold;
        color: #2e4053;
        margin-bottom: 6px;
    }

    #output {
        background: #fff0f5;
        border: 2px dashed #004c4c;
        border-radius: 14px;
        padding: 16px;
        font-size: 1.1rem;
        color: #4a235a;
        animation: floatX 4s ease-in-out infinite;
        box-shadow: 0 4px 8px rgba(214, 51, 132, 0.15);
        transition: all 0.3s ease-in-out;
    }
    
    #output:hover {
        background: #ffe0ec;
        transform: scale(1.01);
        cursor: default;
    }


    
        @keyframes float {
            0% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
            100% { transform: translateY(0px); }
        }
    
        @keyframes floatX {
        0%   { transform: translateX(0); }
        50%  { transform: translateX(15px); }
        100% { transform: translateX(0); }
    }
    
    @keyframes pulseColor {
        0% { color: #ff4081; }
        50% { color: #7e57c2; }
        100% { color: #42a5f5; }
    }
    
    @keyframes backgroundMove {
        0% { background-position: 0% 50%; }
        100% { background-position: 100% 50%; }
    }
    @keyframes bounce {
      0%   { transform: scale(1); }
      50%  { transform: scale(1.1); }
      100% { transform: scale(1); }
    }
    
    .gr-file label[for^=component-] {
        background-color: #ffe6f0;
        border: 2px solid #006666;
        border-radius: 12px;
        padding: 10px;
        transition: all 0.3s ease-in-out;
        box-shadow: 0 4px 10px rgba(255, 105, 180, 0.2);
        font-weight: bold;
        color: #4a235a;
        font-size: 1rem;
        text-align: center;
    }

    .gr-file label[for^=component-]:hover {
        background-color: #f8bbd0;
        transform: scale(1.03);
        box-shadow: 0 6px 12px rgba(255, 105, 180, 0.3);
        cursor: pointer;
    }

    .gr-audio {
        border: 2px solid #006666;
        background-color: ##b2d8d8;
        border-radius: 16px;
        padding: left 12px;
        animation: pulseColor 3s infinite;
        transition: transform 0.3s ease;
        
    }
    
    .gr-audio:hover {
        transform: scale(1.02);
        box-shadow: 0 6px 12px rgba(171, 71, 188, 0.2);
    }



""") as demo:
    gr.Markdown('<h1 class="animated-title">🎬 Comic Generator</h1>')
    gr.Markdown("Describe your story one scene at a time, with AI-generated images, summaries, and explanations.\nPut your own GOOGLE API KEY (named as GOOGLE_API_KEY) in the SECRET VARIABLE.")

    with gr.Accordion("🧩 Story Setup", open=True, elem_id="accordion"):
        title = gr.Textbox(label="πŸ“– Story Title", placeholder="e.g. The Enchanted Forest",elem_id="textarea")
        scene_total = gr.Number(label="πŸ”’ Number of Scenes", precision=0, value=3, elem_id="input")
        theme = gr.Textbox(label="🌍 Global Theme", placeholder="e.g. A magical forest full of glowing creatures", elem_id="textarea")

    gr.Markdown("### ✨ Describe Your Next Scene", elem_id="accordion1")

    with gr.Group(elem_id = "accordion"):
        with gr.Row(elem_classes="custom-rows"):
            char_count = gr.Number(label="πŸ‘₯ Number of Characters", precision=0, value=2, elem_id="input")
            character_names = gr.Textbox(label="πŸ§™β€β™‚οΈ Character Names", elem_id="textarea")
        with gr.Row(elem_classes="custom-rows"):
            dialogue = gr.Textbox(label="πŸ’¬ Dialogue (optional)", placeholder="e.g. 'Protect the forest!'", elem_id="textarea")
            dialogue_speaker = gr.Textbox(label="πŸ—£οΈ Who says the dialogue?", placeholder="e.g. Bramble", elem_id = "textarea")

        with gr.Row(elem_classes="custom-rows"):
            char_styles = gr.Textbox(label="πŸ§₯ Outfit Descriptions", placeholder="e.g. Elora wears a leafy cloak, Bramble has a warrior vest", elem_id="textarea")
            char_moods = gr.Textbox(label="😠 Character Moods", placeholder="e.g. Elora is cautious, Bramble is brave", elem_id="textarea")
            bg_style = gr.Textbox(label="🎨 Background Style", placeholder="e.g. Realistic, Cartoon, Fantasy, Dark Fantasy" ,elem_id="textarea")
    with gr.Group(elem_id="accordion"):
        with gr.Row(elem_classes="custom-rows"):
            with gr.Row(elem_classes="custom-rows"):
                generate_btn = gr.Button("βž• Generate The Scene", elem_id="generate-btn")
                ai_coauthor_btn = gr.Button("πŸ€– Let AI Write This One", elem_id="ai-write-btn")
            with gr.Row(elem_classes="custom-rows"):
                recreate_btn = gr.Button("πŸ”„ Recreate The Scene", elem_id="recreate-btn")
                reset_btn = gr.Button("\n⏭️ Reset", elem_id="reset-btn")
        with gr.Row(elem_classes="custom-rows"):
            recreate_scene_index = gr.Number(label="πŸ”’ Scene Number to Recreate", precision=0, value=1, elem_id="recreate_scene_index")

    status = gr.Markdown(elem_classes="gr-image")
    image_output = gr.Image(label="πŸ–ΌοΈ Scene Image", type="pil", elem_id="output")
    summary_output = gr.Markdown(label="πŸ“ Scene Summary", elem_id = "output")
    explanation_output = gr.Textbox(label="πŸ“– Scene Explanation", lines=6, elem_id="output")
    with gr.Group(elem_id="accordion"):
        with gr.Row(elem_classes="custom-rows"):
            tts_btn = gr.Button("πŸ”Š Read Aloud", elem_id="tts-btn")
            tts_audio = gr.Audio(label="Audio", autoplay=False,elem_classes="gr-audio")
            
    done_btn = gr.Button("βœ… Done", visible = False, elem_id="done-btn")
    
    with gr.Group(elem_id="accordion2"):
        with gr.Row(elem_classes="custom-rows"):
            txt_file = gr.File(label="πŸ“„ Explanations (.txt)")
            pdf_file = gr.File(label="πŸ“˜ Scene PDF")

    # States
    scene_images = gr.State([])
    scene_explanations = gr.State([])
    scene_summaries = gr.State([])
    current_scene_index = gr.State(0)
    recreate_mode = gr.State(True)

    generate_btn.click(
        fn=generate_scene,
        inputs=[
            scene_total, theme, char_count, character_names, dialogue,
            dialogue_speaker, char_styles, char_moods, bg_style,
            scene_images, scene_summaries, scene_explanations,
            gr.State(False), current_scene_index
        ],
        outputs=[
            image_output, summary_output, explanation_output,
            scene_images, scene_summaries, scene_explanations,
            status, done_btn
        ]
    )


    recreate_btn.click(
        fn=recreate_scene_handler,
        inputs=[
            scene_total, theme, char_count, character_names, dialogue,
            dialogue_speaker, char_styles, char_moods, bg_style,
            scene_images, scene_summaries, scene_explanations,
            recreate_scene_index
        ],
        outputs=[
            image_output, summary_output, explanation_output,
            scene_images, scene_summaries, scene_explanations,
            status, done_btn
        ]
    )


   
    ai_coauthor_btn.click(
        fn=ai_write_scene,
        inputs=[theme, scene_total, scene_summaries],
        outputs=[character_names, dialogue, dialogue_speaker, char_styles, char_moods, bg_style, status]
    )

    done_btn.click(
        fn=finalize_story,
        inputs=[scene_images, scene_explanations, title],
        outputs=[txt_file, pdf_file]
    )

    reset_btn.click(
        fn=reset_fields,
        inputs=[],
        outputs=[
            character_names,
            dialogue,
            dialogue_speaker,
            char_styles,
            char_moods,
            bg_style
        ]
    )


    tts_btn.click(
        fn=text_to_speech,
        inputs=[explanation_output],
        outputs=[tts_audio]
    )
    


demo.launch()