WhisperDemo / app.py
hwberry2's picture
Update app.py
03ee9fd
raw
history blame
1.83 kB
import os
import gradio as gr
import openai
from gtts import gTTS
openai.api_key = os.environ["OPEN_AI_KEY"]
def transcribe(audio):
audio_file = open(audio, "rb")
# Call the transcribe method with the file-like object
transcript = openai.Audio.transcribe("whisper-1", audio_file)
return transcript["text"]
def botResponse(user_input):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
#messages=user_input)
messages=[
{"role": "system", "content": "You are a therapist. Respond in less than 5 sentences."},
{"role": "user", "content": user_input}
]
)
system_message = response["choices"][0]["message"]["content"]
return system_message
def giveVoice(bot_message):
myobj = gTTS(text=bot_message)
myobj.save("temp.mp3")
dir = os.getcwd()
new_path = os.path.join(dir, "temp.mp3")
return new_path
def keepLog(gpt):
return "gpt"
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
user_audio = gr.Audio(source="microphone", type="filepath", label="Input Phrase")
submit_btn = gr.Button(value="Transcribe")
with gr.Column():
user_transcript = gr.Text(label="User Transcript")
gpt_transcript = gr.Text(label="GPT Transcript")
gpt_voice = gr.Audio(label="Voice Response")
chat_log = gr.Text(label="Chat Log")
submit_btn.click(transcribe, inputs=user_audio, outputs=user_transcript)
user_transcript.change(botResponse, inputs=user_transcript, outputs=gpt_transcript)
gpt_transcript.change(giveVoice, inputs=gpt_transcript, outputs=gpt_voice)
user_audio.change(keepLog, inputs=user_transcript, outputs=chat_log)
demo.launch(share=False)