recorder_client.py 1.1 KB

123456789101112131415161718192021222324252627
  1. from RealtimeSTT import AudioToTextRecorderClient
  2. # ANSI escape codes for terminal control
  3. CLEAR_LINE = "\033[K" # Clear from cursor to end of line
  4. RESET_CURSOR = "\r" # Move cursor to the beginning of the line
  5. GREEN_TEXT = "\033[92m" # Set text color to green
  6. RESET_COLOR = "\033[0m" # Reset text color to default
  7. def print_realtime_text(text):
  8. print(f"{RESET_CURSOR}{CLEAR_LINE}{GREEN_TEXT}👄 {text}{RESET_COLOR}", end="", flush=True)
  9. # Initialize the audio recorder with the real-time transcription callback
  10. recorder = AudioToTextRecorderClient(on_realtime_transcription_update=print_realtime_text)
  11. # Print the speaking prompt
  12. print("👄 ", end="", flush=True)
  13. try:
  14. while True:
  15. # Fetch finalized transcription text, if available
  16. if text := recorder.text():
  17. # Display the finalized transcription
  18. print(f"{RESET_CURSOR}{CLEAR_LINE}✍️ {text}\n👄 ", end="", flush=True)
  19. except KeyboardInterrupt:
  20. # Handle graceful shutdown on Ctrl+C
  21. print(f"{RESET_CURSOR}{CLEAR_LINE}", end="", flush=True)
  22. recorder.shutdown()