Spaces:
Runtime error
Runtime error
yizhangliu
commited on
Commit
•
7057057
1
Parent(s):
01868df
Update app.py
Browse filesAdd the saving and recovery of historical chat data
app.py
CHANGED
@@ -30,11 +30,12 @@ def get_response_from_chatbot(api, text):
|
|
30 |
try:
|
31 |
resp = api.send_message(text)
|
32 |
api.refresh_auth()
|
|
|
33 |
response = resp['message']
|
34 |
conversation_id = resp['conversation_id']
|
35 |
parent_id = resp['parent_id']
|
36 |
# logger.info(f"response_: {response}")
|
37 |
-
|
38 |
except:
|
39 |
response = "Sorry, I'm busy. Try again later.(2)"
|
40 |
return response
|
@@ -74,7 +75,6 @@ def chat(api, input0, input1, chat_radio, chat_history):
|
|
74 |
else:
|
75 |
prompt_en = getTextTrans(input0, source='zh', target='en') + f',{random.randint(0,sys.maxsize)}'
|
76 |
return api, out_chat, prompt_en, chat_history
|
77 |
-
|
78 |
|
79 |
start_work = """async() => {
|
80 |
function isMobile() {
|
@@ -106,6 +106,36 @@ start_work = """async() => {
|
|
106 |
valueSetter.call(element, value);
|
107 |
}
|
108 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
var gradioEl = document.querySelector('body > gradio-app').shadowRoot;
|
110 |
if (!gradioEl) {
|
111 |
gradioEl = document.querySelector('body > gradio-app');
|
@@ -119,7 +149,6 @@ start_work = """async() => {
|
|
119 |
|
120 |
page1.style.display = "none";
|
121 |
page2.style.display = "block";
|
122 |
-
|
123 |
window['div_count'] = 0;
|
124 |
window['chat_bot'] = window['gradioEl'].querySelectorAll('#chat_bot')[0];
|
125 |
window['chat_bot1'] = window['gradioEl'].querySelectorAll('#chat_bot1')[0];
|
@@ -148,7 +177,23 @@ start_work = """async() => {
|
|
148 |
window['gradioEl'].querySelectorAll('#chat_radio')[0].style.width = '100%';
|
149 |
prompt_row.children[0].setAttribute('style','flex-direction: inherit; flex: 1 1 auto; width: 100%;border-color: green;border-width: 1px !important;')
|
150 |
window['chat_bot1'].children[1].setAttribute('style', 'border-bottom-right-radius:0;top:unset;bottom:0;padding-left:0.1rem');
|
151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
window['prevPrompt'] = '';
|
153 |
window['doCheckPrompt'] = 0;
|
154 |
window['prevImgSrc'] = '';
|
@@ -163,9 +208,10 @@ start_work = """async() => {
|
|
163 |
}
|
164 |
window['div_count'] = chat_bot.children[2].children[0].children.length;
|
165 |
window['chat_bot1'].children[2].scrollTop = window['chat_bot1'].children[2].scrollHeight;
|
|
|
166 |
}
|
167 |
if (window['chat_bot'].children[0].children.length > 1) {
|
168 |
-
|
169 |
} else {
|
170 |
window['chat_bot1'].children[1].textContent = '';
|
171 |
}
|
@@ -201,7 +247,6 @@ start_work = """async() => {
|
|
201 |
user_div.style.backgroundColor = "#16a34a";
|
202 |
user_div.innerHTML = "<p>" + text0.value + "</p>";
|
203 |
window['chat_bot1'].children[2].children[0].appendChild(user_div);
|
204 |
-
|
205 |
var bot_div = document.createElement("div");
|
206 |
bot_div.className = "px-3 py-2 rounded-[22px] rounded-bl-none place-self-start text-white text-sm chat-message svelte-rct66g";
|
207 |
bot_div.style.backgroundColor = "#2563eb";
|
@@ -212,6 +257,7 @@ start_work = """async() => {
|
|
212 |
|
213 |
window['chat_bot1'].children[2].scrollTop = window['chat_bot1'].children[2].scrollHeight;
|
214 |
window['prevImgSrc'] = imgs[0].src;
|
|
|
215 |
}
|
216 |
}
|
217 |
if (tabitems[img_index].children[0].children[1].children[1].children[0].children[0].children.length > 1) {
|
@@ -262,4 +308,4 @@ with gr.Blocks(title='Talk to chatGPT') as demo:
|
|
262 |
with gr.Row(elem_id='tab_img', visible=False).style(height=5):
|
263 |
tab_img = gr.TabbedInterface(tab_actions, tab_titles)
|
264 |
|
265 |
-
demo.launch(debug = True)
|
|
|
30 |
try:
|
31 |
resp = api.send_message(text)
|
32 |
api.refresh_auth()
|
33 |
+
# api.reset_conversation()
|
34 |
response = resp['message']
|
35 |
conversation_id = resp['conversation_id']
|
36 |
parent_id = resp['parent_id']
|
37 |
# logger.info(f"response_: {response}")
|
38 |
+
logger.info(f"conversation_id_: [{conversation_id}] / parent_id: [{parent_id}]")
|
39 |
except:
|
40 |
response = "Sorry, I'm busy. Try again later.(2)"
|
41 |
return response
|
|
|
75 |
else:
|
76 |
prompt_en = getTextTrans(input0, source='zh', target='en') + f',{random.randint(0,sys.maxsize)}'
|
77 |
return api, out_chat, prompt_en, chat_history
|
|
|
78 |
|
79 |
start_work = """async() => {
|
80 |
function isMobile() {
|
|
|
106 |
valueSetter.call(element, value);
|
107 |
}
|
108 |
}
|
109 |
+
function save_conversation(chatbot) {
|
110 |
+
var conversations = new Array();
|
111 |
+
for (var i = 0; i < chatbot.children.length; i++) {
|
112 |
+
conversations[i] = chatbot.children[i].innerHTML;
|
113 |
+
}
|
114 |
+
var json_str = JSON.stringify(conversations);
|
115 |
+
localStorage.setItem('chatgpt_conversations', json_str);
|
116 |
+
}
|
117 |
+
function load_conversation(chatbot) {
|
118 |
+
var json_str = localStorage.getItem('chatgpt_conversations');
|
119 |
+
if (json_str) {
|
120 |
+
conversations = JSON.parse(json_str);
|
121 |
+
for (var i = 0; i < conversations.length; i++) {
|
122 |
+
var new_div = document.createElement("div");
|
123 |
+
if((i%2)===0){
|
124 |
+
new_div.className = "px-3 py-2 rounded-[22px] rounded-br-none text-white text-sm chat-message svelte-rct66g";
|
125 |
+
new_div.style.backgroundColor = "#16a34a";
|
126 |
+
} else {
|
127 |
+
new_div.className = "px-3 py-2 rounded-[22px] rounded-bl-none place-self-start text-white text-sm chat-message svelte-rct66g";
|
128 |
+
new_div.style.backgroundColor = "#2563eb";
|
129 |
+
if (conversations[i].indexOf("<img ") == 0) {
|
130 |
+
new_div.style.width = "80%";
|
131 |
+
new_div.style.padding = "0.2rem";
|
132 |
+
}
|
133 |
+
}
|
134 |
+
new_div.innerHTML = conversations[i];
|
135 |
+
chatbot.appendChild(new_div);
|
136 |
+
}
|
137 |
+
}
|
138 |
+
}
|
139 |
var gradioEl = document.querySelector('body > gradio-app').shadowRoot;
|
140 |
if (!gradioEl) {
|
141 |
gradioEl = document.querySelector('body > gradio-app');
|
|
|
149 |
|
150 |
page1.style.display = "none";
|
151 |
page2.style.display = "block";
|
|
|
152 |
window['div_count'] = 0;
|
153 |
window['chat_bot'] = window['gradioEl'].querySelectorAll('#chat_bot')[0];
|
154 |
window['chat_bot1'] = window['gradioEl'].querySelectorAll('#chat_bot1')[0];
|
|
|
177 |
window['gradioEl'].querySelectorAll('#chat_radio')[0].style.width = '100%';
|
178 |
prompt_row.children[0].setAttribute('style','flex-direction: inherit; flex: 1 1 auto; width: 100%;border-color: green;border-width: 1px !important;')
|
179 |
window['chat_bot1'].children[1].setAttribute('style', 'border-bottom-right-radius:0;top:unset;bottom:0;padding-left:0.1rem');
|
180 |
+
load_conversation(window['chat_bot1'].children[2].children[0]);
|
181 |
+
window['chat_bot1'].children[2].scrollTop = window['chat_bot1'].children[2].scrollHeight;
|
182 |
+
var mousedown_last = 0;
|
183 |
+
window['chat_bot1'].children[2].addEventListener('mousedown', function(e) {
|
184 |
+
mousedown_last = new Date();
|
185 |
+
})
|
186 |
+
window['chat_bot1'].children[2].addEventListener('mouseup', function(e) {
|
187 |
+
var now = new Date();
|
188 |
+
if (now - mousedown_last > 5 * 1000) {
|
189 |
+
if (confirm('Clear outputs?')==true) {
|
190 |
+
window['chat_bot1'].children[2].children[0].innerHTML = '';
|
191 |
+
save_conversation(window['chat_bot1'].children[2].children[0]);
|
192 |
+
}
|
193 |
+
}
|
194 |
+
mousedown_last = 0;
|
195 |
+
})
|
196 |
+
|
197 |
window['prevPrompt'] = '';
|
198 |
window['doCheckPrompt'] = 0;
|
199 |
window['prevImgSrc'] = '';
|
|
|
208 |
}
|
209 |
window['div_count'] = chat_bot.children[2].children[0].children.length;
|
210 |
window['chat_bot1'].children[2].scrollTop = window['chat_bot1'].children[2].scrollHeight;
|
211 |
+
save_conversation(window['chat_bot1'].children[2].children[0]);
|
212 |
}
|
213 |
if (window['chat_bot'].children[0].children.length > 1) {
|
214 |
+
window['chat_bot1'].children[1].textContent = window['chat_bot'].children[0].children[1].textContent;
|
215 |
} else {
|
216 |
window['chat_bot1'].children[1].textContent = '';
|
217 |
}
|
|
|
247 |
user_div.style.backgroundColor = "#16a34a";
|
248 |
user_div.innerHTML = "<p>" + text0.value + "</p>";
|
249 |
window['chat_bot1'].children[2].children[0].appendChild(user_div);
|
|
|
250 |
var bot_div = document.createElement("div");
|
251 |
bot_div.className = "px-3 py-2 rounded-[22px] rounded-bl-none place-self-start text-white text-sm chat-message svelte-rct66g";
|
252 |
bot_div.style.backgroundColor = "#2563eb";
|
|
|
257 |
|
258 |
window['chat_bot1'].children[2].scrollTop = window['chat_bot1'].children[2].scrollHeight;
|
259 |
window['prevImgSrc'] = imgs[0].src;
|
260 |
+
save_conversation(window['chat_bot1'].children[2].children[0]);
|
261 |
}
|
262 |
}
|
263 |
if (tabitems[img_index].children[0].children[1].children[1].children[0].children[0].children.length > 1) {
|
|
|
308 |
with gr.Row(elem_id='tab_img', visible=False).style(height=5):
|
309 |
tab_img = gr.TabbedInterface(tab_actions, tab_titles)
|
310 |
|
311 |
+
demo.launch(debug = True)
|