컴퓨터/AI

[Python] GPT 3 음성 인터랙션 - (Speech to Text 모듈)

COMKONG 2023. 5. 4. 16:01
반응형

GPT 3 API를 호출하여 파이썬으로 질의 응답 할 수 있는 코드이다.

파이썬 코드를 실행하기 전에 준비해야 할 것들이 있다.

1. GPT 3 API 인증 키 확인하기

OpenAI 사이트에서 확인할 수 있다. (유출 안되게 조심할 것)

2. 아나콘다 환경 세팅

아나콘다가 설치 되어있는 사람이라면 새로운 environment 를 만들어 줄 것

conda create -n openchat
conda activate openchat

 그리고 필요한 것들을 pip 로 설치해주자

pip install openai
pip install pyaudio
pip install SpeechRecognition

 

3.  파이썬 코드 실행하기

import openai
import pyaudio
import speech_recognition as sr
from google.cloud import texttospeech


openai.api_key = <생성했던 키 넣기>

# speech to text
def speech_to_text():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Speak something!")
        audio = r.listen(source)
    try:
        text = r.recognize_google(audio)
        print(f"You said: {text}")
        return text
    except sr.UnknownValueError:
        print("Could not understand audio")
        return ""
    except sr.RequestError as e:
        print(f"Could not request results from Google Speech Recognition service; {e}")
        return ""
  
#chat gpt api 를 호출하여 질문하는 함수
def ask_question_v2(question):

    messages.append({"role": "user", "content": f"{question}"})
    completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
    assistant_content = completion.choices[0].message["content"].strip()
    messages.append({"role": "assistant", "content": f"{assistant_content}"})
    return assistant_content


while True:

    # 사용자 음성 인식
    question = speech_to_text()    
    print(question)
    check = input("if this sentence is right, then type 1")
    
    if check == "1":
        print ("ok, let's ask to gpt")
        # 질문하는 함수 호출
        answer = ask_question_v2(question)
        print(answer)
    else:
        print("Repeat the speech")

        
    #print(type(question))

    # 사용자가 "exit" 이나 "stop" 이라고 말하면 음성 대화 종료
    if "exit" in question:
        print("음성 대화를 종료합니다.")
        break
    if "stop" in question:
        print("음성 대화를 종료합니다.")
        break

 

가끔 speech to text 가 정확하지 않아서 해당 단계를 컨펌 해주는 코드를 추가로 넣었다.

STT 결과가 맞으면 1을 입력하면 된다.

(API 호출이 1분에 3번까지 가능하기 때문에 이상한 문장으로 질문 하는 건 아까우니까 추가하였음)

반응형