Skip to content

Commit

Permalink
added numpy in vision
Browse files Browse the repository at this point in the history
  • Loading branch information
jrcastropy committed Jan 22, 2024
1 parent acde80d commit aa8854e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
11 changes: 7 additions & 4 deletions serverless_openai/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,13 @@ def vision_longimage(
max_tokens: Optional[int] = 1024,
) -> OpenAIResults:

if 'data:image/jpeg;base64' in messages.image:
image_np = b64_to_np(messages.image)
else:
image_np = urlimage_to_np(messages.image)
if isinstance(messages.image, str):
if 'data:image/jpeg;base64' in messages.image:
image_np = b64_to_np(messages.image)
else:
image_np = urlimage_to_np(messages.image)
elif isinstance(messages.image, np.ndarray):
image_np = messages.image
img_b64_list = crop_image(image_np)
newm = [
{
Expand Down
18 changes: 11 additions & 7 deletions serverless_openai/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,20 @@ class VisionModels(str, ExtendedEnum):

class VisionMessage(BaseModel):
text: str
image: Union[str, np.ndarray]
role: Roles = Roles.user
image: str


@validator('image', always=True)
def check_url(cls, v, values):
try:
TypeAdapter(HttpUrl).validate_python(v)
return v
except ValidationError:
return encode_image(v)
if isinstance(v, str):
try:
TypeAdapter(HttpUrl).validate_python(v)
return v
except ValidationError:
return encode_image(v)
return v
class Config:
arbitrary_types_allowed = True

class EmbeddingModels(str, ExtendedEnum):
ada2 : str = "text-embedding-ada-002"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

VERSION = '1.2.5'
VERSION = '1.2.6'
DESCRIPTION = "A package for using Openai in serverless environment"
LONG_DESCRIPTION = 'A package for using Openai with scraping and etc. in serverless application such as AWS Lambda and GCP Cloud Function'

Expand Down

0 comments on commit aa8854e

Please sign in to comment.