How krchris/stt_chatbot works
Project type and purpose
• This is a small full‑stack, real‑time web application (“stt_chatbot”) that records user audio in the browser, streams it to a Python FastAPI server, runs automatic speech recognition (via Whisper), forwards the transcribed text to ChatGPT (OpenAI API), and streams the text response back to the browser chat UI.
File structure
• Top‑level files:
• server.py – FastAPI app exposing a GET for the HTML and a WebSocket endpoint for audio streaming.
• chatgpt_client.py – encapsulates OpenAI API calls.
• whisper_stt.py – handles raw audio → .wav conversion and Whisper transcription.
• chatgpt_client.py / whisper_stt.py are imported by server.py to implement the pipeline.
• requirements.txt, .gitignore, LICENSE, README.md – support/config.
• frontend/ directory holds static assets: index.html, style.css, script.js.
README for insights
• server.py serves static HTML (GET) and WebSocket for audio.
• script.js uses MediaDevices to record audio and send it via WebSocket.
• whisper_stt.py produces transcription for ChatGPT.
• chatgpt_client.py sends transcription to ChatGPT and returns the response.
Main components & interactions
a. Frontend (Browser)
• Static UI (HTML/CSS)
• Client‑side JavaScript:
• Captures microphone audio chunks via MediaDevices API
• Opens WebSocket to server.py
• Displays streaming chat messages
b. Backend (FastAPI Server)
• HTTP GET / → serves index.html & static assets
• WebSocket /ws → receives audio frames
• whisper_stt module → converts chunks → .wav → Whisper → returns text
• chatgpt_client module → sends transcribed text to OpenAI → receives reply
• Sends response text back over WebSocket
c. External Service
• OpenAI ChatGPT API (HTTPS)
Architectural patterns & design principles
• Two‑tier architecture: Browser ↔ WebSocket/API ↔ External AI Service
• Separation of concerns:
• UI layer (frontend)
• Transport/API layer (FastAPI)
• Business logic layer (STT + ChatGPT)
• Asynchronous streaming pipeline using WebSockets
Technologies & frameworks
• Frontend: HTML5, CSS3, JavaScript, WebSocket API, MediaDevices
• Backend: Python 3, FastAPI, uvicorn (ASGI server)
• STT: OpenAI Whisper (local audio processing)
• NLU: OpenAI ChatGPT HTTP API