-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket_server.py
71 lines (56 loc) · 2.34 KB
/
websocket_server.py
1
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import datetime
import os
from aiohttp import web
import settings
from speech_recognizer import SpeechRecognizer
from translator import Translator
import ssl
recognizer = SpeechRecognizer(settings.TRANSLATION_TARGET_LANGUAGE, settings.DEVICE)
translator = Translator(settings.DEEPL_API_KEY)
async def websocket_handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
print("websocket connection open")
await ws.send_str("connected")
async for msg in ws:
if msg.type == web.WSMsgType.BINARY:
print("Received audio data")
received_time = datetime.datetime.now()
filename = received_time.strftime("%Y%m%d%H%M%S") + ".wav"
filepath = os.path.join("audio", filename)
with open(filepath, "wb") as f:
f.write(msg.data)
print("Processing audio data")
# await ws.send_str("Processing audio data" + filename)
result, lang = recognizer.recognize(filepath)
if result == "":
continue
# deeplで翻訳
translated = ""
if lang == "ja":
translated = translator.translate(result, settings.TRANSLATION_SOURCE_LANGUAGE, "")
else:
translated = translator.translate(result, settings.TRANSLATION_TARGET_LANGUAGE, "")
ret = "(" + lang + ")" + result + "(" + translated + ")"
ret = f"{received_time.strftime('%Y/%m/%d %H:%M:%S')} {ret}"
print(ret)
await ws.send_str(ret)
# ファイルを削除(例外発生時はログ)
try:
os.remove(filepath)
except:
print("Failed to delete file: " + filepath)
return ws
# audioフォルダの中身を削除、フォルダがなければ作成
audio_dir = os.path.join("audio")
for file in os.listdir(audio_dir):
os.remove(os.path.join(audio_dir, file))
os.makedirs(audio_dir, exist_ok=True)
app = web.Application()
app.router.add_get('/translate', websocket_handler)
if settings.USE_SSL:
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_context.load_cert_chain(settings.SSL_CERT, settings.SSL_KEY)
web.run_app(app, host='0.0.0.0', port=settings.PORT_NO, ssl_context=ssl_context)
else:
web.run_app(app, host='0.0.0.0', port=settings.PORT_NO)