Spaces:
Running
on
Zero
Running
on
Zero
File size: 5,285 Bytes
4c346eb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
import logging
import os
from collections import Counter
from collections.abc import Mapping
from typing import Any
import httpx
from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_fixed
logger = logging.getLogger(__name__)
BASE_URL = os.environ.get("ETHER0_REMOTES_API_BASE_URL")
HEADERS = {
"Authorization": f"Bearer {os.environ.get('ETHER0_REMOTES_API_TOKEN')}",
"Content-Type": "application/json",
}
SERVER_ERRORS_COUNTER = Counter({
"fetch_solubility": 0,
"fetch_purchasable": 0,
"fetch_forward_rxn": 0,
"fetch_rxn_info": 0,
})
THROW_500_ERROR_THRESHOLD = int(
os.environ.get("ETHER0_REMOTES_THROW_500_ERROR_THRESHOLD", "100")
)
# If our server throws a 501, we don't retry
OUR_SERVER_DONT_RETRY_CODE = httpx.codes.NOT_IMPLEMENTED.value
REMOTE_WORKER_COLD_START_TIME = 180 # sec
class RetryableServerError(Exception):
"""Retryable server error."""
@classmethod
def check_raise(
cls, response: httpx.Response, kwargs: Mapping[str, Any] | None = None
) -> None:
if (
response.is_server_error
and response.status_code != OUR_SERVER_DONT_RETRY_CODE
):
raise cls(
f"Retryable server error with status code {response.status_code}"
f" and inputs {kwargs or {}} and response {response=}."
)
@retry(
stop=stop_after_attempt(3),
wait=wait_fixed(1),
retry=retry_if_exception_type((
httpx.ReadTimeout,
httpx.ConnectError,
RetryableServerError,
)),
)
def fetch_solubility(query_smiles: str) -> dict:
response = httpx.post(
f"{BASE_URL}/compute_solubility",
json={"smiles": query_smiles},
headers=HEADERS,
timeout=REMOTE_WORKER_COLD_START_TIME,
)
error_message = ""
if response.is_success:
result = response.json()
if "error" in result:
error_message = result["error"]
else:
solubility = result["mean"]
return {"smiles": query_smiles, "solubility": solubility}
if response.is_redirect or response.is_server_error:
# We should not have redirect responses or server errors, so let's retry these
error_message = response.text
SERVER_ERRORS_COUNTER["fetch_solubility"] += 1
if SERVER_ERRORS_COUNTER["fetch_solubility"] >= THROW_500_ERROR_THRESHOLD:
response.raise_for_status()
RetryableServerError.check_raise(
response, kwargs={"query_smiles": query_smiles}
)
if error_message:
logger.warning(
f"fetch_solubility did not succeed on {query_smiles=} with"
f" {response=} and {error_message=}."
)
return {
"smiles": query_smiles,
"error": f"API error: {response} - {error_message}",
}
@retry(
stop=stop_after_attempt(3),
wait=wait_fixed(1),
retry=retry_if_exception_type((
httpx.ReadTimeout,
httpx.ConnectError,
RetryableServerError,
)),
)
def fetch_purchasable(query_smiles_list: list[str] | str) -> dict[str, bool]:
response = httpx.post(
f"{BASE_URL}/is_purchasable",
json={"smiles": query_smiles_list},
headers=HEADERS,
timeout=REMOTE_WORKER_COLD_START_TIME,
)
if response.is_success:
return response.json()
logger.warning(
f"fetch_purchasable did not succeed on {query_smiles_list=} with"
f" {response=} and {response.text=}."
)
if response.is_redirect or response.is_server_error:
# We should not have redirect responses or server errors, so let's retry these
SERVER_ERRORS_COUNTER["fetch_purchasable"] += 1
if SERVER_ERRORS_COUNTER["fetch_purchasable"] >= THROW_500_ERROR_THRESHOLD:
response.raise_for_status()
RetryableServerError.check_raise(
response, kwargs={"query_smiles_list": query_smiles_list}
)
return {}
@retry(
stop=stop_after_attempt(3),
wait=wait_fixed(1),
retry=retry_if_exception_type((
httpx.ReadTimeout,
httpx.ConnectError,
RetryableServerError,
)),
)
def fetch_forward_rxn(query_rxn_smiles: str) -> dict[str, str]:
response = httpx.post(
f"{BASE_URL}/translate",
json={"reaction": query_rxn_smiles},
headers=HEADERS,
timeout=REMOTE_WORKER_COLD_START_TIME,
)
if response.is_success:
result = response.json()
product = result["product"]
return {"smiles": query_rxn_smiles, "product": product}
logger.warning(
f"fetch_forward_rxn did not succeed on {query_rxn_smiles=} with"
f" {response=} and {response.text=}."
)
if response.is_redirect or response.is_server_error:
# We should not have redirect responses or server errors, so let's retry these
SERVER_ERRORS_COUNTER["fetch_forward_rxn"] += 1
if SERVER_ERRORS_COUNTER["fetch_forward_rxn"] >= THROW_500_ERROR_THRESHOLD:
response.raise_for_status()
RetryableServerError.check_raise(
response, kwargs={"query_rxn_smiles": query_rxn_smiles}
)
return {
"smiles": query_rxn_smiles,
"error": f"API error: {response} - {response.text}",
}
|