// education

음성 인식(STT)과 음성 합성(TTS) 기반 Voice AI 구현

사용자의 목소리를 알아듣고 자연스러운 음성 톤으로 답변을 들려주는 Voice AI 애플리케이션 시스템을 개발합니다.


1. Voice AI 전문 용어 사전 (Glossary)


2. OpenAI Whisper STT & TTS 파이썬 통합 구현

from openai import OpenAI

client = OpenAI()

# 1. 사용자의 녹음 음성 파일(STT) 인코딩
def transcribe_audio(audio_filepath):
    with open(audio_filepath, "rb") as f:
        transcript = client.audio.transcriptions.create(
            model="whisper-1",
            file=f,
            language="ko" # 한국어 강제
        )
    return transcript.text

# 2. AI 답변 텍스트를 사람 목소리 오디오로 생성 (TTS)
def generate_speech(text_content, output_mp3_path):
    response = client.audio.speech.create(
        model="tts-1",
        voice="nova", # 목소리 톤: alloy, echo, fable, onyx, nova, shimmer
        input=text_content
    )
    response.stream_to_file(output_mp3_path)
    return output_mp3_path

# Voice AI 파이프라인 연동 테스트
user_voice_text = transcribe_audio("user_question.mp3")
print("음성 인식 결과:", user_voice_text)

ai_reply = "안녕하세요! 말씀하신 질문에 대해 답변해 드리겠습니다."
generate_speech(ai_reply, "ai_response.mp3")
print("AI 목소리 답변 생성 완료: ai_response.mp3")

3. 자주 묻는 질문 (Q&A)

Q. 음성 대화 시 반응 시간(Latency)을 최소화하려면 어떻게 해야 하나요? A. 전체 텍스트가 다 완성될 때까지 기다리지 않고, LLM이 문장을 생성하는 즉시 스트리밍(Streaming) 단위로 청크를 쪼개어 TTS로 넘기는 Streaming Voice Pipeline을 구축해야 합니다.

← 이전AI 시대의 개발 패러다임: 바이브 코딩(Vibe Coding) 입문 다음 →PDF 문서를 읽고 답하는 RAG 문서 질의응답 시스템