rakibulinux commited on
Commit
c63cbf0
·
1 Parent(s): ac33358

Add connection diagnostics feature and improve error handling in API calls

Browse files
Files changed (2) hide show
  1. api.py +34 -3
  2. ui.py +6 -1
api.py CHANGED
@@ -6,7 +6,10 @@ import numpy as np
6
  BASE_URL = os.environ.get(
7
  "ID_API_URL",
8
  "http://127.0.0.1:8082"
9
- )
 
 
 
10
 
11
  ENDPOINTS = {
12
  "id": f"{BASE_URL}/api/check_id",
@@ -47,14 +50,42 @@ def _check_document(image, endpoint_key: str) -> dict:
47
  )
48
  response.raise_for_status()
49
  return response.json()
50
- except requests.exceptions.ConnectionError:
51
- return {"error": f"Cannot connect to server at {BASE_URL}. Make sure the SDK server is running."}
52
  except requests.exceptions.Timeout:
53
  return {"error": "Request timed out"}
54
  except requests.exceptions.RequestException as e:
55
  return {"error": f"API request failed: {str(e)}"}
56
 
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  def check_id(image) -> dict:
59
  return _check_document(image, "id")
60
 
 
6
  BASE_URL = os.environ.get(
7
  "ID_API_URL",
8
  "http://127.0.0.1:8082"
9
+ ).rstrip("/")
10
+ # ponytail: tolerate /api suffix (website env has it, this code adds it)
11
+ if BASE_URL.endswith("/api"):
12
+ BASE_URL = BASE_URL[:-len("/api")]
13
 
14
  ENDPOINTS = {
15
  "id": f"{BASE_URL}/api/check_id",
 
50
  )
51
  response.raise_for_status()
52
  return response.json()
53
+ except requests.exceptions.ConnectionError as e:
54
+ return {"error": f"Cannot connect to server at {BASE_URL}: {e}. Make sure the SDK server is running."}
55
  except requests.exceptions.Timeout:
56
  return {"error": "Request timed out"}
57
  except requests.exceptions.RequestException as e:
58
  return {"error": f"API request failed: {str(e)}"}
59
 
60
 
61
+ def diagnose() -> dict:
62
+ # ponytail: one function shows where HF->server path breaks
63
+ import socket
64
+ from urllib.parse import urlparse
65
+ out = {"base_url": BASE_URL, "endpoints": ENDPOINTS}
66
+ try:
67
+ p = urlparse(BASE_URL)
68
+ host, port = p.hostname, p.port or (443 if p.scheme == "https" else 80)
69
+ out["dns"] = socket.gethostbyname(host)
70
+ s = socket.create_connection((host, port), timeout=10)
71
+ out["tcp_443"] = "ok"
72
+ s.close()
73
+ except Exception as e:
74
+ out["tcp_443"] = f"FAIL: {type(e).__name__}: {e}"
75
+ return out
76
+ try:
77
+ r = requests.get(BASE_URL, timeout=10)
78
+ out["https_get_root"] = f"{r.status_code}: {r.text[:120]}"
79
+ except Exception as e:
80
+ out["https_get_root"] = f"FAIL: {type(e).__name__}: {e}"
81
+ try:
82
+ r = requests.post(ENDPOINTS["id"], files={"image": ("t.jpg", b"not-an-image", "image/jpeg")}, timeout=15)
83
+ out["https_post_check_id"] = f"{r.status_code}: {r.text[:200]}"
84
+ except Exception as e:
85
+ out["https_post_check_id"] = f"FAIL: {type(e).__name__}: {e}"
86
+ return out
87
+
88
+
89
  def check_id(image) -> dict:
90
  return _check_document(image, "id")
91
 
ui.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from api import check_id, check_credit, check_mrz
3
 
4
  ID_EXAMPLES = [[f"images/id/{i}.jpg"] for i in range(1, 8)]
5
  CREDIT_EXAMPLES = [["images/bank/demo1.jpg"], ["images/bank/demo2.png"], ["images/bank/demo3.png"]]
@@ -127,4 +127,9 @@ def create_interface():
127
  outputs=[result_html, raw_json],
128
  )
129
 
 
 
 
 
 
130
  return demo
 
1
  import gradio as gr
2
+ from api import check_id, check_credit, check_mrz, diagnose
3
 
4
  ID_EXAMPLES = [[f"images/id/{i}.jpg"] for i in range(1, 8)]
5
  CREDIT_EXAMPLES = [["images/bank/demo1.jpg"], ["images/bank/demo2.png"], ["images/bank/demo3.png"]]
 
127
  outputs=[result_html, raw_json],
128
  )
129
 
130
+ with gr.Accordion("Connection diagnostics", open=False):
131
+ diag_btn = gr.Button("Test connection")
132
+ diag_json = gr.JSON(label="Diagnostics")
133
+ diag_btn.click(fn=diagnose, outputs=diag_json)
134
+
135
  return demo