File size: 30,565 Bytes
688c6f3
65eb8e4
688c6f3
 
af8eab3
688c6f3
 
 
 
af8eab3
 
 
 
 
 
 
688c6f3
 
 
 
 
 
 
 
 
af8eab3
 
 
688c6f3
 
 
af8eab3
688c6f3
 
 
 
 
 
af8eab3
688c6f3
 
af8eab3
688c6f3
 
af8eab3
688c6f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af8eab3
688c6f3
 
 
 
 
 
 
 
 
 
af8eab3
688c6f3
 
 
 
 
 
 
 
 
 
 
 
af8eab3
688c6f3
 
 
 
 
 
 
 
 
af8eab3
 
688c6f3
 
 
 
 
af8eab3
688c6f3
 
 
 
 
 
 
af8eab3
688c6f3
 
 
af8eab3
688c6f3
 
 
 
 
 
 
af8eab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
688c6f3
 
5ac732d
688c6f3
 
 
 
 
 
 
 
 
 
 
 
 
 
af8eab3
 
 
 
688c6f3
 
af8eab3
688c6f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af8eab3
688c6f3
 
 
 
 
af8eab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
688c6f3
 
af8eab3
 
 
 
 
 
688c6f3
 
af8eab3
688c6f3
 
 
 
 
 
 
 
 
 
 
af8eab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
688c6f3
 
 
 
 
 
af8eab3
 
 
 
 
 
 
 
 
 
688c6f3
 
 
 
 
 
 
af8eab3
688c6f3
 
 
af8eab3
688c6f3
 
af8eab3
688c6f3
af8eab3
688c6f3
8a8602f
688c6f3
 
 
 
 
 
 
af8eab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
688c6f3
 
 
 
 
 
 
65eb8e4
688c6f3
 
 
 
 
 
 
 
 
 
 
af8eab3
 
 
688c6f3
 
 
af8eab3
688c6f3
 
 
 
 
 
 
 
 
 
af8eab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
688c6f3
 
af8eab3
 
 
688c6f3
 
 
 
 
af8eab3
 
 
 
 
 
 
 
688c6f3
 
 
 
 
af8eab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
688c6f3
 
 
 
 
 
 
 
 
 
af8eab3
 
 
 
 
688c6f3
 
af8eab3
688c6f3
af8eab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
688c6f3
af8eab3
 
 
688c6f3
af8eab3
688c6f3
 
 
65eb8e4
688c6f3
 
 
 
 
 
 
 
 
 
c7e7458
688c6f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af8eab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
688c6f3
 
 
 
c7e7458
688c6f3
 
 
c7e7458
688c6f3
 
 
c7e7458
688c6f3
 
 
65eb8e4
688c6f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ac732d
688c6f3
 
 
 
 
 
 
 
af8eab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
688c6f3
 
 
 
 
 
 
 
 
 
 
af8eab3
 
8a8602f
bfd0106
688c6f3
 
 
 
 
 
 
 
 
93ccd38
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
# app.py
import streamlit as st
import time
import random
import json
import textwrap
import os
import base64
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
import numpy as np
from gtts import gTTS
from pygame import mixer
import tempfile
import requests
from transformers import pipeline, set_seed

