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0c
2253319
 
818a1f3
d8c8d0c
 
2253319
 
 
 
 
 
d8c8d0c
2253319
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
from agent import ReadingCoachAgent

# Create a single instance of the agent
reading_coach = ReadingCoachAgent()
session = {"story": "", "name": "", "grade": "", "progress": 0, "last_feedback": "", "practice_count": 0}

# Define theme colors (Duolingo-inspired)
PRIMARY_COLOR = "#58CC02"  # Green
SECONDARY_COLOR = "#FFC800"  # Yellow
ACCENT_COLOR = "#FF4B4B"  # Red
BG_COLOR = "#F7F7F7"  # Light gray

# Custom CSS for more professional styling
custom_css = """
:root {
    --primary-color: #58CC02;
    --secondary-color: #FFC800;
    --accent-color: #FF4B4B;
    --neutral-color: #4B4B4B;
    --light-bg: #F7F7F7;
    --white: #FFFFFF;
    --border-radius: 16px;
    --shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.container {
    font-family: 'Nunito', sans-serif;
    max-width: 900px;
    margin: 0 auto;
    font-size: 0.95rem;
}

.header {
    text-align: center;
    margin-bottom: 2rem;
    color: var(--neutral-color);
}

.app-title {
    color: var(--primary-color);
    font-size: 2.2rem;
    font-weight: 800;
    margin: 0;
}

.card {
    background: var(--white);
    border-radius: var(--border-radius);
    padding: 1.5rem;
    box-shadow: var(--shadow);
    margin-bottom: 1.5rem;
}

.btn-primary {
    background: var(--primary-color) !important;
    color: var(--white) !important;
    font-weight: bold !important;
    border: none !important;
    padding: 0.75rem 1.5rem !important;
    border-radius: 50px !important;
    cursor: pointer !important;
    transition: transform 0.1s, box-shadow 0.1s, background-color 0.2s !important;
    box-shadow: 0 4px 0 #48a700 !important;
}

.btn-primary:hover {
    background: #62d40a !important;
}

.btn-primary:active {
    background: #4ab000 !important;
    transform: translateY(2px) !important;
    box-shadow: 0 2px 0 #3e9500 !important;
}

.btn-secondary {
    background: var(--secondary-color) !important;
    color: var(--neutral-color) !important;
    font-weight: bold !important;
    border: none !important;
    padding: 0.75rem 1.5rem !important;
    border-radius: 50px !important;
    cursor: pointer !important;
    transition: transform 0.1s, box-shadow 0.1s, background-color 0.2s !important;
    box-shadow: 0 4px 0 #e0b000 !important;
}

.btn-secondary:hover {
    background: #ffd119 !important;
}

.btn-secondary:active {
    background: #e0b000 !important;
    transform: translateY(2px) !important;
    box-shadow: 0 2px 0 #c69e00 !important;
}

.btn-practice {
    background: #9333ea !important;
    color: var(--white) !important;
    font-weight: bold !important;
    border: none !important;
    padding: 0.75rem 1.5rem !important;
    border-radius: 50px !important;
    cursor: pointer !important;
    transition: transform 0.1s, box-shadow 0.1s, background-color 0.2s !important;
    box-shadow: 0 4px 0 #7c2d9c !important;
}

.btn-practice:hover {
    background: #a346f5 !important;
}

.btn-practice:active {
    background: #7e2bd0 !important;
    transform: translateY(2px) !important;
    box-shadow: 0 2px 0 #6b228b !important;
}

.btn-clear {
    background: var(--accent-color) !important;
    color: var(--white) !important;
    font-weight: bold !important;
    border: none !important;
    padding: 0.5rem 1rem !important;
    border-radius: 25px !important;
    cursor: pointer !important;
    transition: transform 0.1s, box-shadow 0.1s, background-color 0.2s !important;
    box-shadow: 0 2px 0 #d93636 !important;
}

.btn-clear:hover {
    background: #ff5f5f !important;
}

.btn-clear:active {
    background: #e43a3a !important;
    transform: translateY(1px) !important;
    box-shadow: 0 1px 0 #c52e2e !important;
}

.progress-container {
    width: 100%;
    background-color: #e0e0e0;
    border-radius: 50px;
    margin: 1rem 0;
    height: 10px;
}

.progress-bar {
    background-color: var(--primary-color);
    height: 100%;
    border-radius: 50px;
    transition: width 0.3s ease;
}

.feedback-area {
    background: #f8f9fa;
    border: 2px solid #e9ecef;
    border-radius: 12px;
    padding: 1rem;
    margin: 1rem 0;
    font-family: 'Consolas', 'Monaco', monospace;
    font-size: 0.9rem;
    line-height: 1.6;
    white-space: pre-wrap;
}

.practice-info {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 1rem;
    border-radius: 12px;
    margin: 1rem 0;
    text-align: center;
}

input, textarea {
    border: 2px solid #e0e0e0 !important;
    border-radius: var(--border-radius) !important;
    padding: 10px !important;
    font-size: 14px !important;
}

input:focus, textarea:focus {
    border-color: var(--primary-color) !important;
    outline: none !important;
}

@media (max-width: 768px) {
    .card {
        padding: 1rem;
    }
}
"""

