Spaces:
Sleeping
Sleeping
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 | |
def portal(): | |
"""Render the portal page with links to different categories.""" | |
return render_template('portal.html') | |
# Route to render the flashcards page | |
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 | |
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) | |