Anurag Agarwal commited on
Commit
4dc4068
·
1 Parent(s): 202f368

Fix SSL cert verification for local testing (truststore)

Browse files
Files changed (1) hide show
  1. inference.py +14 -1
inference.py CHANGED
@@ -24,6 +24,11 @@ import textwrap
24
  import time
25
  from typing import Optional
26
 
 
 
 
 
 
27
  from openai import OpenAI
28
 
29
  API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
@@ -403,13 +408,21 @@ async def ws_step(ws, tool_name: str, args: dict) -> dict:
403
  def wait_for_server(base_url: str, timeout: int = 120) -> bool:
404
  import urllib.request
405
  import urllib.error
 
 
 
 
 
 
 
 
406
 
407
  urls = [f"{base_url}/health", f"{base_url}/"]
408
  deadline = time.time() + timeout
409
  while time.time() < deadline:
410
  for url in urls:
411
  try:
412
- req = urllib.request.urlopen(url, timeout=5)
413
  if req.status == 200:
414
  return True
415
  except Exception:
 
24
  import time
25
  from typing import Optional
26
 
27
+ try:
28
+ import truststore; truststore.inject_into_ssl()
29
+ except ImportError:
30
+ pass
31
+
32
  from openai import OpenAI
33
 
34
  API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
 
408
  def wait_for_server(base_url: str, timeout: int = 120) -> bool:
409
  import urllib.request
410
  import urllib.error
411
+ import ssl
412
+
413
+ ctx = ssl.create_default_context()
414
+ try:
415
+ import certifi
416
+ ctx.load_verify_locations(certifi.where())
417
+ except ImportError:
418
+ pass
419
 
420
  urls = [f"{base_url}/health", f"{base_url}/"]
421
  deadline = time.time() + timeout
422
  while time.time() < deadline:
423
  for url in urls:
424
  try:
425
+ req = urllib.request.urlopen(url, timeout=5, context=ctx)
426
  if req.status == 200:
427
  return True
428
  except Exception: