Commit
•
ad42467
0
Parent(s):
Duplicate from sanchit-gandhi/chatGPT
Browse filesCo-authored-by: Sanchit Gandhi <sanchit-gandhi@users.noreply.huggingface.co>
- .gitattributes +34 -0
- README.md +13 -0
- app.py +216 -0
- packages.txt +1 -0
- requirements.txt +6 -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
|
3 |
+
emoji: 🏃
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: blue
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.12.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
duplicated_from: sanchit-gandhi/chatGPT
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,216 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import os
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
from transformers import pipeline
|
6 |
+
|
7 |
+
from pyChatGPT import ChatGPT
|
8 |
+
|
9 |
+
from speechbrain.pretrained import Tacotron2
|
10 |
+
from speechbrain.pretrained import HIFIGAN
|
11 |
+
|
12 |
+
import json
|
13 |
+
import soundfile as sf
|
14 |
+
|
15 |
+
|
16 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
17 |
+
|
18 |
+
print(f"Is CUDA available: {torch.cuda.is_available()}")
|
19 |
+
print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
|
20 |
+
|
21 |
+
# Intialise STT (Whisper)
|
22 |
+
pipe = pipeline(
|
23 |
+
task="automatic-speech-recognition",
|
24 |
+
model="openai/whisper-base.en",
|
25 |
+
chunk_length_s=30,
|
26 |
+
device=device,
|
27 |
+
)
|
28 |
+
|
29 |
+
# Initialise ChatGPT session
|
30 |
+
session_token = os.environ.get("SessionToken")
|
31 |
+
api = ChatGPT(session_token=session_token)
|
32 |
+
|
33 |
+
# Intialise TTS (tacotron2) and Vocoder (HiFIGAN)
|
34 |
+
tacotron2 = Tacotron2.from_hparams(
|
35 |
+
source="speechbrain/tts-tacotron2-ljspeech",
|
36 |
+
savedir="tmpdir_tts",
|
37 |
+
overrides={"max_decoder_steps": 10000},
|
38 |
+
run_opts={"device": device},
|
39 |
+
)
|
40 |
+
hifi_gan = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-ljspeech", savedir="tmpdir_vocoder")
|
41 |
+
|
42 |
+
|
43 |
+
def get_response_from_chatbot(text, reset_conversation):
|
44 |
+
try:
|
45 |
+
if reset_conversation:
|
46 |
+
api.refresh_auth()
|
47 |
+
api.reset_conversation()
|
48 |
+
resp = api.send_message(text)
|
49 |
+
response = resp["message"]
|
50 |
+
except:
|
51 |
+
response = "Sorry, the chatGPT queue is full. Please try again later."
|
52 |
+
return response
|
53 |
+
|
54 |
+
|
55 |
+
def chat(input_audio, chat_history, reset_conversation):
|
56 |
+
# speech -> text (Whisper)
|
57 |
+
message = pipe(input_audio)["text"]
|
58 |
+
|
59 |
+
# text -> response (chatGPT)
|
60 |
+
response = get_response_from_chatbot(message, reset_conversation)
|
61 |
+
|
62 |
+
# response -> speech (tacotron2)
|
63 |
+
mel_output, mel_length, alignment = tacotron2.encode_text(response)
|
64 |
+
wav = hifi_gan.decode_batch(mel_output)
|
65 |
+
sf.write("out.wav", wav.squeeze().cpu().numpy(), 22050)
|
66 |
+
|
67 |
+
out_chat = []
|
68 |
+
chat_history = chat_history if not reset_conversation else ""
|
69 |
+
if chat_history != "":
|
70 |
+
out_chat = json.loads(chat_history)
|
71 |
+
|
72 |
+
out_chat.append((message, response))
|
73 |
+
chat_history = json.dumps(out_chat)
|
74 |
+
|
75 |
+
return out_chat, chat_history, "out.wav"
|
76 |
+
|
77 |
+
|
78 |
+
start_work = """async() => {
|
79 |
+
function isMobile() {
|
80 |
+
try {
|
81 |
+
document.createEvent("TouchEvent"); return true;
|
82 |
+
} catch(e) {
|
83 |
+
return false;
|
84 |
+
}
|
85 |
+
}
|
86 |
+
function getClientHeight()
|
87 |
+
{
|
88 |
+
var clientHeight=0;
|
89 |
+
if(document.body.clientHeight&&document.documentElement.clientHeight) {
|
90 |
+
var clientHeight = (document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
|
91 |
+
} else {
|
92 |
+
var clientHeight = (document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
|
93 |
+
}
|
94 |
+
return clientHeight;
|
95 |
+
}
|
96 |
+
|
97 |
+
function setNativeValue(element, value) {
|
98 |
+
const valueSetter = Object.getOwnPropertyDescriptor(element.__proto__, 'value').set;
|
99 |
+
const prototype = Object.getPrototypeOf(element);
|
100 |
+
const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
|
101 |
+
|
102 |
+
if (valueSetter && valueSetter !== prototypeValueSetter) {
|
103 |
+
prototypeValueSetter.call(element, value);
|
104 |
+
} else {
|
105 |
+
valueSetter.call(element, value);
|
106 |
+
}
|
107 |
+
}
|
108 |
+
var gradioEl = document.querySelector('body > gradio-app').shadowRoot;
|
109 |
+
if (!gradioEl) {
|
110 |
+
gradioEl = document.querySelector('body > gradio-app');
|
111 |
+
}
|
112 |
+
|
113 |
+
if (typeof window['gradioEl'] === 'undefined') {
|
114 |
+
window['gradioEl'] = gradioEl;
|
115 |
+
|
116 |
+
const page1 = window['gradioEl'].querySelectorAll('#page_1')[0];
|
117 |
+
const page2 = window['gradioEl'].querySelectorAll('#page_2')[0];
|
118 |
+
|
119 |
+
page1.style.display = "none";
|
120 |
+
page2.style.display = "block";
|
121 |
+
window['div_count'] = 0;
|
122 |
+
window['chat_bot'] = window['gradioEl'].querySelectorAll('#chat_bot')[0];
|
123 |
+
window['chat_bot1'] = window['gradioEl'].querySelectorAll('#chat_bot1')[0];
|
124 |
+
chat_row = window['gradioEl'].querySelectorAll('#chat_row')[0];
|
125 |
+
prompt_row = window['gradioEl'].querySelectorAll('#prompt_row')[0];
|
126 |
+
window['chat_bot1'].children[1].textContent = '';
|
127 |
+
|
128 |
+
clientHeight = getClientHeight();
|
129 |
+
new_height = (clientHeight-300) + 'px';
|
130 |
+
chat_row.style.height = new_height;
|
131 |
+
window['chat_bot'].style.height = new_height;
|
132 |
+
window['chat_bot'].children[2].style.height = new_height;
|
133 |
+
window['chat_bot1'].style.height = new_height;
|
134 |
+
window['chat_bot1'].children[2].style.height = new_height;
|
135 |
+
prompt_row.children[0].style.flex = 'auto';
|
136 |
+
prompt_row.children[0].style.width = '100%';
|
137 |
+
|
138 |
+
window['checkChange'] = function checkChange() {
|
139 |
+
try {
|
140 |
+
if (window['chat_bot'].children[2].children[0].children.length > window['div_count']) {
|
141 |
+
new_len = window['chat_bot'].children[2].children[0].children.length - window['div_count'];
|
142 |
+
for (var i = 0; i < new_len; i++) {
|
143 |
+
new_div = window['chat_bot'].children[2].children[0].children[window['div_count'] + i].cloneNode(true);
|
144 |
+
window['chat_bot1'].children[2].children[0].appendChild(new_div);
|
145 |
+
}
|
146 |
+
window['div_count'] = chat_bot.children[2].children[0].children.length;
|
147 |
+
}
|
148 |
+
if (window['chat_bot'].children[0].children.length > 1) {
|
149 |
+
window['chat_bot1'].children[1].textContent = window['chat_bot'].children[0].children[1].textContent;
|
150 |
+
} else {
|
151 |
+
window['chat_bot1'].children[1].textContent = '';
|
152 |
+
}
|
153 |
+
|
154 |
+
} catch(e) {
|
155 |
+
}
|
156 |
+
}
|
157 |
+
window['checkChange_interval'] = window.setInterval("window.checkChange()", 500);
|
158 |
+
}
|
159 |
+
|
160 |
+
return false;
|
161 |
+
}"""
|
162 |
+
|
163 |
+
with gr.Blocks(title="Talk to chatGPT") as demo:
|
164 |
+
gr.Markdown("## Talk to chatGPT ##")
|
165 |
+
gr.HTML(
|
166 |
+
"<p> Demo uses <a href='https://huggingface.co/openai/whisper-base.en' class='underline'>Whisper</a> to convert the input speech"
|
167 |
+
" to transcribed text, <a href='https://chat.openai.com/chat' class='underline'>chatGPT</a> to generate responses, and <a"
|
168 |
+
" href='https://huggingface.co/speechbrain/tts-tacotron2-ljspeech' class='underline'>tacotron2</a> to convert the response to"
|
169 |
+
" output speech: </p>"
|
170 |
+
)
|
171 |
+
gr.HTML("<p> <center><img src='https://raw.githubusercontent.com/sanchit-gandhi/codesnippets/main/pipeline.png' width='870'></center> </p>")
|
172 |
+
gr.HTML(
|
173 |
+
"<p>You can duplicate this space and use your own session token: <a style='display:inline-block'"
|
174 |
+
" href='https://huggingface.co/spaces/sanchit-gandhi/chatGPT?duplicate=true'><img"
|
175 |
+
" 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=10'"
|
176 |
+
" alt='Duplicate Space'></a></p>"
|
177 |
+
)
|
178 |
+
gr.HTML(
|
179 |
+
"<p> Instructions on how to obtain your session token can be found in the video <a style='display:inline-block'"
|
180 |
+
" href='https://youtu.be/TdNSj_qgdFk?t=175'><font style='color:blue;weight:bold;'>here</font></a>."
|
181 |
+
" Add your session token by going to <i>Settings</i> -> <i>New secret</i> and add the token under the name <i>SessionToken</i>. </p>"
|
182 |
+
)
|
183 |
+
with gr.Group(elem_id="page_1", visible=True) as page_1:
|
184 |
+
with gr.Box():
|
185 |
+
with gr.Row():
|
186 |
+
start_button = gr.Button("Let's talk to chatGPT! 🗣", elem_id="start-btn", visible=True)
|
187 |
+
start_button.click(fn=None, inputs=[], outputs=[], _js=start_work)
|
188 |
+
|
189 |
+
with gr.Group(elem_id="page_2", visible=False) as page_2:
|
190 |
+
with gr.Row(elem_id="chat_row"):
|
191 |
+
chatbot = gr.Chatbot(elem_id="chat_bot", visible=False).style(color_map=("green", "blue"))
|
192 |
+
chatbot1 = gr.Chatbot(elem_id="chat_bot1").style(color_map=("green", "blue"))
|
193 |
+
with gr.Row():
|
194 |
+
prompt_input_audio = gr.Audio(
|
195 |
+
source="microphone",
|
196 |
+
type="filepath",
|
197 |
+
label="Record Audio Input",
|
198 |
+
)
|
199 |
+
prompt_output_audio = gr.Audio()
|
200 |
+
|
201 |
+
reset_conversation = gr.Checkbox(label="Reset conversation?", value=False)
|
202 |
+
with gr.Row(elem_id="prompt_row"):
|
203 |
+
chat_history = gr.Textbox(lines=4, label="prompt", visible=False)
|
204 |
+
submit_btn = gr.Button(value="Send to chatGPT", elem_id="submit-btn").style(
|
205 |
+
margin=True,
|
206 |
+
rounded=(True, True, True, True),
|
207 |
+
width=100,
|
208 |
+
)
|
209 |
+
|
210 |
+
submit_btn.click(
|
211 |
+
fn=chat,
|
212 |
+
inputs=[prompt_input_audio, chat_history, reset_conversation],
|
213 |
+
outputs=[chatbot, chat_history, prompt_output_audio],
|
214 |
+
)
|
215 |
+
|
216 |
+
demo.launch(debug=True)
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
libsndfile1
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
git+https://github.com/huggingface/transformers
|
2 |
+
--extra-index-url https://download.pytorch.org/whl/cu113
|
3 |
+
torch
|
4 |
+
speechbrain
|
5 |
+
soundfile
|
6 |
+
pychatGPT
|