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 = """

Whisper-to-chatGPT

Chat with GPT with your voice in your native language !

""" article = """

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 ;)

""" css = ''' #col-container, #col-container-2 {max-width: 510px; margin-left: auto; margin-right: auto;} a {text-decoration-line: underline; font-weight: 600;} div#record_btn > .mt-6 { margin-top: 0!important; } div#record_btn > .mt-6 button { width: 100%; height: 40px; } .footer { margin-bottom: 45px; margin-top: 10px; text-align: center; border-bottom: 1px solid #e5e5e5; } .footer>p { font-size: .8rem; display: inline-block; padding: 0 10px; transform: translateY(10px); background: white; } .dark .footer { border-color: #303030; } .dark .footer>p { background: #0b0f19; } ''' with gr.Blocks(css=css) as demo: with gr.Column(elem_id="col-container"): gr.HTML(title) with gr.Row(): record_input = gr.Audio(source="microphone",type="filepath", show_label=False,elem_id="record_btn") task = gr.Radio(choices=["transcribe","translate"], value="transcribe", show_label=False) with gr.Row(): #reset_conversation = gr.Checkbox(label="Reset conversation?", value=False) send_btn = gr.Button("Send my request !") #custom_token = gr.Textbox(label='If it fails, use your own session token', placeholder="your own session token", max_lines=3) with gr.Column(elem_id="col-container-2"): audio_translation = gr.Textbox(type="text",label="Whisper transcript") gpt_response = gr.Textbox(type="text",label="chatGPT response") gr.HTML(article) send_btn.click(chat_hf, inputs=[record_input, task], outputs=[audio_translation, gpt_response]) demo.queue(max_size=32, concurrency_count=20).launch(debug=True)