salmanmapkar/ChatGPT-Voice
0
1from pyChatGPT import ChatGPT2import gradio as gr3import os, json4from loguru import logger5import random6from transformers import pipeline7import torch8 9session_token = os.environ.get('SessionToken')10api = ChatGPT(session_token)11 12device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")13 14whisper_model = pipeline(15 task="automatic-speech-recognition",16 model="openai/whisper-large-v2",17 chunk_length_s=30,18 device=device,19)20 21all_special_ids = whisper_model.tokenizer.all_special_ids22transcribe_token_id = all_special_ids[-5]23translate_token_id = all_special_ids[-6]24 25def translate_or_transcribe(audio, task):26 whisper_model.model.config.forced_decoder_ids = [[2, transcribe_token_id if task=="Transcribe in Spoken Language" else translate_token_id]]27 text = whisper_model(audio)["text"]28 return text29 30def get_response_from_chatbot(text):31 try:32 resp = api.send_message(text) 33 response = resp['message']34 # logger.info(f"response_: {response}")35 except:36 response = "Sorry, the chatGPT queue is full. Please try again in some time"37 return response38 39def chat(message, chat_history): 40 out_chat = []41 if chat_history != '':42 out_chat = json.loads(chat_history)43 response = get_response_from_chatbot(message)44 out_chat.append((message, response))45 chat_history = json.dumps(out_chat)46 logger.info(f"out_chat_: {len(out_chat)}")47 return out_chat, chat_history48 49start_work = """async() => {50 function isMobile() {51 try {52 document.createEvent("TouchEvent"); return true;53 } catch(e) {54 return false; 55 }56 }57 function getClientHeight()58 {59 var clientHeight=0;60 if(document.body.clientHeight&&document.documentElement.clientHeight) {61 var clientHeight = (document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;62 } else {63 var clientHeight = (document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;64 }65 return clientHeight;66 }67 68 function setNativeValue(element, value) {69 const valueSetter = Object.getOwnPropertyDescriptor(element.__proto__, 'value').set;70 const prototype = Object.getPrototypeOf(element);71 const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;72 73 if (valueSetter && valueSetter !== prototypeValueSetter) {74 prototypeValueSetter.call(element, value);75 } else {76 valueSetter.call(element, value);77 }78 }79 var gradioEl = document.querySelector('body > gradio-app').shadowRoot;80 if (!gradioEl) {81 gradioEl = document.querySelector('body > gradio-app');82 }83 84 if (typeof window['gradioEl'] === 'undefined') {85 window['gradioEl'] = gradioEl;86 87 const page1 = window['gradioEl'].querySelectorAll('#page_1')[0];88 const page2 = window['gradioEl'].querySelectorAll('#page_2')[0]; 89 90 page1.style.display = "none";91 page2.style.display = "block"; 92 window['div_count'] = 0;93 window['chat_bot'] = window['gradioEl'].querySelectorAll('#chat_bot')[0];94 window['chat_bot1'] = window['gradioEl'].querySelectorAll('#chat_bot1')[0]; 95 chat_row = window['gradioEl'].querySelectorAll('#chat_row')[0]; 96 prompt_row = window['gradioEl'].querySelectorAll('#prompt_row')[0]; 97 window['chat_bot1'].children[1].textContent = '';98 99 clientHeight = getClientHeight();100 new_height = (clientHeight-300) + 'px';101 chat_row.style.height = new_height;102 window['chat_bot'].style.height = new_height;103 window['chat_bot'].children[2].style.height = new_height;104 window['chat_bot1'].style.height = new_height;105 window['chat_bot1'].children[2].style.height = new_height;106 prompt_row.children[0].style.flex = 'auto';107 prompt_row.children[0].style.width = '100%';108 109 window['checkChange'] = function checkChange() {110 try {111 if (window['chat_bot'].children[2].children[0].children.length > window['div_count']) {112 new_len = window['chat_bot'].children[2].children[0].children.length - window['div_count'];113 for (var i = 0; i < new_len; i++) { 114 new_div = window['chat_bot'].children[2].children[0].children[window['div_count'] + i].cloneNode(true);115 window['chat_bot1'].children[2].children[0].appendChild(new_div);116 }117 window['div_count'] = chat_bot.children[2].children[0].children.length;118 }119 if (window['chat_bot'].children[0].children.length > 1) {120 window['chat_bot1'].children[1].textContent = window['chat_bot'].children[0].children[1].textContent;121 } else {122 window['chat_bot1'].children[1].textContent = '';123 }124 125 } catch(e) {126 } 127 }128 window['checkChange_interval'] = window.setInterval("window.checkChange()", 500); 129 }130 131 return false;132}"""133 134 135with gr.Blocks(title='Talk to chatGPT') as demo:136 gr.Markdown("## Talk to chatGPT with your voice in your native language ! ##")137 gr.HTML("<p>You can duplicate this space and use your own session token: <a style='display:inline-block' href='https://huggingface.co/spaces/yizhangliu/chatGPT?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14' alt='Duplicate Space'></a></p>")138 gr.HTML("<p> Instruction on how to get session token can be seen in video <a style='display:inline-block' href='https://www.youtube.com/watch?v=TdNSj_qgdFk'><font style='color:blue;weight:bold;'>here</font></a>. Add your session token by going to settings and add under secrets. </p>")139 with gr.Group(elem_id="page_1", visible=True) as page_1:140 with gr.Box(): 141 with gr.Row():142 start_button = gr.Button("Let's talk to chatGPT!", elem_id="start-btn", visible=True) 143 start_button.click(fn=None, inputs=[], outputs=[], _js=start_work)144 145 with gr.Group(elem_id="page_2", visible=False) as page_2: 146 with gr.Row(elem_id="chat_row"):147 chatbot = gr.Chatbot(elem_id="chat_bot", visible=False).style(color_map=("green", "blue"))148 chatbot1 = gr.Chatbot(elem_id="chat_bot1").style(color_map=("green", "blue")) 149 with gr.Row():150 prompt_input_audio = gr.Audio(151 source="microphone",152 type="filepath",153 label="Record Audio Input",154 155 )156 translate_btn = gr.Button("Check Whisper first ? ๐")157 158 whisper_task = gr.Radio(["Translate to English", "Transcribe in Spoken Language"], value="Translate to English", show_label=False)159 with gr.Row(elem_id="prompt_row"):160 prompt_input = gr.Textbox(lines=2, label="Input text",show_label=True)161 chat_history = gr.Textbox(lines=4, label="prompt", visible=False)162 submit_btn = gr.Button(value = "Send to chatGPT",elem_id="submit-btn").style(163 margin=True,164 rounded=(True, True, True, True),165 width=100166 )167 168 169 170 translate_btn.click(fn=translate_or_transcribe, 171 inputs=[prompt_input_audio,whisper_task], 172 outputs=prompt_input173 )174 175 176 submit_btn.click(fn=chat, 177 inputs=[prompt_input, chat_history], 178 outputs=[chatbot, chat_history],179 )180 gr.HTML('''181 <p>Note: Please 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 ;)</p>182 <div class="footer">183 <p>Whisper Model by <a href="https://github.com/openai/whisper" style="text-decoration: underline;" target="_blank">OpenAI</a> - 184 <a href="https://chat.openai.com/chat" target="_blank">chatGPT</a> by <a href="https://openai.com/" style="text-decoration: underline;" target="_blank">OpenAI</a> 185 </p>186 </div>187 ''')188 gr.Markdown("")189 190demo.launch(debug = True, server_port=7860)