OzoneAsai commited on
Commit
042f51e
1 Parent(s): 99ddf7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -51
app.py CHANGED
@@ -1,21 +1,44 @@
1
- from flask import Flask, render_template, request, redirect, url_for
 
 
 
 
2
  from translation_data import translation_dict_A, translation_dict_B, translation_dict_C, translation_dict_D, translation_dict_F, translation_dict_G
3
 
4
  app = Flask(__name__)
5
 
6
- # リストに変換
 
 
 
7
  english_sentences_A = list(translation_dict_A.keys())
8
- japanese_translations_A = list(translation_dict_A.values())
9
  english_sentences_B = list(translation_dict_B.keys())
10
- japanese_translations_B = list(translation_dict_B.values())
11
  english_sentences_C = list(translation_dict_C.keys())
12
- japanese_translations_C = list(translation_dict_C.values())
13
  english_sentences_D = list(translation_dict_D.keys())
14
- japanese_translations_D = list(translation_dict_D.values())
15
  english_sentences_F = list(translation_dict_F.keys())
16
- japanese_translations_F = list(translation_dict_F.values())
17
  english_sentences_G = list(translation_dict_G.keys())
18
- japanese_translations_G = list(translation_dict_G.values())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  @app.route('/')
21
  def portal():
@@ -27,66 +50,31 @@ def index():
27
  index = int(request.args.get('index', 0))
28
  if set_name == 'A':
29
  english = english_sentences_A[index]
30
- japanese = japanese_translations_A[index]
31
  total = len(english_sentences_A)
32
  elif set_name == 'B':
33
  english = english_sentences_B[index]
34
- japanese = japanese_translations_B[index]
35
  total = len(english_sentences_B)
36
  elif set_name == 'C':
37
  english = english_sentences_C[index]
38
- japanese = japanese_translations_C[index]
39
  total = len(english_sentences_C)
40
  elif set_name == 'D':
41
  english = english_sentences_D[index]
42
- japanese = japanese_translations_D[index]
43
  total = len(english_sentences_D)
44
  elif set_name == 'F':
45
  english = english_sentences_F[index]
46
- japanese = japanese_translations_F[index]
47
  total = len(english_sentences_F)
48
  elif set_name == 'G':
49
  english = english_sentences_G[index]
50
- japanese = japanese_translations_G[index]
51
  total = len(english_sentences_G)
52
- return render_template('index.html', set_name=set_name, index=index, english=english, japanese=japanese, total=total)
53
-
54
- @app.route('/next')
55
- def next_card():
56
- set_name = request.args.get('set', 'A')
57
- index = int(request.args.get('index', 0)) + 1
58
- if set_name == 'A' and index >= len(english_sentences_A):
59
- index = 0
60
- elif set_name == 'B' and index >= len(english_sentences_B):
61
- index = 0
62
- elif set_name == 'C' and index >= len(english_sentences_C):
63
- index = 0
64
- elif set_name == 'D' and index >= len(english_sentences_D):
65
- index = 0
66
- elif set_name == 'F' and index >= len(english_sentences_F):
67
- index = 0
68
- elif set_name == 'G' and index >= len(english_sentences_G):
69
- index = 0
70
- return redirect(url_for('index', set=set_name, index=index))
71
 
72
- @app.route('/prev')
73
- def prev_card():
74
- set_name = request.args.get('set', 'A')
75
- index = int(request.args.get('index', 0)) - 1
76
- if index < 0:
77
- if set_name == 'A':
78
- index = len(english_sentences_A) - 1
79
- elif set_name == 'B':
80
- index = len(english_sentences_B) - 1
81
- elif set_name == 'C':
82
- index = len(english_sentences_C) - 1
83
- elif set_name == 'D':
84
- index = len(english_sentences_D) - 1
85
- elif set_name == 'F':
86
- index = len(english_sentences_F) - 1
87
- elif set_name == 'G':
88
- index = len(english_sentences_G) - 1
89
- return redirect(url_for('index', set=set_name, index=index))
90
 
91
  if __name__ == '__main__':
92
  app.run(debug=True, host="0.0.0.0", port=7860)
 
1
+ from flask import Flask, render_template, request, redirect, url_for, send_file
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())
 
17
  english_sentences_D = list(translation_dict_D.keys())
 
18
  english_sentences_F = list(translation_dict_F.keys())
 
19
  english_sentences_G = list(translation_dict_G.keys())
20
+
21
+ # 音声ファイルを生成する関数
22
+ def generate_audio(text, lang='en', filename=None):
23
+ if filename is None:
24
+ filename = f"{AUDIO_DIR}/{text[:10].replace(' ', '_')}.mp3"
25
+
26
+ if not os.path.exists(filename): # ファイルが存在しない場合のみ生成
27
+ tts = gTTS(text=text, lang=lang)
28
+ tts.save(filename)
29
+ return filename
30
+
31
+ # サーバー起動時にすべての英語音声ファイルを生成
32
+ def generate_all_audio_files():
33
+ os.makedirs(AUDIO_DIR, exist_ok=True)
34
+
35
+ # 各セットに対して音声ファイルを生成
36
+ for sentence in english_sentences_A + english_sentences_B + english_sentences_C + english_sentences_D + english_sentences_F + english_sentences_G:
37
+ generate_audio(sentence, 'en')
38
+
39
+ # Flaskアプリ起動時に音声ファイルを生成
40
+ with app.app_context():
41
+ generate_all_audio_files()
42
 
43
  @app.route('/')
44
  def portal():
 
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]
54
  total = len(english_sentences_A)
55
  elif set_name == 'B':
56
  english = english_sentences_B[index]
57
+ japanese = translation_dict_B[english]
58
  total = len(english_sentences_B)
59
  elif set_name == 'C':
60
  english = english_sentences_C[index]
61
+ japanese = translation_dict_C[english]
62
  total = len(english_sentences_C)
63
  elif set_name == 'D':
64
  english = english_sentences_D[index]
65
+ japanese = translation_dict_D[english]
66
  total = len(english_sentences_D)
67
  elif set_name == 'F':
68
  english = english_sentences_F[index]
69
+ japanese = translation_dict_F[english]
70
  total = len(english_sentences_F)
71
  elif set_name == 'G':
72
  english = english_sentences_G[index]
73
+ japanese = translation_dict_G[english]
74
  total = len(english_sentences_G)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
+ audio_url = url_for('static', filename=f"audio/{english[:10].replace(' ', '_')}.mp3")
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)