Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,8 +7,8 @@ from translation_data import (
|
|
7 |
translation_dict_B,
|
8 |
translation_dict_C,
|
9 |
translation_dict_D,
|
10 |
-
translation_dict_F
|
11 |
-
translation_dict_G
|
12 |
)
|
13 |
|
14 |
# Initialize Flask app
|
@@ -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()),
|
@@ -45,98 +45,69 @@ flashcards = {
|
|
45 |
'chinese_sentences': list(translation_dict_F.keys()),
|
46 |
'japanese_translations': list(translation_dict_F.values())
|
47 |
},
|
48 |
-
'G'
|
49 |
-
'chinese_sentences': list(translation_dict_G.keys()),
|
50 |
-
'japanese_translations': list(translation_dict_G.values())
|
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') #
|
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
|
109 |
-
|
110 |
total = len(flashcards[set_name]['chinese_sentences'])
|
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 |
-
|
124 |
-
|
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))
|
@@ -147,24 +118,24 @@ def api_flashcards():
|
|
147 |
chinese_sentences = flashcards[set_name]['chinese_sentences']
|
148 |
japanese_translations = flashcards[set_name]['japanese_translations']
|
149 |
total = len(chinese_sentences)
|
150 |
-
|
151 |
if 0 <= index < total:
|
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,
|
164 |
'index': index,
|
165 |
'total': total,
|
166 |
-
'
|
167 |
-
'
|
168 |
'audio_url': audio_url
|
169 |
})
|
170 |
else:
|
|
|
7 |
translation_dict_B,
|
8 |
translation_dict_C,
|
9 |
translation_dict_D,
|
10 |
+
translation_dict_F
|
11 |
+
# Include translation_dict_E, translation_dict_G if they exist
|
12 |
)
|
13 |
|
14 |
# Initialize Flask app
|
|
|
23 |
# Ensure the audio directory exists
|
24 |
os.makedirs(AUDIO_DIR, exist_ok=True)
|
25 |
|
26 |
+
# Flashcards data organized by categories
|
27 |
flashcards = {
|
28 |
'A': {
|
29 |
'chinese_sentences': list(translation_dict_A.keys()),
|
|
|
45 |
'chinese_sentences': list(translation_dict_F.keys()),
|
46 |
'japanese_translations': list(translation_dict_F.values())
|
47 |
},
|
48 |
+
# Add 'E' and 'G' similarly if needed
|
|
|
|
|
|
|
49 |
}
|
50 |
|
51 |
+
# Helper function to generate audio
|
52 |
def generate_audio(text, set_name, index):
|
53 |
+
"""Generate an audio file from Japanese text using gTTS."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
filename = f"{set_name}_{index}.mp3"
|
55 |
filepath = os.path.join(AUDIO_DIR, filename)
|
56 |
+
|
57 |
if not os.path.exists(filepath):
|
58 |
logging.info(f"Generating audio file: {filepath}")
|
59 |
try:
|
60 |
+
tts = gTTS(text=text, lang='ja') # Japanese TTS
|
61 |
tts.save(filepath)
|
62 |
except Exception as e:
|
63 |
logging.error(f"Error generating audio: {e}")
|
64 |
return None
|
65 |
else:
|
66 |
logging.info(f"Using existing audio file: {filepath}")
|
67 |
+
|
68 |
return filepath
|
69 |
|
70 |
+
# Route for the portal page
|
71 |
@app.route('/')
|
72 |
def portal():
|
73 |
+
"""Render the portal page with links to different categories."""
|
|
|
|
|
74 |
return render_template('portal.html')
|
75 |
|
76 |
+
# Route to render the flashcards page
|
77 |
@app.route('/flashcards')
|
78 |
def flashcards_page():
|
79 |
+
"""Render the flashcards page for a specific set and index."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
set_name = request.args.get('set', 'A')
|
81 |
try:
|
82 |
index = int(request.args.get('index', 0))
|
83 |
except ValueError:
|
84 |
return "Invalid index parameter", 400
|
85 |
+
|
86 |
if set_name not in flashcards:
|
87 |
return "Set not found", 404
|
88 |
+
|
89 |
total = len(flashcards[set_name]['chinese_sentences'])
|
90 |
if not (0 <= index < total):
|
91 |
return "Index out of range", 404
|
92 |
+
|
93 |
chinese = flashcards[set_name]['chinese_sentences'][index]
|
94 |
japanese = flashcards[set_name]['japanese_translations'][index]
|
95 |
audio_url = url_for('static', filename=f"audio/{set_name}_{index}.mp3")
|
96 |
+
|
97 |
return render_template(
|
98 |
'flashcards.html',
|
99 |
set_name=set_name,
|
100 |
index=index,
|
101 |
total=total,
|
102 |
+
japanese=japanese, # Changed from 'english' to 'japanese'
|
103 |
+
chinese=chinese, # Added 'chinese'
|
104 |
audio_url=audio_url
|
105 |
)
|
106 |
|
107 |
+
# API endpoint to fetch flashcard data
|
108 |
@app.route('/api/flashcards')
|
109 |
def api_flashcards():
|
110 |
+
"""API endpoint to fetch flashcard data."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
set_name = request.args.get('set', 'A')
|
112 |
try:
|
113 |
index = int(request.args.get('index', 0))
|
|
|
118 |
chinese_sentences = flashcards[set_name]['chinese_sentences']
|
119 |
japanese_translations = flashcards[set_name]['japanese_translations']
|
120 |
total = len(chinese_sentences)
|
121 |
+
|
122 |
if 0 <= index < total:
|
123 |
chinese = chinese_sentences[index]
|
124 |
japanese = japanese_translations[index]
|
125 |
+
|
126 |
# Generate audio from Japanese text
|
127 |
audio_path = generate_audio(japanese, set_name, index)
|
128 |
if audio_path:
|
129 |
audio_url = url_for('static', filename=f"audio/{set_name}_{index}.mp3")
|
130 |
else:
|
131 |
audio_url = None # Handle audio generation failure
|
132 |
+
|
133 |
return jsonify({
|
134 |
'set_name': set_name,
|
135 |
'index': index,
|
136 |
'total': total,
|
137 |
+
'japanese': japanese, # Changed from 'english' to 'japanese'
|
138 |
+
'chinese': chinese, # Added 'chinese'
|
139 |
'audio_url': audio_url
|
140 |
})
|
141 |
else:
|