Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,13 @@
|
|
1 |
-
from flask import Flask, render_template, request, redirect, url_for,
|
2 |
from gtts import gTTS
|
3 |
import os
|
4 |
|
5 |
-
# ここに翻訳データを格納しておく
|
6 |
-
from translation_data import translation_dict_A, translation_dict_B, translation_dict_C, translation_dict_D, translation_dict_F, translation_dict_G
|
7 |
-
|
8 |
app = Flask(__name__)
|
9 |
|
10 |
-
#
|
11 |
AUDIO_DIR = 'static/audio'
|
12 |
|
13 |
-
#
|
14 |
english_sentences_A = list(translation_dict_A.keys())
|
15 |
english_sentences_B = list(translation_dict_B.keys())
|
16 |
english_sentences_C = list(translation_dict_C.keys())
|
@@ -19,26 +16,40 @@ english_sentences_F = list(translation_dict_F.keys())
|
|
19 |
english_sentences_G = list(translation_dict_G.keys())
|
20 |
|
21 |
# 音声ファイルを生成する関数
|
22 |
-
def generate_audio(text,
|
23 |
-
|
24 |
-
filename = f"{AUDIO_DIR}/{text[:10].replace(' ', '_')}.mp3"
|
25 |
|
26 |
-
|
27 |
-
|
|
|
28 |
tts.save(filename)
|
29 |
return filename
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
generate_all_audio_files()
|
42 |
|
43 |
@app.route('/')
|
44 |
def portal():
|
@@ -48,6 +59,7 @@ def portal():
|
|
48 |
def index():
|
49 |
set_name = request.args.get('set', 'A')
|
50 |
index = int(request.args.get('index', 0))
|
|
|
51 |
if set_name == 'A':
|
52 |
english = english_sentences_A[index]
|
53 |
japanese = translation_dict_A[english]
|
@@ -73,8 +85,52 @@ def index():
|
|
73 |
japanese = translation_dict_G[english]
|
74 |
total = len(english_sentences_G)
|
75 |
|
76 |
-
|
|
|
|
|
|
|
77 |
return render_template('index.html', set_name=set_name, index=index, english=english, japanese=japanese, total=total, audio_url=audio_url)
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
if __name__ == '__main__':
|
80 |
app.run(debug=True, host="0.0.0.0", port=7860)
|
|
|
1 |
+
from flask import Flask, render_template, request, redirect, url_for, jsonify
|
2 |
from gtts import gTTS
|
3 |
import os
|
4 |
|
|
|
|
|
|
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
+
# 音声ファイルの保存ディレクトリ
|
8 |
AUDIO_DIR = 'static/audio'
|
9 |
|
10 |
+
# リストに変換
|
11 |
english_sentences_A = list(translation_dict_A.keys())
|
12 |
english_sentences_B = list(translation_dict_B.keys())
|
13 |
english_sentences_C = list(translation_dict_C.keys())
|
|
|
16 |
english_sentences_G = list(translation_dict_G.keys())
|
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 |
+
# whereAudio エンドポイントを作成して音声ファイルのURLを返す
|
29 |
+
@app.route('/whereAudio')
|
30 |
+
def where_audio():
|
31 |
+
set_name = request.args.get('set', 'A')
|
32 |
+
index = int(request.args.get('index', 0))
|
33 |
+
|
34 |
+
if set_name == 'A':
|
35 |
+
english = english_sentences_A[index]
|
36 |
+
elif set_name == 'B':
|
37 |
+
english = english_sentences_B[index]
|
38 |
+
elif set_name == 'C':
|
39 |
+
english = english_sentences_C[index]
|
40 |
+
elif set_name == 'D':
|
41 |
+
english = english_sentences_D[index]
|
42 |
+
elif set_name == 'F':
|
43 |
+
english = english_sentences_F[index]
|
44 |
+
elif set_name == 'G':
|
45 |
+
english = english_sentences_G[index]
|
46 |
+
|
47 |
+
# 音声ファイルの生成
|
48 |
+
audio_url = url_for('static', filename=f"audio/{set_name}_{index}.mp3")
|
49 |
+
generate_audio(english, set_name, index)
|
50 |
|
51 |
+
# 音声ファイルのURLをJSONで返す
|
52 |
+
return jsonify({'audio_url': audio_url})
|
|
|
53 |
|
54 |
@app.route('/')
|
55 |
def portal():
|
|
|
59 |
def index():
|
60 |
set_name = request.args.get('set', 'A')
|
61 |
index = int(request.args.get('index', 0))
|
62 |
+
|
63 |
if set_name == 'A':
|
64 |
english = english_sentences_A[index]
|
65 |
japanese = translation_dict_A[english]
|
|
|
85 |
japanese = translation_dict_G[english]
|
86 |
total = len(english_sentences_G)
|
87 |
|
88 |
+
# 初回の音声ファイルを生成
|
89 |
+
audio_url = url_for('static', filename=f"audio/{set_name}_{index}.mp3")
|
90 |
+
generate_audio(english, set_name, index)
|
91 |
+
|
92 |
return render_template('index.html', set_name=set_name, index=index, english=english, japanese=japanese, total=total, audio_url=audio_url)
|
93 |
|
94 |
+
@app.route('/next')
|
95 |
+
def next_card():
|
96 |
+
set_name = request.args.get('set', 'A')
|
97 |
+
index = int(request.args.get('index', 0)) + 1
|
98 |
+
|
99 |
+
if set_name == 'A' and index >= len(english_sentences_A):
|
100 |
+
index = 0
|
101 |
+
elif set_name == 'B' and index >= len(english_sentences_B):
|
102 |
+
index = 0
|
103 |
+
elif set_name == 'C' and index >= len(english_sentences_C):
|
104 |
+
index = 0
|
105 |
+
elif set_name == 'D' and index >= len(english_sentences_D):
|
106 |
+
index = 0
|
107 |
+
elif set_name == 'F' and index >= len(english_sentences_F):
|
108 |
+
index = 0
|
109 |
+
elif set_name == 'G' and index >= len(english_sentences_G):
|
110 |
+
index = 0
|
111 |
+
|
112 |
+
return redirect(url_for('index', set=set_name, index=index))
|
113 |
+
|
114 |
+
@app.route('/prev')
|
115 |
+
def prev_card():
|
116 |
+
set_name = request.args.get('set', 'A')
|
117 |
+
index = int(request.args.get('index', 0)) - 1
|
118 |
+
|
119 |
+
if index < 0:
|
120 |
+
if set_name == 'A':
|
121 |
+
index = len(english_sentences_A) - 1
|
122 |
+
elif set_name == 'B':
|
123 |
+
index = len(english_sentences_B) - 1
|
124 |
+
elif set_name == 'C':
|
125 |
+
index = len(english_sentences_C) - 1
|
126 |
+
elif set_name == 'D':
|
127 |
+
index = len(english_sentences_D) - 1
|
128 |
+
elif set_name == 'F':
|
129 |
+
index = len(english_sentences_F) - 1
|
130 |
+
elif set_name == 'G':
|
131 |
+
index = len(english_sentences_G) - 1
|
132 |
+
|
133 |
+
return redirect(url_for('index', set=set_name, index=index))
|
134 |
+
|
135 |
if __name__ == '__main__':
|
136 |
app.run(debug=True, host="0.0.0.0", port=7860)
|