Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,29 @@
|
|
1 |
-
from flask import Flask, render_template, request, jsonify, url_for
|
2 |
from gtts import gTTS
|
3 |
import os
|
4 |
import logging
|
5 |
-
from translation_data import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
# Flask
|
8 |
app = Flask(__name__)
|
9 |
|
10 |
-
#
|
11 |
logging.basicConfig(level=logging.DEBUG)
|
12 |
|
13 |
-
#
|
14 |
-
AUDIO_DIR = 'static
|
15 |
|
16 |
-
#
|
|
|
|
|
|
|
17 |
flashcards = {
|
18 |
'A': {
|
19 |
'english_sentences': list(translation_dict_A.keys()),
|
@@ -41,45 +51,68 @@ flashcards = {
|
|
41 |
}
|
42 |
}
|
43 |
|
44 |
-
#
|
45 |
def generate_audio(text, set_name, index):
|
46 |
-
"""
|
47 |
-
filename = f"{
|
|
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
logging.info(f"音声ファイルを生成中: {filename}")
|
52 |
tts = gTTS(text=text, lang='en')
|
53 |
-
tts.save(
|
54 |
else:
|
55 |
-
logging.info(f"
|
56 |
|
57 |
-
return
|
58 |
|
59 |
-
#
|
60 |
@app.route('/')
|
61 |
def portal():
|
62 |
return render_template('portal.html')
|
63 |
|
64 |
-
#
|
65 |
@app.route('/flashcards')
|
66 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
set_name = request.args.get('set', 'A')
|
68 |
index = int(request.args.get('index', 0))
|
69 |
|
70 |
if set_name in flashcards:
|
71 |
english_sentences = flashcards[set_name]['english_sentences']
|
72 |
japanese_translations = flashcards[set_name]['japanese_translations']
|
73 |
-
|
|
|
|
|
74 |
english = english_sentences[index]
|
75 |
japanese = japanese_translations[index]
|
76 |
-
total = len(english_sentences)
|
77 |
|
78 |
-
#
|
|
|
79 |
audio_url = url_for('static', filename=f"audio/{set_name}_{index}.mp3")
|
80 |
-
|
81 |
-
|
82 |
-
# フラッシュカードの情報を JSON で返す
|
83 |
return jsonify({
|
84 |
'set_name': set_name,
|
85 |
'index': index,
|
@@ -93,10 +126,5 @@ def index():
|
|
93 |
else:
|
94 |
return jsonify({'error': 'Set not found'}), 404
|
95 |
|
96 |
-
# 静的ファイルの処理(CSS、音声ファイルなどのルート)
|
97 |
-
@app.route('/static/<path:filename>')
|
98 |
-
def static_files(filename):
|
99 |
-
return redirect(url_for('static', filename=filename))
|
100 |
-
|
101 |
if __name__ == '__main__':
|
102 |
app.run(debug=True, host="0.0.0.0", port=7860)
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify, url_for
|
2 |
from gtts import gTTS
|
3 |
import os
|
4 |
import logging
|
5 |
+
from translation_data import (
|
6 |
+
translation_dict_A,
|
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
|
15 |
app = Flask(__name__)
|
16 |
|
17 |
+
# Configure logging
|
18 |
logging.basicConfig(level=logging.DEBUG)
|
19 |
|
20 |
+
# Directory to save audio files
|
21 |
+
AUDIO_DIR = os.path.join('static', 'audio')
|
22 |
|
23 |
+
# Ensure the audio directory exists
|
24 |
+
os.makedirs(AUDIO_DIR, exist_ok=True)
|
25 |
+
|
26 |
+
# Flashcards data
|
27 |
flashcards = {
|
28 |
'A': {
|
29 |
'english_sentences': list(translation_dict_A.keys()),
|
|
|
51 |
}
|
52 |
}
|
53 |
|
54 |
+
# Helper function to generate audio
|
55 |
def generate_audio(text, set_name, index):
|
56 |
+
"""Generate an audio file from text using gTTS."""
|
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 |
tts = gTTS(text=text, lang='en')
|
63 |
+
tts.save(filepath)
|
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 |
+
index = int(request.args.get('index', 0))
|
79 |
+
|
80 |
+
if set_name not in flashcards:
|
81 |
+
return "Set not found", 404
|
82 |
+
|
83 |
+
total = len(flashcards[set_name]['english_sentences'])
|
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 |
+
english=flashcards[set_name]['english_sentences'][index],
|
93 |
+
japanese=flashcards[set_name]['japanese_translations'][index],
|
94 |
+
audio_url=url_for('static', filename=f"audio/{set_name}_{index}.mp3")
|
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 |
index = int(request.args.get('index', 0))
|
102 |
|
103 |
if set_name in flashcards:
|
104 |
english_sentences = flashcards[set_name]['english_sentences']
|
105 |
japanese_translations = flashcards[set_name]['japanese_translations']
|
106 |
+
total = len(english_sentences)
|
107 |
+
|
108 |
+
if 0 <= index < total:
|
109 |
english = english_sentences[index]
|
110 |
japanese = japanese_translations[index]
|
|
|
111 |
|
112 |
+
# Generate audio and get the URL
|
113 |
+
audio_path = generate_audio(english, set_name, index)
|
114 |
audio_url = url_for('static', filename=f"audio/{set_name}_{index}.mp3")
|
115 |
+
|
|
|
|
|
116 |
return jsonify({
|
117 |
'set_name': set_name,
|
118 |
'index': index,
|
|
|
126 |
else:
|
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)
|