-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from JohannesHa/qa-endpoint
Qa endpoint
- Loading branch information
Showing
8 changed files
with
504 additions
and
16 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
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,3 +1,4 @@ | ||
from .aleph_alpha_client import AlephAlphaClient, QuotaError, POOLING_OPTIONS | ||
from .image import ImagePrompt | ||
from .utils import load_base64_from_url, load_base64_from_file | ||
from .document import Document |
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,55 @@ | ||
import base64 | ||
from typing import Dict, List, Union | ||
|
||
from aleph_alpha_client.image import ImagePrompt | ||
from aleph_alpha_client.prompt_item import _to_prompt_item | ||
|
||
|
||
class Document: | ||
""" | ||
A document that can be either a docx document or text/image prompts. | ||
""" | ||
|
||
def __init__(self, docx: str = None, prompt: List[Union[str, ImagePrompt]] = None): | ||
# We use a base_64 representation for docx documents, because we want to embed the file | ||
# into a prompt send in JSON. | ||
self.docx = docx | ||
self.prompt = prompt | ||
|
||
@classmethod | ||
def from_docx_bytes(cls, bytes: bytes): | ||
""" | ||
Pass a docx file in bytes and prepare it to be used as a document | ||
""" | ||
docx_base64 = base64.b64encode(bytes).decode() | ||
return cls(docx=docx_base64) | ||
|
||
@classmethod | ||
def from_docx_file(cls, path: str): | ||
""" | ||
Load a docx file from disk and prepare it to be used as a document | ||
""" | ||
with open(path, "rb") as f: | ||
docx_bytes = f.read() | ||
return cls.from_docx_bytes(docx_bytes) | ||
|
||
@classmethod | ||
def from_prompt(cls, prompt: List[Union[str, ImagePrompt]]): | ||
""" | ||
Pass a prompt and prepare it to be used as a document | ||
""" | ||
return cls(prompt=prompt) | ||
|
||
def _to_serializable_document(self) -> Dict[str, str]: | ||
""" | ||
A dict if serialized to JSON is suitable as a document element | ||
""" | ||
if self.docx is not None: | ||
# Serialize docx to Document JSON format | ||
return { | ||
"docx": self.docx, | ||
} | ||
elif self.prompt is not None: | ||
# Serialize prompt to Document JSON format | ||
prompt_data = [_to_prompt_item(prompt_item) for prompt_item in self.prompt] | ||
return {"prompt": prompt_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from typing import Dict, Union | ||
|
||
from aleph_alpha_client.image import ImagePrompt | ||
|
||
|
||
def _to_prompt_item(item: Union[str, ImagePrompt]) -> Dict[str, str]: | ||
if isinstance(item, str): | ||
return {"type": "text", "data": item} | ||
if hasattr(item, "_to_prompt_item"): | ||
return item._to_prompt_item() | ||
else: | ||
raise ValueError( | ||
"The item in the prompt is not valid. Try either a string or an Image." | ||
) |
Binary file not shown.
Oops, something went wrong.