Spaces:
Runtime error
Runtime error
Commit ·
c18076e
1
Parent(s): 113ddc7
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,186 +18,33 @@ def get_response_from_chatbot(text):
|
|
| 18 |
except:
|
| 19 |
response = "Sorry, I'm am tired."
|
| 20 |
return response
|
| 21 |
-
|
| 22 |
-
# def chat(message, history):
|
| 23 |
-
# history = history or []
|
| 24 |
-
# response = get_response_from_chatbot(message)
|
| 25 |
-
# history.append((message, response))
|
| 26 |
-
# return history, history
|
| 27 |
-
|
| 28 |
-
def chat1(message, chat_history):
|
| 29 |
-
split_mark_1 = ',,,,,,,,,,;'
|
| 30 |
-
split_mark_2 = ';;;;;;;;;;;'
|
| 31 |
-
out_chat = []
|
| 32 |
-
if chat_history != '':
|
| 33 |
-
ss0 = chat_history.split(split_mark_1)
|
| 34 |
-
for ss1 in ss0:
|
| 35 |
-
ss2 = ss1.split(split_mark_2)
|
| 36 |
-
out_chat.append((ss2[0], ss2[1]))
|
| 37 |
-
print(f'liuyz_1_{chat_history}')
|
| 38 |
-
response = get_response_from_chatbot(message)
|
| 39 |
-
out_chat.append((message, response))
|
| 40 |
-
if chat_history != '':
|
| 41 |
-
chat_history += split_mark_1
|
| 42 |
-
chat_history += f'{message}{split_mark_2}{response}'
|
| 43 |
-
print(f'liuyz_2_{chat_history}')
|
| 44 |
-
return out_chat, chat_history
|
| 45 |
|
| 46 |
def chat(message, chat_history):
|
| 47 |
out_chat = []
|
| 48 |
if chat_history != '':
|
| 49 |
out_chat = json.loads(chat_history)
|
| 50 |
-
print(f'
|
| 51 |
response = get_response_from_chatbot(message)
|
| 52 |
out_chat.append((message, response))
|
| 53 |
-
# if chat_history != '':
|
| 54 |
-
# chat_history += split_mark_1
|
| 55 |
-
# chat_history += f'{message}{split_mark_2}{response}'
|
| 56 |
chat_history = json.dumps(out_chat)
|
| 57 |
-
print(f'
|
| 58 |
return out_chat, chat_history
|
| 59 |
|
| 60 |
-
|
| 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 |
-
console.log('liuyz_1_')
|
| 95 |
-
if (typeof window['gradioEl'] === 'undefined') {
|
| 96 |
-
console.log('liuyz_2_')
|
| 97 |
-
window['gradioEl'] = gradioEl;
|
| 98 |
-
/*
|
| 99 |
-
tabitems = window['gradioEl'].querySelectorAll('.tabitem');
|
| 100 |
-
for (var i = 0; i < tabitems.length; i++) {
|
| 101 |
-
tabitems[i].childNodes[0].children[0].style.display='none';
|
| 102 |
-
tabitems[i].childNodes[0].children[1].children[0].style.display='none';
|
| 103 |
-
tabitems[i].childNodes[0].children[1].children[1].children[0].children[1].style.display="none";
|
| 104 |
-
}
|
| 105 |
-
tab_demo = window['gradioEl'].querySelectorAll('#tab_demo')[0];
|
| 106 |
-
tab_demo.style.display = "block";
|
| 107 |
-
tab_demo.setAttribute('style', 'height: 100%;');
|
| 108 |
-
*/
|
| 109 |
-
|
| 110 |
-
const page1 = window['gradioEl'].querySelectorAll('#page_1')[0];
|
| 111 |
-
const page2 = window['gradioEl'].querySelectorAll('#page_2')[0];
|
| 112 |
-
console.log('liuyz_3_')
|
| 113 |
-
page1.style.display = "none";
|
| 114 |
-
page2.style.display = "block";
|
| 115 |
-
console.log('liuyz_4_')
|
| 116 |
-
|
| 117 |
-
/*
|
| 118 |
-
window['prevPrompt'] = '';
|
| 119 |
-
window['doCheckPrompt'] = 0;
|
| 120 |
-
window['checkPrompt'] = function checkPrompt() {
|
| 121 |
-
try {
|
| 122 |
-
texts = window['gradioEl'].querySelectorAll('textarea');
|
| 123 |
-
text0 = texts[0];
|
| 124 |
-
text1 = texts[1];
|
| 125 |
-
if (window['doCheckPrompt'] === 0 && window['prevPrompt'] !== text1.value) {
|
| 126 |
-
console.log('_____new prompt___[' + text1.value + ']_');
|
| 127 |
-
window['doCheckPrompt'] = 1;
|
| 128 |
-
window['prevPrompt'] = text1.value;
|
| 129 |
-
for (var i = 2; i < texts.length; i++) {
|
| 130 |
-
setNativeValue(texts[i], text1.value);
|
| 131 |
-
texts[i].dispatchEvent(new Event('input', { bubbles: true }));
|
| 132 |
-
}
|
| 133 |
-
setTimeout(function() {
|
| 134 |
-
btns = window['gradioEl'].querySelectorAll('button');
|
| 135 |
-
for (var i = 0; i < btns.length; i++) {
|
| 136 |
-
if (btns[i].innerText == 'Submit') {
|
| 137 |
-
btns[i].click();
|
| 138 |
-
}
|
| 139 |
-
}
|
| 140 |
-
window['doCheckPrompt'] = 0;
|
| 141 |
-
}, 10);
|
| 142 |
-
}
|
| 143 |
-
} catch(e) {
|
| 144 |
-
}
|
| 145 |
-
}
|
| 146 |
-
window['checkPrompt_interval'] = window.setInterval("window.checkPrompt()", 100);
|
| 147 |
-
*/
|
| 148 |
-
}
|
| 149 |
-
|
| 150 |
-
return false;
|
| 151 |
-
}"""
|
| 152 |
-
|
| 153 |
-
with gr.Blocks(title='Text to Image') as demo:
|
| 154 |
with gr.Group(elem_id="page_1", visible=True) as page_1:
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
width=100
|
| 170 |
-
)
|
| 171 |
-
submit_btn.click(fn=chat,
|
| 172 |
-
inputs=[prompt_input0, chat_history],
|
| 173 |
-
outputs=[chatbot, chat_history],
|
| 174 |
-
)
|
| 175 |
-
|
| 176 |
-
# chatbot = gr.Chatbot(elem_id="chat_bot").style(color_map=("green", "gray"))
|
| 177 |
-
# prompt_input0 = gr.Textbox(lines=4, label="prompt")
|
| 178 |
-
# chatbot.change(chat,
|
| 179 |
-
# ["text", "state"],
|
| 180 |
-
# [chatbot, "state"],
|
| 181 |
-
# allow_flagging="never",
|
| 182 |
-
# show_label=False,
|
| 183 |
-
# show_progress=False)
|
| 184 |
-
# chat_demo = gr.Interface(
|
| 185 |
-
# chat,
|
| 186 |
-
# ["text", "state"],
|
| 187 |
-
# [chatbot, "state"],
|
| 188 |
-
# allow_flagging="never",
|
| 189 |
-
# show_label=False,
|
| 190 |
-
# )
|
| 191 |
-
|
| 192 |
-
# chatbot = gr.Chatbot().style(color_map=("green", "gray"))
|
| 193 |
-
# chatbot.change(show_progress=False)
|
| 194 |
-
|
| 195 |
-
# demo = gr.Interface(
|
| 196 |
-
# chat,
|
| 197 |
-
# ["text", "state"],
|
| 198 |
-
# [chatbot, "state"],
|
| 199 |
-
# allow_flagging="never",
|
| 200 |
-
# show_label=False,
|
| 201 |
-
# )
|
| 202 |
|
| 203 |
demo.launch(debug = True)
|
|
|
|
| 18 |
except:
|
| 19 |
response = "Sorry, I'm am tired."
|
| 20 |
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def chat(message, chat_history):
|
| 23 |
out_chat = []
|
| 24 |
if chat_history != '':
|
| 25 |
out_chat = json.loads(chat_history)
|
| 26 |
+
# print(f'chat_1_{chat_history}')
|
| 27 |
response = get_response_from_chatbot(message)
|
| 28 |
out_chat.append((message, response))
|
|
|
|
|
|
|
|
|
|
| 29 |
chat_history = json.dumps(out_chat)
|
| 30 |
+
# print(f'chat_2_{chat_history}')
|
| 31 |
return out_chat, chat_history
|
| 32 |
|
| 33 |
+
with gr.Blocks(title='chat with chatgpt') as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
with gr.Group(elem_id="page_1", visible=True) as page_1:
|
| 35 |
+
with gr.Row(elem_id="prompt_row"):
|
| 36 |
+
chatbot = gr.Chatbot(elem_id="chat_bot").style(color_map=("green", "gray"))
|
| 37 |
+
with gr.Row():
|
| 38 |
+
prompt_input0 = gr.Textbox(lines=1, label="prompt",show_label=False)
|
| 39 |
+
chat_history = gr.Textbox(lines=4, label="prompt", visible=False)
|
| 40 |
+
submit_btn = gr.Button(value = "submit",elem_id="submit-btn").style(
|
| 41 |
+
margin=True,
|
| 42 |
+
rounded=(True, True, True, True),
|
| 43 |
+
width=100
|
| 44 |
+
)
|
| 45 |
+
submit_btn.click(fn=chat,
|
| 46 |
+
inputs=[prompt_input0, chat_history],
|
| 47 |
+
outputs=[chatbot, chat_history],
|
| 48 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
demo.launch(debug = True)
|