BounharAbdelaziz commited on
Commit
51badc8
1 Parent(s): ccbf7bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -4
app.py CHANGED
@@ -7,13 +7,21 @@ 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
- darija_translated_text = requests.post(API_URL, headers=headers, json=payload)
14
- return darija_translated_text.json()[0]["generated_text"]
 
 
 
 
 
 
 
 
15
 
16
- app = gr.Interface(fn=respond, inputs="text", outputs="text", title="Terjman-Large 👨‍💻🥰", description="Translate English text to Moroccan Darija using our Large model (240M) 🤗")
17
 
18
  if __name__ == "__main__":
19
  app.launch()
 
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
+ app = gr.Interface(fn=respond, inputs="text", outputs="text", title="Terjman-Large-v2 👨‍💻🥰", description="Translate English text to Moroccan Darija using our Large model (240M) 🤗")
25
 
26
  if __name__ == "__main__":
27
  app.launch()