Error handling
Browse files- api_client.py +36 -7
- app.py +6 -4
api_client.py
CHANGED
|
@@ -1,23 +1,51 @@
|
|
| 1 |
import requests
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
#BACKEND_URL = "http://localhost:8000"
|
| 5 |
BACKEND_URL = os.getenv("BACKEND_URL", "https://dcorcoran-pokemon-card-image-processor-api.hf.space")
|
| 6 |
|
| 7 |
def predict(image_bytes: bytes, filename: str) -> dict:
|
|
|
|
| 8 |
try:
|
| 9 |
response = requests.post(
|
| 10 |
f"{BACKEND_URL}/predict",
|
| 11 |
-
files={"file": (filename, image_bytes, "image/png")}
|
|
|
|
| 12 |
)
|
|
|
|
| 13 |
response.raise_for_status()
|
| 14 |
return response.json()
|
| 15 |
-
except requests.exceptions.
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
-
return {
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
def health_check() -> bool:
|
|
@@ -25,4 +53,5 @@ def health_check() -> bool:
|
|
| 25 |
response = requests.get(f"{BACKEND_URL}/health", timeout = 3)
|
| 26 |
return response.status_code == 200
|
| 27 |
except:
|
| 28 |
-
return False
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
import os
|
| 3 |
+
from fastapi import FastAPI, File, UploadFile
|
| 4 |
+
from fastapi.responses import JSONResponse
|
| 5 |
+
import requests
|
| 6 |
+
import io
|
| 7 |
|
| 8 |
#BACKEND_URL = "http://localhost:8000"
|
| 9 |
BACKEND_URL = os.getenv("BACKEND_URL", "https://dcorcoran-pokemon-card-image-processor-api.hf.space")
|
| 10 |
|
| 11 |
def predict(image_bytes: bytes, filename: str) -> dict:
|
| 12 |
+
"""Send image to backend /predict endpoint with detailed error info"""
|
| 13 |
try:
|
| 14 |
response = requests.post(
|
| 15 |
f"{BACKEND_URL}/predict",
|
| 16 |
+
files={"file": (filename, image_bytes, "image/png")},
|
| 17 |
+
timeout=60
|
| 18 |
)
|
| 19 |
+
# Raise HTTPError for 4xx/5xx responses
|
| 20 |
response.raise_for_status()
|
| 21 |
return response.json()
|
| 22 |
+
except requests.exceptions.HTTPError as http_err:
|
| 23 |
+
# Capture status code and backend response
|
| 24 |
+
try:
|
| 25 |
+
error_content = response.json()
|
| 26 |
+
except Exception:
|
| 27 |
+
error_content = response.text
|
| 28 |
+
return {
|
| 29 |
+
"error_type": "HTTPError",
|
| 30 |
+
"status_code": response.status_code,
|
| 31 |
+
"backend_response": error_content,
|
| 32 |
+
"exception": str(http_err)
|
| 33 |
+
}
|
| 34 |
+
except requests.exceptions.ConnectionError as conn_err:
|
| 35 |
+
return {
|
| 36 |
+
"error_type": "ConnectionError",
|
| 37 |
+
"exception": str(conn_err)
|
| 38 |
+
}
|
| 39 |
+
except requests.exceptions.Timeout as timeout_err:
|
| 40 |
+
return {
|
| 41 |
+
"error_type": "Timeout",
|
| 42 |
+
"exception": str(timeout_err)
|
| 43 |
+
}
|
| 44 |
except Exception as e:
|
| 45 |
+
return {
|
| 46 |
+
"error_type": "Unexpected",
|
| 47 |
+
"exception": str(e)
|
| 48 |
+
}
|
| 49 |
|
| 50 |
|
| 51 |
def health_check() -> bool:
|
|
|
|
| 53 |
response = requests.get(f"{BACKEND_URL}/health", timeout = 3)
|
| 54 |
return response.status_code == 200
|
| 55 |
except:
|
| 56 |
+
return False
|
| 57 |
+
|
app.py
CHANGED
|
@@ -19,8 +19,6 @@ st.set_page_config(
|
|
| 19 |
layout="wide"
|
| 20 |
)
|
| 21 |
|
| 22 |
-
BACKEND_URL = os.getenv("BACKEND_URL", "https://dcorcoran-Pokemon_Card_Image_Processor_API.hf.space")
|
| 23 |
-
|
| 24 |
# --------------------------------
|
| 25 |
# ----- HEADER -------------------
|
| 26 |
# --------------------------------
|
|
@@ -48,8 +46,12 @@ uploaded_file = render_upload_section()
|
|
| 48 |
|
| 49 |
if uploaded_file:
|
| 50 |
image_bytes = uploaded_file.read()
|
|
|
|
|
|
|
| 51 |
image = Image.open(io.BytesIO(image_bytes))
|
| 52 |
|
|
|
|
|
|
|
| 53 |
col1, col2 = st.columns([1, 2])
|
| 54 |
|
| 55 |
with col1:
|
|
@@ -60,8 +62,8 @@ if uploaded_file:
|
|
| 60 |
with st.spinner("Analyzing card..."):
|
| 61 |
result = predict(image_bytes, uploaded_file.name)
|
| 62 |
|
| 63 |
-
if "
|
| 64 |
-
st.error(result[
|
| 65 |
else:
|
| 66 |
render_card_data(result)
|
| 67 |
|
|
|
|
| 19 |
layout="wide"
|
| 20 |
)
|
| 21 |
|
|
|
|
|
|
|
| 22 |
# --------------------------------
|
| 23 |
# ----- HEADER -------------------
|
| 24 |
# --------------------------------
|
|
|
|
| 46 |
|
| 47 |
if uploaded_file:
|
| 48 |
image_bytes = uploaded_file.read()
|
| 49 |
+
print("Reading card...")
|
| 50 |
+
|
| 51 |
image = Image.open(io.BytesIO(image_bytes))
|
| 52 |
|
| 53 |
+
print("Successfully uploaded card")
|
| 54 |
+
|
| 55 |
col1, col2 = st.columns([1, 2])
|
| 56 |
|
| 57 |
with col1:
|
|
|
|
| 62 |
with st.spinner("Analyzing card..."):
|
| 63 |
result = predict(image_bytes, uploaded_file.name)
|
| 64 |
|
| 65 |
+
if "error_type" in result:
|
| 66 |
+
st.error(f"Error ({result['error_type']}): {result.get('backend_response') or result.get('exception')}")
|
| 67 |
else:
|
| 68 |
render_card_data(result)
|
| 69 |
|