BounharAbdelaziz commited on
Commit
b48804b
β€’
1 Parent(s): 30aa664

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -10
app.py CHANGED
@@ -7,21 +7,20 @@ HF_TOKEN = os.environ['HF_TOKEN']
7
  headers = {"Authorization": "Bearer "+ HF_TOKEN}
8
 
9
  def respond(english_text):
10
- payload = {
11
  "inputs": english_text,
12
  }
13
- response = requests.post(API_URL, headers=headers, json=payload)
14
 
15
- try:
16
- output = response[0]['generated_text']
17
 
18
- except Exception as e:
19
- if 'estimated_time' in response:
20
- output = f"Model is loading. Estimated time {response['estimated_time']}"
 
21
  else:
22
- output = response
23
-
24
- return output
25
 
26
  app = gr.Interface(fn=respond, inputs="text", outputs="text", title="Terjman-Large πŸ‘¨β€πŸ’»πŸ₯°", description="Translate English text to Moroccan Darija using our Large model (240M) πŸ€—")
27
 
 
7
  headers = {"Authorization": "Bearer "+ HF_TOKEN}
8
 
9
  def respond(english_text):
10
+ payload = {
11
  "inputs": english_text,
12
  }
 
13
 
14
+ while True:
15
+ response = requests.post(API_URL, headers=headers, json=payload).json()
16
 
17
+ if isinstance(response, list) and 'generated_text' in response[0]:
18
+ return response[0]['generated_text']
19
+ elif 'estimated_time' in response:
20
+ time.sleep(5) # Wait for 5 seconds before retrying
21
  else:
22
+ return "An error occurred, please refresh the webpage: " + str(response)
23
+
 
24
 
25
  app = gr.Interface(fn=respond, inputs="text", outputs="text", title="Terjman-Large πŸ‘¨β€πŸ’»πŸ₯°", description="Translate English text to Moroccan Darija using our Large model (240M) πŸ€—")
26