Spaces:
Runtime error
Runtime error
Commit
·
f6ec31f
0
Parent(s):
Duplicate from RamAnanth1/chatGPT_voice
Browse filesCo-authored-by: Ram Ananth <[email protected]>
- .gitattributes +34 -0
- README.md +13 -0
- app.py +201 -0
- 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,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
11 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
12 |
+
|
13 |
+
whisper_model = pipeline(
|
14 |
+
task="automatic-speech-recognition",
|
15 |
+
model="openai/whisper-large-v2",
|
16 |
+
chunk_length_s=30,
|
17 |
+
device=device,
|
18 |
+
)
|
19 |
+
|
20 |
+
all_special_ids = whisper_model.tokenizer.all_special_ids
|
21 |
+
transcribe_token_id = all_special_ids[-5]
|
22 |
+
translate_token_id = all_special_ids[-6]
|
23 |
+
|
24 |
+
def get_api():
|
25 |
+
api = None
|
26 |
+
try:
|
27 |
+
api = ChatGPT(session_token)
|
28 |
+
# api.refresh_auth()
|
29 |
+
except:
|
30 |
+
api = None
|
31 |
+
return api
|
32 |
+
|
33 |
+
def translate_or_transcribe(audio, task):
|
34 |
+
whisper_model.model.config.forced_decoder_ids = [[2, transcribe_token_id if task=="Transcribe in Spoken Language" else translate_token_id]]
|
35 |
+
text = whisper_model(audio)["text"]
|
36 |
+
return text
|
37 |
+
|
38 |
+
def get_response_from_chatbot(api,text):
|
39 |
+
if api is None:
|
40 |
+
return "Sorry, the chatGPT API has some issues. Please try again later"
|
41 |
+
try:
|
42 |
+
resp = api.send_message(text)
|
43 |
+
api.refresh_auth()
|
44 |
+
# api.reset_conversation()
|
45 |
+
response = resp['message']
|
46 |
+
except:
|
47 |
+
response = "Sorry, the chatGPT queue is full. Please try again later"
|
48 |
+
return response
|
49 |
+
|
50 |
+
def chat(api,message, chat_history):
|
51 |
+
out_chat = []
|
52 |
+
if chat_history != '':
|
53 |
+
out_chat = json.loads(chat_history)
|
54 |
+
response = get_response_from_chatbot(api,message)
|
55 |
+
out_chat.append((message, response))
|
56 |
+
chat_history = json.dumps(out_chat)
|
57 |
+
logger.info(f"out_chat_: {len(out_chat)}")
|
58 |
+
return api,out_chat, chat_history
|
59 |
+
|
60 |
+
start_work = """async() => {
|
61 |
+
function isMobile() {
|
62 |
+
try {
|
63 |
+
document.createEvent("TouchEvent"); return true;
|
64 |
+
} catch(e) {
|
65 |
+
return false;
|
66 |
+
}
|
67 |
+
}
|
68 |
+
function getClientHeight()
|
69 |
+
{
|
70 |
+
var clientHeight=0;
|
71 |
+
if(document.body.clientHeight&&document.documentElement.clientHeight) {
|
72 |
+
var clientHeight = (document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
|
73 |
+
} else {
|
74 |
+
var clientHeight = (document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
|
75 |
+
}
|
76 |
+
return clientHeight;
|
77 |
+
}
|
78 |
+
|
79 |
+
function setNativeValue(element, value) {
|
80 |
+
const valueSetter = Object.getOwnPropertyDescriptor(element.__proto__, 'value').set;
|
81 |
+
const prototype = Object.getPrototypeOf(element);
|
82 |
+
const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
|
83 |
+
|
84 |
+
if (valueSetter && valueSetter !== prototypeValueSetter) {
|
85 |
+
prototypeValueSetter.call(element, value);
|
86 |
+
} else {
|
87 |
+
valueSetter.call(element, value);
|
88 |
+
}
|
89 |
+
}
|
90 |
+
var gradioEl = document.querySelector('body > gradio-app').shadowRoot;
|
91 |
+
if (!gradioEl) {
|
92 |
+
gradioEl = document.querySelector('body > gradio-app');
|
93 |
+
}
|
94 |
+
|
95 |
+
if (typeof window['gradioEl'] === 'undefined') {
|
96 |
+
window['gradioEl'] = gradioEl;
|
97 |
+
|
98 |
+
const page1 = window['gradioEl'].querySelectorAll('#page_1')[0];
|
99 |
+
const page2 = window['gradioEl'].querySelectorAll('#page_2')[0];
|
100 |
+
|
101 |
+
page1.style.display = "none";
|
102 |
+
page2.style.display = "block";
|
103 |
+
|
104 |
+
window['div_count'] = 0;
|
105 |
+
window['chat_bot'] = window['gradioEl'].querySelectorAll('#chat_bot')[0];
|
106 |
+
window['chat_bot1'] = window['gradioEl'].querySelectorAll('#chat_bot1')[0];
|
107 |
+
chat_row = window['gradioEl'].querySelectorAll('#chat_row')[0];
|
108 |
+
prompt_row = window['gradioEl'].querySelectorAll('#prompt_row')[0];
|
109 |
+
window['chat_bot1'].children[1].textContent = '';
|
110 |
+
|
111 |
+
clientHeight = getClientHeight();
|
112 |
+
new_height = (clientHeight-300) + 'px';
|
113 |
+
chat_row.style.height = new_height;
|
114 |
+
window['chat_bot'].style.height = new_height;
|
115 |
+
window['chat_bot'].children[2].style.height = new_height;
|
116 |
+
window['chat_bot1'].style.height = new_height;
|
117 |
+
window['chat_bot1'].children[2].style.height = new_height;
|
118 |
+
prompt_row.children[0].style.flex = 'auto';
|
119 |
+
prompt_row.children[0].style.width = '100%';
|
120 |
+
|
121 |
+
window['checkChange'] = function checkChange() {
|
122 |
+
try {
|
123 |
+
if (window['chat_bot'].children[2].children[0].children.length > window['div_count']) {
|
124 |
+
new_len = window['chat_bot'].children[2].children[0].children.length - window['div_count'];
|
125 |
+
for (var i = 0; i < new_len; i++) {
|
126 |
+
new_div = window['chat_bot'].children[2].children[0].children[window['div_count'] + i].cloneNode(true);
|
127 |
+
window['chat_bot1'].children[2].children[0].appendChild(new_div);
|
128 |
+
}
|
129 |
+
window['div_count'] = chat_bot.children[2].children[0].children.length;
|
130 |
+
}
|
131 |
+
if (window['chat_bot'].children[0].children.length > 1) {
|
132 |
+
window['chat_bot1'].children[1].textContent = window['chat_bot'].children[0].children[1].textContent;
|
133 |
+
} else {
|
134 |
+
window['chat_bot1'].children[1].textContent = '';
|
135 |
+
}
|
136 |
+
|
137 |
+
} catch(e) {
|
138 |
+
}
|
139 |
+
}
|
140 |
+
window['checkChange_interval'] = window.setInterval("window.checkChange()", 500);
|
141 |
+
}
|
142 |
+
|
143 |
+
return false;
|
144 |
+
}"""
|
145 |
+
|
146 |
+
|
147 |
+
with gr.Blocks(title='Talk to chatGPT') as demo:
|
148 |
+
gr.Markdown("## Talk to chatGPT with your voice in your native language ! ##")
|
149 |
+
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>")
|
150 |
+
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>")
|
151 |
+
with gr.Group(elem_id="page_1", visible=True) as page_1:
|
152 |
+
with gr.Box():
|
153 |
+
with gr.Row():
|
154 |
+
start_button = gr.Button("Let's talk to chatGPT!", elem_id="start-btn", visible=True)
|
155 |
+
start_button.click(fn=None, inputs=[], outputs=[], _js=start_work)
|
156 |
+
|
157 |
+
with gr.Group(elem_id="page_2", visible=False) as page_2:
|
158 |
+
with gr.Row(elem_id="chat_row"):
|
159 |
+
chatbot = gr.Chatbot(elem_id="chat_bot", visible=False).style(color_map=("green", "blue"))
|
160 |
+
chatbot1 = gr.Chatbot(elem_id="chat_bot1").style(color_map=("green", "blue"))
|
161 |
+
with gr.Row():
|
162 |
+
prompt_input_audio = gr.Audio(
|
163 |
+
source="microphone",
|
164 |
+
type="filepath",
|
165 |
+
label="Record Audio Input",
|
166 |
+
|
167 |
+
)
|
168 |
+
translate_btn = gr.Button("Check Whisper first ? 👍")
|
169 |
+
|
170 |
+
whisper_task = gr.Radio(["Translate to English", "Transcribe in Spoken Language"], value="Translate to English", show_label=False)
|
171 |
+
with gr.Row(elem_id="prompt_row"):
|
172 |
+
prompt_input = gr.Textbox(lines=2, label="Input text",show_label=True)
|
173 |
+
chat_history = gr.Textbox(lines=4, label="prompt", visible=False)
|
174 |
+
submit_btn = gr.Button(value = "Send to chatGPT",elem_id="submit-btn").style(
|
175 |
+
margin=True,
|
176 |
+
rounded=(True, True, True, True),
|
177 |
+
width=100
|
178 |
+
)
|
179 |
+
|
180 |
+
|
181 |
+
|
182 |
+
translate_btn.click(fn=translate_or_transcribe,
|
183 |
+
inputs=[prompt_input_audio,whisper_task],
|
184 |
+
outputs=prompt_input
|
185 |
+
)
|
186 |
+
|
187 |
+
api = gr.State(value=get_api())
|
188 |
+
submit_btn.click(fn=chat,
|
189 |
+
inputs=[api,prompt_input, chat_history],
|
190 |
+
outputs=[api,chatbot, chat_history],
|
191 |
+
)
|
192 |
+
gr.HTML('''
|
193 |
+
<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>
|
194 |
+
<div class="footer">
|
195 |
+
<p>Whisper Model by <a href="https://github.com/openai/whisper" style="text-decoration: underline;" target="_blank">OpenAI</a> -
|
196 |
+
<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>
|
197 |
+
</p>
|
198 |
+
</div>
|
199 |
+
''')
|
200 |
+
|
201 |
+
demo.launch(debug = True)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pyChatGPT
|
2 |
+
loguru
|
3 |
+
--extra-index-url https://download.pytorch.org/whl/cu113
|
4 |
+
torch
|
5 |
+
transformers
|