# Set up the page
st.set_page_config(
    page_title="CodeTales โœจ",
    page_icon="๐Ÿš€",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Initialize pygame mixer
mixer.init()

# Custom CSS
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=Comic+Neue:wght@700&family=Press+Start+2P&display=swap');
            
.stApp {
    background: linear-gradient(135deg, #6e8efb, #a777e3);
}
            
.header {
    font-family: 'Press Start 2P', cursive;
    color: white;
    text-align: center;
    font-size: 2.5rem;
    text-shadow: 3px 3px 0 #000;
    padding: 1rem;
    letter-spacing: 2px;
}
            
.subheader {
    font-family: 'Comic Neue', cursive;
    color: #ffeb3b;
    text-align: center;
    font-size: 1.8rem;
    margin-bottom: 2rem;
}
            
.story-box {
    background-color: rgba(255, 255, 255, 0.9);
    border-radius: 20px;
    padding: 2rem;
    box-shadow: 0 8px 16px rgba(0,0,0,0.2);
    margin-bottom: 2rem;
    border: 4px solid #ff6b6b;
}
            
.robot-speech {
    background-color: #4caf50;
    color: white;
    border-radius: 20px;
    padding: 1.5rem;
    font-size: 1.2rem;
    position: relative;
    margin-top: 2rem;
    border: 3px solid #2e7d32;
}
            
.robot-speech:after {
    content: '';
    position: absolute;
    top: -20px;
    left: 50px;
    border: 10px solid transparent;
    border-bottom-color: #4caf50;
}
            
.generate-btn {
    background: linear-gradient(135deg, #ff5722, #ff9800) !important;
    color: white !important;
    font-weight: bold !important;
    font-size: 1.2rem !important;
    padding: 0.7rem 2rem !important;
    border-radius: 50px !important;
    border: none !important;
    box-shadow: 0 4px 8px rgba(0,0,0,0.3) !important;
    transition: all 0.3s !important;
    margin-top: 1rem;
    font-family: 'Press Start 2P', cursive !important;
    letter-spacing: 1px;
}
            
.generate-btn:hover {
    transform: scale(1.05) !important;
    box-shadow: 0 6px 12px rgba(0,0,0,0.4) !important;
    background: linear-gradient(135deg, #ff7043, #ffa726) !important;
}
            
.code-block {
    background: #2d2d2d;
    color: #f8f8f2;
    padding: 1rem;
    border-radius: 10px;
    font-family: 'Courier New', monospace;
    font-size: 1.1rem;
    margin: 1rem 0;
    overflow-x: auto;
    border-left: 4px solid #ff9800;
}

.animation-frame {
    border: 3px solid #ffeb3b;
    border-radius: 10px;
    margin: 5px;
}

.achievement-badge {
    background: #ffd54f;
    color: #333;
    border-radius: 50%;
    width: 60px;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    box-shadow: 0 4px 8px rgba(0,0,0,0.2);
    margin: 0 auto;
}

.character-option {
    border: 3px solid transparent;
    border-radius: 10px;
    padding: 5px;
    margin: 5px;
    cursor: pointer;
    transition: all 0.3s;
}

.character-option:hover {
    transform: scale(1.05);
}

.character-option.selected {
    border-color: #ff5722;
}

.tab-content {
    padding: 1rem;
    border: 2px solid #4caf50;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.8);
    margin-top: 1rem;
}

.level-indicator {
    background: #4caf50;
    color: white;
    border-radius: 20px;
    padding: 0.5rem 1rem;
    display: inline-block;
    font-weight: bold;
    margin-bottom: 1rem;
}

/* Progress bar styling */
.stProgress > div > div > div {
    background-color: #ff5722 !important;
}
</style>
""", unsafe_allow_html=True)

# Initialize AI models
@st.cache_resource
def load_models():
    """Load open-source AI models"""
    try:
        # Text generation model for code explanations
        story_to_code = pipeline("text-generation", model="gpt2", max_length=100)
        
        # Sentiment analysis for story understanding
        sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
        
        # Named entity recognition for identifying objects
        ner_model = pipeline("ner", model="dbmdz/bert-large-cased-finetuned-conll03-english")
        
        # Text-to-text generation for better explanations
        explanation_generator = pipeline("text2text-generation", model="google/flan-t5-base")
        
        return story_to_code, sentiment_analyzer, ner_model, explanation_generator
    except Exception as e:
        st.error(f"Error loading models: {e}")
        return None, None, None, None

# Image generation functions
def create_storyboard_image(text, width=400, height=300):
    """Create a storyboard image from text"""
    # Create blank image
    img = Image.new('RGB', (width, height), color=(25, 25, 112))  # Dark blue background
    
    # Load a comic-style font (fallback to default if not available)
    try:
        font = ImageFont.truetype("comic.ttf", 16)
    except:
        font = ImageFont.load_default()
    
    draw = ImageDraw.Draw(img)
    
    # Draw title
    draw.text((10, 10), "Your Story Comes to Life!", fill=(255, 215, 0), font=font)
    
    # Draw text box
    draw.rectangle([10, 40, width-10, height-40], fill=(240, 248, 255), outline=(255, 215, 0), width=2)
    
    # Wrap text
    wrapped_text = textwrap.fill(text, width=40)
    draw.text((20, 50), wrapped_text, fill=(25, 25, 112), font=font)
    
    # Draw decorations
    draw.rectangle([width-50, height-30, width-30, height-10], fill=(220, 20, 60), outline=(255, 215, 0), width=1)
    draw.ellipse([20, height-50, 70, height], fill=(30, 144, 255), outline=(255, 215, 0), width=1)
    
    return img

def generate_sprite_animation(story, character="spaceship", theme="space", num_frames=4):
    """Generate a sprite-based animation from story"""
    frames = []
    width, height = 300, 200
    
    for i in range(num_frames):
        # Create base image with theme
        if theme == "space":
            bg_color = (0, 0, 30)
            star_color = (255, 255, 255)
        elif theme == "jungle":
            bg_color = (0, 100, 0)
            star_color = None  # No stars in jungle
        elif theme == "medieval":
            bg_color = (139, 69, 19)
            star_color = None
        else:
            bg_color = (0, 0, 30)
            star_color = (255, 255, 255)
            
        img = Image.new('RGB', (width, height), color=bg_color)
        draw = ImageDraw.Draw(img)
        
        # Draw stars for space theme
        if star_color:
            for _ in range(30):
                x = random.randint(0, width)
                y = random.randint(0, height)
                draw.ellipse([x, y, x+2, y+2], fill=star_color)
        
        # Draw moving elements based on frame
        if character == "spaceship":
            ship_x = 50 + i * 60
            ship_y = 80
            draw.polygon([(ship_x, ship_y), (ship_x+30, ship_y), 
                          (ship_x+15, ship_y-20)], fill=(169, 169, 169))
            
            if "shoot" in story.lower() and i > 1:
                for j in range(3):
                    laser_x = ship_x + 15
                    laser_y = ship_y - 20 + j*5
                    draw.line([(laser_x, laser_y), (width, laser_y)], fill=(255, 0, 0), width=2)
        
        elif character == "dragon":
            dragon_x = 50 + i * 40
            dragon_y = 100
            # Draw dragon body
            draw.ellipse([dragon_x, dragon_y, dragon_x+40, dragon_y+20], fill=(178, 34, 34))
            # Draw dragon head
            draw.ellipse([dragon_x+30, dragon_y-5, dragon_x+50, dragon_y+15], fill=(178, 34, 34))
            # Draw wings
            draw.ellipse([dragon_x+10, dragon_y-15, dragon_x+30, dragon_y], fill=(138, 43, 226))
            
            if "fire" in story.lower() and i > 0:
                for j in range(5):
                    flame_x = dragon_x + 50
                    flame_y = dragon_y + 5 - j*5
                    flame_size = random.randint(5, 15)
                    draw.ellipse([flame_x, flame_y, flame_x+flame_size, flame_y+flame_size], 
                                fill=(255, random.randint(100, 200), 0))
        
        elif character == "knight":
            knight_x = 50 + i * 40
            knight_y = 120
            # Draw knight body
            draw.rectangle([knight_x, knight_y, knight_x+20, knight_y+40], fill=(70, 70, 70))
            # Draw knight head
            draw.ellipse([knight_x+5, knight_y-15, knight_x+15, knight_y-5], fill=(210, 180, 140))
            # Draw sword
            draw.rectangle([knight_x+15, knight_y+10, knight_x+25, knight_y+15], fill=(192, 192, 192))
            draw.polygon([(knight_x+25, knight_y+12), (knight_x+35, knight_y+10), (knight_x+35, knight_y+15)], fill=(192, 192, 192))
            
            if "attack" in story.lower() and i % 2 == 1:
                # Draw sword swing
                draw.line([(knight_x+25, knight_y+12), (knight_x+45, knight_y-10)], fill=(255, 255, 0), width=2)
        
        # Draw enemies based on theme
        if theme == "space" and "alien" in story.lower():
            alien_x = 200
            alien_y = 100 - i*10
            draw.ellipse([alien_x, alien_y, alien_x+20, alien_y+20], fill=(50, 205, 50))
            draw.ellipse([alien_x+5, alien_y+5, alien_x+7, alien_y+7], fill=(0, 0, 0))
            draw.ellipse([alien_x+13, alien_y+5, alien_x+15, alien_y+7], fill=(0, 0, 0))
        
        elif theme == "jungle" and "snake" in story.lower():
            snake_x = 200
            snake_y = 150 - i*5
            for segment in range(5):
                offset = segment * 5
                draw.ellipse([snake_x+offset, snake_y+offset, snake_x+offset+15, snake_y+offset+15], fill=(0, 128, 0))
        
        elif theme == "medieval" and "dragon" in story.lower() and character != "dragon":
            dragon_x = 220
            dragon_y = 80
            draw.ellipse([dragon_x, dragon_y, dragon_x+40, dragon_y+20], fill=(178, 34, 34))
            draw.line([(dragon_x+40, dragon_y+10), (dragon_x+60, dragon_y)], fill=(178, 34, 34), width=3)
        
        frames.append(img)
    
    return frames

def generate_code_explanation(story, explanation_generator):
    """Generate code explanation using open-source model"""
    try:
        # Create a prompt for the model
        prompt = f"Explain to a child how this story would become code: '{story}'"
        
        # Generate text
        result = explanation_generator(
            prompt, 
            max_length=200,
            num_return_sequences=1,
        )
        
        return result[0]['generated_text']
    except:
        # Fallback explanation if model fails
        return f"""See how your story became real code? For example, when you wrote "{story.split()[0]}", 
we used code like: character.move(). That's how we turn words into actions!"""

def text_to_speech(text, lang='en'):
    """Convert text to speech using gTTS"""
    try:
        tts = gTTS(text=text, lang=lang, slow=False)
        with tempfile.NamedTemporaryFile(delete=True, suffix='.mp3') as fp:
            tts.save(fp.name)
            return fp.name
    except Exception as e:
        st.error(f"Text-to-speech error: {e}")
        return None

# Achievement system
ACHIEVEMENTS = {
    "first_story": {"name": "Storyteller", "icon": "๐Ÿ“–", "description": "Created your first story!"},
    "code_master": {"name": "Code Master", "icon": "๐Ÿ’ป", "description": "Used 5 different coding concepts"},
    "animator": {"name": "Animator", "icon": "๐ŸŽฌ", "description": "Created 3 animations"},
    "voice_artist": {"name": "Voice Artist", "icon": "๐ŸŽค", "description": "Used text-to-speech feature"},
    "character_designer": {"name": "Character Designer", "icon": "๐Ÿ‘พ", "description": "Created a custom character"}
}

def unlock_achievement(achievement_id):
    """Unlock an achievement for the user"""
    if achievement_id not in st.session_state.achievements_unlocked:
        st.session_state.achievements_unlocked.append(achievement_id)
        st.session_state.new_achievement = achievement_id
        return True
    return False

# Header section
st.markdown('<div class="header">CodeTales โœจ</div>', unsafe_allow_html=True)
st.markdown('<div class="subheader">Storytime + Coding Magic</div>', unsafe_allow_html=True)
st.markdown('<div style="text-align:center; color:white; font-size:1.2rem; margin-bottom:2rem;">Turn wild stories into playable games with open-source AI magic!</div>', unsafe_allow_html=True)

# How it works section
with st.expander("โœจ How It Works (Like Baking a Cake) ๐ŸŽ‚"):
    st.markdown("""
    **1. Kids Write a Story (The Recipe)**  
    Example: *"The spaceship zooms past aliens and shoots lasers!"*
    
    **2. AI is the Magic Oven ๐Ÿ”ฎ**  
    We turn words into code using open-source AI models
    
    **3. Animation Pops Out (The Cake!) ๐ŸŽฎ**  
    See your story come to life with custom animations!
    
    **4. Robot Teacher Explains ๐Ÿค–**  
    Tavus shows: *"See? 'spaceship.move_right()' makes it fly! That's coding!"*
    
    **5. Level Up! ๐Ÿ†**  
    Earn achievements as you learn and create!
    """)

# Load models
story_to_code, sentiment_analyzer, ner_model, explanation_generator = load_models()

# Initialize session state
if 'animation_generated' not in st.session_state:
    st.session_state.animation_generated = False
if 'story_text' not in st.session_state:
    st.session_state.story_text = ""
if 'animation_frames' not in st.session_state:
    st.session_state.animation_frames = []
if 'code_explanation' not in st.session_state:
    st.session_state.code_explanation = ""
if 'selected_character' not in st.session_state:
    st.session_state.selected_character = "spaceship"
if 'selected_theme' not in st.session_state:
    st.session_state.selected_theme = "space"
if 'sound_enabled' not in st.session_state:
    st.session_state.sound_enabled = True
if 'achievements_unlocked' not in st.session_state:
    st.session_state.achievements_unlocked = []
if 'new_achievement' not in st.session_state:
    st.session_state.new_achievement = None
if 'story_count' not in st.session_state:
    st.session_state.story_count = 0
if 'level' not in st.session_state:
    st.session_state.level = 1
if 'xp' not in st.session_state:
    st.session_state.xp = 0
if 'voice_enabled' not in st.session_state:
    st.session_state.voice_enabled = False
if 'custom_character' not in st.session_state:
    st.session_state.custom_character = None

# Story templates
story_templates = {
    "None": "",
    "Space Adventure": "A brave spaceship zooms through space, shooting lasers at alien spaceships!",
    "Dragon Quest": "A knight fights a fire-breathing dragon to save the princess in the castle.",
    "Jungle Explorer": "An explorer runs through the jungle, jumping over snakes and swinging on vines."
}

# Main content
st.markdown(f'<div class="level-indicator">Level {st.session_state.level} โœจ XP: {st.session_state.xp}/100</div>', unsafe_allow_html=True)
progress = st.progress(st.session_state.xp / 100)

col1, col2 = st.columns([1, 1])

with col1:
    st.markdown('<div class="story-box">', unsafe_allow_html=True)
    st.markdown("### ๐Ÿ“– Write Your Story Here:")
    
    # Story template selector
    selected_template = st.selectbox(
        "Or choose a story template to start with:",
        list(story_templates.keys()),
        index=0
    )
    
    story_text = st.text_area(
        "Tell your adventure story...", 
        height=200,
        placeholder="Once upon a time, a brave spaceship zoomed through space, shooting lasers at alien spaceships...",
        label_visibility="collapsed",
        value=story_templates[selected_template] if selected_template != "None" else st.session_state.story_text
    )
    
    # Character selection
    st.markdown("### ๐Ÿง™ Choose Your Hero")
    char_cols = st.columns(3)
    characters = {
        "spaceship": "๐Ÿš€ Spaceship",
        "dragon": "๐Ÿ‰ Dragon",
        "knight": "๐Ÿ›ก๏ธ Knight"
    }
    
    for i, (char_key, char_label) in enumerate(characters.items()):
        with char_cols[i]:
            if st.button(
                char_label,
                key=f"char_{char_key}",
                use_container_width=True,
                type="primary" if st.session_state.selected_character == char_key else "secondary"
            ):
                st.session_state.selected_character = char_key
    
    # Theme selection
    st.markdown("### ๐ŸŽจ Choose Your World")
    theme_cols = st.columns(3)
    themes = {
        "space": "๐ŸŒŒ Space",
        "jungle": "๐ŸŒฟ Jungle",
        "medieval": "๐Ÿฐ Medieval"
    }
    
    for i, (theme_key, theme_label) in enumerate(themes.items()):
        with theme_cols[i]:
            if st.button(
                theme_label,
                key=f"theme_{theme_key}",
                use_container_width=True,
                type="primary" if st.session_state.selected_theme == theme_key else "secondary"
            ):
                st.session_state.selected_theme = theme_key
    
    # Voice options
    st.session_state.voice_enabled = st.toggle(
        "๐Ÿ—ฃ๏ธ Enable Tavus Voice", 
        value=st.session_state.voice_enabled,
        help="Hear Tavus explain coding concepts with text-to-speech"
    )
    
    # Generate button with animation
    if st.button("โœจ Generate Animation!", use_container_width=True, key="generate", type="primary"):
        if story_text.strip():
            st.session_state.story_text = story_text
            st.session_state.animation_generated = True
            
            with st.spinner("๐Ÿง™โ€โ™‚๏ธ Cooking your story in the magic oven..."):
                # Generate animation frames
                st.session_state.animation_frames = generate_sprite_animation(
                    story_text, 
                    character=st.session_state.selected_character,
                    theme=st.session_state.selected_theme
                )
                
                # Generate code explanation
                st.session_state.code_explanation = generate_code_explanation(story_text, explanation_generator)
                
                # Update user progress
                st.session_state.story_count += 1
                st.session_state.xp += 15
                
                # Check for level up
                if st.session_state.xp >= 100:
                    st.session_state.level += 1
                    st.session_state.xp = st.session_state.xp - 100
                    st.session_state.new_level = True
                
                # Unlock achievements
                if st.session_state.story_count == 1:
                    unlock_achievement("first_story")
                
                # Generate speech if enabled
                if st.session_state.voice_enabled:
                    speech_file = text_to_speech(st.session_state.code_explanation)
                    if speech_file:
                        st.session_state.speech_file = speech_file
                        unlock_achievement("voice_artist")
                
                # Simulate sound effect
                if st.session_state.sound_enabled:
                    st.session_state.sound_message = "๐Ÿ”Š Sound effects added: Laser blasts, explosions, and more!"
                else:
                    st.session_state.sound_message = ""
        else:
            st.warning("Please enter a story first!")
    st.markdown('</div>', unsafe_allow_html=True)

with col2:
    st.markdown('<div class="story-box">', unsafe_allow_html=True)
    st.markdown("### ๐ŸŽฎ Your Game Animation")
    
    if st.session_state.animation_generated and st.session_state.animation_frames:
        # Display animation frames
        st.markdown("**Your Story Comes to Life!**")
        cols = st.columns(len(st.session_state.animation_frames))
        for i, frame in enumerate(st.session_state.animation_frames):
            with cols[i]:
                st.image(frame, caption=f"Frame {i+1}", use_container_width=True)
        
        # Create an animated GIF
        gif_buffer = BytesIO()
        st.session_state.animation_frames[0].save(
            gif_buffer,
            format='GIF',
            save_all=True,
            append_images=st.session_state.animation_frames[1:],
            duration=500,
            loop=0
        )
        gif_data = gif_buffer.getvalue()
        st.download_button(
            label="โฌ‡๏ธ Download Animation",
            data=gif_data,
            file_name="your_story.gif",
            mime="image/gif",
            use_container_width=True
        )
        
        # Display sound message
        if st.session_state.sound_enabled and 'sound_message' in st.session_state:
            st.info(st.session_state.sound_message)
        
        # Display character and theme info
        st.success(f"โœจ Your {characters[st.session_state.selected_character]} in the {themes[st.session_state.selected_theme]} world!")
        
        # Play voice explanation
        if st.session_state.voice_enabled and 'speech_file' in st.session_state:
            if st.button("๐Ÿ”Š Play Tavus Explanation", use_container_width=True):
                try:
                    mixer.music.load(st.session_state.speech_file)
                    mixer.music.play()
                except:
                    st.warning("Couldn't play audio. Try enabling sound in your browser.")
        
    elif st.session_state.animation_generated:
        st.warning("Couldn't generate animation. Try a different story!")
        st.image("https://img.freepik.com/free-vector/hand-drawn-colorful-space-background_23-2148821798.jpg", 
                 use_container_width=True)
    elif story_text:
        # Show storyboard preview
        preview_img = create_storyboard_image(story_text)
        st.image(preview_img, caption="Your Story Preview", use_container_width=True)
        st.info("๐Ÿ‘† Click Generate to bring your story to life!")
    else:
        st.image("https://img.freepik.com/free-vector/hand-drawn-colorful-space-background_23-2148821798.jpg", 
                 use_container_width=True)
        st.info("๐Ÿ‘† Write your story and click Generate to see the magic!")
    
    st.markdown('</div>', unsafe_allow_html=True)

# Tavus explanation section
if st.session_state.animation_generated and st.session_state.story_text:
    st.markdown('<div class="robot-speech">', unsafe_allow_html=True)
    st.markdown("### ๐Ÿค– Tavus the Robot Teacher says:")
    
    # Extract action words from story
    action_words = ["zoom", "shoot", "fly", "move", "jump", "run", "attack", "laser", "alien", "spaceship", 
                   "dragon", "fire", "hero", "sword", "castle", "run", "escape", "fight", "win"]
    found_actions = [word for word in action_words if word in st.session_state.story_text.lower()]
    
    if found_actions:
        # Create explanation based on found words
        explanations = []
        code_snippets = []
        
        for action in found_actions[:3]:  # Limit to 3 explanations
            if action == "zoom":
                explanations.append("makes your spaceship move super fast!")
                code_snippets.append("spaceship.accelerate(speed=10)")
            elif action == "shoot":
                explanations.append("creates laser beams to defeat enemies!")
                code_snippets.append("laser = spaceship.fire_weapon()")
            elif action == "fly":
                explanations.append("keeps your character moving through the air!")
                code_snippets.append("character.apply_gravity(False)")
            elif action == "move":
                explanations.append("changes position on the screen!")
                code_snippets.append("player.move(direction='right')")
            elif action == "jump":
                explanations.append("makes your character leap upwards!")
                code_snippets.append("hero.jump(height=100)")
            elif action == "run":
                explanations.append("makes your character move faster!")
                code_snippets.append("player.speed = player.speed * 2")
            elif action == "attack":
                explanations.append("lets your hero fight the bad guys!")
                code_snippets.append("sword.swing(damage=15)")
            elif action == "laser":
                explanations.append("creates powerful energy beams!")
                code_snippets.append("weapon = Laser(color='red', damage=20)")
            elif action == "alien":
                explanations.append("adds enemies to your game!")
                code_snippets.append("enemy = Alien(position=(100, 200))")
            elif action == "spaceship":
                explanations.append("creates your main character!")
                code_snippets.append("player = Spaceship(image='spaceship.png')")
            elif action == "dragon":
                explanations.append("adds a fire-breathing challenge!")
                code_snippets.append("boss = Dragon(health=100, damage=25)")
            elif action == "fire":
                explanations.append("creates dangerous fire effects!")
                code_snippets.append("flame = FireEffect(position, size=30)")
            elif action == "hero":
                explanations.append("creates the main player character!")
                code_snippets.append("player = Hero(name='Your Hero')")
            elif action == "castle":
                explanations.append("adds a majestic building to your world!")
                code_snippets.append("castle = Building(type='castle', position=(300, 150))")
            elif action == "escape":
                explanations.append("creates exciting chase sequences!")
                code_snippets.append("player.start_escape_sequence(speed=15)")
            elif action == "fight":
                explanations.append("initiates battle mechanics!")
                code_snippets.append("initiate_combat(enemy)")
            elif action == "win":
                explanations.append("creates victory conditions!")
                code_snippets.append("if player.score > 100: show_victory_screen()")
        
        st.markdown("See how your story became real code? Here's what happened:")
        
        for i, (action, explanation, snippet) in enumerate(zip(found_actions, explanations, code_snippets)):
            st.markdown(f"**{i+1}. When you said '{action}'**:")
            st.markdown(f'<div class="code-block">{snippet}</div>', unsafe_allow_html=True)
            st.markdown(f"This code {explanation}")
        
        st.markdown("That's the magic of coding - turning words into actions!")
    else:
        st.markdown("I see you created an awesome story! Every word you write can become game code. Try adding action words like 'jump', 'run', or 'shoot' next time!")
    
    # Show AI-generated explanation
    st.markdown("### ๐Ÿง  AI-Powered Explanation:")
    st.write(st.session_state.code_explanation)
    
    st.markdown("</div>", unsafe_allow_html=True)

# Achievements section
if st.session_state.achievements_unlocked:
    st.markdown("### ๐Ÿ† Your Achievements")
    achievement_cols = st.columns(5)
    for i, ach_id in enumerate(st.session_state.achievements_unlocked):
        with achievement_cols[i % 5]:
            ach = ACHIEVEMENTS[ach_id]
            st.markdown(f'<div class="achievement-badge">{ach["icon"]}</div>', unsafe_allow_html=True)
            st.markdown(f"**{ach['name']}**")
            st.caption(ach['description'])

# New achievement notification
if 'new_achievement' in st.session_state and st.session_state.new_achievement:
    ach = ACHIEVEMENTS[st.session_state.new_achievement]
    st.success(f"๐ŸŽ‰ Achievement Unlocked: {ach['name']} - {ach['description']}")
    st.session_state.new_achievement = None

# New level notification
if 'new_level' in st.session_state and st.session_state.new_level:
    st.balloons()
    st.success(f"๐ŸŒŸ Level Up! You're now Level {st.session_state.level}!")
    st.session_state.new_level = False

# Coding challenges section
st.markdown("### ๐Ÿ’ป Coding Challenges")
with st.expander("Complete challenges to earn XP!"):
    challenge_cols = st.columns(3)
    
    with challenge_cols[0]:
        st.markdown("#### Challenge 1: The Mover")
        st.code("""
# Make the character move to the right
character.move_right(10)
        """)
        if st.button("Run Challenge 1", key="challenge1"):
            st.session_state.xp += 5
            st.success("+5 XP! Character moved right!")
    
    with challenge_cols[1]:
        st.markdown("#### Challenge 2: The Jumper")
        st.code("""
# Make the character jump
character.jump(20)
        """)
        if st.button("Run Challenge 2", key="challenge2"):
            st.session_state.xp += 5
            st.success("+5 XP! Character jumped!")
    
    with challenge_cols[2]:
        st.markdown("#### Challenge 3: The Shooter")
        st.code("""
# Make the character shoot a laser
laser = character.shoot()
        """)
        if st.button("Run Challenge 3", key="challenge3"):
            st.session_state.xp += 5
            st.success("+5 XP! Laser fired!")

# Benefits section
st.markdown("""
## โค Why Everyone Will Love CodeTales

| For Kids ๐Ÿ‘ง๐Ÿ‘ฆ | For Parents & Teachers ๐Ÿ‘ช๐Ÿ‘ฉโ€๐Ÿซ |
|--------------|----------------------------|
| โœจ Feels like playing, not learning | ๐Ÿง  Secretly teaches programming concepts |
| ๐Ÿš€ Brings imagination to life | ๐Ÿ” Develops logical thinking skills |
| ๐ŸŽฎ Creates personal video games | โž— Reinforces math fundamentals |
| ๐Ÿ˜„ Makes learning fun and exciting | ๐Ÿงฉ Encourages problem-solving abilities |
| ๐ŸŒŸ 100% private with no API keys | ๐Ÿ’ฐ Completely free and open-source |
| ๐Ÿ† Achievement system motivates learning | ๐Ÿ“ˆ Progress tracking shows growth |
| ๐Ÿ—ฃ๏ธ Voice explanations for accessibility | ๐ŸŽจ Creative expression through stories |
""")

# Footer
st.markdown("---")
st.markdown("""
<center>
    <p style="color:white; font-size:1.1rem;">
        โœจ Made with open-source magic by CodeTales Team โœจ<br>
        Transforming stories into games since 2023 | No API Keys Required!
    </p>
</center>
""", unsafe_allow_html=True)