from pyChatGPT import ChatGPT import os session_token = os.environ.get('SessionToken') import whisper whisper_model = whisper.load_model("small") def chat_hf(audio, custom_token): try: whisper_text = translate(audio) api = ChatGPT(session_token) resp = api.send_message(whisper_text) api.refresh_auth() # refresh the authorization token api.reset_conversation() # reset the conversation gpt_response = resp['message'] except: whisper_text = translate(audio) api = ChatGPT(custom_token) resp = api.send_message(whisper_text) api.refresh_auth() # refresh the authorization token api.reset_conversation() # reset the conversation gpt_response = resp['message'] 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) #translate_options = whisper.DecodingOptions(task="translate", fp16 = False) transcription = whisper.decode(whisper_model, mel, transcript_options) #translation = whisper.decode(whisper_model, mel, translate_options) print("language spoken: " + transcription.language) print("transcript: " + transcription.text) print("———————————————————————————————————————————") #print("translated: " + translation.text) return transcription.text title = """

Whisper to chatGPT

Chat with GPT with your voice in your native language !
If it fails enter custom session key see video for reference refer Bhavesh Baht video

You can skip the queue by duplicating this space: Duplicate Space

""" article = """ """ css = ''' #col-container {max-width: 700px; margin-left: auto; margin-right: auto;} a {text-decoration-line: underline; font-weight: 600;} .footer { margin-bottom: 45px; margin-top: 35px; 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; } ''' import gradio as gr 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) send_btn = gr.Button("Send my message !") custom_token = gr.Textbox(label='If it fails, use your own session token', placeholder="your own session token") with gr.Column(): audio_translation = gr.Textbox(type="text",label="Whisper translation") gpt_response = gr.Textbox(type="text",label="chatGPT response") gr.HTML(article) send_btn.click(chat_hf, inputs=[record_input, custom_token], outputs=[audio_translation, gpt_response]) demo.queue(max_size=32, concurrency_count=20).launch(debug=True)