def start_session(name, grade, topic):
    """Generate a new story based on name, grade, and topic"""
    if not name.strip() or not grade.strip() or not topic.strip():
        return "Please fill in all fields", gr.update(visible=False), gr.update(visible=False)
    
    try:
        # Store session data
        session["name"] = name
        session["grade"] = grade
        session["practice_count"] = 0
        
        # Clear previous session
        reading_coach.clear_session()
        
        # Generate the story using the correct method
        generated_story = reading_coach.generate_story_for_student(name, grade, topic)
        session["story"] = generated_story
        session["progress"] = 33
        
        # Print for debugging
        print(f"Generated story: {session['story'][:50]}...")
        
        # Return the story and make practice card visible
        return session["story"], gr.update(visible=True), gr.update(visible=False)
        
    except Exception as e:
        print(f"Error in start_session: {e}")
        # Provide a fallback story if generation fails
        fallback = f"Once upon a time, {name} went on an adventure to learn about {topic}..."
        session["story"] = fallback
        return fallback, gr.update(visible=True), gr.update(visible=False)

def generate_audio():
    """Generate audio for the current story"""
    try:
        if not session.get("story"):
            return None
            
        print("Generating audio for story...")
        audio_path = reading_coach.create_audio_from_story(session["story"])
        
        if audio_path:
            session["progress"] = 66
            print(f"Audio generated successfully: {audio_path}")
            print(f"Audio path type: {type(audio_path)}")
            
            # Ensure the path exists and is accessible
            if os.path.exists(audio_path):
                print(f"Audio file exists at: {audio_path}")
                return audio_path
            else:
                print(f"Audio file does not exist at: {audio_path}")
                return None
        else:
            print("No audio path returned from TTS")
            # Instead of returning None, we could return a message or skip audio
            return None
            
    except Exception as e:
        print(f"Error in generate_audio: {e}")
        import traceback
        traceback.print_exc()
        return None

def submit_reading(audio):
    """Process the student's reading and provide comprehensive agentic feedback"""
    try:
        # Handle various audio input formats
        if audio is None:
            return "Please record your reading first.", gr.update(visible=False)
        
        # Debug: Print what we received
        print(f"Received audio input: {type(audio)} - {str(audio)[:100]}...")
        
        # Pass audio directly to the agent - let STT handle the format conversion
        transcribed, feedback, accuracy = reading_coach.analyze_student_reading(audio)
        
        # Store feedback for potential story generation
        session["last_feedback"] = feedback
        session["progress"] = 100
        session["practice_count"] += 1
        
        # Always show practice story section so students can get more challenging stories
        show_practice = True
        
        return feedback, gr.update(visible=show_practice)
        
    except Exception as e:
        print(f"Error in submit_reading: {e}")
        import traceback
        traceback.print_exc()
        return "There was an error processing your reading. Please try again.", gr.update(visible=False)

