该脚本从麦克风获取输入,并将语音转录为文本,然后将文本传递给gpt tex-davinci以生成响应。
但是脚本没有生成任何gpt响应。
import openai
import gradio as gr
import whisper
import time
model = whisper.load_model("base")
prompt_input = ''
def transcribe(audio):
global prompt_input
# 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)
print(f"Detected language: {max(probs, key=probs.get)}")
# decode the audio
options = whisper.DecodingOptions()
result = whisper.decode(model, mel, options)
prompt_input = result.text
gr.Interface(
title='OpenAI Whisper ASR Gradio Web UI',
fn=transcribe,
inputs=[
gr.inputs.Audio(source="microphone", type="filepath")
],
outputs=[
"textbox"
],
live=True).launch(share=True)
openai.api_key = 'API_KEY'
openai.api_base = 'https://api.openai.com'
def ask_gpt(prompt, model):
response = openai.Completion.create(
engine=model,
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.7
)
return response.choices[0].text.strip()
def main():
model = 'text-davinci-003'
while True:
prompt = prompt_input
if prompt.lower() == 'quit':
break
response = ask_gpt(prompt=f'User: {prompt}\nBot: ', model=model)
print(f'Bot: {response}')
if __name__ == '__main__':
main()
我使用openai whisper ASR web app进行语音到文本转换,使用gpt text-davinci生成响应。我在Google Colab中运行它。谢谢
1条答案
按热度按时间omjgkv6w1#
为了调试,我建议修改函数
ask_gpt
,使其返回原始响应。然后,您可以检查代码响应、消息等:API通常用解释发生了什么的“错误”(或其它)字段来响应。
(and结果的解析应该在另一个函数中完成:这将简化测试和调试)。