CnJaFlashC-1 / app.py
OzoneAsai's picture
Update app.py
50f1558 verified
raw
history blame
4.72 kB
from flask import Flask, render_template, request, redirect, url_for, jsonify
from gtts import gTTS
import os
from translation_data import *
app = Flask(__name__)
# 音声ファイルの保存ディレクトリ
AUDIO_DIR = 'static/audio'
# リストに変換
english_sentences_A = list(translation_dict_A.keys())
english_sentences_B = list(translation_dict_B.keys())
english_sentences_C = list(translation_dict_C.keys())
english_sentences_D = list(translation_dict_D.keys())
english_sentences_F = list(translation_dict_F.keys())
english_sentences_G = list(translation_dict_G.keys())
# 音声ファイルを生成する関数
def generate_audio(text, set_name, index):
filename = f"{AUDIO_DIR}/{set_name}_{index}.mp3"
# ファイルが存在しない場合のみ生成
if not os.path.exists(filename):
tts = gTTS(text=text, lang='en')
tts.save(filename)
return filename
# whereAudio エンドポイントを作成して音声ファイルのURLを返す
@app.route('/whereAudio')
def where_audio():
set_name = request.args.get('set', 'A')
index = int(request.args.get('index', 0))
if set_name == 'A':
english = english_sentences_A[index]
elif set_name == 'B':
english = english_sentences_B[index]
elif set_name == 'C':
english = english_sentences_C[index]
elif set_name == 'D':
english = english_sentences_D[index]
elif set_name == 'F':
english = english_sentences_F[index]
elif set_name == 'G':
english = english_sentences_G[index]
# 音声ファイルの生成
audio_url = url_for('static', filename=f"audio/{set_name}_{index}.mp3")
generate_audio(english, set_name, index)
# 音声ファイルのURLをJSONで返す
return jsonify({'audio_url': audio_url})
@app.route('/')
def portal():
return render_template('portal.html')
@app.route('/flashcards')
def index():
set_name = request.args.get('set', 'A')
index = int(request.args.get('index', 0))
if set_name == 'A':
english = english_sentences_A[index]
japanese = translation_dict_A[english]
total = len(english_sentences_A)
elif set_name == 'B':
english = english_sentences_B[index]
japanese = translation_dict_B[english]
total = len(english_sentences_B)
elif set_name == 'C':
english = english_sentences_C[index]
japanese = translation_dict_C[english]
total = len(english_sentences_C)
elif set_name == 'D':
english = english_sentences_D[index]
japanese = translation_dict_D[english]
total = len(english_sentences_D)
elif set_name == 'F':
english = english_sentences_F[index]
japanese = translation_dict_F[english]
total = len(english_sentences_F)
elif set_name == 'G':
english = english_sentences_G[index]
japanese = translation_dict_G[english]
total = len(english_sentences_G)
# 初回の音声ファイルを生成
audio_url = url_for('static', filename=f"audio/{set_name}_{index}.mp3")
generate_audio(english, set_name, index)
return render_template('index.html', set_name=set_name, index=index, english=english, japanese=japanese, total=total, audio_url=audio_url)
@app.route('/next')
def next_card():
set_name = request.args.get('set', 'A')
index = int(request.args.get('index', 0)) + 1
if set_name == 'A' and index >= len(english_sentences_A):
index = 0
elif set_name == 'B' and index >= len(english_sentences_B):
index = 0
elif set_name == 'C' and index >= len(english_sentences_C):
index = 0
elif set_name == 'D' and index >= len(english_sentences_D):
index = 0
elif set_name == 'F' and index >= len(english_sentences_F):
index = 0
elif set_name == 'G' and index >= len(english_sentences_G):
index = 0
return redirect(url_for('index', set=set_name, index=index))
@app.route('/prev')
def prev_card():
set_name = request.args.get('set', 'A')
index = int(request.args.get('index', 0)) - 1
if index < 0:
if set_name == 'A':
index = len(english_sentences_A) - 1
elif set_name == 'B':
index = len(english_sentences_B) - 1
elif set_name == 'C':
index = len(english_sentences_C) - 1
elif set_name == 'D':
index = len(english_sentences_D) - 1
elif set_name == 'F':
index = len(english_sentences_F) - 1
elif set_name == 'G':
index = len(english_sentences_G) - 1
return redirect(url_for('index', set=set_name, index=index))
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=7860)