BaselMousi commited on
Commit
975f9d1
·
1 Parent(s): 7964967

Added tenacity support for API failures

Browse files
Files changed (2) hide show
  1. requirements.txt +1 -0
  2. translator.py +7 -2
requirements.txt CHANGED
@@ -51,6 +51,7 @@ shellingham==1.5.4
51
  six==1.17.0
52
  sniffio==1.3.1
53
  starlette==0.47.3
 
54
  tomlkit==0.12.0
55
  tqdm==4.67.1
56
  typer==0.17.3
 
51
  six==1.17.0
52
  sniffio==1.3.1
53
  starlette==0.47.3
54
+ tenacity==9.1.2
55
  tomlkit==0.12.0
56
  tqdm==4.67.1
57
  typer==0.17.3
translator.py CHANGED
@@ -1,12 +1,17 @@
1
  import requests
2
  import time
 
 
 
 
3
  class Translator:
4
  def __init__(self, api_key, base_url="https://api.fanar.qa/v1/translations", langpair="ar-en", model="Fanar-Shaheen-MT-1"):
5
  self.api_key = api_key
6
  self.base_url = base_url
7
  self.langpair = langpair
8
  self.model = model
9
-
 
10
  def translate(self, text):
11
  headers = {
12
  'Content-Type': 'application/json',
@@ -21,7 +26,7 @@ class Translator:
21
  }
22
 
23
  res = requests.post(self.base_url, headers=headers, json=data)
24
- time.sleep(1)
25
  print(res)
26
  if res.status_code != 200:
27
  raise Exception(f"Error: API request failed with status code {res.status_code}. Details: {res.text}")
 
1
  import requests
2
  import time
3
+
4
+ from tenacity import retry, wait_fixed
5
+
6
+
7
  class Translator:
8
  def __init__(self, api_key, base_url="https://api.fanar.qa/v1/translations", langpair="ar-en", model="Fanar-Shaheen-MT-1"):
9
  self.api_key = api_key
10
  self.base_url = base_url
11
  self.langpair = langpair
12
  self.model = model
13
+
14
+ @retry(wait=wait_fixed(2))
15
  def translate(self, text):
16
  headers = {
17
  'Content-Type': 'application/json',
 
26
  }
27
 
28
  res = requests.post(self.base_url, headers=headers, json=data)
29
+
30
  print(res)
31
  if res.status_code != 200:
32
  raise Exception(f"Error: API request failed with status code {res.status_code}. Details: {res.text}")