import gradio as gr #from pyChatGPT import ChatGPT import os import requests api = os.environ.get('API_ENDPOINT') #session_token = os.environ.get('SessionToken') #cf_clearance_token = os.environ.get('ClearanceToken') #cf_bm_token = os.environ.get('cf_bm_token') whisper = gr.Interface.load(name="spaces/sanchit-gandhi/whisper-large-v2") def call_api(message): response = requests.get(f'{api}?q={message}') if response.status_code == 200: return str(response.text).split('\n', 2)[2] else: return """Sorry, I'm quite busy right now, but please try again later :)""" def chat_hf(audio, task): try: whisper_text = translate(audio, task) if whisper_text == "ERROR: You have to either use the microphone or upload an audio file": gpt_response = "MISSING AUDIO: Record your voice by clicking the microphone button, do not forget to stop recording before sending your message ;)" else: gpt_response = call_api(whisper_text) #api = ChatGPT(session_token, cf_clearance_token, cf_bm_token) #api = ChatGPT(session_token) #api.refresh_auth() # refresh the authorization token #if reset_conversation: # # api.reset_conversation() # reset the conversation #resp = api.send_message(whisper_text) #gpt_response = resp['message'] except: gpt_response = """Sorry, I'm quite busy right now, but please try again later :)""" print(f""" {whisper_text} ———— {gpt_response} """) return whisper_text, gpt_response def translate(audio, task): if task == "transcribe": text_result = whisper(audio, None, "transcribe", fn_index=0) else: text_result = whisper(audio, None, "translate", fn_index=0) return text_result title = """
Chat with GPT with your voice in your native language !
Note: this demo is not able to sustain a conversation from earlier responses.
For more detailed results and dialogue, you should use the official ChatGPT interface.
—
Also, be aware that audio records from iOS devices will not be decoded as expected by Gradio. For the best experience, record your voice from a computer instead of your smartphone ;)