Skip to content

Commit

Permalink
move the utilities from __init__ to a new file
Browse files Browse the repository at this point in the history
  • Loading branch information
mertyg committed Jul 7, 2024
1 parent d09c713 commit 19fe2df
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
18 changes: 0 additions & 18 deletions textgrad/engine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
2 changes: 1 addition & 1 deletion textgrad/engine/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
16 changes: 16 additions & 0 deletions textgrad/engine/engine_utils.py
Original file line number Diff line number Diff line change
@@ -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.")
3 changes: 2 additions & 1 deletion textgrad/engine/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down

0 comments on commit 19fe2df

Please sign in to comment.