Skip to content

Commit

Permalink
typing v1 (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfeil authored Oct 8, 2024
1 parent 2528349 commit 98c5895
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 23 deletions.
10 changes: 5 additions & 5 deletions libs/infinity_emb/infinity_emb/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright (c) 2023-now michaelfeilfeil

from asyncio import Semaphore
from typing import Iterable, Iterator, List, Optional, Set, Union
from typing import Iterable, Iterator, Optional, Union

from infinity_emb.args import EngineArgs

Expand Down Expand Up @@ -122,7 +122,7 @@ def is_running(self) -> bool:
return self.running

@property
def capabilities(self) -> Set[ModelCapabilites]:
def capabilities(self) -> set[ModelCapabilites]:
return self._model.capabilities

@property
Expand Down Expand Up @@ -214,7 +214,7 @@ async def classify(
return scores, usage

async def image_embed(
self, *, images: List[Union[str, "ImageClassType", bytes]]
self, *, images: list[Union[str, "ImageClassType", bytes]]
) -> tuple[list["EmbeddingReturnType"], int]:
"""embed multiple images
Expand All @@ -237,7 +237,7 @@ async def image_embed(
return embeddings, usage

async def audio_embed(
self, *, audios: List[Union[str, bytes]]
self, *, audios: list[Union[str, bytes]]
) -> tuple[list["EmbeddingReturnType"], int]:
"""embed multiple audios
Expand Down Expand Up @@ -383,7 +383,7 @@ async def classify(
return await self[model].classify(sentences=sentences, raw_scores=raw_scores)

async def image_embed(
self, *, model: str, images: List[Union[str, "ImageClassType"]]
self, *, model: str, images: list[Union[str, "ImageClassType"]]
) -> tuple[list["EmbeddingReturnType"], int]:
"""embed multiple images
Expand Down
6 changes: 3 additions & 3 deletions libs/infinity_emb/infinity_emb/fastapi_schemas/data_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from base64 import b64decode as decode64
from base64 import b64encode as encode64
from dataclasses import dataclass
from typing import Any, Dict, MutableMapping, Optional, Tuple, TypeVar, Union
from typing import Any, MutableMapping, Optional, TypeVar, Union

if sys.version_info >= (3, 11):
from typing import Self
Expand Down Expand Up @@ -162,7 +162,7 @@ def is_valid(self) -> bool:
@property
def _parse(
self,
) -> Tuple[Optional[str], Optional[str], Optional[str], bool, bytes]:
) -> tuple[Optional[str], Optional[str], Optional[str], bool, bytes]:
match = _DATA_URI_RE.match(self)
if match is None:
raise InvalidDataURI("Not a valid data URI: %r" % self)
Expand Down Expand Up @@ -228,7 +228,7 @@ def __get_pydantic_json_schema__(
return json_schema

@classmethod
def __modify_schema__(cls, field_schema: Dict[str, Any]) -> None:
def __modify_schema__(cls, field_schema: dict[str, Any]) -> None:
# __modify_schema__ should mutate the dict it receives in place,
# the returned value will be ignored
field_schema.update(
Expand Down
10 changes: 5 additions & 5 deletions libs/infinity_emb/infinity_emb/inference/batch_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import time
from concurrent.futures import ThreadPoolExecutor
from queue import Queue
from typing import Any, List, Optional, Sequence, Set, Union
from typing import Any, Optional, Sequence, Union

import numpy as np

Expand Down Expand Up @@ -228,7 +228,7 @@ async def classify(
async def image_embed(
self,
*,
images: List[Union[str, "ImageClassType", bytes]],
images: list[Union[str, "ImageClassType", bytes]],
) -> tuple[list["EmbeddingReturnType"], int]:
"""Schedule a images and sentences to be embedded. Awaits until embedded.
Expand Down Expand Up @@ -257,7 +257,7 @@ async def image_embed(
async def audio_embed(
self,
*,
audios: List[Union[str, bytes]],
audios: list[Union[str, bytes]],
) -> tuple[list["EmbeddingReturnType"], int]:
"""Schedule audios and sentences to be embedded. Awaits until embedded.
Expand Down Expand Up @@ -310,7 +310,7 @@ async def _schedule(
return result, usage

@property
def capabilities(self) -> Set[ModelCapabilites]:
def capabilities(self) -> set[ModelCapabilites]:
# TODO: try to remove inheritance here and return upon init.
return self.model_worker.capabilities

Expand Down Expand Up @@ -447,7 +447,7 @@ def spawn(self):
self._threadpool.submit(self._postprocess_batch)

@property
def capabilities(self) -> Set[ModelCapabilites]:
def capabilities(self) -> set[ModelCapabilites]:
return self._model.capabilities

def tokenize_lengths(self, *args, **kwargs):
Expand Down
6 changes: 3 additions & 3 deletions libs/infinity_emb/infinity_emb/infinity_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sys
import time
from contextlib import asynccontextmanager
from typing import Any, List, Optional, Union
from typing import Any, Optional, Union

import infinity_emb
from infinity_emb._optional_imports import CHECK_TYPER, CHECK_UVICORN
Expand Down Expand Up @@ -204,8 +204,8 @@ def _resolve_engine(model: str) -> "AsyncEmbeddingEngine":
return engine

def _resolve_mixed_input(
inputs: Union[DataURIorURL, List[DataURIorURL]]
) -> List[Union[str, bytes]]:
inputs: Union[DataURIorURL, list[DataURIorURL]]
) -> list[Union[str, bytes]]:
if hasattr(inputs, "host"):
# if it is a single url
urls_or_bytes: list[Union[str, bytes]] = [str(inputs)]
Expand Down
3 changes: 1 addition & 2 deletions libs/infinity_emb/infinity_emb/sync_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
Awaitable,
Callable,
Iterator,
List,
Optional,
TypeVar,
Union,
Expand Down Expand Up @@ -213,7 +212,7 @@ def classify(self, *, model: str, sentences: list[str], raw_scores: bool = False
)

@add_start_docstrings(AsyncEngineArray.image_embed.__doc__)
def image_embed(self, *, model: str, images: List[Union[str, ImageClassType]]):
def image_embed(self, *, model: str, images: list[Union[str, ImageClassType]]):
"""sync interface of AsyncEngineArray"""
return self.async_run(
self.async_engine_array.image_embed, model=model, images=images
Expand Down
4 changes: 2 additions & 2 deletions libs/infinity_emb/infinity_emb/transformer/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import random
from abc import ABC, abstractmethod
from time import perf_counter
from typing import TYPE_CHECKING, Any, Set, Union
from typing import TYPE_CHECKING, Any, Union

from infinity_emb._optional_imports import CHECK_PIL # , CHECK_SOUNDFILE
from infinity_emb.primitives import (
Expand Down Expand Up @@ -42,7 +42,7 @@


class BaseTransformer(ABC): # Inherit from ABC(Abstract base class)
capabilities: Set[ModelCapabilites] = set()
capabilities: set[ModelCapabilites] = set()
engine_args: "EngineArgs"

@abstractmethod # Decorator to define an abstract method
Expand Down
6 changes: 3 additions & 3 deletions libs/infinity_emb/infinity_emb/transformer/vision/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import asyncio
import io
from typing import List, Union
from typing import Union

from infinity_emb._optional_imports import CHECK_AIOHTTP, CHECK_PIL
from infinity_emb.primitives import (
Expand Down Expand Up @@ -76,8 +76,8 @@ async def resolve_image(


async def resolve_images(
images: List[Union[str, "ImageClassType", bytes]]
) -> List[ImageSingle]:
images: list[Union[str, "ImageClassType", bytes]]
) -> list[ImageSingle]:
"""Resolve images from URLs or ImageClassType Objects using multithreading."""
# TODO: improve parallel requests, safety, error handling
CHECK_AIOHTTP.mark_required()
Expand Down

0 comments on commit 98c5895

Please sign in to comment.