mginoben commited on
Commit
48392ea
1 Parent(s): f6a75a8

Fix error handling issues

Browse files
Files changed (1) hide show
  1. app.py +21 -20
app.py CHANGED
@@ -117,29 +117,30 @@ def query(payload):
117
 
118
 
119
  def predict(text):
 
120
  print(preprocess(text))
121
 
122
- try:
123
- output = query(preprocess(text))[0]
124
- except Exception:
125
- return "Loading", "The model is loading please try again later."
126
-
127
- output = [tuple(i.values()) for i in output]
128
- output = dict((x, y) for x, y in output)
129
-
130
- predicted_label = list(output.keys())[0]
131
-
132
- if predicted_label == 'Abusive':
133
- output_text = text
134
- for profanity in profanities:
135
- compiled = re.compile(re.escape(profanity), re.IGNORECASE)
136
- mask = ""
137
- for i in profanity:
138
- mask += "*" if i != " " else " "
139
- output_text = compiled.sub(mask, output_text)
140
- return output, output_text
141
  else:
142
- return output, text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
 
145
  hf_writer = gr.HuggingFaceDatasetSaver('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', 'tagalog-profanity-feedbacks')
 
117
 
118
 
119
  def predict(text):
120
+ output = query(preprocess(text))
121
  print(preprocess(text))
122
 
123
+ if 'error' in output:
124
+ return output['error'], 'Error occured. Try again later.'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  else:
126
+ output = [tuple(i.values()) for i in output[0]]
127
+ output = dict((x, y) for x, y in output)
128
+
129
+ predicted_label = list(output.keys())[0]
130
+
131
+ if predicted_label == 'Abusive':
132
+ output_text = text
133
+ for profanity in profanities:
134
+ compiled = re.compile(re.escape(profanity), re.IGNORECASE)
135
+ mask = ""
136
+ for i in profanity:
137
+ mask += "*" if i != " " else " "
138
+ output_text = compiled.sub(mask, output_text)
139
+ return output, output_text
140
+ else:
141
+ return output, text
142
+
143
+
144
 
145
 
146
  hf_writer = gr.HuggingFaceDatasetSaver('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', 'tagalog-profanity-feedbacks')