-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1201 lines (988 loc) · 46.1 KB
/
app.py
File metadata and controls
1201 lines (988 loc) · 46.1 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
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Main Gradio application for BBC Audio Scraper & Chat System.
Provides web interface for downloading, transcribing, and chatting with BBC audio.
"""
import gradio as gr
import base64
from pathlib import Path
from config import Config
from src.scraper.rss_scraper import RSScraper
from src.scraper.get_iplayer_wrapper import GetIPlayerWrapper
from src.transcription.transcriber import WhisperTranscriber
from src.transcription.audio_processor import AudioProcessor
from src.chat.vector_store import VectorStore
from src.chat.chat_engine import ChatEngine
from src.utils.file_manager import FileManager
from src.utils.pdf_generator import PDFGenerator
from src.utils.logger import setup_logger
from src.utils.history_manager import HistoryManager
from src.utils.recommendation_engine import RecommendationEngine
logger = setup_logger(__name__)
# Initialize components
rss_scraper = RSScraper()
iplayer = GetIPlayerWrapper()
transcriber = WhisperTranscriber()
audio_processor = AudioProcessor()
vector_store = VectorStore()
chat_engine = ChatEngine(vector_store)
file_manager = FileManager()
pdf_generator = PDFGenerator()
history_manager = HistoryManager()
recommendation_engine = RecommendationEngine()
# ============================================================================
# TAB 1: DOWNLOAD AUDIO
# ============================================================================
def download_from_rss(feed_url: str, limit: int):
"""Download episodes from RSS feed"""
try:
if not feed_url:
return "❌ Please enter an RSS feed URL"
limit = int(limit) if limit else None
files = rss_scraper.download_episodes(feed_url, limit)
if files:
return f"✅ Downloaded {len(files)} episode(s):\n" + "\n".join([f"- {f.name}" for f in files])
else:
return "❌ No episodes downloaded. Check the feed URL."
except Exception as e:
return f"❌ Error: {str(e)}"
def download_with_iplayer(query: str):
"""Search and download with get_iplayer"""
try:
if not query:
return "❌ Please enter a search query or URL"
# Check if it's a URL or search query
if query.startswith('http'):
success = iplayer.download_by_url(query)
if success:
return f"✅ Downloaded from URL: {query}"
else:
return "❌ Download failed. Check logs for details."
else:
# Search for programmes
results = iplayer.search(query)
if not results:
return f"❌ No programmes found for: {query}"
# Show first 5 results
output = f"Found {len(results)} programme(s):\n\n"
for i, prog in enumerate(results[:5], 1):
output += f"{i}. {prog['name']} - {prog['episode']}\n"
output += f" PID: {prog['pid']}\n\n"
output += "\n💡 To download, use the PID with format: pid:<PID>"
return output
except Exception as e:
return f"❌ Error: {str(e)}"
def list_downloads():
"""List downloaded audio files"""
files = file_manager.list_audio_files()
if files:
return "\n".join([f"📁 {f.name}" for f in files])
return "No audio files found"
def get_popular_feeds():
"""Get list of popular BBC feeds"""
feeds = rss_scraper.list_available_feeds()
output = "📻 Popular BBC Podcast Feeds:\n\n"
for name, url in feeds.items():
output += f"**{name.replace('_', ' ').title()}**\n{url}\n\n"
return output
# ============================================================================
# TAB 2: TRANSCRIBE
# ============================================================================
def transcribe_file(audio_file, model_size: str, language: str):
"""Transcribe a single audio file"""
try:
if not audio_file:
return "❌ Please select an audio file"
# Update model if changed
if model_size != transcriber.model_size:
transcriber.model_size = model_size
transcriber.model = None # Force reload
# Transcribe
transcript_path = transcriber.transcribe_and_save(Path(audio_file), language)
if transcript_path:
return f"✅ Transcription complete!\nSaved to: {transcript_path.name}"
else:
return "❌ Transcription failed"
except Exception as e:
return f"❌ Error: {str(e)}"
def transcribe_all(model_size: str, language: str):
"""Transcribe all audio files"""
try:
audio_files = file_manager.list_audio_files()
if not audio_files:
return "❌ No audio files found to transcribe"
# Update model if changed
if model_size != transcriber.model_size:
transcriber.model_size = model_size
transcriber.model = None
transcripts = transcriber.batch_transcribe(audio_files, language)
return f"✅ Transcribed {len(transcripts)}/{len(audio_files)} files successfully!"
except Exception as e:
return f"❌ Error: {str(e)}"
def list_transcripts():
"""List all transcripts"""
transcripts = file_manager.list_transcripts()
if transcripts:
return "\n".join([f"📄 {t.name}" for t in transcripts])
return "No transcripts found"
def load_transcript(transcript_name: str):
"""Load a transcript for viewing"""
try:
transcript_path = Config.TRANSCRIPTS_DIR / transcript_name
if transcript_path.exists():
with open(transcript_path, 'r', encoding='utf-8') as f:
return f.read()
return "Transcript not found"
except Exception as e:
return f"Error: {str(e)}"
def export_transcript_to_pdf(transcript_name: str):
"""Export a single transcript to PDF"""
try:
if not transcript_name:
return "❌ Please select a transcript", None
transcript_path = Config.TRANSCRIPTS_DIR / transcript_name
if not transcript_path.exists():
return "❌ Transcript not found", None
# Generate PDF
pdf_path = pdf_generator.generate_pdf(transcript_path)
return f"✅ PDF generated successfully!\nSaved to: {pdf_path.name}", str(pdf_path)
except Exception as e:
return f"❌ Error: {str(e)}", None
def export_all_transcripts_to_pdf():
"""Export all transcripts to PDF"""
try:
transcripts = file_manager.list_transcripts()
if not transcripts:
return "❌ No transcripts found to export"
pdf_paths = pdf_generator.batch_generate_pdfs()
if pdf_paths:
return f"✅ Generated {len(pdf_paths)} PDF(s) successfully!\n\nPDFs saved in: pdfs/"
else:
return "❌ No PDFs were generated"
except Exception as e:
return f"❌ Error: {str(e)}"
# ============================================================================
# TAB 3: PDF READER
# ============================================================================
def get_available_content():
"""Get list of available audio files and their corresponding PDFs/transcripts"""
audio_files = file_manager.list_audio_files()
completed_names = history_manager.get_completed_content_names()
content_list = []
for audio_file in audio_files:
# Get base name without extension
base_name = audio_file.stem
# Skip if this content is completed
display_name = file_manager.format_display_name(audio_file)
if display_name in completed_names:
continue
# Check for corresponding transcript and PDF
transcript_path = Config.TRANSCRIPTS_DIR / f"{base_name}_transcript.txt"
pdf_path = Config.PDF_DIR / f"{base_name}_transcript.pdf"
if transcript_path.exists():
content_list.append({
'name': base_name,
'audio': str(audio_file),
'transcript': str(transcript_path) if transcript_path.exists() else None,
'pdf': str(pdf_path) if pdf_path.exists() else None
})
return content_list
def load_content_for_reading(content_name: str):
"""Load audio and PDF for a selected content"""
try:
if not content_name:
return None, "<p style='text-align: center; padding: 50px; color: #666;'>❌ Please select content to view</p>", "❌ Please select content to view"
content_list = get_available_content()
selected = next((c for c in content_list if c['name'] == content_name), None)
if not selected:
return None, "<p style='text-align: center; padding: 50px; color: #666;'>❌ Content not found</p>", "❌ Content not found"
# Track that this content was accessed
history_manager.mark_as_accessed(content_name)
# Check if PDF exists, if not offer to generate it
pdf_html = ""
pdf_status = ""
if selected['pdf'] and Path(selected['pdf']).exists():
# Read PDF and convert to base64
with open(selected['pdf'], 'rb') as f:
pdf_data = base64.b64encode(f.read()).decode('utf-8')
# Create HTML with embedded PDF viewer using base64 data URI
pdf_html = f"""
<iframe src="data:application/pdf;base64,{pdf_data}"
width="100%"
height="800px"
style="border: 1px solid #ddd; border-radius: 4px;">
<p>Your browser does not support PDFs.
<a href="data:application/pdf;base64,{pdf_data}" download="{content_name}.pdf">Download the PDF</a>
</p>
</iframe>
"""
pdf_status = f"✅ Ready to read: {content_name}"
else:
pdf_html = "<p style='text-align: center; padding: 50px; color: #EAC36B;'>⚠️ PDF not found. Click 'Generate PDF' to create it.</p>"
pdf_status = f"⚠️ PDF not found. Click 'Generate PDF' to create it."
return selected['audio'], pdf_html, pdf_status
except Exception as e:
return None, f"<p style='text-align: center; padding: 50px; color: #D96B6B;'>❌ Error: {str(e)}</p>", f"❌ Error: {str(e)}"
def generate_pdf_for_reader(content_name: str):
"""Generate PDF for the selected content if it doesn't exist"""
try:
if not content_name:
return "<p style='text-align: center; padding: 50px; color: #666;'>❌ Please select content first</p>", "❌ Please select content first"
transcript_path = Config.TRANSCRIPTS_DIR / f"{content_name}_transcript.txt"
if not transcript_path.exists():
return "<p style='text-align: center; padding: 50px; color: #D96B6B;'>❌ Transcript not found</p>", "❌ Transcript not found"
# Generate PDF
pdf_path = pdf_generator.generate_pdf(transcript_path)
# Read PDF and convert to base64
with open(pdf_path, 'rb') as f:
pdf_data = base64.b64encode(f.read()).decode('utf-8')
# Create HTML with embedded PDF viewer using base64 data URI
pdf_html = f"""
<iframe src="data:application/pdf;base64,{pdf_data}"
width="100%"
height="800px"
style="border: 1px solid #ddd; border-radius: 4px;">
<p>Your browser does not support PDFs.
<a href="data:application/pdf;base64,{pdf_data}" download="{content_name}.pdf">Download the PDF</a>
</p>
</iframe>
"""
return pdf_html, f"✅ PDF generated successfully!"
except Exception as e:
return f"<p style='text-align: center; padding: 50px; color: #D96B6B;'>❌ Error: {str(e)}</p>", f"❌ Error: {str(e)}"
def mark_content_as_completed(content_name: str):
"""Mark content as completed"""
try:
if not content_name:
return "❌ Please select content first"
history_manager.mark_as_completed(content_name)
return f"✅ Marked '{content_name}' as completed!"
except Exception as e:
return f"❌ Error: {str(e)}"
def get_recommendations():
"""Get personalized recommendations based on listening history"""
try:
# Get completed titles
completed_titles = history_manager.get_completed_titles()
# Get full listening history with metadata
listening_history = history_manager.get_history(status_filter='completed')
# Get all available audio files
audio_files = file_manager.list_audio_files()
all_titles = [file_manager.format_display_name(f) for f in audio_files]
# Filter out completed titles from available
available_titles = [t for t in all_titles if t not in completed_titles]
# Generate recommendations with history metadata
recommendations = recommendation_engine.generate_recommendations(
completed_titles=completed_titles,
available_titles=available_titles,
listening_history=listening_history,
top_n=5
)
# Format for display
display_text = recommendation_engine.format_recommendations_for_display(recommendations)
# Extract recommended titles for sorting
recommended_titles = [rec['title'] for rec in recommendations if rec['title'] not in ['API Not Configured', 'No History Yet', 'No Content Available', 'Error']]
# Sort audio files: recommended first, then alphabetically
sorted_choices = []
audio_file_map = {file_manager.format_display_name(f): str(f) for f in audio_files}
# Add recommended files first
for title in recommended_titles:
if title in audio_file_map and title not in completed_titles:
sorted_choices.append((title, audio_file_map[title]))
# Add remaining files alphabetically
remaining_titles = sorted([t for t in available_titles if t not in recommended_titles])
for title in remaining_titles:
if title in audio_file_map:
sorted_choices.append((title, audio_file_map[title]))
return display_text, gr.Dropdown(choices=sorted_choices)
except Exception as e:
logger.error(f"Error getting recommendations: {e}")
return f"❌ Error generating recommendations: {str(e)}", gr.Dropdown()
# ============================================================================
# TAB 4: CHAT
# ============================================================================
def load_transcripts_to_vector_store():
"""Load all transcripts into vector store"""
try:
count = vector_store.add_all_transcripts()
stats = vector_store.get_stats()
return f"✅ Loaded {count} chunks from transcripts\n\nVector Store Stats:\n- Total chunks: {stats['total_chunks']}\n- Collection: {stats['collection_name']}"
except Exception as e:
return f"❌ Error: {str(e)}"
def chat_with_transcripts(message: str, history):
"""Chat with the transcripts"""
if not chat_engine.is_ready():
return history + [[message, "❌ Google AI API key not configured. Please set GOOGLE_AI_API_KEY in .env file."]]
try:
result = chat_engine.ask(message, use_rag=True)
response = result['response']
# Add source citations if available
if result['sources']:
sources_text = chat_engine.format_sources(result['sources'])
response += f"\n\n**Sources:**\n{sources_text}"
return history + [[message, response]]
except Exception as e:
return history + [[message, f"❌ Error: {str(e)}"]]
def clear_chat():
"""Clear chat history"""
chat_engine.clear_history()
return []
# ============================================================================
# TAB 5: HISTORY
# ============================================================================
def get_listening_history_display(status_filter: str = "All"):
"""Get listening history formatted for display"""
try:
filter_map = {
"All": None,
"In Progress": "in_progress",
"Completed": "completed"
}
records = history_manager.get_history(filter_map.get(status_filter))
if not records:
return "No history found"
output = []
for record in records:
status_emoji = "✅" if record['status'] == 'completed' else "📖"
output.append(f"{status_emoji} **{record['content_name']}**")
output.append(f" Status: {record['status'].replace('_', ' ').title()}")
output.append(f" Last accessed: {record.get('last_accessed', 'N/A')}")
if record.get('completed_at'):
output.append(f" Completed: {record['completed_at']}")
output.append("")
return "\n".join(output)
except Exception as e:
return f"❌ Error: {str(e)}"
def get_history_statistics():
"""Get history statistics"""
try:
stats = history_manager.get_statistics()
return f"""📊 **Listening Statistics**
✅ Completed: {stats['completed']}
📚 Total Content: {stats['total_content']}
📈 Completion Rate: {stats['completion_rate']}%
"""
except Exception as e:
return f"❌ Error: {str(e)}"
def get_chat_sessions_display():
"""Get chat sessions formatted for display"""
try:
sessions = chat_engine.list_sessions()
if not sessions:
return "No chat sessions found"
output = []
for session in sessions:
from datetime import datetime
start_time = datetime.fromisoformat(session['start_time']).strftime('%Y-%m-%d %H:%M')
output.append(f"💬 **{session['session_name']}**")
output.append(f" Date: {start_time}")
output.append(f" Messages: {session['message_count']}")
output.append(f" Preview: {session['preview']}")
output.append(f" ID: `{session['session_id']}`")
output.append("")
return "\n".join(output)
except Exception as e:
return f"❌ Error: {str(e)}"
def export_chat_session(session_id: str, export_format: str):
"""Export a chat session"""
try:
if not session_id:
return "❌ Please enter a session ID", None
format_map = {
"Text (.txt)": "txt",
"Markdown (.md)": "md",
"JSON (.json)": "json"
}
export_path = chat_engine.export_session(session_id, format_map.get(export_format, "txt"))
if export_path:
return f"✅ Exported to: {export_path.name}", str(export_path)
else:
return "❌ Export failed", None
except Exception as e:
return f"❌ Error: {str(e)}", None
def delete_chat_session(session_id: str):
"""Delete a chat session"""
try:
if not session_id:
return "❌ Please enter a session ID"
if chat_engine.delete_session(session_id):
return f"✅ Deleted session: {session_id}"
else:
return "❌ Session not found"
except Exception as e:
return f"❌ Error: {str(e)}"
def start_new_chat_session():
"""Start a new chat session"""
session_id = chat_engine.start_new_session()
return f"✅ Started new session: {session_id}", []
# ============================================================================
# GRADIO INTERFACE
# ============================================================================
# Custom CSS for modern design
custom_css = """
/* Modern color palette and design system */
:root {
--primary-50: #eff6ff;
--primary-100: #dbeafe;
--primary-500: #D96B6B;
--primary-600: #C55A5A;
--primary-700: #5C4A4A;
--success-500: #97CFC6;
--warning-500: #EAC36B;
--error-500: #D96B6B;
}
/* Header styling */
.gradio-container h1 {
background: linear-gradient(135deg, #D96B6B 0%, #5C4A4A 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
font-weight: 800;
font-size: 2.5rem !important;
margin-bottom: 0.5rem !important;
}
/* Tab styling */
.tab-nav button {
font-weight: 600 !important;
font-size: 0.95rem !important;
padding: 0.75rem 1.5rem !important;
border-radius: 0.5rem !important;
transition: all 0.2s ease !important;
}
.tab-nav button[aria-selected="true"] {
background: linear-gradient(135deg, #D96B6B 0%, #5C4A4A 100%) !important;
color: white !important;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1) !important;
}
.tab-nav button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(217, 107, 107, 0.3) !important;
}
/* Button improvements */
.primary {
background: linear-gradient(135deg, #D96B6B 0%, #5C4A4A 100%) !important;
border: none !important;
color: white !important;
font-weight: 600 !important;
padding: 0.75rem 1.5rem !important;
border-radius: 0.5rem !important;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1) !important;
transition: all 0.2s ease !important;
}
.primary:hover {
transform: translateY(-2px);
box-shadow: 0 10px 15px -3px rgba(217, 107, 107, 0.4) !important;
}
.secondary {
background: linear-gradient(135deg, #97CFC6 0%, #7AB8A8 100%) !important;
border: none !important;
color: white !important;
font-weight: 600 !important;
border-radius: 0.5rem !important;
}
/* Card-like containers */
.gr-box {
border-radius: 1rem !important;
border: 1px solid #e5e7eb !important;
box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1) !important;
transition: all 0.2s ease !important;
}
.gr-box:hover {
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1) !important;
}
/* Input fields */
input, textarea, select {
border-radius: 0.5rem !important;
border: 2px solid #e5e7eb !important;
transition: all 0.2s ease !important;
}
input:focus, textarea:focus, select:focus {
border-color: #D96B6B !important;
box-shadow: 0 0 0 3px rgba(217, 107, 107, 0.1) !important;
}
/* Dropdown styling */
.gr-dropdown {
border-radius: 0.5rem !important;
}
/* Status messages */
.gr-textbox:has(> label:contains("Status")) {
font-weight: 500;
}
/* Chat interface */
.message-wrap {
border-radius: 1rem !important;
padding: 1rem !important;
margin: 0.5rem 0 !important;
}
.message.user {
background: linear-gradient(135deg, #D96B6B 0%, #5C4A4A 100%) !important;
color: white !important;
}
.message.bot {
background: #f3f4f6 !important;
border: 1px solid #e5e7eb !important;
}
/* Audio player */
audio {
border-radius: 0.75rem !important;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1) !important;
}
/* File upload area */
.upload-container {
border: 2px dashed #d1d5db !important;
border-radius: 1rem !important;
transition: all 0.2s ease !important;
}
.upload-container:hover {
border-color: #D96B6B !important;
background: #eff6ff !important;
}
/* Markdown content */
.prose {
line-height: 1.7 !important;
}
.prose h2 {
color: #1f2937 !important;
font-weight: 700 !important;
margin-top: 1.5rem !important;
}
.prose h3 {
color: #374151 !important;
font-weight: 600 !important;
}
/* Loading states */
.loading {
background: linear-gradient(90deg, #f3f4f6 25%, #e5e7eb 50%, #f3f4f6 75%);
background-size: 200% 100%;
animation: loading 1.5s ease-in-out infinite;
}
@keyframes loading {
0% { background-position: 200% 0; }
100% { background-position: -200% 0; }
}
/* Scrollbar styling */
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
::-webkit-scrollbar-track {
background: #f3f4f6;
border-radius: 4px;
}
::-webkit-scrollbar-thumb {
background: linear-gradient(135deg, #D96B6B 0%, #5C4A4A 100%);
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background: linear-gradient(135deg, #C55A5A 0%, #4A3A3A 100%);
}
/* Responsive improvements */
@media (max-width: 100%) {
.gradio-container h1 {
font-size: 1.75rem !important;
}
.tab-nav button {
padding: 0.5rem 1rem !important;
font-size: 0.875rem !important;
}
}
"""
# Custom theme
custom_theme = gr.themes.Soft(
primary_hue="blue",
secondary_hue="purple",
neutral_hue="slate",
).set(
body_background_fill="linear-gradient(to bottom right, #f8fafc, #f1f5f9)",
button_primary_background_fill="linear-gradient(135deg, #667eea 0%, #764ba2 100%)",
button_primary_background_fill_hover="linear-gradient(135deg, #5568d3 0%, #6941a5 100%)",
button_primary_text_color="white",
button_secondary_background_fill="linear-gradient(135deg, #10b981 0%, #059669 100%)",
button_secondary_text_color="white",
input_border_color="#e5e7eb",
input_border_width="2px",
block_border_width="1px",
block_shadow="0 1px 3px 0 rgba(0, 0, 0, 0.1)",
)
with gr.Blocks(
title="BBC Audio Transcript & Chat - Alex Snow School",
theme=custom_theme,
css=custom_css,
) as app:
gr.Markdown("""
<div style='text-align: center; padding: 2rem 1rem; background: linear-gradient(135deg, #2D3748 0%, #1A202C 100%); border-radius: 1rem; margin-bottom: 1.5rem; box-shadow: 0 10px 30px rgba(0,0,0,0.3);'>
<h1 style='color: white; font-size: 2.5rem; margin: 0; font-weight: 800; text-shadow: 2px 2px 4px rgba(0,0,0,0.3);'>
🎙️ <span style='background: linear-gradient(135deg, #D96B6B 0%, #EAC36B 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;'>BBC Audio Transcript & Chat</span>
</h1>
<p style='font-size: 1.2rem; margin: 1rem 0 0 0; font-weight: 600;'>
<span style='color: #97CFC6;'>📚 Download</span>
<span style='color: rgba(255,255,255,0.5);'>•</span>
<span style='color: #EAC36B;'>📝 Transcribe</span>
<span style='color: rgba(255,255,255,0.5);'>•</span>
<span style='color: #D96B6B;'>💬 Chat with AI</span>
</p>
<p style='color: rgba(255,255,255,0.7); font-size: 0.95rem; margin: 0.75rem 0 0 0;'>
⚡ Powered by <span style='color: #97CFC6; font-weight: 600;'>Whisper AI</span> & <span style='color: #EAC36B; font-weight: 600;'>Google Gemini</span>
</p>
<p style='color: rgba(255,255,255,0.5); font-size: 0.85rem; margin: 0.5rem 0 0 0;'>
🎓 <a href='https://alexsnowschool.org/' target='_blank' style='color: #97CFC6; text-decoration: none; font-weight: 600; transition: color 0.2s;' onmouseover='this.style.color="#EAC36B"' onmouseout='this.style.color="#97CFC6"'>Alex Snow School</a>
</p>
</div>
""")
with gr.Tabs():
# ====================================================================
# TAB 1: DOWNLOAD
# ====================================================================
with gr.Tab("📥 Download Audio"):
gr.Markdown("### Download BBC Audio Programmes")
with gr.Row():
with gr.Column():
gr.Markdown("#### Option 1: RSS Feed (Recommended)")
rss_url = gr.Textbox(
label="RSS Feed URL",
placeholder="https://podcasts.files.bbci.co.uk/p00fzl9g.rss",
lines=1
)
rss_limit = gr.Number(label="Max Episodes", value=5, precision=0)
rss_btn = gr.Button("Download from RSS", variant="primary")
gr.Markdown("#### Popular BBC Feeds")
feeds_btn = gr.Button("Show Popular Feeds")
with gr.Column():
gr.Markdown("#### Option 2: get_iplayer")
iplayer_query = gr.Textbox(
label="Search Query or URL",
placeholder="Reith Lectures",
lines=1
)
iplayer_btn = gr.Button("Search/Download")
download_output = gr.Textbox(label="Output", lines=10)
gr.Markdown("#### Downloaded Files")
refresh_downloads_btn = gr.Button("Refresh List")
downloads_list = gr.Textbox(label="Audio Files", lines=5)
# Event handlers
rss_btn.click(download_from_rss, [rss_url, rss_limit], download_output)
iplayer_btn.click(download_with_iplayer, iplayer_query, download_output)
feeds_btn.click(get_popular_feeds, None, download_output)
refresh_downloads_btn.click(list_downloads, None, downloads_list)
gr.Markdown("""
---
### 💡 Quick Start Guide
1. **Download**: Use the RSS feed URL for Reith Lectures: `https://podcasts.files.bbci.co.uk/p02p8xh7.rss`
2. **Transcribe**: Select downloaded audio and click "Transcribe" (uses free Whisper AI)
3. **Chat**: Load transcripts to vector store, then ask questions about the content!
**Note**: Make sure to set your `GOOGLE_AI_API_KEY` in the `.env` file for chat functionality.
""")
# ====================================================================
# TAB 2: TRANSCRIBE
# ====================================================================
with gr.Tab("📝 Transcribe"):
gr.Markdown("### Transcribe Audio to Text")
# Recommendations at the top
gr.Markdown("### 🎯 What to Listen Next?")
gr.Markdown("Get AI-powered recommendations based on what you've completed")
with gr.Row():
with gr.Column(scale=2):
recommendations_display = gr.Markdown(value="Click 'Get Recommendations' to see personalized suggestions!")
with gr.Column(scale=1):
get_recommendations_btn = gr.Button("✨ Get Recommendations", variant="primary", size="lg")
gr.Markdown("---")
with gr.Row():
with gr.Column():
audio_file = gr.Dropdown(
label="Select Audio File (Recommended first)",
choices=[
(file_manager.format_display_name(f), str(f))
for f in file_manager.list_audio_files_sorted_by_date()
if file_manager.format_display_name(f) not in history_manager.get_completed_content_names()
],
interactive=True
)
refresh_audio_btn = gr.Button("Refresh Audio List")
model_size = gr.Dropdown(
label="Whisper Model Size",
choices=["tiny", "base", "small", "medium", "large"],
value="base",
info="Larger = more accurate but slower"
)
language = gr.Textbox(
label="Language Code",
value="english",
info="Use 'english', 'spanish', 'french', etc."
)
transcribe_btn = gr.Button("Transcribe Selected File", variant="primary")
with gr.Column():
transcribe_output = gr.Textbox(label="Status", lines=10)
# Event handlers
refresh_audio_btn.click(
lambda: gr.Dropdown(choices=[
(file_manager.format_display_name(f), str(f))
for f in file_manager.list_audio_files_sorted_by_date()
if file_manager.format_display_name(f) not in history_manager.get_completed_content_names()
]),
None,
audio_file
)
transcribe_btn.click(
transcribe_file,
[audio_file, model_size, language],
transcribe_output
)
gr.Markdown("---")
gr.Markdown("#### 📄 Export to PDF")
gr.Markdown("Generate formatted PDF documents from your transcripts for offline reading")
with gr.Row():
with gr.Column():
transcript_selector = gr.Dropdown(
label="Select Transcript to Export",
choices=[
t.name for t in file_manager.list_transcripts()
if file_manager.format_display_name(t) not in history_manager.get_completed_content_names()
],
interactive=True
)
refresh_pdf_list_btn = gr.Button("Refresh Transcript List")
with gr.Row():
export_single_btn = gr.Button("📄 Export Selected to PDF", variant="primary")
export_all_btn = gr.Button("📚 Export All to PDF")
with gr.Column():
pdf_output = gr.Textbox(label="Export Status", lines=3)
pdf_file = gr.File(label="Download PDF", visible=True)
# PDF Export Event handlers
refresh_pdf_list_btn.click(
lambda: gr.Dropdown(choices=[
t.name for t in file_manager.list_transcripts()
if file_manager.format_display_name(t) not in history_manager.get_completed_content_names()
]),
None,
transcript_selector
)
export_single_btn.click(
export_transcript_to_pdf,
transcript_selector,
[pdf_output, pdf_file]
)
export_all_btn.click(
export_all_transcripts_to_pdf,
None,
pdf_output
)
# Recommendation event handler - updates both display and audio dropdown
get_recommendations_btn.click(
get_recommendations,
None,
[recommendations_display, audio_file]
)
# ====================================================================
# TAB 3: PDF READER
# ====================================================================
with gr.Tab("📖 Read & Listen"):
gr.Markdown("### Read Transcripts While Listening to Audio")
gr.Markdown("Select content to view the PDF transcript alongside the audio player")
with gr.Row():
with gr.Column(scale=1):
content_selector = gr.Dropdown(
label="Select Content",
choices=[c['name'] for c in get_available_content()],
interactive=True
)
refresh_content_btn = gr.Button("🔄 Refresh Content List")
load_content_btn = gr.Button("📖 Load Content", variant="primary")
generate_pdf_btn = gr.Button("📄 Generate PDF (if missing)")
mark_completed_btn = gr.Button("✅ Mark as Completed", variant="secondary")
reader_status = gr.Textbox(label="Status", lines=2)
gr.Markdown("---")
gr.Markdown("---")
# Audio player at the top - compact
gr.Markdown("#### 🎧 Audio Player")
audio_player = gr.Audio(
label="",
type="filepath",
interactive=False,
show_label=False
)
gr.Markdown("---")
# PDF viewer takes full width for optimal reading
gr.Markdown("#### 📄 PDF Transcript")
pdf_viewer = gr.HTML(
label="PDF Transcript",
value="<p style='text-align: center; padding: 50px; color: #666;'>Select content and click 'Load Content' to view PDF</p>"
)
# Event handlers for PDF Reader
refresh_content_btn.click(
lambda: gr.Dropdown(choices=[c['name'] for c in get_available_content()]),
None,
content_selector
)
load_content_btn.click(
load_content_for_reading,
content_selector,
[audio_player, pdf_viewer, reader_status]
)
generate_pdf_btn.click(
generate_pdf_for_reader,
content_selector,
[pdf_viewer, reader_status]
)
mark_completed_btn.click(
mark_content_as_completed,