Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,92 @@
|
|
1 |
-
from flask import Flask, render_template, request,
|
2 |
from gtts import gTTS
|
3 |
import os
|
|
|
|
|
4 |
|
|
|
5 |
app = Flask(__name__)
|
6 |
|
|
|
|
|
|
|
7 |
# 音声ファイルの保存ディレクトリ
|
8 |
AUDIO_DIR = 'static/audio'
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
#
|
19 |
def generate_audio(text, set_name, index):
|
|
|
20 |
filename = f"{AUDIO_DIR}/{set_name}_{index}.mp3"
|
21 |
|
22 |
-
#
|
23 |
if not os.path.exists(filename):
|
|
|
24 |
tts = gTTS(text=text, lang='en')
|
25 |
tts.save(filename)
|
|
|
|
|
|
|
26 |
return filename
|
27 |
|
28 |
-
# フラッシュカードの内容をJSON
|
29 |
@app.route('/flashcards')
|
30 |
def index():
|
31 |
set_name = request.args.get('set', 'A')
|
32 |
index = int(request.args.get('index', 0))
|
33 |
|
34 |
-
if set_name
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
total = len(english_sentences_C)
|
46 |
-
elif set_name == 'D':
|
47 |
-
english = english_sentences_D[index]
|
48 |
-
japanese = translation_dict_D[english]
|
49 |
-
total = len(english_sentences_D)
|
50 |
-
elif set_name == 'F':
|
51 |
-
english = english_sentences_F[index]
|
52 |
-
japanese = translation_dict_F[english]
|
53 |
-
total = len(english_sentences_F)
|
54 |
-
elif set_name == 'G':
|
55 |
-
english = english_sentences_G[index]
|
56 |
-
japanese = translation_dict_G[english]
|
57 |
-
total = len(english_sentences_G)
|
58 |
-
|
59 |
-
# 音声ファイルの生成
|
60 |
-
audio_url = url_for('static', filename=f"audio/{set_name}_{index}.mp3")
|
61 |
-
generate_audio(english, set_name, index)
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
72 |
|
73 |
if __name__ == '__main__':
|
74 |
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 translation_dict_A, translation_dict_B, translation_dict_C, translation_dict_D, translation_dict_F, translation_dict_G
|
6 |
|
7 |
+
# Flask アプリケーションの初期化
|
8 |
app = Flask(__name__)
|
9 |
|
10 |
+
# ログの設定
|
11 |
+
logging.basicConfig(level=logging.DEBUG)
|
12 |
+
|
13 |
# 音声ファイルの保存ディレクトリ
|
14 |
AUDIO_DIR = 'static/audio'
|
15 |
|
16 |
+
# フラッシュカードのデータを管理
|
17 |
+
flashcards = {
|
18 |
+
'A': {
|
19 |
+
'english_sentences': list(translation_dict_A.keys()),
|
20 |
+
'japanese_translations': list(translation_dict_A.values())
|
21 |
+
},
|
22 |
+
'B': {
|
23 |
+
'english_sentences': list(translation_dict_B.keys()),
|
24 |
+
'japanese_translations': list(translation_dict_B.values())
|
25 |
+
},
|
26 |
+
'C': {
|
27 |
+
'english_sentences': list(translation_dict_C.keys()),
|
28 |
+
'japanese_translations': list(translation_dict_C.values())
|
29 |
+
},
|
30 |
+
'D': {
|
31 |
+
'english_sentences': list(translation_dict_D.keys()),
|
32 |
+
'japanese_translations': list(translation_dict_D.values())
|
33 |
+
},
|
34 |
+
'F': {
|
35 |
+
'english_sentences': list(translation_dict_F.keys()),
|
36 |
+
'japanese_translations': list(translation_dict_F.values())
|
37 |
+
},
|
38 |
+
'G': {
|
39 |
+
'english_sentences': list(translation_dict_G.keys()),
|
40 |
+
'japanese_translations': list(translation_dict_G.values())
|
41 |
+
}
|
42 |
+
}
|
43 |
|
44 |
+
# ヘルパー関数: 音声ファイルを生成
|
45 |
def generate_audio(text, set_name, index):
|
46 |
+
"""テキストに基づいて音声ファイルを生成する関数"""
|
47 |
filename = f"{AUDIO_DIR}/{set_name}_{index}.mp3"
|
48 |
|
49 |
+
# もしファイルが存在しない場合、生成
|
50 |
if not os.path.exists(filename):
|
51 |
+
logging.info(f"音声ファイルを生成中: {filename}")
|
52 |
tts = gTTS(text=text, lang='en')
|
53 |
tts.save(filename)
|
54 |
+
else:
|
55 |
+
logging.info(f"既存の音声ファイルを使用: {filename}")
|
56 |
+
|
57 |
return filename
|
58 |
|
59 |
+
# フラッシュカードの内容を JSON で返すエンドポイント
|
60 |
@app.route('/flashcards')
|
61 |
def index():
|
62 |
set_name = request.args.get('set', 'A')
|
63 |
index = int(request.args.get('index', 0))
|
64 |
|
65 |
+
if set_name in flashcards:
|
66 |
+
english_sentences = flashcards[set_name]['english_sentences']
|
67 |
+
japanese_translations = flashcards[set_name]['japanese_translations']
|
68 |
+
if 0 <= index < len(english_sentences):
|
69 |
+
english = english_sentences[index]
|
70 |
+
japanese = japanese_translations[index]
|
71 |
+
total = len(english_sentences)
|
72 |
+
|
73 |
+
# 音声ファイルの生成
|
74 |
+
audio_url = url_for('static', filename=f"audio/{set_name}_{index}.mp3")
|
75 |
+
generate_audio(english, set_name, index)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
# フラッシュカードの情報を JSON で返す
|
78 |
+
return jsonify({
|
79 |
+
'set_name': set_name,
|
80 |
+
'index': index,
|
81 |
+
'total': total,
|
82 |
+
'english': english,
|
83 |
+
'japanese': japanese,
|
84 |
+
'audio_url': audio_url
|
85 |
+
})
|
86 |
+
else:
|
87 |
+
return jsonify({'error': 'Index out of range'}), 404
|
88 |
+
else:
|
89 |
+
return jsonify({'error': 'Set not found'}), 404
|
90 |
|
91 |
if __name__ == '__main__':
|
92 |
app.run(debug=True, host="0.0.0.0", port=7860)
|