deepsync commited on
Commit
595c50d
·
1 Parent(s): 5599d7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -1
app.py CHANGED
@@ -28,13 +28,17 @@ def get_alternate_text(text, language):
28
  }
29
  response = requests.post(ENDPOINT, data=json.dumps(payload), headers=headers)
30
  resp_text = None
31
- while resp_text is None:
 
32
  try:
33
  resp_text = response.json().get('choices')[0]['message']['content'].strip("\n").replace('"', "")
34
  except Exception as e:
35
  print(e)
36
  response = requests.post(ENDPOINT, data=json.dumps(payload), headers=headers)
 
37
  continue
 
 
38
  texts = list(filter(lambda x: len(x.strip()) > 0, resp_text.split("\n")))
39
  counts = list(map(lambda x: len(x.split()), texts))
40
  return len(text.split()), {"data": list(zip(texts, counts)), "headers": ["Alternatives", "Word Count"]}
 
28
  }
29
  response = requests.post(ENDPOINT, data=json.dumps(payload), headers=headers)
30
  resp_text = None
31
+ retries = 0
32
+ while resp_text is None and retries < 5:
33
  try:
34
  resp_text = response.json().get('choices')[0]['message']['content'].strip("\n").replace('"', "")
35
  except Exception as e:
36
  print(e)
37
  response = requests.post(ENDPOINT, data=json.dumps(payload), headers=headers)
38
+ retries += 1
39
  continue
40
+ if retries >= 5:
41
+ return "Error. Please refresh and try", None
42
  texts = list(filter(lambda x: len(x.strip()) > 0, resp_text.split("\n")))
43
  counts = list(map(lambda x: len(x.split()), texts))
44
  return len(text.split()), {"data": list(zip(texts, counts)), "headers": ["Alternatives", "Word Count"]}