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