ggbetz commited on
Commit
e63a601
1 Parent(s): 25b3351

catch request errors

Browse files
Files changed (1) hide show
  1. app.py +30 -10
app.py CHANGED
@@ -71,7 +71,6 @@ GEN_CHAINS = [
71
 
72
  INFERENCE_PARAMS = {
73
  'max_length':450,
74
- # 'clean_up_tokenization_spaces': False,
75
  'use_cache':False
76
  }
77
 
@@ -91,11 +90,21 @@ def build_inference_api():
91
  headers = {}# {"Authorization": f"Bearer {st.secrets['api_token']}"}
92
 
93
  def query(inputs: str, parameters):
94
- payload = {"inputs": inputs}
95
- payload.update(parameters)
 
 
 
96
  data = json.dumps(payload)
97
  response = requests.request("POST", API_URL, headers=headers, data=data)
98
- return json.loads(response.content.decode("utf-8"))
 
 
 
 
 
 
 
99
 
100
  return query
101
 
@@ -276,16 +285,24 @@ def run_model(mode_set, user_input):
276
  inputs = inquire_prompt
277
  attempts = 0
278
  out = None
279
- while out==None and attempts<MAX_API_CALLS:
280
  attempts += 1
281
  try:
282
- # api call
283
- out = inference(inputs,INFERENCE_PARAMS)
 
 
284
  except Exception:
285
- if attempts<MAX_API_CALLS:
286
- st.warning(f"HF Inference API call (attempt {attempts} of {MAX_API_CALLS}) has failed. Trying again...")
 
 
 
287
  else:
288
- raise
 
 
 
289
  out = out[0]['generated_text']
290
  # cleanup formalization
291
  if to_key in ['premises_formalized','conclusion_formalized']:
@@ -390,6 +407,9 @@ def main():
390
  with st.spinner("Processing (may take a couple of minutes) ..."):
391
  output = run_model(modes_s,user_input)
392
 
 
 
 
393
  # get latest generated reasons, conclusions, argdown
394
  argdown_raw = [out['output'] for out in output if out['mode']['to']=='argdown_reconstruction']
395
  argdown_raw = argdown_raw[-1] if len(argdown_raw)>0 else None
 
71
 
72
  INFERENCE_PARAMS = {
73
  'max_length':450,
 
74
  'use_cache':False
75
  }
76
 
 
90
  headers = {}# {"Authorization": f"Bearer {st.secrets['api_token']}"}
91
 
92
  def query(inputs: str, parameters):
93
+ payload = {
94
+ "inputs": inputs,
95
+ "parameters": parameters,
96
+ "options": {"wait_for_model": True},
97
+ }
98
  data = json.dumps(payload)
99
  response = requests.request("POST", API_URL, headers=headers, data=data)
100
+ content = response.content.decode("utf-8")
101
+ try:
102
+ # as json
103
+ result_json = json.loads(content)
104
+ except Exception:
105
+ result_json = {"error": content}
106
+
107
+ return result_json
108
 
109
  return query
110
 
 
285
  inputs = inquire_prompt
286
  attempts = 0
287
  out = None
288
+ while not out and attempts<MAX_API_CALLS:
289
  attempts += 1
290
  try:
291
+ # api call
292
+ out = inference(inputs, params)
293
+ if not isinstance(out, list):
294
+ raise ValueError('Response is not a list.')
295
  except Exception:
296
+ if attempts < MAX_API_CALLS:
297
+ st.warning(
298
+ f"HF Inference API call (attempt {attempts} of {MAX_API_CALLS}) has failed. Response: {out}. Trying again..."
299
+ )
300
+ out = None
301
  else:
302
+ st.warning(
303
+ f"HF Inference API call (attempt {attempts} of {MAX_API_CALLS}) has failed. Response: {out}. Stopping."
304
+ )
305
+ return None
306
  out = out[0]['generated_text']
307
  # cleanup formalization
308
  if to_key in ['premises_formalized','conclusion_formalized']:
 
407
  with st.spinner("Processing (may take a couple of minutes) ..."):
408
  output = run_model(modes_s,user_input)
409
 
410
+ if output is None:
411
+ return None
412
+
413
  # get latest generated reasons, conclusions, argdown
414
  argdown_raw = [out['output'] for out in output if out['mode']['to']=='argdown_reconstruction']
415
  argdown_raw = argdown_raw[-1] if len(argdown_raw)>0 else None