RamAnanth1 commited on
Commit
87fa1b1
·
0 Parent(s):

Duplicate from RamAnanth1/chatGPT_voice

Browse files
Files changed (5) hide show
  1. .gitattributes +34 -0
  2. README.md +13 -0
  3. app.py +191 -0
  4. packages.txt +3 -0
  5. requirements.txt +5 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: ChatGPT Voice
3
+ emoji: 📊
4
+ colorFrom: blue
5
+ colorTo: blue
6
+ sdk: gradio
7
+ sdk_version: 3.12.0
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: RamAnanth1/chatGPT_voice
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pyChatGPT import ChatGPT
2
+ import gradio as gr
3
+ import os, json
4
+ from loguru import logger
5
+ import random
6
+ from transformers import pipeline
7
+ import torch
8
+
9
+ session_token = os.environ.get('SessionToken')
10
+ api = ChatGPT(session_token)
11
+
12
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
13
+
14
+ whisper_model = pipeline(
15
+ task="automatic-speech-recognition",
16
+ model="openai/whisper-large-v2",
17
+ chunk_length_s=30,
18
+ device=device,
19
+ )
20
+
21
+ all_special_ids = whisper_model.tokenizer.all_special_ids
22
+ transcribe_token_id = all_special_ids[-5]
23
+ translate_token_id = all_special_ids[-6]
24
+
25
+ def 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 text
29
+
30
+ def 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 response
38
+
39
+ def 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_history
48
+
49
+ start_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
+
93
+ window['div_count'] = 0;
94
+ window['chat_bot'] = window['gradioEl'].querySelectorAll('#chat_bot')[0];
95
+ window['chat_bot1'] = window['gradioEl'].querySelectorAll('#chat_bot1')[0];
96
+ chat_row = window['gradioEl'].querySelectorAll('#chat_row')[0];
97
+ prompt_row = window['gradioEl'].querySelectorAll('#prompt_row')[0];
98
+ window['chat_bot1'].children[1].textContent = '';
99
+
100
+ clientHeight = getClientHeight();
101
+ new_height = (clientHeight-300) + 'px';
102
+ chat_row.style.height = new_height;
103
+ window['chat_bot'].style.height = new_height;
104
+ window['chat_bot'].children[2].style.height = new_height;
105
+ window['chat_bot1'].style.height = new_height;
106
+ window['chat_bot1'].children[2].style.height = new_height;
107
+ prompt_row.children[0].style.flex = 'auto';
108
+ prompt_row.children[0].style.width = '100%';
109
+
110
+ window['checkChange'] = function checkChange() {
111
+ try {
112
+ if (window['chat_bot'].children[2].children[0].children.length > window['div_count']) {
113
+ new_len = window['chat_bot'].children[2].children[0].children.length - window['div_count'];
114
+ for (var i = 0; i < new_len; i++) {
115
+ new_div = window['chat_bot'].children[2].children[0].children[window['div_count'] + i].cloneNode(true);
116
+ window['chat_bot1'].children[2].children[0].appendChild(new_div);
117
+ }
118
+ window['div_count'] = chat_bot.children[2].children[0].children.length;
119
+ }
120
+ if (window['chat_bot'].children[0].children.length > 1) {
121
+ window['chat_bot1'].children[1].textContent = window['chat_bot'].children[0].children[1].textContent;
122
+ } else {
123
+ window['chat_bot1'].children[1].textContent = '';
124
+ }
125
+
126
+ } catch(e) {
127
+ }
128
+ }
129
+ window['checkChange_interval'] = window.setInterval("window.checkChange()", 500);
130
+ }
131
+
132
+ return false;
133
+ }"""
134
+
135
+
136
+ with gr.Blocks(title='Talk to chatGPT') as demo:
137
+ gr.Markdown("## Talk to chatGPT with your voice in your native language ! ##")
138
+ 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>")
139
+ 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>")
140
+ with gr.Group(elem_id="page_1", visible=True) as page_1:
141
+ with gr.Box():
142
+ with gr.Row():
143
+ start_button = gr.Button("Let's talk to chatGPT!", elem_id="start-btn", visible=True)
144
+ start_button.click(fn=None, inputs=[], outputs=[], _js=start_work)
145
+
146
+ with gr.Group(elem_id="page_2", visible=False) as page_2:
147
+ with gr.Row(elem_id="chat_row"):
148
+ chatbot = gr.Chatbot(elem_id="chat_bot", visible=False).style(color_map=("green", "blue"))
149
+ chatbot1 = gr.Chatbot(elem_id="chat_bot1").style(color_map=("green", "blue"))
150
+ with gr.Row():
151
+ prompt_input_audio = gr.Audio(
152
+ source="microphone",
153
+ type="filepath",
154
+ label="Record Audio Input",
155
+
156
+ )
157
+ translate_btn = gr.Button("Check Whisper first ? 👍")
158
+
159
+ whisper_task = gr.Radio(["Translate to English", "Transcribe in Spoken Language"], value="Translate to English", show_label=False)
160
+ with gr.Row(elem_id="prompt_row"):
161
+ prompt_input = gr.Textbox(lines=2, label="Input text",show_label=True)
162
+ chat_history = gr.Textbox(lines=4, label="prompt", visible=False)
163
+ submit_btn = gr.Button(value = "Send to chatGPT",elem_id="submit-btn").style(
164
+ margin=True,
165
+ rounded=(True, True, True, True),
166
+ width=100
167
+ )
168
+
169
+
170
+
171
+ translate_btn.click(fn=translate_or_transcribe,
172
+ inputs=[prompt_input_audio,whisper_task],
173
+ outputs=prompt_input
174
+ )
175
+
176
+
177
+ submit_btn.click(fn=chat,
178
+ inputs=[prompt_input, chat_history],
179
+ outputs=[chatbot, chat_history],
180
+ )
181
+ gr.HTML('''
182
+ <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>
183
+ <div class="footer">
184
+ <p>Whisper Model by <a href="https://github.com/openai/whisper" style="text-decoration: underline;" target="_blank">OpenAI</a> -
185
+ <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>
186
+ </p>
187
+ </div>
188
+ ''')
189
+ gr.Markdown("![visitor badge](https://visitor-badge.glitch.me/badge?page_id=RamAnanth1.chatGPT_voice)")
190
+
191
+ demo.launch(debug = True)
packages.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ libsndfile1
2
+ chromium-browser
3
+ xvfb
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ pyChatGPT==0.4
2
+ loguru
3
+ --extra-index-url https://download.pytorch.org/whl/cu113
4
+ torch
5
+ transformers