Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
|
|
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"]}
|