-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathecho.py
More file actions
63 lines (49 loc) · 1.8 KB
/
echo.py
File metadata and controls
63 lines (49 loc) · 1.8 KB
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
from __future__ import annotations
import logging
import os
from logging import getLogger
from stackchan_server.app import StackChanApp
from stackchan_server.speech_recognition import WhisperCppSpeechToText, WhisperServerSpeechToText
from stackchan_server.speech_synthesis import VoiceVoxSpeechSynthesizer
from stackchan_server.ws_proxy import EmptyTranscriptError, WsProxy
logger = getLogger(__name__)
logging.basicConfig(
level=os.getenv("STACKCHAN_LOG_LEVEL", "INFO"),
format="%(asctime)s.%(msecs)03d %(levelname)s:%(name)s:%(message)s",
datefmt="%H:%M:%S",
)
def _create_app() -> StackChanApp:
whisper_server_url = os.getenv("STACKCHAN_WHISPER_SERVER_URL")
whisper_server_port = os.getenv("STACKCHAN_WHISPER_SERVER_PORT")
whisper_model = os.getenv("STACKCHAN_WHISPER_MODEL")
if whisper_server_url or whisper_server_port:
return StackChanApp(
speech_recognizer=WhisperServerSpeechToText(server_url=whisper_server_url),
speech_synthesizer=VoiceVoxSpeechSynthesizer(),
)
if whisper_model:
return StackChanApp(
speech_recognizer=WhisperCppSpeechToText(
model_path=whisper_model,
),
speech_synthesizer=VoiceVoxSpeechSynthesizer(),
)
return StackChanApp()
app = _create_app()
@app.setup
async def setup(proxy: WsProxy):
logger.info("WebSocket connected")
@app.talk_session
async def talk_session(proxy: WsProxy):
while True:
try:
text = await proxy.listen()
except EmptyTranscriptError:
return
if not text:
return
logger.info("Heard: %s", text)
await proxy.speak(text)
if __name__ == "__main__":
import uvicorn
uvicorn.run("example_apps.echo:app.fastapi", host="0.0.0.0", port=8000, reload=True)