rein0421 commited on
Commit
524d49f
·
verified ·
1 Parent(s): 79dfd5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -121
app.py CHANGED
@@ -1,121 +1,39 @@
1
- import json, random, os
2
- from flask import Flask, jsonify, request, send_from_directory
3
-
4
- app = Flask(__name__)
5
-
6
- DATA_FILE = "sum.json" # 全フレーズの JSON ファイル
7
- WEAK_FILE = "weak_phrases.json" # 苦手フレーズ用の JSON ファイル
8
-
9
- def load_json(file_path):
10
- if os.path.exists(file_path):
11
- with open(file_path, "r", encoding="utf-8") as f:
12
- return json.load(f)
13
- else:
14
- return {"phrases": []}
15
-
16
- def save_json(file_path, data):
17
- with open(file_path, "w", encoding="utf-8") as f:
18
- json.dump(data, f, indent=4, ensure_ascii=False)
19
-
20
- def get_all_phrases():
21
- data = load_json(DATA_FILE)
22
- phrases = []
23
- for category in data["phrases"]:
24
- for phrase in category["phrases"]:
25
- phrases.append({
26
- "category": category.get("category_english", ""),
27
- "category_japanese": category.get("category_japanese", ""),
28
- "phrase": phrase.get("phrase", ""),
29
- "description": phrase.get("description", ""),
30
- "example": phrase.get("example", "")
31
- })
32
- return phrases
33
-
34
- def get_phrases_by_category(category_name):
35
- data = load_json(DATA_FILE)
36
- for category in data["phrases"]:
37
- if category.get("category_english", "").lower() == category_name.lower() or category.get("category_japanese", "") == category_name:
38
- return category["phrases"]
39
- return []
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)