def generate_practice_story():
    """Generate a new targeted story based on previous feedback"""
    try:
        if not session.get("name") or not session.get("grade"):
            return "Please complete a reading session first to get a personalized practice story.", ""
        
        # Generate targeted story using the correct method
        new_story = reading_coach.generate_practice_story(session["name"], session["grade"])
        
        # Update session with new story
        session["story"] = new_story
        session["progress"] = 33
        session["practice_count"] += 1
        
        # Clear previous feedback
        session["last_feedback"] = ""
        
        # Create adaptive message based on practice count and performance
        if session["practice_count"] == 1:
            practice_msg = f"🌟 New Challenge Story Generated!\nThis story is tailored to help you continue growing as a reader."
        else:
            practice_msg = f"πŸš€ Challenge Story #{session['practice_count']} Ready!\nKeep pushing yourself - you're doing amazing!"
        
        return new_story, practice_msg
        
    except Exception as e:
        print(f"Error generating practice story: {e}")
        return "There was an error generating a new practice story. Please try again.", ""



def clear_all_audio():
    """Clear all audio components and reset audio session"""
    return None, None, "πŸ”„ All audio cleared!"

def reset_session():
    """Reset the session to start over"""
    session.clear()
    session.update({"story": "", "name": "", "grade": "", "progress": 0, "last_feedback": "", "practice_count": 0})
    reading_coach.reset_all_data()
    
    return (
        gr.update(value=""), 
        gr.update(value=""), 
        gr.update(value=""), 
        gr.update(value=""),
        gr.update(value=None),
        gr.update(value=""),
        gr.update(value=""),
        gr.update(visible=False),
        gr.update(visible=False),
        0
    )

def clear_recording():
    """Clear the recorded audio"""
    return None

def record_again():
    """Reset recording for a new attempt"""
    return None

def update_progress_bar(progress):
    """Update the progress bar width based on progress percentage"""
    return f"<div class='progress-container'><div class='progress-bar' style='width: {progress}%'></div></div>"

