Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -83,7 +83,7 @@ DEFAULT_SUGGESTIONS = [{
|
|
83 |
|
84 |
DEFAULT_CONVERSATIONS_HISTORY = [{"role": "placeholder"}]
|
85 |
|
86 |
-
DEFAULT_LOCALE = 'en_US'
|
87 |
|
88 |
DEFAULT_THEME = {
|
89 |
"token": {
|
@@ -108,10 +108,10 @@ def format_history(history):
|
|
108 |
class Gradio_Events:
|
109 |
|
110 |
@staticmethod
|
111 |
-
def
|
112 |
-
|
113 |
history = state_value["conversations_history"][
|
114 |
state_value["conversation_id"]]
|
|
|
115 |
history_messages = format_history(history)
|
116 |
|
117 |
history.append({
|
@@ -170,9 +170,10 @@ class Gradio_Events:
|
|
170 |
state: gr.update(value=state_value)
|
171 |
}
|
172 |
raise e
|
|
|
173 |
|
174 |
@staticmethod
|
175 |
-
def
|
176 |
if not state_value["conversation_id"]:
|
177 |
random_id = str(uuid.uuid4())
|
178 |
history = []
|
@@ -191,7 +192,46 @@ class Gradio_Events:
|
|
191 |
"key": str(uuid.uuid4()),
|
192 |
"content": sender_value
|
193 |
})
|
194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
|
196 |
@staticmethod
|
197 |
def preprocess_submit(clear_input=True):
|
@@ -203,9 +243,7 @@ class Gradio_Events:
|
|
203 |
if "meta" in conversation:
|
204 |
conversation["meta"]["disabled"] = True
|
205 |
return {
|
206 |
-
|
207 |
-
sender: gr.update(value=None, loading=True) if clear_input else gr.update(loading=True),
|
208 |
-
}),
|
209 |
conversations:
|
210 |
gr.update(active_key=state_value["conversation_id"],
|
211 |
items=list(
|
@@ -269,23 +307,7 @@ class Gradio_Events:
|
|
269 |
0 else DEFAULT_CONVERSATIONS_HISTORY), gr.update(
|
270 |
value=state_value)
|
271 |
|
272 |
-
@staticmethod
|
273 |
-
def regenerate_message(state_value, e: gr.EventData):
|
274 |
-
conversation_key = e._data["component"]["conversationKey"]
|
275 |
-
history = state_value["conversations_history"][
|
276 |
-
state_value["conversation_id"]]
|
277 |
-
index = -1
|
278 |
-
for i, conversation in enumerate(history):
|
279 |
-
if conversation["key"] == conversation_key:
|
280 |
-
index = i
|
281 |
-
break
|
282 |
-
if index == -1:
|
283 |
-
return gr.skip()
|
284 |
-
history = history[:index]
|
285 |
-
state_value["conversations_history"][
|
286 |
-
state_value["conversation_id"]] = history
|
287 |
|
288 |
-
return gr.update(items=history), gr.update(value=state_value)
|
289 |
|
290 |
@staticmethod
|
291 |
def edit_message(state_value, e: gr.EventData):
|
@@ -771,7 +793,7 @@ with gr.Blocks(css=css, fill_width=True) as demo:
|
|
771 |
with ms.Slot("icon"):
|
772 |
antd.Icon("EditOutlined")
|
773 |
with antd.Popconfirm(
|
774 |
-
title="Delete the message",
|
775 |
description=get_text(
|
776 |
"Are you sure to delete this message?",
|
777 |
"确定要删除这条消息吗?"),
|
@@ -885,50 +907,19 @@ with gr.Blocks(css=css, fill_width=True) as demo:
|
|
885 |
regenerating_event = chatbot_regenerate_popconfirm.confirm(
|
886 |
fn=Gradio_Events.regenerate_message,
|
887 |
inputs=[state],
|
888 |
-
outputs=[chatbot, state
|
889 |
-
|
890 |
-
|
891 |
-
|
892 |
-
|
893 |
-
add_conversation_btn, conversations,
|
894 |
-
|
895 |
-
|
896 |
-
|
897 |
-
|
898 |
-
|
899 |
-
|
900 |
-
|
901 |
-
inputs=[sender, state],
|
902 |
-
outputs=[state
|
903 |
-
]).then(fn=Gradio_Events.preprocess_submit(clear_input=True),
|
904 |
-
inputs=[state],
|
905 |
-
outputs=[
|
906 |
-
sender, clear_btn, conversation_delete_menu_item,
|
907 |
-
add_conversation_btn, conversations, chatbot,
|
908 |
-
state
|
909 |
-
]).then(fn=Gradio_Events.submit,
|
910 |
-
inputs=[state],
|
911 |
-
outputs=[chatbot, state])
|
912 |
-
regenerating_event.then(fn=Gradio_Events.postprocess_submit,
|
913 |
-
inputs=[state],
|
914 |
-
outputs=[
|
915 |
-
sender, conversation_delete_menu_item,
|
916 |
-
clear_btn, conversations, add_conversation_btn,
|
917 |
-
chatbot, state
|
918 |
-
])
|
919 |
-
submit_event.then(fn=Gradio_Events.postprocess_submit,
|
920 |
-
inputs=[state],
|
921 |
-
outputs=[
|
922 |
-
sender, conversation_delete_menu_item, clear_btn,
|
923 |
-
conversations, add_conversation_btn, chatbot, state
|
924 |
-
])
|
925 |
-
# sender.cancel(fn=None, cancels=[submit_event, regenerating_event])
|
926 |
-
# sender.cancel(fn=Gradio_Events.cancel,
|
927 |
-
# inputs=[state],
|
928 |
-
# outputs=[
|
929 |
-
# sender, conversation_delete_menu_item, clear_btn,
|
930 |
-
# conversations, add_conversation_btn, chatbot, state
|
931 |
-
# ])
|
932 |
|
933 |
if __name__ == "__main__":
|
934 |
-
demo.queue(default_concurrency_limit=
|
|
|
83 |
|
84 |
DEFAULT_CONVERSATIONS_HISTORY = [{"role": "placeholder"}]
|
85 |
|
86 |
+
DEFAULT_LOCALE = 'zh_CN' if is_modelscope_studio else 'en_US'
|
87 |
|
88 |
DEFAULT_THEME = {
|
89 |
"token": {
|
|
|
108 |
class Gradio_Events:
|
109 |
|
110 |
@staticmethod
|
111 |
+
def _submit(state_value):
|
|
|
112 |
history = state_value["conversations_history"][
|
113 |
state_value["conversation_id"]]
|
114 |
+
# submit
|
115 |
history_messages = format_history(history)
|
116 |
|
117 |
history.append({
|
|
|
170 |
state: gr.update(value=state_value)
|
171 |
}
|
172 |
raise e
|
173 |
+
|
174 |
|
175 |
@staticmethod
|
176 |
+
def submit(sender_value, state_value):
|
177 |
if not state_value["conversation_id"]:
|
178 |
random_id = str(uuid.uuid4())
|
179 |
history = []
|
|
|
192 |
"key": str(uuid.uuid4()),
|
193 |
"content": sender_value
|
194 |
})
|
195 |
+
|
196 |
+
# preprocess submit
|
197 |
+
yield Gradio_Events.preprocess_submit()(state_value)
|
198 |
+
try:
|
199 |
+
for chunk in Gradio_Events._submit(state_value):
|
200 |
+
yield chunk
|
201 |
+
finally:
|
202 |
+
# postprocess submit
|
203 |
+
yield Gradio_Events.postprocess_submit(state_value)
|
204 |
+
|
205 |
+
@staticmethod
|
206 |
+
def regenerate_message(state_value, e: gr.EventData):
|
207 |
+
conversation_key = e._data["component"]["conversationKey"]
|
208 |
+
history = state_value["conversations_history"][
|
209 |
+
state_value["conversation_id"]]
|
210 |
+
index = -1
|
211 |
+
for i, conversation in enumerate(history):
|
212 |
+
if conversation["key"] == conversation_key:
|
213 |
+
index = i
|
214 |
+
break
|
215 |
+
if index == -1:
|
216 |
+
yield gr.skip()
|
217 |
+
history = history[:index]
|
218 |
+
state_value["conversations_history"][
|
219 |
+
state_value["conversation_id"]] = history
|
220 |
+
|
221 |
+
yield {
|
222 |
+
chatbot:gr.update(items=history),
|
223 |
+
state: gr.update(value=state_value)
|
224 |
+
}
|
225 |
+
|
226 |
+
# preprocess submit
|
227 |
+
yield Gradio_Events.preprocess_submit(clear_input=False)(state_value)
|
228 |
+
try:
|
229 |
+
for chunk in Gradio_Events._submit(state_value):
|
230 |
+
yield chunk
|
231 |
+
finally:
|
232 |
+
# postprocess submit
|
233 |
+
yield Gradio_Events.postprocess_submit(state_value)
|
234 |
+
|
235 |
|
236 |
@staticmethod
|
237 |
def preprocess_submit(clear_input=True):
|
|
|
243 |
if "meta" in conversation:
|
244 |
conversation["meta"]["disabled"] = True
|
245 |
return {
|
246 |
+
sender: gr.update(value=None, loading=True) if clear_input else gr.update(loading=True),
|
|
|
|
|
247 |
conversations:
|
248 |
gr.update(active_key=state_value["conversation_id"],
|
249 |
items=list(
|
|
|
307 |
0 else DEFAULT_CONVERSATIONS_HISTORY), gr.update(
|
308 |
value=state_value)
|
309 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
310 |
|
|
|
311 |
|
312 |
@staticmethod
|
313 |
def edit_message(state_value, e: gr.EventData):
|
|
|
793 |
with ms.Slot("icon"):
|
794 |
antd.Icon("EditOutlined")
|
795 |
with antd.Popconfirm(
|
796 |
+
title=get_text("Delete the message", "删除消息"),
|
797 |
description=get_text(
|
798 |
"Are you sure to delete this message?",
|
799 |
"确定要删除这条消息吗?"),
|
|
|
907 |
regenerating_event = chatbot_regenerate_popconfirm.confirm(
|
908 |
fn=Gradio_Events.regenerate_message,
|
909 |
inputs=[state],
|
910 |
+
outputs=[sender, clear_btn, conversation_delete_menu_item, add_conversation_btn, conversations, chatbot, state])
|
911 |
+
|
912 |
+
submit_event = sender.submit(fn=Gradio_Events.submit,
|
913 |
+
inputs=[sender, state],
|
914 |
+
outputs=[sender, clear_btn, conversation_delete_menu_item,
|
915 |
+
add_conversation_btn, conversations,chatbot, state])
|
916 |
+
sender.cancel(fn=None, cancels=[submit_event, regenerating_event])
|
917 |
+
sender.cancel(fn=Gradio_Events.cancel,
|
918 |
+
inputs=[state],
|
919 |
+
outputs=[
|
920 |
+
sender, conversation_delete_menu_item, clear_btn,
|
921 |
+
conversations, add_conversation_btn, chatbot, state
|
922 |
+
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
923 |
|
924 |
if __name__ == "__main__":
|
925 |
+
demo.queue(default_concurrency_limit=20).launch(ssr_mode=False)
|