import os import openai import whisper import gradio as gr openai.api_key = os.environ.get('SessionToken') whisper_model = whisper.load_model("small") conversation = "" user_name = "MH" bot_name = "bbDemo" def chat_hf(audio): conversation = "" try: whisper_text = translate(audio) user_input = whisper_text # Conversation route prompt = user_name + ": " + user_input + "\n" + bot_name+ ": " conversation += prompt # allows for context # fetch the response from open AI api response = openai.Completion.create(engine='text-davinci-003', prompt=conversation, max_tokens=50) response_str = response["choices"][0]["text"].replace("\n", "") response_str = response_str.split(user_name + ": ", 1)[0].split(bot_name + ": ", 1)[0] conversation += response_str + "\n" gpt_response = response_str except: # Conversation route whisper_text = translate(audio) user_input = whisper_text prompt = user_name + ": " + user_input + "\n" + bot_name+ ": " conversation += prompt # allows for context # fetch the response from open AI api response = openai.Completion.create(engine='text-davinci-003', prompt=conversation, max_tokens=1024) response_str = response["choices"][0]["text"].replace("\n", "") response_str = response_str.split(user_name + ": ", 1)[0].split(bot_name + ": ", 1)[0] conversation += response_str + "\n" gpt_response = response_str print("Error") return whisper_text, gpt_response def translate(audio): print(""" — Sending audio to Whisper ... — """) audio = whisper.load_audio(audio) audio = whisper.pad_or_trim(audio) mel = whisper.log_mel_spectrogram(audio).to(whisper_model.device) _, probs = whisper_model.detect_language(mel) transcript_options = whisper.DecodingOptions(task="transcribe", fp16 = False) transcription = whisper.decode(whisper_model, mel, transcript_options) print("language spoken: " + transcription.language) print("transcript: " + transcription.text) print("———————————————————————————————————————————") return transcription.text title = """
Chat with GPT with your voice in your native language!