Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,121 +1,39 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
def get_thanks_phrases():
|
42 |
-
data = load_json(DATA_FILE)
|
43 |
-
phrases = []
|
44 |
-
for category in data["phrases"]:
|
45 |
-
# この組み合わせのみ対象
|
46 |
-
if category.get("category_english") == "Thanks for..." and category.get("category_japanese") == "〜に感謝する":
|
47 |
-
for phrase in category["phrases"]:
|
48 |
-
phrases.append({
|
49 |
-
"category": category.get("category_english", ""),
|
50 |
-
"category_japanese": category.get("category_japanese", ""),
|
51 |
-
"phrase": phrase.get("phrase", ""),
|
52 |
-
"description": phrase.get("description", ""),
|
53 |
-
"example": phrase.get("example", "")
|
54 |
-
})
|
55 |
-
return phrases
|
56 |
-
|
57 |
-
def get_random_phrase(phrases):
|
58 |
-
if not phrases:
|
59 |
-
return None
|
60 |
-
return random.choice(phrases)
|
61 |
-
|
62 |
-
@app.route("/phrases", methods=["GET"])
|
63 |
-
def phrases():
|
64 |
-
mode = request.args.get("mode", "random")
|
65 |
-
if mode == "weak":
|
66 |
-
weak_data = load_json(WEAK_FILE)
|
67 |
-
phrase_list = weak_data.get("phrases", [])
|
68 |
-
elif mode == "category":
|
69 |
-
category = request.args.get("category", "")
|
70 |
-
phrase_list = get_phrases_by_category(category)
|
71 |
-
elif mode == "thanks":
|
72 |
-
phrase_list = get_thanks_phrases()
|
73 |
-
else:
|
74 |
-
phrase_list = get_all_phrases()
|
75 |
-
return jsonify(phrase_list)
|
76 |
-
|
77 |
-
@app.route("/add_weak", methods=["POST"])
|
78 |
-
def add_weak():
|
79 |
-
data = request.get_json()
|
80 |
-
phrase_text = data.get("phrase")
|
81 |
-
if not phrase_text:
|
82 |
-
return jsonify({"error": "フレーズが入力されていません"}), 400
|
83 |
-
|
84 |
-
all_phrases = get_all_phrases()
|
85 |
-
phrase_obj = next((p for p in all_phrases if p["phrase"] == phrase_text), None)
|
86 |
-
if not phrase_obj:
|
87 |
-
return jsonify({"error": "指定されたフレーズが見つかりません"}), 404
|
88 |
-
|
89 |
-
weak_data = load_json(WEAK_FILE)
|
90 |
-
if "phrases" not in weak_data:
|
91 |
-
weak_data["phrases"] = []
|
92 |
-
if phrase_obj not in weak_data["phrases"]:
|
93 |
-
weak_data["phrases"].append(phrase_obj)
|
94 |
-
save_json(WEAK_FILE, weak_data)
|
95 |
-
return jsonify({"message": "苦手フレーズとして追加しました", "phrase": phrase_obj})
|
96 |
-
else:
|
97 |
-
return jsonify({"message": "既に苦手フレーズとして登録済みです", "phrase": phrase_obj})
|
98 |
-
|
99 |
-
@app.route("/categories", methods=["GET"])
|
100 |
-
def categories():
|
101 |
-
data = load_json(DATA_FILE)
|
102 |
-
cats = []
|
103 |
-
for category in data["phrases"]:
|
104 |
-
cats.append({
|
105 |
-
"english": category.get("category_english", ""),
|
106 |
-
"japanese": category.get("category_japanese", "")
|
107 |
-
})
|
108 |
-
return jsonify(cats)
|
109 |
-
|
110 |
-
@app.route("/")
|
111 |
-
def index():
|
112 |
-
return send_from_directory(".", "index.html")
|
113 |
-
|
114 |
-
@app.route("/<path:filename>")
|
115 |
-
def static_files(filename):
|
116 |
-
return send_from_directory(".", filename)
|
117 |
-
|
118 |
-
if __name__ == "__main__":
|
119 |
-
# Hugging Face Spaces では PORT 環境変数が指定されるので、それを利用
|
120 |
-
port = int(os.environ.get("PORT", 7860))
|
121 |
-
app.run(debug=True, host="0.0.0.0", port=port)
|
|
|
1 |
+
from flask import Flask, request, jsonify,render_template
|
2 |
+
import base64
|
3 |
+
from pyannote.audio import Model, Inference
|
4 |
+
import numpy as np
|
5 |
+
import os
|
6 |
+
import shutil
|
7 |
+
from pydub import AudioSegment
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
app = Flask(__name__)
|
13 |
+
|
14 |
+
@app.route('/')
|
15 |
+
def root():
|
16 |
+
return render_template("record.html")
|
17 |
+
|
18 |
+
@app.route('/upload_audio', methods=['POST'])
|
19 |
+
def upload_audio():
|
20 |
+
try:
|
21 |
+
data = request.get_json() # クライアントから送られてきたJSONデータ
|
22 |
+
audio_data = data.get('audio_data') # Base64エンコードされた音声データ
|
23 |
+
|
24 |
+
if not audio_data:
|
25 |
+
return jsonify({"error": "音声データが送信されていません"}), 400
|
26 |
+
|
27 |
+
# Base64デコード
|
28 |
+
audio_binary = base64.b64decode(audio_data)
|
29 |
+
|
30 |
+
# WAVファイルとして保存
|
31 |
+
with open('recorded_audio.wav', 'wb') as f:
|
32 |
+
f.write(audio_binary)
|
33 |
+
|
34 |
+
return jsonify({"message": "音声が正常に保存されました"}), 200
|
35 |
+
except Exception as e:
|
36 |
+
return jsonify({"error": str(e)}), 500
|
37 |
+
|
38 |
+
if __name__ == '__main__':
|
39 |
+
app.run(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|