import torch
import os
import gradio as gr
from transformers import pipeline
from pyChatGPT import ChatGPT
from speechbrain.pretrained import Tacotron2
from speechbrain.pretrained import HIFIGAN
import json
import soundfile as sf
device = "cuda:0" if torch.cuda.is_available() else "cpu"
print(f"Is CUDA available: {torch.cuda.is_available()}")
print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
# Intialise STT (Whisper)
pipe = pipeline(
task="automatic-speech-recognition",
model="openai/whisper-base.en",
chunk_length_s=30,
device=device,
)
# Initialise ChatGPT session
session_token = os.environ.get("SessionToken")
api = ChatGPT(session_token=session_token)
# Intialise TTS (tacotron2) and Vocoder (HiFIGAN)
tacotron2 = Tacotron2.from_hparams(
source="speechbrain/tts-tacotron2-ljspeech",
savedir="tmpdir_tts",
overrides={"max_decoder_steps": 10000},
run_opts={"device": device},
)
hifi_gan = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-ljspeech", savedir="tmpdir_vocoder")
def get_response_from_chatbot(text, reset_conversation):
try:
if reset_conversation:
api.refresh_auth()
api.reset_conversation()
resp = api.send_message(text)
response = resp["message"]
except:
response = "Sorry, the chatGPT queue is full. Please try again later."
return response
def chat(input_audio, chat_history, reset_conversation):
# speech -> text (Whisper)
message = pipe(input_audio)["text"]
# text -> response (chatGPT)
response = get_response_from_chatbot(message, reset_conversation)
# response -> speech (tacotron2)
mel_output, mel_length, alignment = tacotron2.encode_text(response)
wav = hifi_gan.decode_batch(mel_output)
sf.write("out.wav", wav.squeeze().cpu().numpy(), 22050)
out_chat = []
chat_history = chat_history if not reset_conversation else ""
if chat_history != "":
out_chat = json.loads(chat_history)
out_chat.append((message, response))
chat_history = json.dumps(out_chat)
return out_chat, chat_history, "out.wav"
start_work = """async() => {
function isMobile() {
try {
document.createEvent("TouchEvent"); return true;
} catch(e) {
return false;
}
}
function getClientHeight()
{
var clientHeight=0;
if(document.body.clientHeight&&document.documentElement.clientHeight) {
var clientHeight = (document.body.clientHeight Demo uses Whisper to convert the input speech"
" to transcribed text, chatGPT to generate responses, and tacotron2 to convert the response to"
" output speech:
You can duplicate this space and use your own session token:
" ) gr.HTML( "Instructions on how to obtain your session token can be found in the video here." " Add your session token by going to Settings -> New secret and add the token under the name SessionToken.
" ) with gr.Group(elem_id="page_1", visible=True) as page_1: with gr.Box(): with gr.Row(): start_button = gr.Button("Let's talk to chatGPT! 🗣", elem_id="start-btn", visible=True) start_button.click(fn=None, inputs=[], outputs=[], _js=start_work) with gr.Group(elem_id="page_2", visible=False) as page_2: with gr.Row(elem_id="chat_row"): chatbot = gr.Chatbot(elem_id="chat_bot", visible=False).style(color_map=("green", "blue")) chatbot1 = gr.Chatbot(elem_id="chat_bot1").style(color_map=("green", "blue")) with gr.Row(): prompt_input_audio = gr.Audio( source="microphone", type="filepath", label="Record Audio Input", ) prompt_output_audio = gr.Audio() reset_conversation = gr.Checkbox(label="Reset conversation?", value=False) with gr.Row(elem_id="prompt_row"): chat_history = gr.Textbox(lines=4, label="prompt", visible=False) submit_btn = gr.Button(value="Send to chatGPT", elem_id="submit-btn").style( margin=True, rounded=(True, True, True, True), width=100, ) submit_btn.click( fn=chat, inputs=[prompt_input_audio, chat_history, reset_conversation], outputs=[chatbot, chat_history, prompt_output_audio], ) demo.launch(debug=True)