IT/AI

Whisper와 GPT로 나만의 개인비서 만들기

뱁새유니버스 2023. 1. 8. 17:17

5일 만에 100만명 가입을 이끌어낸 openAI의 language model GPT! 유튜브와 해외 개발 사이트, discord 등을 들어가면 정말 많은 사람들이 열심히 gpt를 활용해 개발하는 것을 볼 수 있습니다. 

그래서 저도 whisper(음성인식 AI, 약 75개 언어 학습)와 GPT(언어AI모델)을 활용해 AI 비서를 간단하게 만들어보았습니다. 

import whisper
import gradio as gr
import warnings
import openai

warnings.filterwarnings("ignore")

# Use your API key to authenticate
openai.api_key = "개인 api 키값"

model = whisper.load_model("base")

model.device

def transcribe(audio):
    # load audio and pad/trim it to fit 30 seconds
    audio=whisper.load_audio(audio)
    audio=whisper.pad_or_trim(audio)

    # make log-Mel spectrogram and move to the same device as the model
    mel = whisper.log_mel_spectrogram(audio).to(model.device)

    # detect the spoken language
    _, probs = model.detect_language(mel)

    # decode the audio
    #options = whisper.DecodingOptions()
    options = whisper.DecodingOptions(fp16=False)
    result = whisper.decode(model, mel, options)
    result_text = result.text

    # Pass the generated text to Audio
    # Use the openai API to generate a response
    response = openai.Completion.create(
        engine="ada",
        prompt=result_text,
        max_tokens=1024,
        n=1,
        temperature=0.5
    ).choices[0].text

    out_result = response
    print(out_result)

    return [result_text, out_result]

output_1 = gr.Textbox(label="Speech to Text")
output_2 = gr.Textbox(label="ChatGPT Output")

gr.Interface(
    title = 'OpenAI Whisper and ChatGPT ASR Gradio Web UI',
    fn=transcribe,
    inputs=[
        gr.inputs.Audio(source="microphone", type="filepath")
    ],

    outputs=[output_1, output_2],
    live=True).launch()

참쉽죠?

gradio는 웹앱 형태로 제공하는 파이썬 라이브러리입니다.

whisper와 gpt는 openai에서 제공하는 AI모델이구요. 

whisper는 각자의 컴퓨터 자원에 따라 여러 모델이 있습니다. 저는 가장 기본적인 base모델을 사용했습니다.

이코드만 복사해서 openai의 openapi 키값을 넣어주고 각 라이브러리를 import하면 웹앱 형태로 확인이 가능합니다.

일론머스크가 누구냐고 물어봤습니다. 잘대답해주네요.

한글로도 물어봤습니다. 아이폰이 좋아? 갤럭시가 좋아?

대충 아이폰이 좋다는 것 같은데, 추상적으로 얘기해주네요? 뭔가 더 학습을 강화하거나 좋게 만들 수 없을까? 생각이 듭니다.

개인 맞춤형 AI를 만들어보고 싶은 욕심이 생깁니다. 이것저것 연구해보면서 mongoDB연결해서 data를 축적할 수 있는지도 시도해봐야겠습니다!

반응형