Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,493 +1,503 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify, render_template, send_from_directory
|
| 2 |
-
import base64
|
| 3 |
-
from pydub import AudioSegment
|
| 4 |
-
import os
|
| 5 |
-
import shutil
|
| 6 |
-
import requests
|
| 7 |
-
import tempfile
|
| 8 |
-
import json
|
| 9 |
-
from process import AudioProcessor
|
| 10 |
-
from transcription import TranscriptionMaker
|
| 11 |
-
from analyze import TextAnalyzer
|
| 12 |
-
|
| 13 |
-
process = AudioProcessor()
|
| 14 |
-
transcripter = TranscriptionMaker()
|
| 15 |
-
app = Flask(__name__)
|
| 16 |
-
|
| 17 |
-
# GASのエンドポイントURL
|
| 18 |
-
GAS_URL = "https://script.google.com/macros/s/AKfycbwR2cnMKVU1AxoT9NDaeZaNUaTiwRX64Ul0sH0AU4ccP49Byph-TpxtM_Lwm4G9zLnuYA/exec"
|
| 19 |
-
|
| 20 |
-
users = [] # 選択されたユーザーのリスト
|
| 21 |
-
all_users = [] # 利用可能なすべてのユーザーのリスト
|
| 22 |
-
transcription_text = ""
|
| 23 |
-
harassment_keywords = [
|
| 24 |
-
"バカ", "馬鹿", "アホ", "死ね", "クソ", "うざい",
|
| 25 |
-
"きもい", "キモい", "ブス", "デブ", "ハゲ",
|
| 26 |
-
"セクハラ", "パワハラ", "モラハラ"
|
| 27 |
-
]
|
| 28 |
-
total_audio = ""
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
@app.route('/index', methods=['GET', 'POST'])
|
| 32 |
-
def index():
|
| 33 |
-
return render_template('index.html', users=users)
|
| 34 |
-
|
| 35 |
-
# フィードバック画面(テンプレート: feedback.html)
|
| 36 |
-
@app.route('/feedback', methods=['GET', 'POST'])
|
| 37 |
-
def feedback():
|
| 38 |
-
return render_template('feedback.html')
|
| 39 |
-
|
| 40 |
-
# 会話詳細画面(テンプレート: talkDetail.html)
|
| 41 |
-
@app.route('/talk_detail', methods=['GET', 'POST'])
|
| 42 |
-
def talk_detail():
|
| 43 |
-
return render_template('talkDetail.html')
|
| 44 |
-
|
| 45 |
-
# 音声登録画面(テンプレート: userRegister.html)
|
| 46 |
-
@app.route('/userregister', methods=['GET', 'POST'])
|
| 47 |
-
def userregister():
|
| 48 |
-
return render_template('userRegister.html')
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
@app.route('
|
| 52 |
-
def
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
print(
|
| 217 |
-
raise Exception("
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
#
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
raise
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
if
|
| 264 |
-
raise Exception(f"
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
total_audio = segments_dir
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
return jsonify({"
|
| 487 |
-
except Exception as e:
|
| 488 |
-
print("Error in /
|
| 489 |
-
return jsonify({"error": "サーバーエラー", "details": str(e)}), 500
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 493 |
app.run(debug=True, host="0.0.0.0", port=port)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, render_template, send_from_directory
|
| 2 |
+
import base64
|
| 3 |
+
from pydub import AudioSegment
|
| 4 |
+
import os
|
| 5 |
+
import shutil
|
| 6 |
+
import requests
|
| 7 |
+
import tempfile
|
| 8 |
+
import json
|
| 9 |
+
from process import AudioProcessor
|
| 10 |
+
from transcription import TranscriptionMaker
|
| 11 |
+
from analyze import TextAnalyzer
|
| 12 |
+
|
| 13 |
+
process = AudioProcessor()
|
| 14 |
+
transcripter = TranscriptionMaker()
|
| 15 |
+
app = Flask(__name__)
|
| 16 |
+
|
| 17 |
+
# GASのエンドポイントURL
|
| 18 |
+
GAS_URL = "https://script.google.com/macros/s/AKfycbwR2cnMKVU1AxoT9NDaeZaNUaTiwRX64Ul0sH0AU4ccP49Byph-TpxtM_Lwm4G9zLnuYA/exec"
|
| 19 |
+
|
| 20 |
+
users = [] # 選択されたユーザーのリスト
|
| 21 |
+
all_users = [] # 利用可能なすべてのユーザーのリスト
|
| 22 |
+
transcription_text = ""
|
| 23 |
+
harassment_keywords = [
|
| 24 |
+
"バカ", "馬鹿", "アホ", "死ね", "クソ", "うざい",
|
| 25 |
+
"きもい", "キモい", "ブス", "デブ", "ハゲ",
|
| 26 |
+
"セクハラ", "パワハラ", "モラハラ"
|
| 27 |
+
]
|
| 28 |
+
total_audio = ""
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@app.route('/index', methods=['GET', 'POST'])
|
| 32 |
+
def index():
|
| 33 |
+
return render_template('index.html', users=users)
|
| 34 |
+
|
| 35 |
+
# フィードバック画面(テンプレート: feedback.html)
|
| 36 |
+
@app.route('/feedback', methods=['GET', 'POST'])
|
| 37 |
+
def feedback():
|
| 38 |
+
return render_template('feedback.html')
|
| 39 |
+
|
| 40 |
+
# 会話詳細画面(テンプレート: talkDetail.html)
|
| 41 |
+
@app.route('/talk_detail', methods=['GET', 'POST'])
|
| 42 |
+
def talk_detail():
|
| 43 |
+
return render_template('talkDetail.html')
|
| 44 |
+
|
| 45 |
+
# 音声登録画面(テンプレート: userRegister.html)
|
| 46 |
+
@app.route('/userregister', methods=['GET', 'POST'])
|
| 47 |
+
def userregister():
|
| 48 |
+
return render_template('userRegister.html')
|
| 49 |
+
|
| 50 |
+
#(テンプレート: main.css)
|
| 51 |
+
@app.route('main.css', methods=['GET', 'POST'])
|
| 52 |
+
def feedback():
|
| 53 |
+
return render_template('main.css')
|
| 54 |
+
|
| 55 |
+
#(テンプレート: menu.css)
|
| 56 |
+
@app.route('menu.css', methods=['GET', 'POST'])
|
| 57 |
+
def feedback():
|
| 58 |
+
return render_template('menu.css')
|
| 59 |
+
|
| 60 |
+
# 人数確認
|
| 61 |
+
@app.route('/confirm', methods=['GET'])
|
| 62 |
+
def confirm():
|
| 63 |
+
global all_users
|
| 64 |
+
# 最新のユーザーリストを取得
|
| 65 |
+
try:
|
| 66 |
+
update_all_users()
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(f"ユーザーリストの更新エラー: {str(e)}")
|
| 69 |
+
return jsonify({'members': users, 'all_members': all_users}), 200
|
| 70 |
+
|
| 71 |
+
# リセット画面(テンプレート: reset.html)
|
| 72 |
+
@app.route('/reset_html', methods=['GET', 'POST'])
|
| 73 |
+
def reset_html():
|
| 74 |
+
return render_template('reset.html')
|
| 75 |
+
|
| 76 |
+
# メンバー削除&累積音声削除
|
| 77 |
+
@app.route('/reset_member', methods=['GET', 'POST'])
|
| 78 |
+
def reset_member():
|
| 79 |
+
global users
|
| 80 |
+
global total_audio
|
| 81 |
+
global transcription_text
|
| 82 |
+
|
| 83 |
+
# 一時ディレクトリのクリーンアップ
|
| 84 |
+
if total_audio:
|
| 85 |
+
process.delete_files_in_directory(total_audio)
|
| 86 |
+
process.delete_files_in_directory('/tmp/data/transcription_audio')
|
| 87 |
+
|
| 88 |
+
# 書き起こしテキストの削除
|
| 89 |
+
if os.path.exists(transcription_text):
|
| 90 |
+
try:
|
| 91 |
+
os.remove(transcription_text)
|
| 92 |
+
print(f"{transcription_text} を削除しました。")
|
| 93 |
+
except Exception as e:
|
| 94 |
+
print(f"ファイル削除中にエラーが発生しました: {e}")
|
| 95 |
+
|
| 96 |
+
transcription_text = ""
|
| 97 |
+
|
| 98 |
+
try:
|
| 99 |
+
data = request.get_json()
|
| 100 |
+
if not data or "names" not in data:
|
| 101 |
+
return jsonify({"status": "error", "message": "Invalid request body"}), 400
|
| 102 |
+
|
| 103 |
+
names = data.get("names", [])
|
| 104 |
+
|
| 105 |
+
# GASからファイルを削除
|
| 106 |
+
for name in names:
|
| 107 |
+
try:
|
| 108 |
+
delete_from_cloud(f"{name}.wav")
|
| 109 |
+
print(f"クラウドから {name}.wav を削除しました。")
|
| 110 |
+
except Exception as e:
|
| 111 |
+
print(f"クラウド削除中にエラーが発生しました: {e}")
|
| 112 |
+
return jsonify({"status": "error", "message": f"Failed to delete {name} from cloud: {e}"}), 500
|
| 113 |
+
|
| 114 |
+
# usersリストから削除するユーザーを除外
|
| 115 |
+
users = [u for u in users if u not in names]
|
| 116 |
+
|
| 117 |
+
# 全ユーザーリストの更新
|
| 118 |
+
update_all_users()
|
| 119 |
+
|
| 120 |
+
return jsonify({"status": "success", "message": "Members deleted successfully", "users": users}), 200
|
| 121 |
+
|
| 122 |
+
except Exception as e:
|
| 123 |
+
print(f"An unexpected error occurred: {e}")
|
| 124 |
+
return jsonify({"status": "error", "message": f"Internal server error: {e}"}), 500
|
| 125 |
+
|
| 126 |
+
# 書き起こし作成エンドポイント
|
| 127 |
+
@app.route('/transcription', methods=['GET', 'POST'])
|
| 128 |
+
def transcription():
|
| 129 |
+
global transcription_text
|
| 130 |
+
global total_audio
|
| 131 |
+
|
| 132 |
+
if not os.path.exists(transcription_text) or not transcription_text:
|
| 133 |
+
try:
|
| 134 |
+
if not total_audio or not os.path.exists(total_audio):
|
| 135 |
+
return jsonify({"error": "No audio segments provided"}), 400
|
| 136 |
+
audio_directory = transcripter.merge_segments(total_audio, '/tmp/data/transcription_audio')
|
| 137 |
+
transcription_text = transcripter.create_transcription(audio_directory)
|
| 138 |
+
print("transcription")
|
| 139 |
+
print(transcription_text)
|
| 140 |
+
except Exception as e:
|
| 141 |
+
return jsonify({"error": str(e)}), 500
|
| 142 |
+
|
| 143 |
+
try:
|
| 144 |
+
with open(transcription_text, 'r', encoding='utf-8') as file:
|
| 145 |
+
file_content = file.read()
|
| 146 |
+
print(file_content)
|
| 147 |
+
return jsonify({'transcription': file_content}), 200
|
| 148 |
+
except FileNotFoundError:
|
| 149 |
+
return jsonify({"error": "Transcription file not found"}), 404
|
| 150 |
+
except Exception as e:
|
| 151 |
+
return jsonify({"error": f"Unexpected error: {str(e)}"}), 500
|
| 152 |
+
|
| 153 |
+
# AI分析エンドポイント
|
| 154 |
+
@app.route('/analyze', methods=['GET', 'POST'])
|
| 155 |
+
def analyze():
|
| 156 |
+
global transcription_text
|
| 157 |
+
global total_audio
|
| 158 |
+
|
| 159 |
+
if not os.path.exists(transcription_text) or not transcription_text:
|
| 160 |
+
try:
|
| 161 |
+
if not total_audio:
|
| 162 |
+
return jsonify({"error": "No audio segments provided"}), 400
|
| 163 |
+
audio_directory = transcripter.merge_segments(total_audio, '/tmp/data/transcription_audio')
|
| 164 |
+
transcription_text = transcripter.create_transcription(audio_directory)
|
| 165 |
+
except Exception as e:
|
| 166 |
+
return jsonify({"error": str(e)}), 500
|
| 167 |
+
|
| 168 |
+
analyzer = TextAnalyzer(transcription_text, harassment_keywords)
|
| 169 |
+
api_key = os.environ.get("DEEPSEEK")
|
| 170 |
+
if api_key is None:
|
| 171 |
+
raise ValueError("DEEPSEEK_API_KEY が設定されていません。")
|
| 172 |
+
|
| 173 |
+
results = analyzer.analyze(api_key=api_key)
|
| 174 |
+
|
| 175 |
+
print(json.dumps(results, ensure_ascii=False, indent=2))
|
| 176 |
+
|
| 177 |
+
if "deepseek_analysis" in results and results["deepseek_analysis"]:
|
| 178 |
+
deepseek_data = results["deepseek_analysis"]
|
| 179 |
+
conversation_level = deepseek_data.get("conversationLevel")
|
| 180 |
+
harassment_present = deepseek_data.get("harassmentPresent")
|
| 181 |
+
harassment_type = deepseek_data.get("harassmentType")
|
| 182 |
+
repetition = deepseek_data.get("repetition")
|
| 183 |
+
pleasantConversation = deepseek_data.get("pleasantConversation")
|
| 184 |
+
blameOrHarassment = deepseek_data.get("blameOrHarassment")
|
| 185 |
+
|
| 186 |
+
print("\n--- DeepSeek 分析結果 ---")
|
| 187 |
+
print(f"会話レベル: {conversation_level}")
|
| 188 |
+
print(f"ハラスメントの有無: {harassment_present}")
|
| 189 |
+
print(f"ハラスメントの種類: {harassment_type}")
|
| 190 |
+
print(f"繰り返しの程度: {repetition}")
|
| 191 |
+
print(f"会話の心地よさ: {pleasantConversation}")
|
| 192 |
+
print(f"非難またはハラスメントの程度: {blameOrHarassment}")
|
| 193 |
+
|
| 194 |
+
return jsonify({"results": results}), 200
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
# クラウドから音声を取得してローカルに保存する関数
|
| 198 |
+
def download_from_cloud(filename, local_path):
|
| 199 |
+
try:
|
| 200 |
+
payload = {
|
| 201 |
+
"action": "download",
|
| 202 |
+
"fileName": filename
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
print(f"クラウドから {filename} をダウンロード中...")
|
| 206 |
+
response = requests.post(GAS_URL, json=payload)
|
| 207 |
+
if response.status_code != 200:
|
| 208 |
+
print(f"ダウンロードエラー: ステータスコード {response.status_code}")
|
| 209 |
+
print(f"レスポンス: {response.text}")
|
| 210 |
+
raise Exception(f"クラウドからのダウンロードに失敗しました: {response.text}")
|
| 211 |
+
|
| 212 |
+
try:
|
| 213 |
+
res_json = response.json()
|
| 214 |
+
except:
|
| 215 |
+
print("JSONデコードエラー、レスポンス内容:")
|
| 216 |
+
print(response.text[:500]) # 最初の500文字だけ表示
|
| 217 |
+
raise Exception("サーバーからの応答をJSONとして解析できませんでした")
|
| 218 |
+
|
| 219 |
+
if res_json.get("status") != "success":
|
| 220 |
+
print(f"ダウンロードステータスエラー: {res_json.get('message')}")
|
| 221 |
+
raise Exception(f"クラウドからのダウンロードに失敗しました: {res_json.get('message')}")
|
| 222 |
+
|
| 223 |
+
# Base64文字列をデコード
|
| 224 |
+
base64_data = res_json.get("base64Data")
|
| 225 |
+
if not base64_data:
|
| 226 |
+
print("Base64データが存在しません")
|
| 227 |
+
raise Exception("応答にBase64データが含まれていません")
|
| 228 |
+
|
| 229 |
+
try:
|
| 230 |
+
audio_binary = base64.b64decode(base64_data)
|
| 231 |
+
except Exception as e:
|
| 232 |
+
print(f"Base64デコードエラー: {str(e)}")
|
| 233 |
+
raise Exception(f"音声データのデコードに失敗しました: {str(e)}")
|
| 234 |
+
|
| 235 |
+
# 指定パスに保存
|
| 236 |
+
os.makedirs(os.path.dirname(local_path), exist_ok=True)
|
| 237 |
+
with open(local_path, 'wb') as f:
|
| 238 |
+
f.write(audio_binary)
|
| 239 |
+
|
| 240 |
+
print(f"{filename} をローカルに保存しました: {local_path}")
|
| 241 |
+
|
| 242 |
+
# データの整合性チェック(ファイルサイズが0より大きいかなど)
|
| 243 |
+
if os.path.getsize(local_path) <= 0:
|
| 244 |
+
raise Exception(f"保存されたファイル {local_path} のサイズが0バイトです")
|
| 245 |
+
|
| 246 |
+
return local_path
|
| 247 |
+
except Exception as e:
|
| 248 |
+
print(f"ダウンロード中にエラーが発生しました: {str(e)}")
|
| 249 |
+
# エラーを上位に伝播させる
|
| 250 |
+
raise
|
| 251 |
+
|
| 252 |
+
# クラウドからファイルを削除する関数
|
| 253 |
+
def delete_from_cloud(filename):
|
| 254 |
+
payload = {
|
| 255 |
+
"action": "delete",
|
| 256 |
+
"fileName": filename
|
| 257 |
+
}
|
| 258 |
+
response = requests.post(GAS_URL, json=payload)
|
| 259 |
+
if response.status_code != 200:
|
| 260 |
+
raise Exception(f"クラウドからの削除に失敗しました: {response.text}")
|
| 261 |
+
|
| 262 |
+
res_json = response.json()
|
| 263 |
+
if res_json.get("status") != "success":
|
| 264 |
+
raise Exception(f"クラウドからの削除に失敗しました: {res_json.get('message')}")
|
| 265 |
+
|
| 266 |
+
return True
|
| 267 |
+
# すべてのベース音声ユーザーリストを更新する関数
|
| 268 |
+
def update_all_users():
|
| 269 |
+
global all_users
|
| 270 |
+
|
| 271 |
+
payload = {"action": "list"}
|
| 272 |
+
response = requests.post(GAS_URL, json=payload)
|
| 273 |
+
if response.status_code != 200:
|
| 274 |
+
raise Exception(f"GAS一覧取得エラー: {response.text}")
|
| 275 |
+
|
| 276 |
+
res_json = response.json()
|
| 277 |
+
if res_json.get("status") != "success":
|
| 278 |
+
raise Exception(f"GAS一覧取得失敗: {res_json.get('message')}")
|
| 279 |
+
|
| 280 |
+
# ファイル名から拡張子を除去してユーザーリストを作成
|
| 281 |
+
all_users = [os.path.splitext(filename)[0] for filename in res_json.get("fileNames", [])]
|
| 282 |
+
return all_users
|
| 283 |
+
|
| 284 |
+
# 音声アップロード&解析エンドポイント
|
| 285 |
+
@app.route('/upload_audio', methods=['POST'])
|
| 286 |
+
def upload_audio():
|
| 287 |
+
global total_audio
|
| 288 |
+
global users
|
| 289 |
+
|
| 290 |
+
try:
|
| 291 |
+
data = request.get_json()
|
| 292 |
+
if not data or 'audio_data' not in data:
|
| 293 |
+
return jsonify({"error": "音声データがありません"}), 400
|
| 294 |
+
|
| 295 |
+
# リクエストからユーザーリストを取得(指定がなければ現在のusersを使用)
|
| 296 |
+
if 'selected_users' in data and data['selected_users']:
|
| 297 |
+
users = data['selected_users']
|
| 298 |
+
print(f"選択されたユーザー: {users}")
|
| 299 |
+
|
| 300 |
+
if not users:
|
| 301 |
+
return jsonify({"error": "選択されたユーザーがいません"}), 400
|
| 302 |
+
|
| 303 |
+
# Base64デコードして音声バイナリを取得
|
| 304 |
+
audio_binary = base64.b64decode(data['audio_data'])
|
| 305 |
+
|
| 306 |
+
upload_name = 'tmp'
|
| 307 |
+
audio_dir = "/tmp/data"
|
| 308 |
+
os.makedirs(audio_dir, exist_ok=True)
|
| 309 |
+
audio_path = os.path.join(audio_dir, f"{upload_name}.wav")
|
| 310 |
+
with open(audio_path, 'wb') as f:
|
| 311 |
+
f.write(audio_binary)
|
| 312 |
+
|
| 313 |
+
print(f"処理を行うユーザー: {users}")
|
| 314 |
+
|
| 315 |
+
# ベース音声を一時ディレクトリにダウンロード
|
| 316 |
+
temp_dir = "/tmp/data/base_audio"
|
| 317 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 318 |
+
|
| 319 |
+
# 各ユーザーの参照音声ファイルのパスをリストに格納
|
| 320 |
+
reference_paths = []
|
| 321 |
+
for user in users:
|
| 322 |
+
try:
|
| 323 |
+
ref_path = os.path.join(temp_dir, f"{user}.wav")
|
| 324 |
+
if not os.path.exists(ref_path):
|
| 325 |
+
# クラウドから取得
|
| 326 |
+
download_from_cloud(f"{user}.wav", ref_path)
|
| 327 |
+
print(f"クラウドから {user}.wav をダウンロードしました")
|
| 328 |
+
|
| 329 |
+
if not os.path.exists(ref_path):
|
| 330 |
+
return jsonify({"error": "参照音声ファイルが見つかりません", "details": ref_path}), 500
|
| 331 |
+
|
| 332 |
+
reference_paths.append(ref_path)
|
| 333 |
+
except Exception as e:
|
| 334 |
+
return jsonify({"error": f"ユーザー {user} の音声取得に失敗しました", "details": str(e)}), 500
|
| 335 |
+
|
| 336 |
+
# 複数人の場合は参照パスのリストを、1人の場合は単一のパスを渡す
|
| 337 |
+
if len(users) > 1:
|
| 338 |
+
print("複数人の場合の処理")
|
| 339 |
+
matched_times, segments_dir = process.process_multi_audio(reference_paths, audio_path, threshold=0.05)
|
| 340 |
+
total_audio = segments_dir
|
| 341 |
+
# 各メンバーのrateを計算
|
| 342 |
+
total_time = sum(matched_times)
|
| 343 |
+
rates = [(time / total_time) * 100 if total_time > 0 else 0 for time in matched_times]
|
| 344 |
+
|
| 345 |
+
# ユーザー名と話した割合をマッピング
|
| 346 |
+
user_rates = {users[i]: rates[i] for i in range(len(users))}
|
| 347 |
+
return jsonify({"rates": rates, "user_rates": user_rates}), 200
|
| 348 |
+
else:
|
| 349 |
+
matched_time, unmatched_time, segments_dir = process.process_audio(reference_paths[0], audio_path, threshold=0.05)
|
| 350 |
+
total_audio = segments_dir
|
| 351 |
+
print("単一ユーザーの処理")
|
| 352 |
+
print(total_audio)
|
| 353 |
+
total_time = matched_time + unmatched_time
|
| 354 |
+
rate = (matched_time / total_time) * 100 if total_time > 0 else 0
|
| 355 |
+
return jsonify({"rate": rate, "user": users[0]}), 200
|
| 356 |
+
|
| 357 |
+
except Exception as e:
|
| 358 |
+
print("Error in /upload_audio:", str(e))
|
| 359 |
+
return jsonify({"error": "サーバーエラー", "details": str(e)}), 500
|
| 360 |
+
|
| 361 |
+
# ユーザー選択画面(テンプレート: userSelect.html)
|
| 362 |
+
@app.route('/')
|
| 363 |
+
@app.route('/userselect', methods=['GET'])
|
| 364 |
+
def userselect():
|
| 365 |
+
return render_template('userSelect.html')
|
| 366 |
+
|
| 367 |
+
# 選択したユーザーを設定するエンドポイント
|
| 368 |
+
@app.route('/select_users', methods=['POST'])
|
| 369 |
+
def select_users():
|
| 370 |
+
global users
|
| 371 |
+
|
| 372 |
+
try:
|
| 373 |
+
data = request.get_json()
|
| 374 |
+
if not data or 'users' not in data:
|
| 375 |
+
return jsonify({"error": "ユーザーリストがありません"}), 400
|
| 376 |
+
|
| 377 |
+
users = data['users']
|
| 378 |
+
print(f"選択されたユーザー: {users}")
|
| 379 |
+
|
| 380 |
+
return jsonify({"status": "success", "selected_users": users}), 200
|
| 381 |
+
except Exception as e:
|
| 382 |
+
print("Error in /select_users:", str(e))
|
| 383 |
+
return jsonify({"error": "サーバーエラー", "details": str(e)}), 500
|
| 384 |
+
|
| 385 |
+
@app.route('/reset', methods=['GET'])
|
| 386 |
+
def reset():
|
| 387 |
+
global users
|
| 388 |
+
users = []
|
| 389 |
+
global total_audio
|
| 390 |
+
global transcription_text
|
| 391 |
+
|
| 392 |
+
# 一時ディレクトリのクリーンアップ
|
| 393 |
+
if total_audio:
|
| 394 |
+
process.delete_files_in_directory(total_audio)
|
| 395 |
+
process.delete_files_in_directory('/tmp/data/transcription_audio')
|
| 396 |
+
|
| 397 |
+
# 書き起こしテキストの削除
|
| 398 |
+
if os.path.exists(transcription_text):
|
| 399 |
+
try:
|
| 400 |
+
os.remove(transcription_text)
|
| 401 |
+
print(f"{transcription_text} を削除しました。")
|
| 402 |
+
except Exception as e:
|
| 403 |
+
print(f"ファイル削除中にエラーが発生しました: {e}")
|
| 404 |
+
|
| 405 |
+
transcription_text = ""
|
| 406 |
+
|
| 407 |
+
return jsonify({"status": "success", "message": "Users reset"}), 200
|
| 408 |
+
|
| 409 |
+
@app.route('/copy_selected_files', methods=['POST'])
|
| 410 |
+
def copy_selected_files():
|
| 411 |
+
try:
|
| 412 |
+
data = request.get_json()
|
| 413 |
+
if not data or "names" not in data:
|
| 414 |
+
return jsonify({"error": "namesパラメータが存在しません"}), 400
|
| 415 |
+
|
| 416 |
+
names = data["names"]
|
| 417 |
+
dest_dir = "/tmp/data/selected_audio" # コピー先のフォルダ
|
| 418 |
+
os.makedirs(dest_dir, exist_ok=True)
|
| 419 |
+
|
| 420 |
+
copied_files = []
|
| 421 |
+
for name in names:
|
| 422 |
+
dest_path = os.path.join(dest_dir, f"{name}.wav")
|
| 423 |
+
try:
|
| 424 |
+
# クラウドから直接ダウンロード
|
| 425 |
+
download_from_cloud(f"{name}.wav", dest_path)
|
| 426 |
+
copied_files.append(name)
|
| 427 |
+
print(f"{name}.wav を {dest_path} にダウンロードしました。")
|
| 428 |
+
except Exception as e:
|
| 429 |
+
print(f"ダウンロード中にエラーが発生しました: {e}")
|
| 430 |
+
continue
|
| 431 |
+
|
| 432 |
+
return jsonify({"status": "success", "copied": copied_files}), 200
|
| 433 |
+
|
| 434 |
+
except Exception as e:
|
| 435 |
+
print("Error in /copy_selected_files:", str(e))
|
| 436 |
+
return jsonify({"error": "サーバー内部エラー", "details": str(e)}), 500
|
| 437 |
+
|
| 438 |
+
@app.route('/clear_tmp', methods=['GET'])
|
| 439 |
+
def clear_tmp():
|
| 440 |
+
try:
|
| 441 |
+
tmp_dir = "/tmp/data" # アプリケーションが使用しているtmpフォルダ
|
| 442 |
+
# ファイルのみの削除
|
| 443 |
+
process.delete_files_in_directory(tmp_dir)
|
| 444 |
+
# フォルダがあれば再帰的に削除
|
| 445 |
+
for item in os.listdir(tmp_dir):
|
| 446 |
+
item_path = os.path.join(tmp_dir, item)
|
| 447 |
+
if os.path.isdir(item_path):
|
| 448 |
+
shutil.rmtree(item_path)
|
| 449 |
+
print(f"ディレクトリを削除しました: {item_path}")
|
| 450 |
+
|
| 451 |
+
return jsonify({"status": "success", "message": "tmp配下がすべて削除されました"}), 200
|
| 452 |
+
|
| 453 |
+
except Exception as e:
|
| 454 |
+
print("Error in /clear_tmp:", str(e))
|
| 455 |
+
return jsonify({"error": "サーバー内部エラー", "details": str(e)}), 500
|
| 456 |
+
|
| 457 |
+
@app.route('/upload_base_audio', methods=['POST'])
|
| 458 |
+
def upload_base_audio():
|
| 459 |
+
global all_users
|
| 460 |
+
|
| 461 |
+
try:
|
| 462 |
+
data = request.get_json()
|
| 463 |
+
if not data or 'audio_data' not in data or 'name' not in data:
|
| 464 |
+
return jsonify({"error": "音声データまたは名前がありません"}), 400
|
| 465 |
+
name = data['name']
|
| 466 |
+
print(f"登録名: {name}")
|
| 467 |
+
|
| 468 |
+
# GASのアップロードエンドポイントにリクエスト
|
| 469 |
+
payload = {
|
| 470 |
+
"action": "upload",
|
| 471 |
+
"fileName": f"{name}.wav",
|
| 472 |
+
"base64Data": data['audio_data']
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
response = requests.post(GAS_URL, json=payload)
|
| 476 |
+
if response.status_code != 200:
|
| 477 |
+
return jsonify({"error": "GASアップロードエラー", "details": response.text}), 500
|
| 478 |
+
|
| 479 |
+
res_json = response.json()
|
| 480 |
+
if res_json.get("status") != "success":
|
| 481 |
+
return jsonify({"error": "GASアップロード失敗", "details": res_json.get("message")}), 500
|
| 482 |
+
|
| 483 |
+
# 全ユーザーリストを更新
|
| 484 |
+
update_all_users()
|
| 485 |
+
|
| 486 |
+
return jsonify({"state": "Registration Success!", "driveFileId": res_json.get("fileId")}), 200
|
| 487 |
+
except Exception as e:
|
| 488 |
+
print("Error in /upload_base_audio:", str(e))
|
| 489 |
+
return jsonify({"error": "サーバーエラー", "details": str(e)}), 500
|
| 490 |
+
|
| 491 |
+
@app.route('/list_base_audio', methods=['GET'])
|
| 492 |
+
def list_base_audio():
|
| 493 |
+
try:
|
| 494 |
+
global all_users
|
| 495 |
+
all_users = update_all_users()
|
| 496 |
+
return jsonify({"status": "success", "fileNames": all_users}), 200
|
| 497 |
+
except Exception as e:
|
| 498 |
+
print("Error in /list_base_audio:", str(e))
|
| 499 |
+
return jsonify({"error": "サーバーエラー", "details": str(e)}), 500
|
| 500 |
+
|
| 501 |
+
if __name__ == '__main__':
|
| 502 |
+
port = int(os.environ.get("PORT", 7860))
|
| 503 |
app.run(debug=True, host="0.0.0.0", port=port)
|