Improved error handling
Browse files
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 |
-
|
|
|
|
| 10 |
headers = {"Authorization": f"Bearer {os.environ['BEARER']}"}
|
| 11 |
payload = {
|
| 12 |
"inputs": text,
|
| 13 |
}
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|