Source code for utils

import base64
import contextlib
import inspect
import io
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
    import numpy as np


[docs] def partial_format(value: str, **formats: dict[str, Any]) -> str: """Partially format a string given a variable amount of formats.""" for template_key, template_value in formats.items(): with contextlib.suppress(KeyError): value = value.format(**{template_key: template_value}) return value
[docs] def encode_image_to_base64(img: "np.ndarray") -> str: """Encode an image to a base64 string, to be included as an image_url in a Message.""" try: from PIL import Image except ImportError as e: raise ImportError( "Image processing requires the 'image' extra for 'Pillow'. Please:" " `pip install aviary[image]`." ) from e image = Image.fromarray(img) buffer = io.BytesIO() image.save(buffer, format="PNG") return ( f"data:image/png;base64,{base64.b64encode(buffer.getvalue()).decode('utf-8')}" )
[docs] def is_coroutine_callable(obj) -> bool: """Get if the input object is awaitable.""" if inspect.isfunction(obj) or inspect.ismethod(obj): return inspect.iscoroutinefunction(obj) if callable(obj): return inspect.iscoroutinefunction(obj.__call__) return False