prabalGaur commited on
Commit
f19cb84
·
verified ·
1 Parent(s): 2ed47a5

Upload community_contributions/english_learning_tutor_ns_sly/main.py with huggingface_hub

Browse files
community_contributions/english_learning_tutor_ns_sly/main.py ADDED
@@ -0,0 +1,353 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Main entry point for AI English Tutor.
3
+
4
+ Features
5
+ --------
6
+ - First exercise displayed on UI load
7
+ - User records pronunciation
8
+ - AI evaluates accent and pronunciation
9
+ - Generates next exercise automatically
10
+ - Tracks user progress
11
+ """
12
+
13
+ import logging
14
+ import gradio as gr
15
+
16
+ from speech.speech_to_text import SpeechToText
17
+ from agents.orchestrator_agent import LearningOrchestrator
18
+ from progress.progress_tracker import ProgressTracker
19
+
20
+
21
+ # ---------------------------
22
+ # Global components
23
+ # ---------------------------
24
+
25
+ USER_NAME = "ns_sly"
26
+
27
+ stt = None
28
+ orchestrator = None
29
+ tracker = None
30
+
31
+
32
+ # ---------------------------
33
+ # Initialize system
34
+ # ---------------------------
35
+
36
+ def initialize_system():
37
+ global stt, orchestrator, tracker
38
+
39
+ print("Initializing AI English Tutor components...")
40
+
41
+ stt = SpeechToText()
42
+ orchestrator = LearningOrchestrator()
43
+ tracker = ProgressTracker()
44
+
45
+ print("Initialization complete.")
46
+
47
+
48
+ # ---------------------------
49
+ # Generate first exercise
50
+ # ---------------------------
51
+
52
+ def generate_first_exercise():
53
+
54
+ level = "Beginner"
55
+ category = "Accent Practice"
56
+
57
+ try:
58
+ exercise = orchestrator.lesson.retrieve(level, category)
59
+
60
+ except Exception as e:
61
+
62
+ print("Exercise generation failed:", e)
63
+
64
+ exercise = "The weather today is very pleasant."
65
+
66
+ return exercise, exercise
67
+
68
+
69
+ def generate_next_exercise(level, category):
70
+
71
+
72
+ try:
73
+ exercise = orchestrator.lesson.retrieve(level, category)
74
+
75
+ except Exception as e:
76
+
77
+ print("Exercise generation failed:", e)
78
+
79
+ exercise = "The weather today is very pleasant."
80
+
81
+ return exercise, exercise
82
+
83
+ # ---------------------------
84
+ # Evaluate pronunciation
85
+ # ---------------------------
86
+
87
+ def evaluate_answer(audio, level, category, exercise, username):
88
+
89
+ if audio is None:
90
+ return (
91
+ "No audio detected",
92
+ "",
93
+ "",
94
+ 0,
95
+ level,
96
+ 0,
97
+ 0,
98
+ exercise,
99
+ exercise
100
+ )
101
+
102
+ # ---------------------------
103
+ # Speech → text
104
+ # ---------------------------
105
+
106
+ spoken_text = stt.transcribe(audio)
107
+
108
+ # ---------------------------
109
+ # Run orchestrator
110
+ # ---------------------------
111
+
112
+ result = orchestrator.run_exercise(
113
+ level=level,
114
+ exercise=exercise,
115
+ spoken_text=spoken_text,
116
+ user_audio=audio,
117
+ username=username,
118
+ category=category
119
+ )
120
+
121
+ # ---------------------------
122
+ # Update progress
123
+ # ---------------------------
124
+
125
+
126
+
127
+ # ---------------------------
128
+ # Format pronunciation heatmap
129
+ # ---------------------------
130
+
131
+ heatmap = " ".join(
132
+ f"🟢{w['word']}" if w["status"] == "correct"
133
+ else f"🔴{w['word']}"
134
+ for w in result["heatmap"]
135
+ )
136
+
137
+
138
+ overall_progress = tracker.get_overall_progress(username)
139
+
140
+ # Generate next exercise
141
+
142
+ try:
143
+
144
+ next_exercise = orchestrator.lesson.retrieve(
145
+ level,
146
+ category
147
+ )
148
+
149
+ except Exception:
150
+
151
+ next_exercise = exercise
152
+
153
+ mistakes_text = "\n".join(result["grammar"].mistakes) or "No mistakes found"
154
+ corrections_text = "\n".join(result["grammar"].corrections) or "No corrections found"
155
+ return (
156
+ spoken_text,
157
+ heatmap,
158
+ result["tips"],
159
+ result["pronunciation"]["pronunciation_score"],
160
+ result["pronunciation"]["accent_score"],
161
+ result["recommended_level"],
162
+ result["level_progress"],
163
+ overall_progress,
164
+ next_exercise,
165
+ exercise,
166
+ result["grammar"].grammar_score,
167
+ mistakes_text,
168
+ corrections_text,
169
+ result["grammar"].explanation or "No explanation provided"
170
+ )
171
+
172
+ # ---------------------------
173
+ # Build Gradio UI
174
+ # ---------------------------
175
+
176
+ def build_ui():
177
+
178
+ level_choices = [
179
+ "Beginner",
180
+ "Intermediate",
181
+ "Advanced",
182
+ "Native"
183
+ ]
184
+
185
+ category_choices = [
186
+ "Accent Practice",
187
+ "Conversation",
188
+ "Business Communication",
189
+ "Tech Workplace",
190
+ "CEO Communication",
191
+ "Sales"
192
+ ]
193
+
194
+ with gr.Blocks(title="AI English Accent Tutor") as ui:
195
+
196
+ gr.Markdown("# English Accent & Conversation Tutor")
197
+ gr.Markdown(
198
+ "Practice English pronunciation, accent, and fluency using AI."
199
+ )
200
+
201
+ with gr.Row():
202
+
203
+ level = gr.Dropdown(
204
+ level_choices,
205
+ value="Beginner",
206
+ label="Level"
207
+ )
208
+
209
+ category = gr.Dropdown(
210
+ category_choices,
211
+ value="Accent Practice",
212
+ label="Category"
213
+ )
214
+ username = gr.Textbox(
215
+ value=USER_NAME,
216
+ label="userame",
217
+ interactive=True
218
+ )
219
+
220
+ exercise_box = gr.Textbox(
221
+ label="Pronunciation Exercise",
222
+ interactive=False,
223
+ max_lines=5,
224
+ lines=2
225
+ )
226
+
227
+ next_btn = gr.Button("Next Exercise")
228
+
229
+
230
+ audio_input = gr.Audio(
231
+ type="filepath",
232
+ label="Record your pronunciation"
233
+ )
234
+
235
+ submit_btn = gr.Button("Submit Answer")
236
+
237
+ gr.Markdown("### Evaluation")
238
+
239
+ transcription = gr.Textbox(label="Your Speech", interactive=False)
240
+
241
+ heatmap = gr.Textbox(label="Pronunciation Heatmap", interactive=False)
242
+
243
+ tips = gr.Textbox(label="Pronunciation Tips", interactive=False, lines=2, max_lines=5)
244
+
245
+ pronunciation_score = gr.Number(label="Pronunciation Accuracy Score", interactive=False)
246
+
247
+ accent_score = gr.Number(label="Accent Similarity Score", interactive=False)
248
+
249
+
250
+ recommended_level = gr.Textbox(label="Recommended Level", interactive=False)
251
+
252
+ #grammer stats
253
+ grammar_score = gr.Number(label="Grammar Score", interactive=False)
254
+ grammar_mistakes = gr.Textbox(label="Grammar Mistakes", interactive=False, lines=2, max_lines=5)
255
+ grammar_corrections = gr.Textbox(label="Grammar Corrections", interactive=False, lines=2, max_lines=5)
256
+ grammar_explanation = gr.Textbox(label="Grammar Explanation", interactive=False, lines=3, max_lines=5)
257
+
258
+ gr.Markdown("### Progress")
259
+
260
+ level_progress = gr.Number(label="Level Progress", interactive=False)
261
+
262
+ overall_progress = gr.Number(label="Overall Progress", interactive=False)
263
+
264
+ # store current exercise
265
+ current_exercise = gr.State(value="")
266
+
267
+ # ---------------------------
268
+ # Load first exercise
269
+ # ---------------------------
270
+
271
+ ui.load(
272
+ generate_first_exercise,
273
+ inputs=[],
274
+ outputs=[exercise_box, current_exercise]
275
+ )
276
+
277
+ #next button logic
278
+ next_btn.click(generate_next_exercise, inputs=[level, category], outputs=[exercise_box, current_exercise])
279
+
280
+ # ---------------------------
281
+ # Submit pronunciation
282
+ # ---------------------------
283
+
284
+ submit_btn.click(
285
+ evaluate_answer,
286
+ inputs=[
287
+ audio_input,
288
+ level,
289
+ category,
290
+ current_exercise,
291
+ username
292
+ ],
293
+ outputs=[
294
+ transcription,
295
+ heatmap,
296
+ tips,
297
+ pronunciation_score,
298
+ accent_score,
299
+ recommended_level,
300
+ level_progress,
301
+ overall_progress,
302
+ exercise_box,
303
+ current_exercise,
304
+ grammar_score,
305
+ grammar_mistakes,
306
+ grammar_corrections,
307
+ grammar_explanation
308
+ ]
309
+ )
310
+
311
+ return ui
312
+
313
+
314
+ # ---------------------------
315
+ # Run application
316
+ # ---------------------------
317
+
318
+ def run_app():
319
+
320
+ print("Launching Gradio UI...")
321
+
322
+ ui = build_ui()
323
+
324
+ ui.launch(
325
+ server_name="0.0.0.0",
326
+ server_port=7860
327
+ )
328
+
329
+
330
+ # ---------------------------
331
+ # Main entry point
332
+ # ---------------------------
333
+
334
+ if __name__ == "__main__":
335
+
336
+ root = logging.getLogger()
337
+ root.setLevel(logging.INFO)
338
+
339
+ handler = logging.StreamHandler()
340
+
341
+ handler.setFormatter(
342
+ logging.Formatter(
343
+ "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
344
+ )
345
+ )
346
+
347
+ root.addHandler(handler)
348
+
349
+ root.info("Starting AI English Tutor...")
350
+
351
+ initialize_system()
352
+
353
+ run_app()