ryanzhangfan commited on
Commit
624a982
1 Parent(s): a20173d

display error messages to users on chat panel

Browse files
demo/chat_frontend.py CHANGED
@@ -6,7 +6,7 @@
6
  # Email : zhangfan@baai.ac.cn
7
  # Institute : Beijing Academy of Artificial Intelligence (BAAI)
8
  # Create On : 2023-12-12 18:05
9
- # Last Modified : 2023-12-21 00:40
10
  # File Name : chat_frontend.py
11
  # Description :
12
  #
@@ -45,7 +45,7 @@ def submit(
45
 
46
  if check_text + check_image + check_video != 1:
47
  logging.info(f"{meta.log_id}: invalid input: give multi madality simultaneously for single modality input")
48
- gr.Error("Invalid input number, must give exactly one modality input at a time")
49
  return meta.format_chatbot(), meta, None, None, ""
50
 
51
  if check_text:
@@ -82,6 +82,11 @@ def generate(
82
 
83
  meta.pop_error()
84
  meta.pop()
 
 
 
 
 
85
  prompt = meta.format_chat()
86
 
87
  prompt_list, image_list = [], {}
@@ -132,9 +137,9 @@ def generate(
132
  if content["code"] == 0:
133
  meta.append(Role.ASSISTANT, DataMeta.build(text=content["data"]))
134
  else:
135
- meta.append(Role.ASSISTANT, DataMeta.build(text=f"GENERATE FAILED: {content['data']}", is_error=True))
136
  else:
137
- meta.append(Role.ASSISTANT, DataMeta.build(text=f"GENERATE FAILED: http failed with code {rsp.status_code}, msg: {rsp.text}", is_error=True))
138
 
139
  return meta.format_chatbot(), meta
140
 
@@ -217,7 +222,6 @@ def build_chat(args):
217
 
218
  gr.Markdown(TERM_OF_USE)
219
 
220
-
221
  clear_btn.click(clear_history, inputs=state, outputs=[chatbot, state])
222
  textbox.submit(
223
  submit,
 
6
  # Email : zhangfan@baai.ac.cn
7
  # Institute : Beijing Academy of Artificial Intelligence (BAAI)
8
  # Create On : 2023-12-12 18:05
9
+ # Last Modified : 2023-12-21 04:08
10
  # File Name : chat_frontend.py
11
  # Description :
12
  #
 
45
 
46
  if check_text + check_image + check_video != 1:
47
  logging.info(f"{meta.log_id}: invalid input: give multi madality simultaneously for single modality input")
48
+ meta.append(Role.ASSISTANT, DataMeta.build(text=f"Generate Failed: Invalid input number, must give exactly one modality input at a time", is_error=True))
49
  return meta.format_chatbot(), meta, None, None, ""
50
 
51
  if check_text:
 
82
 
83
  meta.pop_error()
84
  meta.pop()
85
+
86
+ if len(meta) == 0:
87
+ meta.append(Role.ASSISTANT, DataMeta.build(text=f"Generate Failed: Please enter a valid input", is_error=True))
88
+ return meta.format_chatbot(), meta
89
+
90
  prompt = meta.format_chat()
91
 
92
  prompt_list, image_list = [], {}
 
137
  if content["code"] == 0:
138
  meta.append(Role.ASSISTANT, DataMeta.build(text=content["data"]))
139
  else:
140
+ meta.append(Role.ASSISTANT, DataMeta.build(text=f"Generate Failed: {content['data']}", is_error=True))
141
  else:
142
+ meta.append(Role.ASSISTANT, DataMeta.build(text=f"Generate Failed: http failed with code {rsp.status_code}, msg: {rsp.text}", is_error=True))
143
 
144
  return meta.format_chatbot(), meta
145
 
 
222
 
223
  gr.Markdown(TERM_OF_USE)
224
 
 
225
  clear_btn.click(clear_history, inputs=state, outputs=[chatbot, state])
226
  textbox.submit(
227
  submit,
demo/generation_frontend.py CHANGED
@@ -6,7 +6,7 @@
6
  # Email : zhangfan@baai.ac.cn
7
  # Institute : Beijing Academy of Artificial Intelligence (BAAI)
8
  # Create On : 2023-12-11 15:35
9
- # Last Modified : 2023-12-21 00:41
10
  # File Name : generation_frontend.py
11
  # Description :
12
  #
@@ -48,13 +48,13 @@ def submit(
48
  if enable_grd:
49
  if text == "" and image is None:
50
  logging.info(f"{meta.log_id}: invalid input: no valid data for grounding input")
51
- gr.Error("text or image must be given if enable grounding generation")
52
  return meta.format_chatbot(), meta, False, 0, 0, EVA_IMAGE_SIZE, EVA_IMAGE_SIZE, None, ""
53
 
54
  meta.append(Role.USER, DataMeta.build(text=text, image=image, coordinate=[left, top, right, bottom]))
55
  elif image is not None and text != "":
56
  logging.info(f"{meta.log_id}: invalid input: give text and image simultaneously for single modality input")
57
- gr.Error("Do not submit text and image data at the same time!!!")
58
  return meta.format_chatbot(), meta, False, 0, 0, EVA_IMAGE_SIZE, EVA_IMAGE_SIZE, None, ""
59
  elif image is not None:
60
  meta.append(Role.USER, DataMeta.build(image=image))
@@ -76,6 +76,11 @@ def generate(meta, classifier_free_guidance, steps):
76
 
77
  meta.pop_error()
78
  meta.pop()
 
 
 
 
 
79
  prompt = meta.format_prompt()
80
 
81
  prompt_list, image_list = [], {}
@@ -122,9 +127,9 @@ def generate(meta, classifier_free_guidance, steps):
122
  image = Image.open(io.BytesIO(base64.b64decode(content["data"])))
123
  meta.append(Role.ASSISTANT, DataMeta.build(image=image, resize=False))
124
  else:
125
- meta.append(Role.ASSISTANT, DataMeta.build(text=f"GENERATE FAILED: {content['data']}", is_error=True))
126
  else:
127
- meta.append(Role.ASSISTANT, DataMeta.build(text=f"GENERATE FAILED: http failed with code {rsp.status_code}, msg: {rsp.text}", is_error=True))
128
 
129
  return meta.format_chatbot(), meta
130
 
 
6
  # Email : zhangfan@baai.ac.cn
7
  # Institute : Beijing Academy of Artificial Intelligence (BAAI)
8
  # Create On : 2023-12-11 15:35
9
+ # Last Modified : 2023-12-21 04:17
10
  # File Name : generation_frontend.py
11
  # Description :
12
  #
 
48
  if enable_grd:
49
  if text == "" and image is None:
50
  logging.info(f"{meta.log_id}: invalid input: no valid data for grounding input")
51
+ meta.append(Role.ASSISTANT, DataMeta.build(text=f"Input Error: Text or image must be given if enable grounding generation", is_error=True))
52
  return meta.format_chatbot(), meta, False, 0, 0, EVA_IMAGE_SIZE, EVA_IMAGE_SIZE, None, ""
53
 
54
  meta.append(Role.USER, DataMeta.build(text=text, image=image, coordinate=[left, top, right, bottom]))
55
  elif image is not None and text != "":
56
  logging.info(f"{meta.log_id}: invalid input: give text and image simultaneously for single modality input")
57
+ meta.append(Role.ASSISTANT, DataMeta.build(text=f"Input Error: Do not submit text and image data at the same time!!!", is_error=True))
58
  return meta.format_chatbot(), meta, False, 0, 0, EVA_IMAGE_SIZE, EVA_IMAGE_SIZE, None, ""
59
  elif image is not None:
60
  meta.append(Role.USER, DataMeta.build(image=image))
 
76
 
77
  meta.pop_error()
78
  meta.pop()
79
+
80
+ if len(meta) == 0:
81
+ meta.append(Role.ASSISTANT, DataMeta.build(text=f"Generate Failed: Please enter a valid input", is_error=True))
82
+ return meta.format_chatbot(), meta
83
+
84
  prompt = meta.format_prompt()
85
 
86
  prompt_list, image_list = [], {}
 
127
  image = Image.open(io.BytesIO(base64.b64decode(content["data"])))
128
  meta.append(Role.ASSISTANT, DataMeta.build(image=image, resize=False))
129
  else:
130
+ meta.append(Role.ASSISTANT, DataMeta.build(text=f"Generate Failed: {content['data']}", is_error=True))
131
  else:
132
+ meta.append(Role.ASSISTANT, DataMeta.build(text=f"Generate Failed: http failed with code {rsp.status_code}, msg: {rsp.text}", is_error=True))
133
 
134
  return meta.format_chatbot(), meta
135
 
demo/meta.py CHANGED
@@ -8,14 +8,14 @@
8
  # Email : zhangfan@baai.ac.cn
9
  # Institute : Beijing Academy of Artificial Intelligence (BAAI)
10
  # Create On : 2023-12-12 02:54
11
- # Last Modified : 2023-12-20 16:45
12
  # File Name : meta.py
13
  # Description :
14
  #
15
  # ===========================================================================================
16
 
17
  import base64
18
- from dataclasses import dataclass, field
19
  import io
20
  from enum import Enum
21
  from PIL import Image
@@ -306,3 +306,6 @@ class ConvMeta:
306
  if self.message[-1][0] == Role.USER:
307
  return False
308
  return True
 
 
 
 
8
  # Email : zhangfan@baai.ac.cn
9
  # Institute : Beijing Academy of Artificial Intelligence (BAAI)
10
  # Create On : 2023-12-12 02:54
11
+ # Last Modified : 2023-12-21 04:17
12
  # File Name : meta.py
13
  # Description :
14
  #
15
  # ===========================================================================================
16
 
17
  import base64
18
+ from dataclasses import dataclass
19
  import io
20
  from enum import Enum
21
  from PIL import Image
 
306
  if self.message[-1][0] == Role.USER:
307
  return False
308
  return True
309
+
310
+ def __len__(self):
311
+ return len(self.message)