-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
491 lines (409 loc) Β· 18 KB
/
streamlit_app.py
File metadata and controls
491 lines (409 loc) Β· 18 KB
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
import streamlit as st
import os
import sys
from src.pipeline.pipeline import Pipeline, LanguageNotAvailableError
from src.pipeline.yt_fetch import YTFetch
import re
from urllib.parse import urlparse, parse_qs
from openai import OpenAI
import tempfile
from typing import Optional
# Comprehensive language mapping from full names to ISO 639-1 codes
LANGUAGE_MAPPING = {
# Major European Languages
"English": "en",
"Spanish": "es",
"French": "fr",
"German": "de",
"Italian": "it",
"Portuguese": "pt",
"Russian": "ru",
"Polish": "pl",
"Dutch": "nl",
"Greek": "el",
# Asian Languages
"Chinese (Simplified)": "zh",
"Japanese": "ja",
"Korean": "ko",
"Hindi": "hi",
"Arabic": "ar",
"Hebrew": "he",
"Turkish": "tr",
"Thai": "th",
"Vietnamese": "vi",
"Indonesian": "id",
"Malay": "ms",
"Tagalog": "tl",
"Bengali": "bn",
"Urdu": "ur",
"Persian": "fa",
"Tamil": "ta",
"Telugu": "te",
"Gujarati": "gu",
"Marathi": "mr",
"Mongolian": "mn",
"Georgian": "ka",
# Nordic Languages
"Swedish": "sv",
"Norwegian": "no",
"Danish": "da",
"Finnish": "fi",
# Eastern European Languages
"Czech": "cs",
"Slovak": "sk",
"Ukrainian": "uk",
"Romanian": "ro",
"Hungarian": "hu",
"Bulgarian": "bg",
"Croatian": "hr",
"Serbian": "sr",
"Slovenian": "sl",
"Lithuanian": "lt",
"Latvian": "lv",
"Estonian": "et",
"Albanian": "sq",
"Macedonian": "mk",
# Other Languages
"Swahili": "sw",
"Catalan": "ca",
"Basque": "eu",
"Galician": "gl"
}
# Reverse mapping for displaying language names from codes
LANGUAGE_CODE_TO_NAME = {v: k for k, v in LANGUAGE_MAPPING.items()}
# Group languages by region for better UX
LANGUAGE_GROUPS = {
"Popular": ["English", "Spanish", "French", "German", "Italian", "Portuguese", "Chinese (Simplified)", "Japanese", "Korean", "Arabic", "Hindi", "Russian"],
"European": ["Dutch", "Greek", "Polish", "Swedish", "Norwegian", "Danish", "Finnish", "Czech", "Romanian", "Hungarian", "Turkish"],
"Asian": ["Thai", "Vietnamese", "Indonesian", "Malay", "Tagalog", "Bengali", "Urdu", "Persian", "Tamil", "Telugu", "Hebrew"],
"Eastern European": ["Ukrainian", "Bulgarian", "Croatian", "Serbian", "Slovak", "Slovenian", "Lithuanian", "Latvian", "Estonian", "Albanian", "Macedonian"],
"Other": ["Swahili", "Catalan", "Basque", "Galician", "Gujarati", "Marathi", "Mongolian", "Georgian"]
}
# Flatten all languages for the selectbox
ALL_LANGUAGES = []
for group_languages in LANGUAGE_GROUPS.values():
ALL_LANGUAGES.extend(group_languages)
# Remove duplicates while preserving order
ALL_LANGUAGES = list(dict.fromkeys(ALL_LANGUAGES))
# Page config
st.set_page_config(
page_title="Language Learning Assistant",
page_icon="π",
layout="wide"
)
# Initialize session state - must happen before any other st calls
if 'pipeline' not in st.session_state:
st.session_state['pipeline'] = Pipeline()
if 'lesson_data' not in st.session_state:
st.session_state['lesson_data'] = None
if 'video_id' not in st.session_state:
st.session_state['video_id'] = None
if 'client' not in st.session_state:
st.session_state['client'] = None
if 'available_languages' not in st.session_state:
st.session_state['available_languages'] = None
if 'current_url' not in st.session_state:
st.session_state['current_url'] = None
def extract_video_id(url: str) -> Optional[str]:
"""Extract YouTube video ID from URL."""
try:
patterns = [
r'(?:youtube\.com/watch\?v=|youtu\.be/|youtube\.com/embed/)([^&\n?#]+)',
r'youtube\.com/v/([^&\n?#]+)',
]
for pattern in patterns:
match = re.search(pattern, url)
if match:
return match.group(1)
parsed = urlparse(url)
if parsed.hostname in ["youtu.be"]:
return parsed.path.lstrip("/")
elif parsed.hostname in ["www.youtube.com", "youtube.com"]:
query = parse_qs(parsed.query)
return query.get("v", [None])[0]
except:
return None
return None
def is_youtube_url(text: str) -> bool:
"""Check if text is a YouTube URL."""
youtube_domains = ["youtube.com", "youtu.be", "www.youtube.com", "m.youtube.com"]
try:
parsed = urlparse(text)
return any(domain in parsed.netloc for domain in youtube_domains)
except:
return False
def check_and_display_languages(url: str) -> bool:
"""
Check available languages for a YouTube video and display them.
Returns True if check was successful, False otherwise.
"""
try:
# Check if we need to refresh the language list
if st.session_state['current_url'] != url:
is_available, languages = st.session_state['pipeline'].check_language_availability(url, "dummy")
st.session_state['available_languages'] = languages
st.session_state['current_url'] = url
if st.session_state['available_languages']:
# Create a formatted list of available languages
lang_info = []
for lang in st.session_state['available_languages']:
lang_code = lang.get('language_code', 'unknown')
lang_name = lang.get('language', 'Unknown')
is_generated = lang.get('is_generated', False)
# Try to get the display name from our mapping
display_name = LANGUAGE_CODE_TO_NAME.get(lang_code, lang_name)
if is_generated:
lang_info.append(f"{display_name} ({lang_code}) - Auto-generated")
else:
lang_info.append(f"{display_name} ({lang_code})")
# Display available languages in an expander
with st.expander("π Available languages for this video", expanded=False):
st.markdown("**Available transcripts:**")
for info in lang_info:
st.markdown(f"β’ {info}")
return True
return False
except Exception as e:
st.error(f"Error checking available languages: {str(e)}")
return False
def get_pronunciation_feedback(original: str, transcribed: str, level: str, language_code: str) -> str:
"""Get feedback on pronunciation using GPT."""
try:
# Get language name for the prompt
language_name = LANGUAGE_CODE_TO_NAME.get(language_code, "the target language")
prompt = f"""
As a language coach for {language_name}, compare what the student said to the original text.
Be encouraging and specific. Consider the pronunciation challenges specific to {language_name}.
Original: "{original[:150]}"
Student said: "{transcribed}"
Level: {level}
In 2-3 sentences, mention what they did well and one area to improve.
"""
response = st.session_state['client'].chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens=100
)
return response.choices[0].message.content
except:
return "Good effort! Keep practicing to improve your pronunciation."
def transcribe_audio(audio_bytes: bytes, language_code: str) -> str:
"""Transcribe audio using Whisper API."""
try:
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
tmp_file.write(audio_bytes)
tmp_path = tmp_file.name
with open(tmp_path, "rb") as audio_file:
response = st.session_state['client'].audio.transcriptions.create(
model="whisper-1",
file=audio_file,
language=language_code # Hint Whisper about the expected language
)
os.unlink(tmp_path)
return response.text
except Exception as e:
return f"Error transcribing: {str(e)}"
# Header
st.title("π Language Learning with YouTube")
st.markdown("Transform YouTube videos into personalized language lessons in 50+ languages!")
# Sidebar
with st.sidebar:
st.header("βοΈ Settings")
api_key = st.text_input("OpenAI API Key", type="password")
if api_key:
os.environ["OPENAI_API_KEY"] = api_key
if not st.session_state['client']:
st.session_state['client'] = OpenAI(api_key=api_key)
st.markdown("---")
st.markdown("### π How it works:")
st.markdown("1. Paste a YouTube URL")
st.markdown("2. Choose your language & level")
st.markdown("3. Get simplified lessons")
st.markdown("4. Practice with Shadow Mode!")
st.markdown("---")
st.markdown("### π Supported Languages:")
st.markdown(f"**{len(LANGUAGE_MAPPING)}** languages available!")
# Show language groups
with st.expander("View all languages"):
for group, languages in LANGUAGE_GROUPS.items():
st.markdown(f"**{group}:**")
st.markdown(", ".join(languages[:5]) + ("..." if len(languages) > 5 else ""))
# Main content area
if not api_key:
st.warning("π Please enter your OpenAI API key in the sidebar to get started.")
else:
# Input section
col1, col2 = st.columns([3, 1])
with col1:
url_input = st.text_input(
"YouTube URL",
placeholder="https://youtube.com/watch?v=..."
)
with col2:
st.markdown("<br>", unsafe_allow_html=True) # Spacing
# Check languages button
if url_input and is_youtube_url(url_input):
check_langs_btn = st.button("π Check Languages", use_container_width=True)
if check_langs_btn:
check_and_display_languages(url_input)
# Show available languages if we have a valid URL
if url_input and is_youtube_url(url_input):
check_and_display_languages(url_input)
# Language and level selection
col3, col4, col5 = st.columns(3)
with col3:
# Advanced language selector with search
language = st.selectbox(
"Language",
ALL_LANGUAGES,
index=0, # Default to English
help="Choose the language of the video (50+ languages supported!)",
placeholder="Search for a language..."
)
with col4:
topic = st.text_input(
"Topic (optional)",
placeholder="e.g., cooking, science, travel",
help="Helps find the most relevant parts"
)
with col5:
level = st.selectbox(
"Your Level",
["A2", "B1", "B2"],
index=1,
help="A2: Elementary, B1: Intermediate, B2: Upper-intermediate"
)
# Generate lesson button
generate_btn = st.button("π― Generate Lesson", type="primary", use_container_width=True)
# Generate lesson
if generate_btn and url_input:
if not is_youtube_url(url_input):
st.error("Please enter a valid YouTube URL")
else:
video_id = extract_video_id(url_input)
if not video_id:
st.error("Could not extract video ID from URL")
else:
with st.spinner(f"π Creating your personalized lesson in {language}..."):
try:
# Get language code
language_code = LANGUAGE_MAPPING.get(language, "en")
# Use the pipeline to generate lesson
print(f"Generating lesson for: {language} ({language_code})")
results = st.session_state['pipeline'].generate_simplified_lesson(
url=url_input,
language=language_code,
topic=topic or language, # Use language as fallback topic
level=level,
n_chunks=3
)
if results:
st.session_state['lesson_data'] = results
st.session_state['video_id'] = video_id
st.session_state['current_language'] = language_code
st.success(f"β
Lesson generated successfully in {language}!")
else:
st.error("Could not generate lesson. Please try another video.")
except LanguageNotAvailableError as e:
st.error("π« " + str(e))
# Show available languages in a nice format
if e.available_languages:
st.info("π‘ **Tip:** This video has transcripts in the following languages:")
# Group available languages
manual_langs = []
auto_langs = []
for lang in e.available_languages:
lang_code = lang.get('language_code', 'unknown')
lang_name = lang.get('language', 'Unknown')
display_name = LANGUAGE_CODE_TO_NAME.get(lang_code, lang_name)
if lang.get('is_generated', False):
auto_langs.append(f"{display_name} ({lang_code})")
else:
manual_langs.append(f"{display_name} ({lang_code})")
if manual_langs:
st.markdown("**Manual transcripts (recommended):**")
st.markdown(", ".join(manual_langs))
if auto_langs:
st.markdown("**Auto-generated transcripts:**")
st.markdown(", ".join(auto_langs))
st.markdown("---")
st.markdown("**What you can do:**")
st.markdown("1. Select one of the available languages above")
st.markdown("2. Try a different YouTube video")
st.markdown("3. Use YouTube's auto-translate feature if available")
except Exception as e:
st.error(f"Error: {str(e)}")
# Display lesson content (rest of the code remains the same)
if 'lesson_data' in st.session_state and st.session_state['lesson_data'] is not None and 'video_id' in st.session_state and st.session_state['video_id'] is not None:
st.markdown("---")
# Two column layout
lesson_col, shadow_col = st.columns([2, 1])
with lesson_col:
# Video player
st.markdown("### π₯ Video")
st.markdown(
f'<iframe width="100%" height="315" '
f'src="https://www.youtube.com/embed/{st.session_state["video_id"]}" '
f'frameborder="0" allowfullscreen></iframe>',
unsafe_allow_html=True
)
# Lesson content
current_lang = st.session_state.get('current_language', 'en')
lang_name = LANGUAGE_CODE_TO_NAME.get(current_lang, "Unknown")
st.markdown(f"### π Your Lesson ({lang_name})")
for i, chunk in enumerate(st.session_state['lesson_data'], 1):
with st.expander(f"Lesson Part {i}", expanded=(i == 1)):
col_a, col_b = st.columns(2)
with col_a:
st.markdown("**Original Text**")
st.info(chunk.get('original', ''))
st.caption(f"Words: {len(chunk.get('original', '').split())}")
with col_b:
st.markdown(f"**Simplified ({level})**")
st.success(chunk.get('rewritten', ''))
st.caption(f"Words: {chunk.get('word_count', 0)}")
with shadow_col:
st.markdown("### π€ Shadow Mode")
st.markdown("Practice by repeating the simplified text!")
# Select which chunk to practice
chunk_to_practice = st.selectbox(
"Choose part to practice",
options=range(len(st.session_state['lesson_data'])),
format_func=lambda x: f"Part {x + 1}"
)
# Display the text to practice
practice_text = st.session_state['lesson_data'][chunk_to_practice]['rewritten']
st.info(practice_text)
# Audio recorder
st.markdown("**Record yourself:**")
audio = st.audio_input("Click to start recording")
if audio:
with st.spinner("Analyzing your pronunciation..."):
audio_bytes = audio.getvalue()
current_lang = st.session_state.get('current_language', 'en')
# Transcribe with language hint
user_text = transcribe_audio(audio_bytes, current_lang)
# Show results
st.markdown("**You said:**")
st.warning(user_text)
# Get feedback
feedback = get_pronunciation_feedback(
practice_text,
user_text,
level,
current_lang
)
st.markdown("**Feedback:**")
st.success(feedback)
# Tips
with st.expander("π‘ Practice Tips"):
st.markdown("""
- Listen to the video section first
- Read the simplified text aloud
- Record yourself
- Compare and try again!
- Focus on one part at a time
""")
# Footer
st.markdown("---")
st.caption(f"Made with β€οΈ using Streamlit, YouTube Transcript API, and OpenAI | Supporting {len(LANGUAGE_MAPPING)} languages!")