Spaces:
Sleeping
Sleeping
File size: 4,871 Bytes
5fbc846 042f51e 671ed40 5fbc846 6871757 5fbc846 9180f22 5fbc846 41f9c0c 5fbc846 671ed40 5fbc846 042f51e 5fbc846 6871757 671ed40 6bba304 671ed40 6bba304 671ed40 6bba304 671ed40 6bba304 671ed40 6bba304 671ed40 6871757 671ed40 042f51e 6871757 a3431d8 6871757 5fbc846 6871757 5fbc846 86df83a 6871757 86df83a 671ed40 5fbc846 6871757 5fbc846 042f51e 6871757 0c089ff 6871757 0c089ff 6871757 f71c727 5fbc846 6871757 5fbc846 86df83a 6871757 5fbc846 6871757 6bba304 5fbc846 6871757 86df83a 6871757 5fbc846 6871757 86df83a 5fbc846 6871757 5fbc846 6871757 41f9c0c 86df83a a3431d8 671ed40 6bba304 671ed40 6bba304 6871757 5fbc846 6bba304 671ed40 6871757 86df83a 6871757 671ed40 6871757 671ed40 a3431d8 41f9c0c 86df83a 6bba304 |
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 |
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
# Include translation_dict_E, translation_dict_G if they exist
)
# 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 organized by categories
flashcards = {
'A': {
'chinese_sentences': list(translation_dict_A.keys()),
'japanese_translations': list(translation_dict_A.values())
},
'B': {
'chinese_sentences': list(translation_dict_B.keys()),
'japanese_translations': list(translation_dict_B.values())
},
'C': {
'chinese_sentences': list(translation_dict_C.keys()),
'japanese_translations': list(translation_dict_C.values())
},
'D': {
'chinese_sentences': list(translation_dict_D.keys()),
'japanese_translations': list(translation_dict_D.values())
},
'F': {
'chinese_sentences': list(translation_dict_F.keys()),
'japanese_translations': list(translation_dict_F.values())
},
# Add 'E' and 'G' similarly if needed
}
# Helper function to generate audio
def generate_audio(text, set_name, index):
"""Generate an audio file from Japanese 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}")
try:
tts = gTTS(text=text, lang='ja') # Japanese TTS
tts.save(filepath)
except Exception as e:
logging.error(f"Error generating audio: {e}")
return None
else:
logging.info(f"Using existing audio file: {filepath}")
return filepath
# Route for the portal page
@app.route('/')
def portal():
"""Render the portal page with links to different categories."""
return render_template('portal.html')
# Route to render the flashcards page
@app.route('/flashcards')
def flashcards_page():
"""Render the flashcards page for a specific set and index."""
set_name = request.args.get('set', 'A')
try:
index = int(request.args.get('index', 0))
except ValueError:
return "Invalid index parameter", 400
if set_name not in flashcards:
return "Set not found", 404
total = len(flashcards[set_name]['chinese_sentences'])
if not (0 <= index < total):
return "Index out of range", 404
chinese = flashcards[set_name]['chinese_sentences'][index]
japanese = flashcards[set_name]['japanese_translations'][index]
audio_url = url_for('static', filename=f"audio/{set_name}_{index}.mp3")
return render_template(
'flashcards.html',
set_name=set_name,
index=index,
total=total,
japanese=japanese, # Changed from 'english' to 'japanese'
chinese=chinese, # Added 'chinese'
audio_url=audio_url
)
# API endpoint to fetch flashcard data
@app.route('/api/flashcards')
def api_flashcards():
"""API endpoint to fetch flashcard data."""
set_name = request.args.get('set', 'A')
try:
index = int(request.args.get('index', 0))
except ValueError:
return jsonify({'error': 'Invalid index parameter'}), 400
if set_name in flashcards:
chinese_sentences = flashcards[set_name]['chinese_sentences']
japanese_translations = flashcards[set_name]['japanese_translations']
total = len(chinese_sentences)
if 0 <= index < total:
chinese = chinese_sentences[index]
japanese = japanese_translations[index]
# Generate audio from Japanese text
audio_path = generate_audio(japanese, set_name, index)
if audio_path:
audio_url = url_for('static', filename=f"audio/{set_name}_{index}.mp3")
else:
audio_url = None # Handle audio generation failure
return jsonify({
'set_name': set_name,
'index': index,
'total': total,
'japanese': japanese, # Changed from 'english' to 'japanese'
'chinese': chinese, # Added 'chinese'
'audio_url': audio_url
})
else:
return jsonify({'error': 'Index out of range'}), 404
else:
return jsonify({'error': 'Set not found'}), 404
if __name__ == '__main__':
# Run the Flask app on all available IPs on port 7860
app.run(debug=True, host="0.0.0.0", port=7860)
|