WhisperDemo / app.py
hwberry2's picture
Update app.py
42cc7c9
raw
history blame
No virus
2.3 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):
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"
system_response = botResponse(transcript)
return system_response
def botResponse(user_input):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=user_input)
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, outputs=gpt_transcript)
demo.launch(share=False)