Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions example_apps/echo.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from __future__ import annotations

from logging import StreamHandler, getLogger
import logging
import os
from logging import getLogger

from stackchan_server.app import StackChanApp
from stackchan_server.ws_proxy import EmptyTranscriptError, WsProxy


logger = getLogger(__name__)
logger.addHandler(StreamHandler())
logger.setLevel("DEBUG")
logging.basicConfig(
level=os.getenv("STACKCHAN_LOG_LEVEL", "INFO"),
format="%(asctime)s.%(msecs)03d %(levelname)s:%(name)s:%(message)s",
datefmt="%H:%M:%S",
)


app = StackChanApp()
Expand Down
1 change: 0 additions & 1 deletion misc/api_test/test_cloud_sst.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from google.cloud import speech

sst_client = speech.SpeechClient()
Expand Down
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ readme = "README.md"
requires-python = ">=3.13"
dependencies = [
"fastapi>=0.128.0",
"google-genai>=1.59.0",
"google-cloud-speech>=2.35.0",
"uvicorn[standard]>=0.40.0",
"voicevox-client>=1.1.0",
Expand All @@ -20,9 +21,7 @@ dev = [
"ruff>=0.15.2",
"ty>=0.0.17",
]
example-gemini = [
"google-genai>=1.59.0",
]
example-gemini = []
example-claude-agent-sdk = [
"claude-agent-sdk>=0.1.39",
]
Expand Down
16 changes: 13 additions & 3 deletions stackchan_server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
from fastapi import FastAPI, WebSocket, WebSocketDisconnect

from .speech_recognition import create_speech_recognizer
from .types import SpeechRecognizer
from .speech_synthesis import create_speech_synthesizer
from .types import SpeechRecognizer, SpeechSynthesizer
from .ws_proxy import WsProxy

logger = getLogger(__name__)


class StackChanApp:
def __init__(self, speech_recognizer: SpeechRecognizer | None = None) -> None:
def __init__(
self,
speech_recognizer: SpeechRecognizer | None = None,
speech_synthesizer: SpeechSynthesizer | None = None,
) -> None:
self.speech_recognizer = speech_recognizer or create_speech_recognizer()
self.speech_synthesizer = speech_synthesizer or create_speech_synthesizer()
self.fastapi = FastAPI(title="StackChan WebSocket Server")
self._setup_fn: Optional[Callable[[WsProxy], Awaitable[None]]] = None
self._talk_session_fn: Optional[Callable[[WsProxy], Awaitable[None]]] = None
Expand All @@ -38,7 +44,11 @@ def talk_session(self, fn: Callable[["WsProxy"], Awaitable[None]]):

async def _handle_ws(self, websocket: WebSocket) -> None:
await websocket.accept()
proxy = WsProxy(websocket, speech_recognizer=self.speech_recognizer)
proxy = WsProxy(
websocket,
speech_recognizer=self.speech_recognizer,
speech_synthesizer=self.speech_synthesizer,
)
await proxy.start()
try:
if self._setup_fn:
Expand Down
Loading