From 19fe2df39fe29e33f56b7a59af1fe250b2281678 Mon Sep 17 00:00:00 2001 From: mertyg Date: Sun, 7 Jul 2024 11:26:16 -0700 Subject: [PATCH] move the utilities from __init__ to a new file --- textgrad/engine/__init__.py | 18 ------------------ textgrad/engine/anthropic.py | 2 +- textgrad/engine/engine_utils.py | 16 ++++++++++++++++ textgrad/engine/openai.py | 3 ++- 4 files changed, 19 insertions(+), 20 deletions(-) create mode 100644 textgrad/engine/engine_utils.py diff --git a/textgrad/engine/__init__.py b/textgrad/engine/__init__.py index 5f3b1f8..0625168 100644 --- a/textgrad/engine/__init__.py +++ b/textgrad/engine/__init__.py @@ -51,21 +51,3 @@ def get_engine(engine_name: str, **kwargs) -> EngineLM: return ChatCohere(model_string=engine_name, **kwargs) else: raise ValueError(f"Engine {engine_name} not supported") - - -def is_jpeg(data): - jpeg_signature = b'\xFF\xD8\xFF' - return data.startswith(jpeg_signature) - -def is_png(data): - png_signature = b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A' - return data.startswith(png_signature) - - -def get_image_type_from_bytes(data): - if is_jpeg(data): - return "jpeg" - elif is_png(data): - return "png" - else: - raise ValueError("Image type not supported, only jpeg and png supported.") \ No newline at end of file diff --git a/textgrad/engine/anthropic.py b/textgrad/engine/anthropic.py index 112db70..bf266c8 100644 --- a/textgrad/engine/anthropic.py +++ b/textgrad/engine/anthropic.py @@ -14,7 +14,7 @@ import json from typing import List, Union from .base import EngineLM, CachedEngine -from textgrad.engine import get_image_type_from_bytes +from .engine_utils import get_image_type_from_bytes class ChatAnthropic(EngineLM, CachedEngine): SYSTEM_PROMPT = "You are a helpful, creative, and smart assistant." diff --git a/textgrad/engine/engine_utils.py b/textgrad/engine/engine_utils.py new file mode 100644 index 0000000..51caff9 --- /dev/null +++ b/textgrad/engine/engine_utils.py @@ -0,0 +1,16 @@ +def is_jpeg(data): + jpeg_signature = b'\xFF\xD8\xFF' + return data.startswith(jpeg_signature) + +def is_png(data): + png_signature = b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A' + return data.startswith(png_signature) + + +def get_image_type_from_bytes(data): + if is_jpeg(data): + return "jpeg" + elif is_png(data): + return "png" + else: + raise ValueError("Image type not supported, only jpeg and png supported.") \ No newline at end of file diff --git a/textgrad/engine/openai.py b/textgrad/engine/openai.py index 49f2219..a6d10ec 100644 --- a/textgrad/engine/openai.py +++ b/textgrad/engine/openai.py @@ -13,9 +13,10 @@ wait_random_exponential, ) from typing import List, Union -from textgrad.engine import get_image_type_from_bytes from .base import EngineLM, CachedEngine +from .engine_utils import get_image_type_from_bytes + class ChatOpenAI(EngineLM, CachedEngine): DEFAULT_SYSTEM_PROMPT = "You are a helpful, creative, and smart assistant."