def launch_ui():
    with gr.Blocks(css=custom_css, title="ReadRight - AI Reading Coach") as demo:
        with gr.Column(elem_classes="container"):
            # Header
            gr.HTML("""
                <div class="header">
                    <h1 class="app-title">πŸ¦‰ ReadRight</h1>
                    <p>AI-powered reading coach for kids</p>
                </div>
            """)
            
            # Progress tracker
            progress_bar = gr.HTML(update_progress_bar(0), elem_id="progress-bar")
            
            # Step 1: Story Setup
            with gr.Column(elem_classes="card") as setup_card:
                gr.HTML("<h2>🌟 Let's Create Your Personalized Reading Adventure!</h2>")
                
                with gr.Row():
                    with gr.Column(scale=2):
                        with gr.Row():
                            name = gr.Text(label="πŸ‘€ Your Name", placeholder="Enter your name...")
                        
                        with gr.Row():
                            grade = gr.Dropdown(
                                label="πŸ“š Grade Level",
                                choices=["Grade 1", "Grade 2", "Grade 3", "Grade 4", "Grade 5", "Grade 6", "Grade 7", "Grade 8", "Grade 9", "Grade 10"],
                                value="Grade 1",
                                allow_custom_value=True
                            )
                        
                        with gr.Row():
                            topic = gr.Text(label="🌟 Story Topic", placeholder="e.g., space adventure, friendly dinosaurs, ocean exploration...")
                        
                        with gr.Row():
                            btn_start = gr.Button("πŸš€ Create My Story", elem_classes="btn-primary")
                    
                    with gr.Column(scale=1):
                        gr.HTML("""
                            <div style="background: linear-gradient(135deg, var(--primary-color) 0%, #48a700 100%); 
                                        color: white; padding: 1.5rem; border-radius: var(--border-radius); 
                                        margin-left: 1rem; height: fit-content; box-shadow: var(--shadow);">
                                <h3 style="margin-top: 0; color: white; font-size: 1.1rem; font-weight: 700;">πŸ“š How to Use ReadRight</h3>
                                <div style="font-size: 0.85rem; line-height: 1.5;">
                                    <p style="margin: 0.5rem 0;"><span style="background: rgba(255,255,255,0.2); padding: 2px 8px; border-radius: 12px; font-weight: 600;">Step 1:</span> Enter your name, grade, and choose a fun story topic</p>
                                    <p style="margin: 0.5rem 0;"><span style="background: rgba(255,255,255,0.2); padding: 2px 8px; border-radius: 12px; font-weight: 600;">Step 2:</span> Click "Create My Story" to generate your personalized reading adventure</p>
                                    <p style="margin: 0.5rem 0;"><span style="background: rgba(255,255,255,0.2); padding: 2px 8px; border-radius: 12px; font-weight: 600;">Step 3:</span> Listen to the story first by clicking "Listen to Story"</p>
                                    <p style="margin: 0.5rem 0;"><span style="background: rgba(255,255,255,0.2); padding: 2px 8px; border-radius: 12px; font-weight: 600;">Step 4:</span> Record yourself reading the story aloud</p>
                                    <p style="margin: 0.5rem 0;"><span style="background: rgba(255,255,255,0.2); padding: 2px 8px; border-radius: 12px; font-weight: 600;">Step 5:</span> Get personalized feedback from your AI reading coach</p>
                                    <p style="margin: 0.5rem 0;"><span style="background: rgba(255,255,255,0.2); padding: 2px 8px; border-radius: 12px; font-weight: 600;">Step 6:</span> Practice with targeted stories if needed!</p>
                                </div>
                            </div>
                        """)
                
            # Step 2: Reading Practice
            with gr.Column(elem_classes="card", visible=False) as practice_card:
                gr.HTML("<h2>πŸ“– Time to Practice Reading!</h2>")
                
                # Story display
                story = gr.Markdown(label="πŸ“– Your Personalized Story")
                
                with gr.Row():
                    btn_play = gr.Button("πŸ”Š Listen to Story", elem_classes="btn-secondary")
                
                # Audio playback
                audio_out = gr.Audio(label="🎡 Story Audio - Listen and Follow Along", visible=True)
                
                gr.HTML("<h3>🎀 Now Read the Story Aloud!</h3>")
                
                # Recording
                record = gr.Audio(label="🎀 Record Your Reading", sources=["microphone"],type="filepath")
                
                with gr.Row():
                    btn_record_again = gr.Button("🎀 Record Again", elem_classes="btn-secondary")
                
                with gr.Row():
                    btn_submit = gr.Button("✨ Get AI Feedback", elem_classes="btn-primary")
                
                # Agentic Feedback area
                feedback = gr.TextArea(
                    label="πŸ€– Your Personalized AI Reading Coach Feedback", 
                    interactive=False, 
                    elem_classes="feedback-area",
                    lines=12
                )
            
            # Step 3: Continue Learning (appears after reading feedback)
            with gr.Column(elem_classes="card", visible=False) as practice_story_card:
                gr.HTML("""
                    <div class="practice-info">
                        <h2>πŸš€ Keep Learning & Growing!</h2>
                        <p>Ready for your next challenge? Get a new story that's perfect for your reading level!</p>
                    </div>
                """)
                
                practice_info = gr.Text(label="Practice Information", interactive=False)
                
                with gr.Row():
                    btn_generate_practice = gr.Button("🌟 Get Next Challenge Story", elem_classes="btn-practice")
                    btn_reset = gr.Button("πŸ”„ Start Fresh Session", elem_classes="btn-clear")
            
            # Event handlers
            btn_start.click(
                start_session, 
                inputs=[name, grade, topic], 
                outputs=[story, practice_card, practice_story_card]
            )
            
            btn_play.click(
                generate_audio, 
                inputs=[], 
                outputs=[audio_out]
            )
            
            btn_submit.click(
                submit_reading, 
                inputs=[record], 
                outputs=[feedback, practice_story_card]
            )
            
            btn_generate_practice.click(
                generate_practice_story,
                inputs=[],
                outputs=[story, practice_info]
            )
            
            btn_record_again.click(
                record_again,
                inputs=[],
                outputs=[record]
            )
            
            btn_reset.click(
                reset_session,
                inputs=[],
                outputs=[name, grade, topic, story, record, feedback, practice_info, practice_card, practice_story_card]
            )

    return demo