stt_vosk.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # START : uvicorn stt_vosk:app --host 0.0.0.0 --port 8000
  2. from fastapi import FastAPI, WebSocket
  3. from vosk import Model, KaldiRecognizer
  4. import json
  5. import asyncio
  6. app = FastAPI()
  7. # Vosk 한국어 모델 로드
  8. model = Model("./model/vosk-model-small-ko-0.22")
  9. SAMPLE_RATE = 16000
  10. @app.websocket("/audio-stream")
  11. async def websocket_endpoint(websocket: WebSocket):
  12. await websocket.accept()
  13. recognizer = KaldiRecognizer(model, SAMPLE_RATE)
  14. recognizer.SetWords(True) # 단어 정보 포함
  15. print("🎤 클라이언트 연결됨")
  16. is_speaking = False # 발화 상태 추적
  17. try:
  18. while True:
  19. audio_chunk = await websocket.receive_bytes()
  20. if recognizer.AcceptWaveform(audio_chunk):
  21. # 최종 결과 (발화 종료 시 전송)
  22. final_result = json.loads(recognizer.Result())
  23. if final_result.get("text"): # 텍스트가 존재할 경우에만 전송
  24. await websocket.send_text(json.dumps({
  25. "type": "finalTranscript",
  26. "text": final_result["text"]
  27. }))
  28. is_speaking = False # 발화 종료
  29. else:
  30. # 부분 결과 (발화 중일 때만 전송)
  31. partial_result = json.loads(recognizer.PartialResult())
  32. if partial_result.get("partial"):
  33. if not is_speaking: # 새로운 발화 시작 감지
  34. is_speaking = True
  35. await websocket.send_text(json.dumps({
  36. "type": "interimTranscript",
  37. "text": partial_result["partial"]
  38. }))
  39. except Exception as e:
  40. print(f"❌ 오류 발생: {e}")