-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic support for Dall-E image generation and Ollama image recogn…
…ition
- Loading branch information
Showing
9 changed files
with
133 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from domain.image import IrisImage | ||
from domain.message import IrisMessage, IrisMessageRole |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from datetime import datetime | ||
|
||
|
||
class IrisImage: | ||
prompt: str | ||
base64: str | ||
url: str | None | ||
timestamp: datetime | ||
_raw_data: any | ||
|
||
def __init__( | ||
self, | ||
prompt: str, | ||
base64: str, | ||
url: str, | ||
timestamp: datetime, | ||
raw_data: any = None, | ||
): | ||
self.prompt = prompt | ||
self.base64 = base64 | ||
self.url = url | ||
self.timestamp = timestamp | ||
self._raw_data = raw_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import base64 | ||
from datetime import datetime | ||
from typing import Literal, Any | ||
|
||
import requests | ||
from openai import OpenAI | ||
|
||
from domain import IrisImage | ||
from llm.wrapper.abstract_llm_wrapper import AbstractLlmWrapper | ||
|
||
|
||
class OpenAIDalleWrapper(AbstractLlmWrapper): | ||
type: Literal["openai_dalle"] | ||
model: str | ||
_client: OpenAI | ||
|
||
def model_post_init(self, __context: Any) -> None: | ||
self._client = OpenAI(api_key=self.api_key) | ||
|
||
def generate_images( | ||
self, | ||
prompt: str, | ||
n: int = 1, | ||
size: Literal[ | ||
"256x256", "512x512", "1024x1024", "1792x1024", "1024x1792" | ||
] = "256x256", | ||
quality: Literal["standard", "hd"] = "standard", | ||
**kwargs | ||
) -> [IrisImage]: | ||
response = self._client.images.generate( | ||
model=self.model, | ||
prompt=prompt, | ||
size=size, | ||
quality=quality, | ||
n=n, | ||
response_format="url", | ||
**kwargs | ||
) | ||
|
||
images = response.data | ||
iris_images = [] | ||
for image in images: | ||
if image.revised_prompt is None: | ||
image.revised_prompt = prompt | ||
if image.b64_json is None: | ||
image_response = requests.get(image.url) | ||
image.b64_json = base64.b64encode(image_response.content).decode( | ||
"utf-8" | ||
) | ||
|
||
iris_images.append( | ||
IrisImage( | ||
prompt=image.revised_prompt, | ||
base64=image.b64_json, | ||
url=image.url, | ||
timestamp=datetime.fromtimestamp(response.created), | ||
raw_data=image, | ||
) | ||
) | ||
|
||
return iris_images |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ black==24.1.1 | |
flake8==7.0.0 | ||
pre-commit==3.6.0 | ||
pydantic==2.6.1 | ||
requests==2.31.0 |