File size: 4,046 Bytes
5fbc846
042f51e
 
671ed40
5fbc846
 
 
 
 
 
 
 
9180f22
5fbc846
41f9c0c
 
5fbc846
671ed40
 
5fbc846
 
042f51e
5fbc846
 
 
 
671ed40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
042f51e
5fbc846
a3431d8
5fbc846
 
 
042f51e
5fbc846
 
a3431d8
5fbc846
671ed40
5fbc846
671ed40
5fbc846
042f51e
5fbc846
0c089ff
 
 
 
5fbc846
f71c727
5fbc846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41f9c0c
 
a3431d8
671ed40
 
 
5fbc846
 
 
671ed40
 
 
5fbc846
 
671ed40
5fbc846
671ed40
 
 
 
 
 
 
 
 
 
 
 
a3431d8
41f9c0c
9180f22
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
from flask import Flask, render_template, request, jsonify, url_for
from gtts import gTTS
import os
import logging
from translation_data import (
    translation_dict_A,
    translation_dict_B,
    translation_dict_C,
    translation_dict_D,
    translation_dict_F,
    translation_dict_G
)

# Initialize Flask app
app = Flask(__name__)

# Configure logging
logging.basicConfig(level=logging.DEBUG)

# Directory to save audio files
AUDIO_DIR = os.path.join('static', 'audio')

# Ensure the audio directory exists
os.makedirs(AUDIO_DIR, exist_ok=True)

# Flashcards data
flashcards = {
    'A': {
        'english_sentences': list(translation_dict_A.keys()),
        'japanese_translations': list(translation_dict_A.values())
    },
    'B': {
        'english_sentences': list(translation_dict_B.keys()),
        'japanese_translations': list(translation_dict_B.values())
    },
    'C': {
        'english_sentences': list(translation_dict_C.keys()),
        'japanese_translations': list(translation_dict_C.values())
    },
    'D': {
        'english_sentences': list(translation_dict_D.keys()),
        'japanese_translations': list(translation_dict_D.values())
    },
    'F': {
        'english_sentences': list(translation_dict_F.keys()),
        'japanese_translations': list(translation_dict_F.values())
    },
    'G': {
        'english_sentences': list(translation_dict_G.keys()),
        'japanese_translations': list(translation_dict_G.values())
    }
}

# Helper function to generate audio
def generate_audio(text, set_name, index):
    """Generate an audio file from text using gTTS."""
    filename = f"{set_name}_{index}.mp3"
    filepath = os.path.join(AUDIO_DIR, filename)
    
    if not os.path.exists(filepath):
        logging.info(f"Generating audio file: {filepath}")
        tts = gTTS(text=text, lang='en')
        tts.save(filepath)
    else:
        logging.info(f"Using existing audio file: {filepath}")
    
    return filepath

# Route for the portal page
@app.route('/')
def portal():
    return render_template('portal.html')

# Route to render the flashcards page
@app.route('/flashcards')
def flashcards_page():
    set_name = request.args.get('set', 'A')
    index = int(request.args.get('index', 0))
    
    if set_name not in flashcards:
        return "Set not found", 404
    
    total = len(flashcards[set_name]['english_sentences'])
    if not (0 <= index < total):
        return "Index out of range", 404
    
    return render_template(
        'flashcards.html',
        set_name=set_name,
        index=index,
        total=total,
        english=flashcards[set_name]['english_sentences'][index],
        japanese=flashcards[set_name]['japanese_translations'][index],
        audio_url=url_for('static', filename=f"audio/{set_name}_{index}.mp3")
    )

# API endpoint to fetch flashcard data
@app.route('/api/flashcards')
def api_flashcards():
    set_name = request.args.get('set', 'A')
    index = int(request.args.get('index', 0))

    if set_name in flashcards:
        english_sentences = flashcards[set_name]['english_sentences']
        japanese_translations = flashcards[set_name]['japanese_translations']
        total = len(english_sentences)
        
        if 0 <= index < total:
            english = english_sentences[index]
            japanese = japanese_translations[index]
            
            # Generate audio and get the URL
            audio_path = generate_audio(english, set_name, index)
            audio_url = url_for('static', filename=f"audio/{set_name}_{index}.mp3")
            
            return jsonify({
                'set_name': set_name,
                'index': index,
                'total': total,
                'english': english,
                'japanese': japanese,
                'audio_url': audio_url
            })
        else:
            return jsonify({'error': 'Index out of range'}), 404
    else:
        return jsonify({'error': 'Set not found'}), 404

if __name__ == '__main__':
    app.run(debug=True, host="0.0.0.0", port=7860)