Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -23,7 +23,7 @@ AUDIO_DIR = os.path.join('static', 'audio')
|
|
23 |
# Ensure the audio directory exists
|
24 |
os.makedirs(AUDIO_DIR, exist_ok=True)
|
25 |
|
26 |
-
# Flashcards data
|
27 |
flashcards = {
|
28 |
'A': {
|
29 |
'chinese_sentences': list(translation_dict_A.keys()),
|
@@ -51,31 +51,58 @@ flashcards = {
|
|
51 |
}
|
52 |
}
|
53 |
|
54 |
-
# Helper function to generate audio
|
55 |
def generate_audio(text, set_name, index):
|
56 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
filename = f"{set_name}_{index}.mp3"
|
58 |
filepath = os.path.join(AUDIO_DIR, filename)
|
59 |
|
60 |
if not os.path.exists(filepath):
|
61 |
logging.info(f"Generating audio file: {filepath}")
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
64 |
else:
|
65 |
logging.info(f"Using existing audio file: {filepath}")
|
66 |
|
67 |
return filepath
|
68 |
|
69 |
-
# Route for the portal page
|
70 |
@app.route('/')
|
71 |
def portal():
|
|
|
|
|
|
|
72 |
return render_template('portal.html')
|
73 |
|
74 |
-
# Route to render the flashcards page
|
75 |
@app.route('/flashcards')
|
76 |
def flashcards_page():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
set_name = request.args.get('set', 'A')
|
78 |
-
|
|
|
|
|
|
|
79 |
|
80 |
if set_name not in flashcards:
|
81 |
return "Set not found", 404
|
@@ -84,21 +111,37 @@ def flashcards_page():
|
|
84 |
if not (0 <= index < total):
|
85 |
return "Index out of range", 404
|
86 |
|
|
|
|
|
|
|
|
|
87 |
return render_template(
|
88 |
'flashcards.html',
|
89 |
set_name=set_name,
|
90 |
index=index,
|
91 |
total=total,
|
92 |
-
chinese=
|
93 |
-
japanese=
|
94 |
-
audio_url=
|
95 |
)
|
96 |
|
97 |
-
# API endpoint to fetch flashcard data
|
98 |
@app.route('/api/flashcards')
|
99 |
def api_flashcards():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
set_name = request.args.get('set', 'A')
|
101 |
-
|
|
|
|
|
|
|
102 |
|
103 |
if set_name in flashcards:
|
104 |
chinese_sentences = flashcards[set_name]['chinese_sentences']
|
@@ -109,9 +152,12 @@ def api_flashcards():
|
|
109 |
chinese = chinese_sentences[index]
|
110 |
japanese = japanese_translations[index]
|
111 |
|
112 |
-
# Generate audio
|
113 |
-
audio_path = generate_audio(
|
114 |
-
|
|
|
|
|
|
|
115 |
|
116 |
return jsonify({
|
117 |
'set_name': set_name,
|
@@ -127,4 +173,5 @@ def api_flashcards():
|
|
127 |
return jsonify({'error': 'Set not found'}), 404
|
128 |
|
129 |
if __name__ == '__main__':
|
|
|
130 |
app.run(debug=True, host="0.0.0.0", port=7860)
|
|
|
23 |
# Ensure the audio directory exists
|
24 |
os.makedirs(AUDIO_DIR, exist_ok=True)
|
25 |
|
26 |
+
# Flashcards data with Chinese sentences and Japanese translations
|
27 |
flashcards = {
|
28 |
'A': {
|
29 |
'chinese_sentences': list(translation_dict_A.keys()),
|
|
|
51 |
}
|
52 |
}
|
53 |
|
|
|
54 |
def generate_audio(text, set_name, index):
|
55 |
+
"""
|
56 |
+
Generate an audio file from Japanese text using gTTS.
|
57 |
+
|
58 |
+
Args:
|
59 |
+
text (str): The Japanese text to convert to speech.
|
60 |
+
set_name (str): The flashcard set identifier.
|
61 |
+
index (int): The index of the flashcard within the set.
|
62 |
+
|
63 |
+
Returns:
|
64 |
+
str: The file path to the generated audio.
|
65 |
+
"""
|
66 |
filename = f"{set_name}_{index}.mp3"
|
67 |
filepath = os.path.join(AUDIO_DIR, filename)
|
68 |
|
69 |
if not os.path.exists(filepath):
|
70 |
logging.info(f"Generating audio file: {filepath}")
|
71 |
+
try:
|
72 |
+
tts = gTTS(text=text, lang='ja') # Set language to Japanese
|
73 |
+
tts.save(filepath)
|
74 |
+
except Exception as e:
|
75 |
+
logging.error(f"Error generating audio: {e}")
|
76 |
+
return None
|
77 |
else:
|
78 |
logging.info(f"Using existing audio file: {filepath}")
|
79 |
|
80 |
return filepath
|
81 |
|
|
|
82 |
@app.route('/')
|
83 |
def portal():
|
84 |
+
"""
|
85 |
+
Render the portal page.
|
86 |
+
"""
|
87 |
return render_template('portal.html')
|
88 |
|
|
|
89 |
@app.route('/flashcards')
|
90 |
def flashcards_page():
|
91 |
+
"""
|
92 |
+
Render the flashcards page for a specific set and index.
|
93 |
+
|
94 |
+
Query Parameters:
|
95 |
+
set (str): The flashcard set identifier (default: 'A').
|
96 |
+
index (int): The index of the flashcard within the set (default: 0).
|
97 |
+
|
98 |
+
Returns:
|
99 |
+
Rendered HTML template or error message.
|
100 |
+
"""
|
101 |
set_name = request.args.get('set', 'A')
|
102 |
+
try:
|
103 |
+
index = int(request.args.get('index', 0))
|
104 |
+
except ValueError:
|
105 |
+
return "Invalid index parameter", 400
|
106 |
|
107 |
if set_name not in flashcards:
|
108 |
return "Set not found", 404
|
|
|
111 |
if not (0 <= index < total):
|
112 |
return "Index out of range", 404
|
113 |
|
114 |
+
chinese = flashcards[set_name]['chinese_sentences'][index]
|
115 |
+
japanese = flashcards[set_name]['japanese_translations'][index]
|
116 |
+
audio_url = url_for('static', filename=f"audio/{set_name}_{index}.mp3")
|
117 |
+
|
118 |
return render_template(
|
119 |
'flashcards.html',
|
120 |
set_name=set_name,
|
121 |
index=index,
|
122 |
total=total,
|
123 |
+
chinese=chinese,
|
124 |
+
japanese=japanese,
|
125 |
+
audio_url=audio_url
|
126 |
)
|
127 |
|
|
|
128 |
@app.route('/api/flashcards')
|
129 |
def api_flashcards():
|
130 |
+
"""
|
131 |
+
API endpoint to fetch flashcard data.
|
132 |
+
|
133 |
+
Query Parameters:
|
134 |
+
set (str): The flashcard set identifier (default: 'A').
|
135 |
+
index (int): The index of the flashcard within the set (default: 0).
|
136 |
+
|
137 |
+
Returns:
|
138 |
+
JSON response containing flashcard data or error message.
|
139 |
+
"""
|
140 |
set_name = request.args.get('set', 'A')
|
141 |
+
try:
|
142 |
+
index = int(request.args.get('index', 0))
|
143 |
+
except ValueError:
|
144 |
+
return jsonify({'error': 'Invalid index parameter'}), 400
|
145 |
|
146 |
if set_name in flashcards:
|
147 |
chinese_sentences = flashcards[set_name]['chinese_sentences']
|
|
|
152 |
chinese = chinese_sentences[index]
|
153 |
japanese = japanese_translations[index]
|
154 |
|
155 |
+
# Generate audio from Japanese text
|
156 |
+
audio_path = generate_audio(japanese, set_name, index)
|
157 |
+
if audio_path:
|
158 |
+
audio_url = url_for('static', filename=f"audio/{set_name}_{index}.mp3")
|
159 |
+
else:
|
160 |
+
audio_url = None # Handle audio generation failure
|
161 |
|
162 |
return jsonify({
|
163 |
'set_name': set_name,
|
|
|
173 |
return jsonify({'error': 'Set not found'}), 404
|
174 |
|
175 |
if __name__ == '__main__':
|
176 |
+
# Run the Flask app on all available IPs on port 7860
|
177 |
app.run(debug=True, host="0.0.0.0", port=7860)
|