Tony Lian commited on
Commit
e6327f4
β€’
1 Parent(s): 2622704

Improve error handling in parsing

Browse files
Files changed (1) hide show
  1. utils/parse.py +10 -7
utils/parse.py CHANGED
@@ -5,8 +5,11 @@ import matplotlib.pyplot as plt
5
  import numpy as np
6
  import warnings
7
  import inflect
 
8
 
9
  p = inflect.engine()
 
 
10
 
11
  img_dir = "imgs"
12
  objects_text = "Objects: "
@@ -27,7 +30,7 @@ def parse_input(text=None, no_input=False):
27
 
28
  if not text:
29
  if no_input:
30
- return
31
 
32
  text = input("Enter the response: ")
33
  if objects_text in text:
@@ -38,7 +41,7 @@ def parse_input(text=None, no_input=False):
38
  gen_boxes, bg_prompt = text_split
39
  elif len(text_split) == 1:
40
  if no_input:
41
- return
42
  gen_boxes = text
43
  bg_prompt = ""
44
  while not bg_prompt:
@@ -47,7 +50,7 @@ def parse_input(text=None, no_input=False):
47
  if bg_prompt_text_no_trailing_space in bg_prompt:
48
  bg_prompt = bg_prompt.split(bg_prompt_text_no_trailing_space)[1]
49
  else:
50
- raise ValueError(f"text: {text}")
51
  try:
52
  gen_boxes = ast.literal_eval(gen_boxes)
53
  except SyntaxError as e:
@@ -65,7 +68,7 @@ def parse_input_with_negative(text=None, no_input=False):
65
 
66
  if not text:
67
  if no_input:
68
- return
69
 
70
  text = input("Enter the response: ")
71
  if objects_text in text:
@@ -76,7 +79,7 @@ def parse_input_with_negative(text=None, no_input=False):
76
  gen_boxes, text_rem = text_split
77
  elif len(text_split) == 1:
78
  if no_input:
79
- return
80
  gen_boxes = text
81
  text_rem = ""
82
  while not text_rem:
@@ -85,7 +88,7 @@ def parse_input_with_negative(text=None, no_input=False):
85
  if bg_prompt_text_no_trailing_space in text_rem:
86
  text_rem = text_rem.split(bg_prompt_text_no_trailing_space)[1]
87
  else:
88
- raise ValueError(f"text: {text}")
89
 
90
  text_split = text_rem.split(neg_prompt_text_no_trailing_space)
91
 
@@ -101,7 +104,7 @@ def parse_input_with_negative(text=None, no_input=False):
101
  if neg_prompt_text_no_trailing_space in neg_prompt:
102
  neg_prompt = neg_prompt.split(neg_prompt_text_no_trailing_space)[1]
103
  else:
104
- raise ValueError(f"text: {text}")
105
 
106
  try:
107
  gen_boxes = ast.literal_eval(gen_boxes)
 
5
  import numpy as np
6
  import warnings
7
  import inflect
8
+ import gradio as gr
9
 
10
  p = inflect.engine()
11
+ # user_error = ValueError
12
+ user_error = gr.Error
13
 
14
  img_dir = "imgs"
15
  objects_text = "Objects: "
 
30
 
31
  if not text:
32
  if no_input:
33
+ raise user_error(f"No input parsed in \"{text}\".")
34
 
35
  text = input("Enter the response: ")
36
  if objects_text in text:
 
41
  gen_boxes, bg_prompt = text_split
42
  elif len(text_split) == 1:
43
  if no_input:
44
+ raise user_error(f"Invalid input (no background prompt): {text}")
45
  gen_boxes = text
46
  bg_prompt = ""
47
  while not bg_prompt:
 
50
  if bg_prompt_text_no_trailing_space in bg_prompt:
51
  bg_prompt = bg_prompt.split(bg_prompt_text_no_trailing_space)[1]
52
  else:
53
+ raise user_error(f"Invalid input (possibly multiple background prompts): {text}")
54
  try:
55
  gen_boxes = ast.literal_eval(gen_boxes)
56
  except SyntaxError as e:
 
68
 
69
  if not text:
70
  if no_input:
71
+ raise user_error(f"No input parsed in \"{text}\".")
72
 
73
  text = input("Enter the response: ")
74
  if objects_text in text:
 
79
  gen_boxes, text_rem = text_split
80
  elif len(text_split) == 1:
81
  if no_input:
82
+ raise user_error(f"Invalid input (no background prompt): {text}")
83
  gen_boxes = text
84
  text_rem = ""
85
  while not text_rem:
 
88
  if bg_prompt_text_no_trailing_space in text_rem:
89
  text_rem = text_rem.split(bg_prompt_text_no_trailing_space)[1]
90
  else:
91
+ raise user_error(f"Invalid input (possibly multiple background prompts): {text}")
92
 
93
  text_split = text_rem.split(neg_prompt_text_no_trailing_space)
94
 
 
104
  if neg_prompt_text_no_trailing_space in neg_prompt:
105
  neg_prompt = neg_prompt.split(neg_prompt_text_no_trailing_space)[1]
106
  else:
107
+ raise user_error(f"Invalid input (possibly multiple negative prompts): {text}")
108
 
109
  try:
110
  gen_boxes = ast.literal_eval(gen_boxes)