summaryrefslogtreecommitdiff
path: root/internal/gui/generator.go
blob: 14f0f60bbb61db786bee258618745c0f1e644dfc (plain)
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
package gui

import (
	"context"
	"fmt"
	"math/rand"
	"os"
	"path/filepath"
	"strings"
	"time"

	"fyne.io/fyne/v2"
	"github.com/sashabaranov/go-openai"

	"codeberg.org/snonux/totalrecall/internal/audio"
	"codeberg.org/snonux/totalrecall/internal/image"
)

func randomVoiceAndSpeed(voices []string) (string, float64) {
	rng := rand.New(rand.NewSource(time.Now().UnixNano()))
	voice := voices[rng.Intn(len(voices))]
	speed := 0.90 + rng.Float64()*0.10
	return voice, speed
}

// translateWord translates a Bulgarian word to English
func (a *Application) translateWord(word string) (string, error) {
	if a.config.OpenAIKey == "" {
		return "", fmt.Errorf("OpenAI API key not configured")
	}

	client := openai.NewClient(a.config.OpenAIKey)

	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(a.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
}

// translateEnglishToBulgarian translates an English word to Bulgarian
func (a *Application) translateEnglishToBulgarian(word string) (string, error) {
	if a.config.OpenAIKey == "" {
		return "", fmt.Errorf("OpenAI API key not configured")
	}

	client := openai.NewClient(a.config.OpenAIKey)

	req := openai.ChatCompletionRequest{
		Model: openai.GPT4oMini,
		Messages: []openai.ChatCompletionMessage{
			{
				Role:    openai.ChatMessageRoleUser,
				Content: fmt.Sprintf("Translate the English word '%s' to Bulgarian. Respond with only the Bulgarian translation in Cyrillic script, nothing else.", word),
			},
		},
		MaxTokens:   50,
		Temperature: 0.3,
	}

	resp, err := client.CreateChatCompletion(a.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
}

// generateAudio generates audio for a word
func (a *Application) generateAudio(ctx context.Context, word string, cardDir string) (string, error) {
	// Check if this is a regeneration by looking for existing audio file
	isRegeneration := false
	if cardDir != "" {
		audioFile := filepath.Join(cardDir, fmt.Sprintf("audio.%s", a.config.AudioFormat))
		if _, err := os.Stat(audioFile); err == nil {
			isRegeneration = true
		}
	}

	// Always use random voice and speed
	allVoices := []string{"alloy", "ash", "ballad", "coral", "echo", "fable", "onyx", "nova", "sage", "shimmer", "verse"}

	// Select a random voice
	voice, speed := randomVoiceAndSpeed(allVoices)

	// Create a copy of audio config with selected voice and speed
	audioConfig := *a.audioConfig
	audioConfig.OpenAIVoice = voice
	audioConfig.OpenAISpeed = speed
	audioConfig.OutputDir = a.config.OutputDir // Ensure correct output directory

	// Log the audio generation details
	if isRegeneration {
		fmt.Printf("Regenerating audio for '%s' with voice: %s, speed: %.2f\n", word, voice, speed)
	} else {
		fmt.Printf("Generating audio for '%s' with voice: %s, speed: %.2f\n", word, voice, speed)
	}

	// Create audio provider
	provider, err := audio.NewProvider(&audioConfig)
	if err != nil {
		return "", err
	}

	// Use the provided card directory
	if cardDir == "" {
		return "", fmt.Errorf("card directory not provided")
	}

	// Generate filename in subdirectory
	outputFile := filepath.Join(cardDir, fmt.Sprintf("audio.%s", a.config.AudioFormat))

	// Generate audio
	err = provider.GenerateAudio(ctx, word, outputFile)
	if err != nil {
		return "", err
	}

	// Save audio attribution
	if err := a.saveAudioAttribution(word, outputFile, voice, speed); err != nil {
		// Non-fatal error, just log it
		fmt.Printf("Warning: Failed to save audio attribution: %v\n", err)
	}

	// Save voice metadata for GUI display
	metadataFile := filepath.Join(cardDir, "audio_metadata.txt")
	metadata := fmt.Sprintf("voice=%s\nspeed=%.2f\n", voice, speed)
	if err := os.WriteFile(metadataFile, []byte(metadata), 0644); err != nil {
		fmt.Printf("Warning: Failed to save audio metadata: %v\n", err)
	}

	return outputFile, nil
}

// generateAudioFront generates front audio for a bg-bg card
func (a *Application) generateAudioFront(ctx context.Context, word string, cardDir string) (string, error) {
	fmt.Printf("DEBUG (generateAudioFront): Called with word: %s, cardDir: %s\n", word, cardDir)

	if cardDir == "" {
		fmt.Printf("DEBUG (generateAudioFront): Card directory not provided, returning error\n")
		return "", fmt.Errorf("card directory not provided")
	}

	allVoices := []string{"alloy", "ash", "ballad", "coral", "echo", "fable", "onyx", "nova", "sage", "shimmer", "verse"}
	voice, speed := randomVoiceAndSpeed(allVoices)

	audioConfig := *a.audioConfig
	audioConfig.OpenAIVoice = voice
	audioConfig.OpenAISpeed = speed
	audioConfig.OutputDir = a.config.OutputDir

	provider, err := audio.NewProvider(&audioConfig)
	if err != nil {
		fmt.Printf("DEBUG (generateAudioFront): Failed to create audio provider: %v\n", err)
		return "", err
	}

	fmt.Printf("DEBUG (generateAudioFront): Generating front audio for '%s' with voice: %s, speed: %.2f\n", word, voice, speed)
	fmt.Printf("Generating front audio for '%s' with voice: %s, speed: %.2f\n", word, voice, speed)
	frontFile := filepath.Join(cardDir, fmt.Sprintf("audio_front.%s", a.config.AudioFormat))
	fmt.Printf("DEBUG (generateAudioFront): Will write to: %s\n", frontFile)
	if err := provider.GenerateAudio(ctx, word, frontFile); err != nil {
		return "", fmt.Errorf("failed to generate front audio: %w", err)
	}
	fmt.Printf("DEBUG (generateAudioFront): Successfully wrote front audio to: %s\n", frontFile)

	// Update metadata
	metadataFile := filepath.Join(cardDir, "audio_metadata.txt")
	metadata := fmt.Sprintf("voice=%s\nspeed=%.2f\ncardtype=bg-bg\n", voice, speed)
	if err := os.WriteFile(metadataFile, []byte(metadata), 0644); err != nil {
		fmt.Printf("Warning: Failed to save audio metadata: %v\n", err)
	}

	return frontFile, nil
}

// generateAudioBack generates back audio for a bg-bg card
func (a *Application) generateAudioBack(ctx context.Context, text string, cardDir string) (string, error) {
	fmt.Printf("DEBUG (generateAudioBack): Called with text: %s, cardDir: %s\n", text, cardDir)

	if cardDir == "" {
		fmt.Printf("DEBUG (generateAudioBack): Card directory not provided, returning error\n")
		return "", fmt.Errorf("card directory not provided")
	}

	allVoices := []string{"alloy", "ash", "ballad", "coral", "echo", "fable", "onyx", "nova", "sage", "shimmer", "verse"}
	voice, speed := randomVoiceAndSpeed(allVoices)

	audioConfig := *a.audioConfig
	audioConfig.OpenAIVoice = voice
	audioConfig.OpenAISpeed = speed
	audioConfig.OutputDir = a.config.OutputDir

	provider, err := audio.NewProvider(&audioConfig)
	if err != nil {
		fmt.Printf("DEBUG (generateAudioBack): Failed to create audio provider: %v\n", err)
		return "", err
	}

	fmt.Printf("DEBUG (generateAudioBack): Generating back audio for '%s' with voice: %s, speed: %.2f\n", text, voice, speed)
	fmt.Printf("Generating back audio for '%s' with voice: %s, speed: %.2f\n", text, voice, speed)
	backFile := filepath.Join(cardDir, fmt.Sprintf("audio_back.%s", a.config.AudioFormat))
	fmt.Printf("DEBUG (generateAudioBack): Will write to: %s\n", backFile)
	if err := provider.GenerateAudio(ctx, text, backFile); err != nil {
		return "", fmt.Errorf("failed to generate back audio: %w", err)
	}
	fmt.Printf("DEBUG (generateAudioBack): Successfully wrote back audio to: %s\n", backFile)

	return backFile, nil
}

// generateAudioBgBg generates audio for both sides of a bg-bg card
func (a *Application) generateAudioBgBg(ctx context.Context, front, back, cardDir string) (string, string, error) {
	if cardDir == "" {
		return "", "", fmt.Errorf("card directory not provided")
	}

	allVoices := []string{"alloy", "ash", "ballad", "coral", "echo", "fable", "onyx", "nova", "sage", "shimmer", "verse"}
	voice, speed := randomVoiceAndSpeed(allVoices)

	audioConfig := *a.audioConfig
	audioConfig.OpenAIVoice = voice
	audioConfig.OpenAISpeed = speed
	audioConfig.OutputDir = a.config.OutputDir

	provider, err := audio.NewProvider(&audioConfig)
	if err != nil {
		return "", "", err
	}

	// Generate front audio
	fmt.Printf("Generating front audio for '%s' with voice: %s, speed: %.2f\n", front, voice, speed)
	frontFile := filepath.Join(cardDir, fmt.Sprintf("audio_front.%s", a.config.AudioFormat))
	if err := provider.GenerateAudio(ctx, front, frontFile); err != nil {
		return "", "", fmt.Errorf("failed to generate front audio: %w", err)
	}

	// Generate back audio
	fmt.Printf("Generating back audio for '%s' with voice: %s, speed: %.2f\n", back, voice, speed)
	backFile := filepath.Join(cardDir, fmt.Sprintf("audio_back.%s", a.config.AudioFormat))
	if err := provider.GenerateAudio(ctx, back, backFile); err != nil {
		return frontFile, "", fmt.Errorf("failed to generate back audio: %w", err)
	}

	// Save audio attribution
	if err := a.saveAudioAttribution(front, frontFile, voice, speed); err != nil {
		fmt.Printf("Warning: Failed to save audio attribution: %v\n", err)
	}

	// Save voice metadata
	metadataFile := filepath.Join(cardDir, "audio_metadata.txt")
	metadata := fmt.Sprintf("voice=%s\nspeed=%.2f\ncardtype=bg-bg\n", voice, speed)
	if err := os.WriteFile(metadataFile, []byte(metadata), 0644); err != nil {
		fmt.Printf("Warning: Failed to save audio metadata: %v\n", err)
	}

	return frontFile, backFile, nil
}

// generateImagesWithPrompt downloads a single image for a word with optional custom prompt and translation
func (a *Application) generateImagesWithPrompt(ctx context.Context, word string, customPrompt string, translation string, cardDir string) (string, error) {
	// Create image searcher based on provider
	var searcher image.ImageSearcher
	var err error

	switch a.config.ImageProvider {
	case "openai":
		openaiConfig := &image.OpenAIConfig{
			APIKey:  a.config.OpenAIKey,
			Model:   "dall-e-2", // DALL-E 2 supports 512x512
			Size:    "512x512",  // Half of 1024x1024
			Quality: "standard",
			Style:   "natural",
		}

		openaiClient := image.NewOpenAIClient(openaiConfig)
		searcher = openaiClient
		if openaiConfig.APIKey == "" {
			return "", fmt.Errorf("OpenAI API key is required for image generation")
		}
	default:
		return "", fmt.Errorf("unknown image provider: %s", a.config.ImageProvider)
	}

	// Use the provided card directory
	if cardDir == "" {
		return "", fmt.Errorf("card directory not provided")
	}

	// Create downloader
	downloadOpts := &image.DownloadOptions{
		OutputDir:         cardDir,
		OverwriteExisting: true,
		CreateDir:         true,
		FileNamePattern:   "image",
		MaxSizeBytes:      5 * 1024 * 1024, // 5MB
	}

	downloader := image.NewDownloader(searcher, downloadOpts)

	// Set up callback for OpenAI to update prompt immediately when it's generated
	if a.config.ImageProvider == "openai" {
		if openaiClient, ok := searcher.(*image.OpenAIClient); ok {
			openaiClient.SetPromptCallback(func(prompt string) {
				// Save the prompt to disk immediately for this word
				promptFile := filepath.Join(cardDir, "image_prompt.txt")
				if err := os.WriteFile(promptFile, []byte(prompt), 0644); err != nil {
					fmt.Printf("Warning: Failed to save prompt for '%s': %v\n", word, err)
				}

				// Only update UI if this word is still the current word
				a.mu.Lock()
				isCurrentWord := a.currentWord == word
				a.mu.Unlock()

				if isCurrentWord {
					fyne.Do(func() {
						a.imagePromptEntry.SetText(prompt)
					})
				}
			})
		}
	}

	// Create search options with custom prompt and translation if provided
	searchOpts := image.DefaultSearchOptions(word)
	if customPrompt != "" {
		searchOpts.CustomPrompt = customPrompt
	}
	if translation != "" {
		searchOpts.Translation = translation
	}

	// Download single image
	_, path, err := downloader.DownloadBestMatchWithOptions(ctx, searchOpts)
	if err != nil {
		return "", err
	}

	// The prompt has already been saved and UI updated via the callback

	return path, nil
}

// saveAudioAttribution saves attribution info for generated audio
func (a *Application) saveAudioAttribution(word, audioFile, voice string, speed float64) error {
	attribution := fmt.Sprintf("Audio generated by OpenAI TTS\n\n")
	attribution += fmt.Sprintf("Bulgarian word: %s\n", word)
	attribution += fmt.Sprintf("Model: %s\n", a.audioConfig.OpenAIModel)
	attribution += fmt.Sprintf("Voice: %s\n", voice)
	attribution += fmt.Sprintf("Speed: %.2f\n", speed)

	if a.audioConfig.OpenAIInstruction != "" {
		attribution += fmt.Sprintf("\nVoice instructions:\n%s\n", a.audioConfig.OpenAIInstruction)
	}

	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
}