WhisperDemo / app.py
hwberry2's picture
Update app.py (#1)
96f6f8c
raw
history blame
2.27 kB
import os
import gradio as gr
import openai
from gtts import gTTS
openai.api_key = os.environ["OPEN_AI_KEY"]
#messages = gr.State([
# {"role": "system", "content": "You are a therapist. Respond in less than 5 sentences."}
#])
messages = [{"role": "system", "content": "You are a therapist. Respond in less than 5 sentences."}]
def transcribe(audio, test):
stringIn = test
audio_file = open(audio, "rb")
# Call the transcribe method with the file-like object
transcript = openai.Audio.transcribe("whisper-1", audio_file)
#msg_contents.append({"role": "user", "content": transcript["text"]})
#chat_transcript = ""
#for message in msg_contents:
# if (message["role"] != "system"):
# chat_transcript += message["role"] + ": " + message["content"] + "\n\n"
return transcript
def botResponse(chat_log, msg_contents):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=msg_contents)
system_message = response["choices"][0]["message"]["content"]
msg_contents.append({"role": "assistant", "content": system_message})
chat_transcript = chat_log
for message in msg_contents:
if (message["role"] != "system"):
chat_transcript += message["role"] + ": " + message["content"] + "\n\n"
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
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")
submit2_btn = gr.Button(value="Bot Response")
submit3_btn = gr.Button(value="Give Voice")
with gr.Column():
#gpt_response = gr.Audio(label="Voice Response")
gpt_transcript = gr.Text(label="Generate Transcript")
gpt_transcript2 = gr.Text(label="Bot Response")
gpt_response = gr.Audio(label="Voice Response")
submit_btn.click(transcribe, inputs=[user_audio, "test"] outputs=gpt_transcript)
demo.launch(share=False)