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)