versae commited on
Commit
73dfaab
·
1 Parent(s): ec54f3f

Improved error handling

Browse files
Files changed (1) hide show
  1. app.py +24 -11
app.py CHANGED
@@ -1,25 +1,38 @@
1
  import os
 
2
 
3
  import requests
4
  import streamlit as st
5
 
6
-
7
  API_URL = "https://api-inference.huggingface.co/models/andrek/nb2nn"
8
 
9
- def translate(text):
 
10
  headers = {"Authorization": f"Bearer {os.environ['BEARER']}"}
11
  payload = {
12
  "inputs": text,
13
  }
14
- try:
15
- response = requests.post(API_URL, headers=headers, json=payload)
16
- json_response = response.json()[0]
17
- if "error" in json_response:
18
- return f"Model still loading: {json_response}"
19
- elif "translation_text" in json_response:
20
- return json_response["translation_text"]
21
- except Exception as exc:
22
- return f"Oops, something went wrong: {exc}"
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
 
25
  st.set_page_config(
 
1
  import os
2
+ import time
3
 
4
  import requests
5
  import streamlit as st
6
 
 
7
  API_URL = "https://api-inference.huggingface.co/models/andrek/nb2nn"
8
 
9
+
10
+ def translate(text, wait=True):
11
  headers = {"Authorization": f"Bearer {os.environ['BEARER']}"}
12
  payload = {
13
  "inputs": text,
14
  }
15
+ response = requests.post(API_URL, headers=headers, json=payload)
16
+ json_response = response.json()
17
+ if (isinstance(json_response, dict)
18
+ and "error" in json_response
19
+ and "estimated_time" in json_response):
20
+ st.write(json_response)
21
+ if wait:
22
+ with st.spinner(json_response["error"]):
23
+ bar = st.progress(0)
24
+ for progress in range(json_response["estimated_time"]):
25
+ bar.progress(progress / json_response["estimated_time"])
26
+ time.sleep(1)
27
+ bar.empty()
28
+ return translate(text, wait=False)
29
+ else:
30
+ return "We could not load the model"
31
+ elif (isinstance(json_response, list)
32
+ and "translation_text" in json_response[0]):
33
+ return json_response[0]["translation_text"]
34
+ else:
35
+ return f"Oops, something went wrong: {str(json_response)}"
36
 
37
 
38
  st.set_page_config(