rein0421 commited on
Commit
29f9ef3
·
verified ·
1 Parent(s): 61014cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -121
app.py CHANGED
@@ -1,121 +1,44 @@
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, send_from_directory
2
+ import base64
3
+ import os
4
+
5
+ app = Flask(__name__)
6
+
7
+ @app.route('/')
8
+ def index():
9
+ return send_from_directory(".", "index.html")
10
+
11
+ @app.route('/upload_audio', methods=['POST'])
12
+ def upload_audio():
13
+ try:
14
+ data = request.get_json()
15
+ if not data:
16
+ return jsonify({"error": "JSONが送信されていません"}), 400
17
+
18
+ audio_data = data.get('audio_data')
19
+ if not audio_data:
20
+ return jsonify({"error": "音声データが送信されていません"}), 400
21
+
22
+ # Base64デコード
23
+ try:
24
+ audio_binary = base64.b64decode(audio_data)
25
+ except Exception as decode_err:
26
+ return jsonify({"error": "Base64デコードに失敗しました", "details": str(decode_err)}), 400
27
+
28
+ # 書き込み用ディレクトリとして /tmp/data を使用(/tmp は書き込み可能)
29
+ persist_dir = "/tmp/data"
30
+ os.makedirs(persist_dir, exist_ok=True)
31
+
32
+ filepath = os.path.join(persist_dir, "recorded_audio.wav")
33
+ with open(filepath, 'wb') as f:
34
+ f.write(audio_binary)
35
+
36
+ return jsonify({"message": "音声が正常に保存されました", "filepath": filepath}), 200
37
+
38
+ except Exception as e:
39
+ app.logger.error("エラー: %s", str(e))
40
+ return jsonify({"error": "サーバー内部エラー", "details": str(e)}), 500
41
+
42
+ if __name__ == '__main__':
43
+ port = int(os.environ.get("PORT", 7860))
44
+ app.run(debug=True, host="0.0.0.0", port=port)