Skip to content

replace requests with httpx and factor out clients #1574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11"]
defaults:
run:
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ GO := go
GOOS := $(shell $(GO) env GOOS)
GOARCH := $(shell $(GO) env GOARCH)

PYTHON := python
PYTHON ?= python
PYTEST := $(PYTHON) -m pytest
PYRIGHT := $(PYTHON) -m pyright
RUFF := $(PYTHON) -m ruff
Expand Down
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ authors = [{ name = "Replicate", email = "team@replicate.com" }]
license.file = "LICENSE"
urls."Source" = "https://github.com/replicate/cog"

requires-python = ">=3.7"
requires-python = ">=3.8"
dependencies = [
# intentionally loose. perhaps these should be vendored to not collide with user code?
"attrs>=20.1,<24",
"fastapi>=0.75.2,<0.99.0",
# we may not need http2
"httpx[http2]>=0.21.0,<1",
"pydantic>=1.9,<2",
"PyYAML",
"requests>=2,<3",
Expand All @@ -27,9 +29,9 @@ dependencies = [
optional-dependencies = { "dev" = [
"black",
"build",
"httpx",
'hypothesis<6.80.0; python_version < "3.8"',
'hypothesis; python_version >= "3.8"',
"respx",
'numpy<1.22.0; python_version < "3.8"',
'numpy; python_version >= "3.8"',
"pillow",
Expand Down
75 changes: 0 additions & 75 deletions python/cog/files.py

This file was deleted.

23 changes: 1 addition & 22 deletions python/cog/json.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import io
from datetime import datetime
from enum import Enum
from types import GeneratorType
from typing import Any, Callable
from typing import Any

from pydantic import BaseModel

from .types import Path


def make_encodeable(obj: Any) -> Any:
"""
Expand Down Expand Up @@ -39,21 +36,3 @@ def make_encodeable(obj: Any) -> Any:
if isinstance(obj, np.ndarray):
return obj.tolist()
return obj


def upload_files(obj: Any, upload_file: Callable[[io.IOBase], str]) -> Any:
"""
Iterates through an object from make_encodeable and uploads any files.

When a file is encountered, it will be passed to upload_file. Any paths will be opened and converted to files.
"""
if isinstance(obj, dict):
return {key: upload_files(value, upload_file) for key, value in obj.items()}
if isinstance(obj, list):
return [upload_files(value, upload_file) for value in obj]
if isinstance(obj, Path):
with obj.open("rb") as f:
return upload_file(f)
if isinstance(obj, io.IOBase):
return upload_file(obj)
return obj
1 change: 1 addition & 0 deletions python/cog/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,5 @@ def setup_logging(*, log_level: int = logging.NOTSET) -> None:

# Reconfigure log levels for some overly chatty libraries
logging.getLogger("uvicorn.access").setLevel(logging.WARNING)
# FIXME: no more urllib3(?)
logging.getLogger("urllib3.connectionpool").setLevel(logging.ERROR)
39 changes: 25 additions & 14 deletions python/cog/predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@
from .types import (
File as CogFile,
)
from .types import (
Input,
URLPath,
)
from .types import Input
from .types import (
Path as CogPath,
)
Expand All @@ -49,7 +46,7 @@

class BasePredictor(ABC):
def setup(
self, weights: Optional[Union[CogFile, CogPath]] = None
self, weights: Optional[Union[CogFile, CogPath, str]] = None
) -> Optional[Awaitable[None]]:
"""
An optional method to prepare the model so multiple predictions run efficiently.
Expand Down Expand Up @@ -79,34 +76,40 @@ async def run_setup_async(predictor: BasePredictor) -> None:
return await maybe_coro


def get_weights_argument(predictor: BasePredictor) -> Union[CogFile, CogPath, None]:
def get_weights_argument(predictor: BasePredictor) -> Union[CogFile, CogPath, str, None]:
# by the time we get here we assume predictor has a setup method
weights_type = get_weights_type(predictor.setup)
if weights_type is None:
return None
weights_url = os.environ.get("COG_WEIGHTS")
weights_path = "weights"
weights_path = "weights" # this is the source of a bug isn't it?

# TODO: Cog{File,Path}.validate(...) methods accept either "real"
# paths/files or URLs to those things. In future we can probably tidy this
# up a little bit.
# TODO: CogFile/CogPath should have subclasses for each of the subtypes

# this is a breaking change
# previously, CogPath wouldn't be converted in setup(); now it is
# essentially everyone needs to switch from Path to str (or a new URL type)
if weights_url:
if weights_type == CogFile:
return cast(CogFile, CogFile.validate(weights_url))
if weights_type == CogPath:
# TODO: So this can be a url. evil!
return cast(CogPath, CogPath.validate(weights_url))
if weights_type == str:
return weights_url
raise ValueError(
f"Predictor.setup() has an argument 'weights' of type {weights_type}, but only File and Path are supported"
f"Predictor.setup() has an argument 'weights' of type {weights_type}, but only File, Path and str are supported"
)
if os.path.exists(weights_path):
if weights_type == CogFile:
return cast(CogFile, open(weights_path, "rb"))
if weights_type == CogPath:
return CogPath(weights_path)
raise ValueError(
f"Predictor.setup() has an argument 'weights' of type {weights_type}, but only File and Path are supported"
f"Predictor.setup() has an argument 'weights' of type {weights_type}, but only File, Path and str are supported"
)
return None

Expand Down Expand Up @@ -212,17 +215,25 @@ def cleanup(self) -> None:
Cleanup any temporary files created by the input.
"""
for _, value in self:
# Handle URLPath objects specially for cleanup.
if isinstance(value, URLPath):
value.unlink()
# Note this is pathlib.Path, which cog.Path is a subclass of. A pathlib.Path object shouldn't make its way here,
# # Handle URLPath objects specially for cleanup.
# if isinstance(value, URLPath):
# value.unlink()
# Note this is pathlib.Path, of which cog.Path is a subclass of.
# A pathlib.Path object shouldn't make its way here,
# but both have an unlink() method, so may as well be safe.
elif isinstance(value, Path):
#
# URLTempFile, DataURLTempFilePath, pathlib.Path, doesn't matter
# everyone can be unlinked
if isinstance(value, Path):
try:
value.unlink()
except FileNotFoundError:
pass

# if we had a separate method to traverse the input and apply some function to each value
# we could have cleanup/get_tempfile/convert functions that operate on a single value
# and do it that way. convert is supposed to mutate though, so it's tricky


def validate_input_type(type: Type[Any], name: str) -> None:
if type is inspect.Signature.empty:
Expand Down
10 changes: 9 additions & 1 deletion python/cog/schema.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import secrets
import typing as t
from datetime import datetime
from enum import Enum
Expand Down Expand Up @@ -36,7 +37,14 @@ class PredictionBaseModel(pydantic.BaseModel, extra=pydantic.Extra.allow):


class PredictionRequest(PredictionBaseModel):
id: t.Optional[str]
# there's a problem here where the idempotent endpoint is supposed to
# let you pass id in the route and omit it from the input
# however this fills in the default
# maybe it should be allowed to be optional without the factory initially
# and be filled in later
#
# actually, this changes the public api so we should really do this differently
id: str = pydantic.Field(default_factory=lambda: secrets.token_hex(4))
created_at: t.Optional[datetime]

# TODO: deprecate this
Expand Down
Loading