summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-07-15 23:42:32 +0300
committerPaul Buetow <paul@buetow.org>2025-07-15 23:42:32 +0300
commitb105333c061ea165b3b79317415cbb8b9cfb7c75 (patch)
treec2682cc156c372d85ab52d514df4316ceda9071d /cmd
parent61529facc2c5321de9f0ab9123cb1de25bcab62c (diff)
feat: add English translations and detailed attribution files
- Automatic Bulgarian to English translation for all words - Save translations to separate _translation.txt files - Include translations in Anki CSV export - Add detailed attribution files for audio and images: - Audio: model, voice, speed, instructions, processed text - Image: model, size, quality, style, full prompt used - Expand image styles to 42 different options (including superhero comic, yoga, etc.) - Improve image prompts to strongly avoid text generation - Fix image overwrite issue - now overwrites existing files instead of failing 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat (limited to 'cmd')
-rw-r--r--cmd/totalrecall/main.go129
1 files changed, 124 insertions, 5 deletions
diff --git a/cmd/totalrecall/main.go b/cmd/totalrecall/main.go
index 2a43d51..658301d 100644
--- a/cmd/totalrecall/main.go
+++ b/cmd/totalrecall/main.go
@@ -212,6 +212,22 @@ func runCommand(cmd *cobra.Command, args []string) error {
}
func processWord(word string) error {
+ // Translate the word first
+ fmt.Printf(" Translating to English...\n")
+ translation, err := translateWord(word)
+ if err != nil {
+ fmt.Printf(" Warning: Translation failed: %v\n", err)
+ translation = "" // Continue without translation
+ } else {
+ fmt.Printf(" Translation: %s\n", translation)
+ // Store translation for Anki export
+ wordTranslations[word] = translation
+ // Save translation to file
+ if err := saveTranslation(word, translation); err != nil {
+ fmt.Printf(" Warning: Failed to save translation: %v\n", err)
+ }
+ }
+
// Generate audio
if !skipAudio {
fmt.Printf(" Generating audio...\n")
@@ -311,13 +327,25 @@ func generateAudioWithVoice(word, voice string) error {
filename := sanitizeFilename(word)
// Add voice name to filename if generating multiple voices
+ var outputFile string
if allVoices {
- outputFile := filepath.Join(outputDir, fmt.Sprintf("%s_%s.%s", filename, voice, audioFormat))
- return provider.GenerateAudio(ctx, word, outputFile)
+ outputFile = filepath.Join(outputDir, fmt.Sprintf("%s_%s.%s", filename, voice, audioFormat))
+ } else {
+ outputFile = filepath.Join(outputDir, fmt.Sprintf("%s.%s", filename, audioFormat))
}
- outputFile := filepath.Join(outputDir, fmt.Sprintf("%s.%s", filename, audioFormat))
- return provider.GenerateAudio(ctx, word, outputFile)
+ // Generate the audio
+ err = provider.GenerateAudio(ctx, word, outputFile)
+ if err != nil {
+ return err
+ }
+
+ // Save audio attribution
+ if err := saveAudioAttribution(word, outputFile, providerConfig); err != nil {
+ fmt.Printf(" Warning: Failed to save audio attribution: %v\n", err)
+ }
+
+ return nil
}
func downloadImages(word string) error {
@@ -388,7 +416,7 @@ func downloadImages(word string) error {
// Create downloader
downloadOpts := &image.DownloadOptions{
OutputDir: outputDir,
- OverwriteExisting: false,
+ OverwriteExisting: true, // Allow overwriting existing files
CreateDir: true,
FileNamePattern: "{word}_{index}",
MaxSizeBytes: 5 * 1024 * 1024, // 5MB
@@ -490,6 +518,13 @@ func generateAnkiCSV() error {
return fmt.Errorf("failed to generate cards: %w", err)
}
+ // Add translations to cards
+ for i := range gen.GetCards() {
+ if translation, ok := wordTranslations[gen.GetCards()[i].Bulgarian]; ok {
+ gen.GetCards()[i].Translation = translation
+ }
+ }
+
// Generate CSV
if err := gen.GenerateCSV(); err != nil {
return fmt.Errorf("failed to generate CSV: %w", err)
@@ -593,6 +628,90 @@ func listAvailableModels() error {
return nil
}
+func translateWord(word string) (string, error) {
+ // Use OpenAI to translate Bulgarian to English
+ apiKey := getOpenAIKey()
+ if apiKey == "" {
+ return "", fmt.Errorf("OpenAI API key not found")
+ }
+
+ client := openai.NewClient(apiKey)
+ ctx := context.Background()
+
+ req := openai.ChatCompletionRequest{
+ Model: openai.GPT4oMini,
+ Messages: []openai.ChatCompletionMessage{
+ {
+ Role: openai.ChatMessageRoleUser,
+ Content: fmt.Sprintf("Translate the Bulgarian word '%s' to English. Respond with only the English translation, nothing else.", word),
+ },
+ },
+ MaxTokens: 50,
+ Temperature: 0.3,
+ }
+
+ resp, err := client.CreateChatCompletion(ctx, req)
+ if err != nil {
+ return "", fmt.Errorf("OpenAI API error: %w", err)
+ }
+
+ if len(resp.Choices) == 0 {
+ return "", fmt.Errorf("no translation returned")
+ }
+
+ translation := strings.TrimSpace(resp.Choices[0].Message.Content)
+ return translation, nil
+}
+
+func saveTranslation(word, translation string) error {
+ // Save translation to a text file
+ filename := sanitizeFilename(word)
+ outputFile := filepath.Join(outputDir, fmt.Sprintf("%s_translation.txt", filename))
+
+ content := fmt.Sprintf("%s = %s\n", word, translation)
+
+ if err := os.WriteFile(outputFile, []byte(content), 0644); err != nil {
+ return fmt.Errorf("failed to write translation file: %w", err)
+ }
+
+ return nil
+}
+
+// Global map to store translations for Anki export
+var wordTranslations = make(map[string]string)
+
+func saveAudioAttribution(word, audioFile string, config *audio.Config) error {
+ // Create attribution text
+ attribution := fmt.Sprintf("Audio generated by OpenAI TTS\n\n")
+ attribution += fmt.Sprintf("Bulgarian word: %s\n", word)
+ attribution += fmt.Sprintf("Model: %s\n", config.OpenAIModel)
+ attribution += fmt.Sprintf("Voice: %s\n", config.OpenAIVoice)
+ attribution += fmt.Sprintf("Speed: %.2f\n", config.OpenAISpeed)
+
+ if config.OpenAIInstruction != "" {
+ attribution += fmt.Sprintf("\nVoice instructions:\n%s\n", config.OpenAIInstruction)
+ }
+
+ // Add preprocessing information
+ cleanedWord := strings.TrimSpace(word)
+ punctuationToRemove := []string{"!", "?", ".", ",", ";", ":", "\"", "'", "(", ")", "[", "]", "{", "}", "-", "—", "–"}
+ for _, punct := range punctuationToRemove {
+ cleanedWord = strings.ReplaceAll(cleanedWord, punct, "")
+ }
+ processedText := fmt.Sprintf("%s...", strings.TrimSpace(cleanedWord))
+ attribution += fmt.Sprintf("\nProcessed text sent to TTS: %s\n", processedText)
+
+ attribution += fmt.Sprintf("\nGenerated at: %s\n", time.Now().Format("2006-01-02 15:04:05"))
+
+ // Save to file
+ attrPath := strings.TrimSuffix(audioFile, filepath.Ext(audioFile)) + "_attribution.txt"
+ if err := os.WriteFile(attrPath, []byte(attribution), 0644); err != nil {
+ return fmt.Errorf("failed to write audio attribution file: %w", err)
+ }
+
+ return nil
+}
+
func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)