-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhancer.py
More file actions
1372 lines (1187 loc) · 52.8 KB
/
enhancer.py
File metadata and controls
1372 lines (1187 loc) · 52.8 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
"""Flashcard enhancement engine using Claude AI and Azure TTS."""
import asyncio
import csv
import hashlib
import json
import logging
import os
import re
import shutil
import time
import zipfile
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime, timezone
from pathlib import Path
from typing import Dict, List, Optional
import anthropic
import azure.cognitiveservices.speech as speechsdk
from azure.identity import DefaultAzureCredential
from dotenv import load_dotenv
from rich.console import Console
from rich.progress import BarColumn, Progress, SpinnerColumn, TextColumn
from models import (
AdditionalMeaning,
AudioFiles,
BatchInfo,
BatchProcessingStatus,
BatchProgress,
BatchResult,
CardData,
EnhancedCard,
EnhancedContent,
EnhancementConfig,
ExampleSentence,
ProcessingProgress,
current_version,
)
load_dotenv()
console = Console()
class FlashcardEnhancer:
"""Enhanced flashcard generator using Claude AI and Azure TTS."""
def __init__(self, config: EnhancementConfig):
self.config = config
self._setup_logging()
self._setup_directories()
# Initialize Claude client
self.client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
# Initialize Azure TTS if enabled
self._speech_config = None
if self.config.tts.enabled:
self._init_azure_tts()
def _setup_logging(self):
self.config.log_dir.mkdir(parents=True, exist_ok=True)
logging.basicConfig(
level=logging.WARNING,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler(self.config.log_dir / "enhancement.log"),
logging.StreamHandler(),
],
)
self.logger = logging.getLogger(__name__)
def _setup_directories(self):
for d in [
self.config.cached_cards_dir,
self.config.audio_dir,
self.config.output_dir,
self.config.input_dir,
self.config.log_dir,
]:
d.mkdir(parents=True, exist_ok=True)
def _init_azure_tts(self):
endpoint = os.environ.get("SPEECH_ENDPOINT")
resource_id = os.environ.get("SPEECH_RESOURCE_ID")
if not endpoint or not resource_id:
self.logger.warning(
"SPEECH_ENDPOINT or SPEECH_RESOURCE_ID not set, TTS disabled"
)
self.config.tts.enabled = False
return
try:
self._speech_config = speechsdk.SpeechConfig(endpoint=endpoint)
aad_token = (
DefaultAzureCredential()
.get_token("https://cognitiveservices.azure.com/.default")
.token
)
self._speech_config.authorization_token = (
f"aad#{resource_id}#{aad_token}"
)
self._speech_config.set_speech_synthesis_output_format(
speechsdk.SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3
)
console.print("[green]Azure TTS initialized[/green]")
except Exception as e:
self.logger.warning(f"Failed to init Azure TTS: {e}")
self.config.tts.enabled = False
def _get_target_language(self) -> str:
"""Infer target language from the Azure voice name."""
voice = self.config.tts.azure.male_voice
# Voice names like zu-ZA-ThembaNeural -> extract language
lang_map = {
"zu": "Zulu",
"af": "Afrikaans",
"sw": "Swahili",
"ar": "Arabic",
"fa": "Persian",
"ja": "Japanese",
"ko": "Korean",
"zh": "Chinese",
"es": "Spanish",
"fr": "French",
"de": "German",
"pt": "Portuguese",
"hi": "Hindi",
"ru": "Russian",
}
lang_code = voice.split("-")[0] if "-" in voice else "en"
return lang_map.get(lang_code, lang_code)
# --- CSV Loading ---
def load_cards(self) -> List[CardData]:
"""Load cards from all CSV files in the frequency directory."""
freq_dir = Path(self.config.frequency_dir)
if not freq_dir.exists():
console.print(
f"[red]Frequency directory not found: {freq_dir}[/red]"
)
return []
cards = []
csv_files = sorted(freq_dir.glob("*.csv"))
for csv_file in csv_files:
console.print(f"[blue]Loading {csv_file.name}...[/blue]")
with open(csv_file, "r", encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
if len(row) < 2:
continue
word = row[0].strip()
english = row[1].strip()
if not word or not english:
continue
card_id = hashlib.md5(word.encode()).hexdigest()[:8]
cards.append(CardData(id=card_id, word=word, english=english))
console.print(f"[green]Loaded {len(cards)} cards from {len(csv_files)} files[/green]")
return cards
# --- Claude Enhancement ---
def _build_enhancement_prompt(self, card: CardData) -> str:
"""Build the Claude prompt for enhancing a card."""
target_lang = self._get_target_language()
return f"""Provide detailed linguistic information for this {target_lang} word/phrase.
{target_lang} word/phrase: {card.word}
English meaning: {card.english}
Please provide the following in JSON format:
1. **word**: The {target_lang} word/phrase (as provided)
2. **romanization**: Romanization for an English speaker
3. **pronunciation_ipa**: IPA phonetic transcription in square brackets
4. **main_part_of_speech**: Part of speech (e.g., "n.", "adj.", "v.", "pron.", "prep.")
5. **examples**: Array of 3 example sentences, each with "target_language" ({target_lang} sentence) and "english" (translation) keys
6. **etymology**: Brief origin and development (2-3 sentences)
7. **additional_meanings**: Array of alternative meanings, each with "meaning" and "part_of_speech" keys
8. **pos_tags**: Array of part-of-speech tags (e.g., ["noun"], ["verb", "transitive"])
Format as valid JSON only:
{{
"word": "{card.word}",
"romanization": "romanization here",
"pronunciation_ipa": "[IPA here]",
"main_part_of_speech": "n.",
"examples": [
{{"target_language": "{target_lang} sentence", "english": "English translation"}},
{{"target_language": "{target_lang} sentence", "english": "English translation"}},
{{"target_language": "{target_lang} sentence", "english": "English translation"}}
],
"etymology": "Etymology explanation...",
"additional_meanings": [
{{"meaning": "alternative meaning", "part_of_speech": "n."}}
],
"pos_tags": ["part_of_speech"]
}}
Focus on accuracy. Provide authentic {target_lang} examples that naturally use this word."""
def _parse_enhancement_response(
self, response_text: str, card: CardData
) -> Optional[EnhancedContent]:
"""Parse a Claude response into EnhancedContent."""
json_start = response_text.find("{")
json_end = response_text.rfind("}") + 1
if json_start < 0 or json_end <= json_start:
self.logger.error(f"No JSON in response for '{card.word}'")
return None
json_text = response_text[json_start:json_end]
try:
data = json.loads(json_text)
except json.JSONDecodeError:
cleaned = json_text.replace("\n", "\\n").replace("\r", "\\r")
try:
data = json.loads(cleaned)
except json.JSONDecodeError:
self.logger.error(f"JSON parse failed for '{card.word}'")
return None
examples = [
ExampleSentence(
target_language=ex.get("target_language", ""),
english=ex.get("english", ""),
)
for ex in data.get("examples", [])
]
additional_meanings = []
for md in data.get("additional_meanings", []):
if isinstance(md, dict):
additional_meanings.append(
AdditionalMeaning(
meaning=md.get("meaning", ""),
part_of_speech=md.get("part_of_speech", "n."),
)
)
return EnhancedContent(
word=data.get("word", card.word),
romanization=data.get("romanization", ""),
pronunciation_ipa=data.get("pronunciation_ipa", ""),
main_part_of_speech=data.get("main_part_of_speech", "n."),
examples=examples,
etymology=data.get("etymology", ""),
additional_meanings=additional_meanings,
pos_tags=data.get("pos_tags", []),
)
def enhance_card_with_claude(
self, card: CardData
) -> Optional[EnhancedContent]:
"""Generate enhanced content using Claude AI (single request)."""
prompt = self._build_enhancement_prompt(card)
try:
message = self.client.messages.create(
model=self.config.model,
max_tokens=1000,
temperature=0.3,
messages=[{"role": "user", "content": prompt}],
)
return self._parse_enhancement_response(message.content[0].text, card)
except Exception as e:
self.logger.error(f"Claude API error for '{card.word}': {e}")
return None
# --- Azure TTS ---
def generate_tts_audio(
self, text: str, voice_name: str, filename: str
) -> Optional[str]:
"""Generate TTS audio using Azure Speech SDK."""
if not self.config.tts.enabled or not self._speech_config:
return None
audio_path = self.config.audio_dir / filename
if audio_path.exists():
console.print(f"[cyan]Using existing audio: {filename}[/cyan]")
return str(audio_path)
try:
audio_config = speechsdk.audio.AudioOutputConfig(filename=str(audio_path))
synthesizer = speechsdk.SpeechSynthesizer(
speech_config=self._speech_config, audio_config=audio_config
)
# Extract language from voice name (e.g. zu-ZA-ThembaNeural -> zu-ZA)
parts = voice_name.split("-")
lang = f"{parts[0]}-{parts[1]}" if len(parts) >= 2 else "en-US"
ssml = (
f'<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="{lang}">'
f'<voice name="{voice_name}">'
f"{text}"
f"</voice></speak>"
)
result = synthesizer.speak_ssml_async(ssml).get()
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
console.print(f"[green]Generated audio: {filename}[/green]")
return str(audio_path)
else:
details = result.cancellation_details
self.logger.error(
f"TTS failed for '{text}': {details.reason} - {details.error_details}"
)
return None
except Exception as e:
self.logger.error(f"TTS error for '{text}': {e}")
return None
def generate_card_audio(
self, card: CardData, enhanced: EnhancedContent, card_index: int
) -> AudioFiles:
"""Generate all audio files for a card (word + examples)."""
audio_files = AudioFiles()
if not self.config.tts.enabled:
return audio_files
try:
# Generate male voice for main word
word_filename = f"word_{card.id}.mp3"
word_audio = self.generate_tts_audio(
enhanced.word, self.config.tts.azure.male_voice, word_filename
)
if word_audio:
audio_files.word = word_audio
# Generate example sentence audio (alternate voices)
for i, example in enumerate(enhanced.examples):
use_male = (card_index + i) % 2 == 0
voice = (
self.config.tts.azure.male_voice
if use_male
else self.config.tts.azure.female_voice
)
voice_label = "male" if use_male else "female"
example_filename = f"example_{card.id}_{i}_{voice_label}.mp3"
example_audio = self.generate_tts_audio(
example.target_language, voice, example_filename
)
if example_audio:
audio_files.examples.append(example_audio)
example.audio_file = example_audio
except Exception as e:
self.logger.error(f"Audio generation failed for card {card.id}: {e}")
return audio_files
def generate_all_audio_parallel(
self, enhanced_cards: List[EnhancedCard], card_start_index: int = 0
) -> None:
"""Generate all audio files in parallel using ThreadPoolExecutor."""
if not self.config.tts.enabled:
return
# Collect all audio tasks: (text, voice, filename, card_ref, slot)
# slot is ("word",) or ("example", example_index)
tasks = []
for i, ec in enumerate(enhanced_cards):
card_index = card_start_index + i
card = ec.original
# Word audio
word_filename = f"word_{card.id}.mp3"
tasks.append((
ec, enhanced_cards, card_index,
ec.enhanced.word, self.config.tts.azure.male_voice,
word_filename, ("word",)
))
# Example audio
for j, example in enumerate(ec.enhanced.examples):
use_male = (card_index + j) % 2 == 0
voice = (
self.config.tts.azure.male_voice
if use_male
else self.config.tts.azure.female_voice
)
voice_label = "male" if use_male else "female"
example_filename = f"example_{card.id}_{j}_{voice_label}.mp3"
tasks.append((
ec, enhanced_cards, card_index,
example.target_language, voice,
example_filename, ("example", j)
))
# Filter out tasks where audio already exists
pending = []
for task in tasks:
audio_path = self.config.audio_dir / task[5]
if not audio_path.exists():
pending.append(task)
else:
# Apply existing path
self._apply_audio_result(task, str(audio_path))
if not pending:
console.print("[cyan]All audio files already exist[/cyan]")
return
console.print(
f"[blue]Generating {len(pending)} audio files "
f"({len(tasks) - len(pending)} cached) "
f"with {self.config.audio_concurrency} workers...[/blue]"
)
completed = 0
failed = 0
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
console=console,
) as prog:
progress_task = prog.add_task("Generating audio...", total=len(pending))
with ThreadPoolExecutor(max_workers=self.config.audio_concurrency) as executor:
future_to_task = {
executor.submit(
self.generate_tts_audio, t[3], t[4], t[5]
): t
for t in pending
}
for future in as_completed(future_to_task):
task_info = future_to_task[future]
try:
result = future.result()
if result:
self._apply_audio_result(task_info, result)
completed += 1
else:
failed += 1
except Exception as e:
self.logger.error(f"Audio task failed: {e}")
failed += 1
prog.advance(progress_task)
console.print(
f"[green]Audio generation complete: {completed} succeeded, {failed} failed[/green]"
)
def _apply_audio_result(self, task_info: tuple, audio_path: str) -> None:
"""Apply an audio generation result back to the EnhancedCard."""
ec = task_info[0]
slot = task_info[6]
if slot[0] == "word":
ec.enhanced.audio_files.word = audio_path
elif slot[0] == "example":
example_idx = slot[1]
# Ensure examples list is long enough
while len(ec.enhanced.audio_files.examples) <= example_idx:
ec.enhanced.audio_files.examples.append("")
ec.enhanced.audio_files.examples[example_idx] = audio_path
if example_idx < len(ec.enhanced.examples):
ec.enhanced.examples[example_idx].audio_file = audio_path
# --- Cleanup ---
def clean_empty_audio(self) -> int:
"""Delete empty (0-byte) audio files from the audio directory."""
removed = 0
for f in self.config.audio_dir.glob("*.mp3"):
if f.stat().st_size == 0:
f.unlink()
removed += 1
if removed:
console.print(f"[yellow]Removed {removed} empty audio files[/yellow]")
else:
console.print("[green]No empty audio files found[/green]")
return removed
# --- Progress ---
def load_progress(self) -> ProcessingProgress:
if self.config.progress_file.exists():
try:
with open(self.config.progress_file, "r") as f:
return ProcessingProgress(**json.load(f))
except Exception:
pass
return ProcessingProgress()
def save_progress(self, progress: ProcessingProgress):
with open(self.config.progress_file, "w") as f:
json.dump(progress.model_dump(), f, indent=2)
# --- Batch API ---
def load_batch_progress(self) -> BatchProgress:
if self.config.batch_progress_file.exists():
try:
with open(self.config.batch_progress_file, "r") as f:
return BatchProgress(**json.load(f))
except Exception:
pass
return BatchProgress()
def save_batch_progress(self, progress: BatchProgress):
with open(self.config.batch_progress_file, "w") as f:
json.dump(progress.model_dump(), f, indent=2)
def submit_batch(self, cards: List[CardData]) -> Optional[BatchInfo]:
"""Submit a batch of cards for Claude processing."""
console.print(f"[blue]Submitting batch of {len(cards)} cards...[/blue]")
requests = []
card_id_mapping = {}
for card in cards:
sanitized_id = card.id
card_id_mapping[sanitized_id] = card.id
prompt = self._build_enhancement_prompt(card)
requests.append({
"custom_id": sanitized_id,
"params": {
"model": self.config.model,
"max_tokens": 1000,
"temperature": 0.3,
"messages": [{"role": "user", "content": prompt}],
},
})
try:
batch_response = self.client.messages.batches.create(requests=requests)
batch_info = BatchInfo(
batch_id=batch_response.id,
status=BatchProcessingStatus(batch_response.processing_status),
created_at=str(batch_response.created_at),
expires_at=str(batch_response.expires_at) if batch_response.expires_at else None,
total_requests=len(requests),
card_id_mapping=card_id_mapping,
)
console.print(f"[green]Batch submitted: {batch_info.batch_id}[/green]")
return batch_info
except Exception as e:
self.logger.error(f"Failed to submit batch: {e}")
console.print(f"[red]Failed to submit batch: {e}[/red]")
return None
def check_batch_status(self, batch_id: str) -> Optional[BatchInfo]:
"""Check the status of a submitted batch."""
try:
resp = self.client.messages.batches.retrieve(batch_id)
status_mapping = {
"in_progress": BatchProcessingStatus.IN_PROGRESS,
"canceling": BatchProcessingStatus.CANCELING,
"ended": BatchProcessingStatus.ENDED,
}
return BatchInfo(
batch_id=resp.id,
status=status_mapping.get(
resp.processing_status, BatchProcessingStatus.IN_PROGRESS
),
created_at=str(resp.created_at),
expires_at=str(resp.expires_at) if resp.expires_at else None,
ended_at=str(resp.ended_at) if resp.ended_at else None,
results_url=resp.results_url,
total_requests=(
resp.request_counts.processing
+ resp.request_counts.succeeded
+ resp.request_counts.errored
+ resp.request_counts.canceled
+ resp.request_counts.expired
),
succeeded=resp.request_counts.succeeded,
failed=resp.request_counts.errored + resp.request_counts.canceled + resp.request_counts.expired,
processing=resp.request_counts.processing,
)
except Exception as e:
self.logger.error(f"Failed to check batch status: {e}")
return None
def download_batch_results(self, batch_info: BatchInfo) -> List[BatchResult]:
"""Download and parse batch results."""
if not batch_info.results_url:
self.logger.error("No results URL available")
return []
try:
console.print("[blue]Downloading batch results...[/blue]")
import httpx
with httpx.Client() as client:
response = client.get(
batch_info.results_url,
headers={
"x-api-key": os.getenv("ANTHROPIC_API_KEY"),
"anthropic-version": "2023-06-01",
},
)
response.raise_for_status()
results = []
for line in response.text.strip().split("\n"):
if line:
data = json.loads(line)
results.append(
BatchResult(
custom_id=data["custom_id"],
result=data.get("result"),
error=data.get("error"),
)
)
console.print(f"[green]Downloaded {len(results)} results[/green]")
return results
except Exception as e:
self.logger.error(f"Failed to download batch results: {e}")
console.print(f"[red]Failed to download results: {e}[/red]")
return []
def _build_batch(
self,
cards: List[CardData],
start: int = 0,
build_start: Optional[float] = None,
) -> bool:
"""Run the build pipeline using the Claude Batch API."""
batch_progress = self.load_batch_progress()
batch_progress.total_cards = len(cards)
# Separate cached vs uncached cards
cached_cards = []
uncached_cards = []
for card in cards:
cached_path = self.config.cached_cards_dir / f"{card.id}.json"
if cached_path.exists():
try:
with open(cached_path, "r", encoding="utf-8") as f:
ec = EnhancedCard(**json.load(f))
cached_cards.append(ec)
continue
except Exception:
pass
uncached_cards.append(card)
if cached_cards:
console.print(f"[cyan]{len(cached_cards)} cards loaded from cache[/cyan]")
if not uncached_cards:
console.print("[cyan]All cards already cached[/cyan]")
all_enhanced = cached_cards
else:
# Submit uncached cards in chunks of batch_api_size
cards_map = {c.id: c for c in uncached_cards}
for i in range(0, len(uncached_cards), self.config.batch_size):
chunk = uncached_cards[i : i + self.config.batch_size]
batch_info = self.submit_batch(chunk)
if batch_info:
batch_progress.batches.append(batch_info)
batch_progress.cards_by_batch[batch_info.batch_id] = [
c.id for c in chunk
]
self.save_batch_progress(batch_progress)
# Poll until all batches complete
while batch_progress.active_batches:
active = batch_progress.active_batches
console.print(
f"[blue]Waiting for {len(active)} batch(es)... "
f"checking every {self.config.batch_check_interval}s[/blue]"
)
for batch_info in active:
updated = self.check_batch_status(batch_info.batch_id)
if updated:
for j, b in enumerate(batch_progress.batches):
if b.batch_id == batch_info.batch_id:
# Preserve card_id_mapping from original submission
updated.card_id_mapping = b.card_id_mapping
batch_progress.batches[j] = updated
break
console.print(
f" Batch {updated.batch_id[:12]}... "
f"status={updated.status.value} "
f"succeeded={updated.succeeded} "
f"failed={updated.failed} "
f"processing={updated.processing}"
)
if updated.status == BatchProcessingStatus.ENDED:
console.print(
f"[green]Batch {updated.batch_id[:12]}... completed![/green]"
)
self.save_batch_progress(batch_progress)
if batch_progress.active_batches:
time.sleep(self.config.batch_check_interval)
# Download and process results from all completed batches
new_enhanced = []
for batch_info in batch_progress.batches:
if batch_info.status != BatchProcessingStatus.ENDED:
continue
if not batch_info.results_url:
# Re-fetch to get results_url
batch_info = self.check_batch_status(batch_info.batch_id)
if not batch_info or not batch_info.results_url:
continue
results = self.download_batch_results(batch_info)
for result in results:
card = cards_map.get(result.custom_id)
if not card:
continue
if result.error:
self.logger.error(
f"Batch error for {card.word}: {result.error}"
)
batch_progress.failed_cards.append(card.id)
continue
try:
response_text = result.result["message"]["content"][0]["text"]
enhanced = self._parse_enhancement_response(
response_text, card
)
if enhanced:
ec = EnhancedCard(original=card, enhanced=enhanced)
new_enhanced.append(ec)
batch_progress.completed_cards.append(card.id)
else:
batch_progress.failed_cards.append(card.id)
except Exception as e:
self.logger.error(
f"Failed to process batch result for {card.word}: {e}"
)
batch_progress.failed_cards.append(card.id)
self.save_batch_progress(batch_progress)
console.print(
f"[green]Batch processing complete: "
f"{len(new_enhanced)} succeeded, "
f"{len(batch_progress.failed_cards)} failed[/green]"
)
# Generate audio in parallel for new cards
if new_enhanced:
self.generate_all_audio_parallel(new_enhanced, card_start_index=start)
# Save to cache
for ec in new_enhanced:
cached_path = self.config.cached_cards_dir / f"{ec.original.id}.json"
with open(cached_path, "w", encoding="utf-8") as f:
json.dump(ec.model_dump(), f, indent=2, ensure_ascii=False)
all_enhanced = cached_cards + new_enhanced
# Generate audio for cached cards that may be missing audio
if cached_cards:
self.generate_all_audio_parallel(cached_cards, card_start_index=start)
for ec in cached_cards:
cached_path = self.config.cached_cards_dir / f"{ec.original.id}.json"
with open(cached_path, "w", encoding="utf-8") as f:
json.dump(ec.model_dump(), f, indent=2, ensure_ascii=False)
console.print(
f"\n[green]Enhanced {len(all_enhanced)}/{len(cards)} cards[/green]"
)
if all_enhanced:
self.create_mochi_file(all_enhanced)
self._print_timing_summary(build_start, len(all_enhanced))
return len(all_enhanced) > 0
# --- Review Data Preservation ---
def extract_review_data_from_mochi(self, mochi_path: Path) -> Dict[str, Dict]:
"""Extract review data from an existing .mochi file."""
review_data = {}
if not mochi_path.exists():
return review_data
temp_dir = self.config.build_dir / "temp_extraction"
try:
temp_dir.mkdir(exist_ok=True)
with zipfile.ZipFile(mochi_path, "r") as zf:
zf.extractall(temp_dir)
data_file = temp_dir / "data.json"
if not data_file.exists():
return review_data
with open(data_file, "r", encoding="utf-8") as f:
data = json.load(f)
for deck in data.get("~:decks", []):
if not isinstance(deck, dict):
continue
cards = deck.get("~:cards", {})
card_list = (
cards.get("~#list", [])
if isinstance(cards, dict)
else cards if isinstance(cards, list) else []
)
for card in card_list:
if not isinstance(card, dict):
continue
fields = card.get("~:fields", {})
name_field = fields.get("~:name", {})
card_name = (
name_field.get("~:value", "")
if isinstance(name_field, dict)
else ""
)
# Strip audio markers from name
card_name_clean = re.sub(
r"\n\n!\[[^\]]*\]\([^)]+\)", "", card_name
).strip()
if not card_name_clean:
continue
card_review = {
"reviews": card.get("~:reviews", []),
"reverse_reviews": card.get("~:reverse-reviews", []),
"cloze_reviews": card.get("~:cloze/reviews", {}),
"needs_rereview": card.get("~:needs-rereview?", False),
"reverse_needs_rereview": card.get(
"~:reverse/needs-rereview?", False
),
"cloze_needs_rereview": card.get(
"~:cloze/needs-rereview?", {}
),
"created_at": card.get("~:created-at", {}),
"cloze_indexes": card.get(
"~:cloze/indexes", {"~#set": []}
),
"references": card.get("~:references", {"~#set": []}),
}
if (
card_review["reviews"]
or card_review["reverse_reviews"]
or card_review["cloze_reviews"]
):
review_data[card_name_clean] = card_review
except Exception as e:
self.logger.error(f"Failed to extract review data: {e}")
finally:
shutil.rmtree(temp_dir, ignore_errors=True)
return review_data
def load_existing_review_data(self) -> Dict[str, Dict]:
"""Load review data from all .mochi files in the input directory."""
all_review_data = {}
if not self.config.input_dir.exists():
return all_review_data
for mochi_file in self.config.input_dir.glob("*.mochi"):
console.print(f"[blue]Loading review data from {mochi_file.name}...[/blue]")
review_data = self.extract_review_data_from_mochi(mochi_file)
all_review_data.update(review_data)
if all_review_data:
console.print(
f"[green]Found review data for {len(all_review_data)} cards[/green]"
)
return all_review_data
# --- Mochi File Creation ---
def create_mochi_file(
self, enhanced_cards: List[EnhancedCard], deck_name: Optional[str] = None
) -> bool:
"""Create a .mochi file from enhanced cards."""
try:
final_deck_name = deck_name or self.config.deck_name
existing_review_data = self.load_existing_review_data()
def det_id(name: str) -> str:
return f"~:{hashlib.md5(name.encode()).hexdigest()[:8]}"
deck_id = det_id(f"deck_{final_deck_name}")
template_id = det_id(f"template_{final_deck_name}")
timestamp_ms = int(datetime.now().timestamp() * 1000)
# Field IDs
english_fid = det_id("english_field")
word_helper_fid = det_id("word_helper_field")
pos_fid = det_id("main_pos_field")
add_meanings_fid = det_id("add_meanings_field")
roman_fid = det_id("romanization_field")
ipa_fid = det_id("ipa_field")
examples_fid = det_id("examples_field")
etymology_fid = det_id("etymology_field")
final_english_fid = det_id("final_english_field")
template = {
"~:id": template_id,
"~:pos": "A",
"~:name": final_deck_name,
"~:content": (
"# <<Word>>\n\n---\n\n### <<WordHelper>>\n\n"
"(<<MainPartOfSpeech>> <<English>>)\n<<AdditionalMeanings>>\n\n"
"<<Romanization>>\n<<IPA>>\n\n"
"**Examples:**\n\n<<Examples>>\n\n"
"**Etymology:**\n<<Etymology>>\n\n"
"---\n\n# <<FinalEnglish>>"
),
"~:cloze?": None,
"~:fields": {
"~:name": {
"~:id": "~:name",
"~:pos": "k0",
"~:name": "Word",
"~:type": "~:text",
},
word_helper_fid: {
"~:id": word_helper_fid,
"~:pos": "k1",
"~:name": "WordHelper",
"~:type": "~:text",
},
english_fid: {
"~:id": english_fid,
"~:pos": "k2",
"~:name": "English",
"~:type": "~:text",
},
pos_fid: {
"~:id": pos_fid,
"~:pos": "k3",
"~:name": "MainPartOfSpeech",
"~:type": "~:text",
},
add_meanings_fid: {
"~:id": add_meanings_fid,
"~:pos": "k4",
"~:name": "AdditionalMeanings",
"~:type": "~:text",
},
roman_fid: {
"~:id": roman_fid,
"~:pos": "k5",
"~:name": "Romanization",
"~:type": "~:text",
},
ipa_fid: {
"~:id": ipa_fid,
"~:pos": "k6",
"~:name": "IPA",
"~:type": "~:text",
},
examples_fid: {
"~:id": examples_fid,
"~:pos": "k7",
"~:name": "Examples",
"~:type": "~:text",
},
etymology_fid: {
"~:id": etymology_fid,
"~:pos": "k8",
"~:name": "Etymology",
"~:type": "~:text",
},
final_english_fid: {
"~:id": final_english_fid,
"~:pos": "k9",
"~:name": "FinalEnglish",
"~:type": "~:text",
},
},