← back to invincible-jha__aumai-modality

Function bodies 14 total

All specs Real LLM only Function bodies
convert_command function · python · L49-L96 (48 LOC)
src/aumai_modality/cli.py
def convert_command(
    input_path: str,
    target_modality: str,
    source_modality: str | None,
    output_path: str | None,
) -> None:
    """Convert a file from one modality to another."""
    raw_bytes = Path(input_path).read_bytes()

    router = ModalityRouter()
    if source_modality:
        detected = Modality(source_modality)
    else:
        detected = router.detect(raw_bytes)
        click.echo(f"Detected source modality: {detected.value}", err=True)

    modal_input = ModalInput(
        modality=detected,
        content=raw_bytes,
        mime_type=_guess_mime(input_path, detected),
    )

    converter = ModalityConverter()
    target = Modality(target_modality)

    try:
        result = converter.convert(modal_input, target)
    except ValueError as exc:
        click.echo(f"Error: {exc}", err=True)
        sys.exit(1)

    output_content = (
        result.output.content
        if isinstance(result.output.content, str)
        else result.output.content.decode(
detect_command function · python · L107-L112 (6 LOC)
src/aumai_modality/cli.py
def detect_command(input_path: str) -> None:
    """Detect the modality of a file."""
    raw_bytes = Path(input_path).read_bytes()
    router = ModalityRouter()
    detected = router.detect(raw_bytes)
    click.echo(f"Detected modality: {detected.value}")
_guess_mime function · python · L115-L129 (15 LOC)
src/aumai_modality/cli.py
def _guess_mime(path: str, modality: Modality) -> str:
    """Guess MIME type from file extension and modality."""
    ext = Path(path).suffix.lower()
    mime_map: dict[str, str] = {
        ".json": "application/json",
        ".txt": "text/plain",
        ".md": "text/markdown",
        ".png": "image/png",
        ".jpg": "image/jpeg",
        ".jpeg": "image/jpeg",
        ".mp3": "audio/mpeg",
        ".wav": "audio/wav",
        ".mp4": "video/mp4",
    }
    return mime_map.get(ext, "application/octet-stream")
TextHandler.handle method · python · L53-L63 (11 LOC)
src/aumai_modality/core.py
    def handle(self, input_data: ModalInput) -> ModalOutput:
        content = (
            input_data.content.decode("utf-8")
            if isinstance(input_data.content, bytes)
            else input_data.content
        )
        return ModalOutput(
            modality=Modality.text,
            content=content,
            mime_type="text/plain",
        )
TextHandler.to_text method · python · L65-L71 (7 LOC)
src/aumai_modality/core.py
    def to_text(self, input_data: ModalInput) -> str:
        content = (
            input_data.content.decode("utf-8")
            if isinstance(input_data.content, bytes)
            else input_data.content
        )
        return str(content)
TextHandler.from_text method · python · L73-L78 (6 LOC)
src/aumai_modality/core.py
    def from_text(self, text: str) -> ModalOutput:
        return ModalOutput(
            modality=Modality.text,
            content=text,
            mime_type="text/plain",
        )
StructuredHandler.handle method · python · L95-L111 (17 LOC)
src/aumai_modality/core.py
    def handle(self, input_data: ModalInput) -> ModalOutput:
        raw = (
            input_data.content.decode("utf-8")
            if isinstance(input_data.content, bytes)
            else input_data.content
        )
        # Normalize: ensure the content is valid JSON
        try:
            parsed: Any = json.loads(str(raw))
            serialized = json.dumps(parsed, ensure_ascii=False, indent=2)
        except (json.JSONDecodeError, TypeError):
            serialized = json.dumps({"text": str(raw)}, ensure_ascii=False, indent=2)
        return ModalOutput(
            modality=Modality.structured,
            content=serialized,
            mime_type="application/json",
        )
Same scanner, your repo: https://repobility.com — Repobility
StructuredHandler.to_text method · python · L113-L124 (12 LOC)
src/aumai_modality/core.py
    def to_text(self, input_data: ModalInput) -> str:
        """Serialize structured content to a JSON string."""
        raw = (
            input_data.content.decode("utf-8")
            if isinstance(input_data.content, bytes)
            else str(input_data.content)
        )
        try:
            parsed = json.loads(raw)
            return json.dumps(parsed, ensure_ascii=False, indent=2)
        except (json.JSONDecodeError, TypeError):
            return raw
StructuredHandler.from_text method · python · L126-L136 (11 LOC)
src/aumai_modality/core.py
    def from_text(self, text: str) -> ModalOutput:
        """Wrap plain text as a JSON object."""
        try:
            parsed: Any = json.loads(text)
        except (json.JSONDecodeError, ValueError):
            parsed = {"text": text}
        return ModalOutput(
            modality=Modality.structured,
            content=json.dumps(parsed, ensure_ascii=False, indent=2),
            mime_type="application/json",
        )
ModalityConverter.__init__ method · python · L160-L166 (7 LOC)
src/aumai_modality/core.py
    def __init__(
        self,
        handlers: dict[Modality, ModalityHandler] | None = None,
    ) -> None:
        self._handlers: dict[Modality, ModalityHandler] = (
            dict(handlers) if handlers else dict(_HANDLER_REGISTRY)
        )
ModalityConverter.convert method · python · L172-L209 (38 LOC)
src/aumai_modality/core.py
    def convert(
        self, input_data: ModalInput, target: Modality
    ) -> ConversionResult:
        """
        Convert *input_data* to *target* modality.

        Raises ``ValueError`` if source or target modality has no handler.
        """
        source = input_data.modality
        source_handler = self._handlers.get(source)
        target_handler = self._handlers.get(target)

        if source_handler is None:
            raise ValueError(
                f"No handler registered for source modality {source.value!r}."
            )
        if target_handler is None:
            raise ValueError(
                f"No handler registered for target modality {target.value!r}."
            )

        if source == target:
            output = source_handler.handle(input_data)
            quality = 1.0
        else:
            # Two-step: source -> text -> target
            intermediate_text = source_handler.to_text(input_data)
            output = target_handler.from_text(inter
ModalityRouter.__init__ method · python · L228-L234 (7 LOC)
src/aumai_modality/core.py
    def __init__(
        self,
        handlers: dict[Modality, ModalityHandler] | None = None,
    ) -> None:
        self._handlers: dict[Modality, ModalityHandler] = (
            dict(handlers) if handlers else dict(_HANDLER_REGISTRY)
        )
ModalityRouter.route method · python · L236-L243 (8 LOC)
src/aumai_modality/core.py
    def route(self, input_data: ModalInput) -> ModalOutput:
        """Dispatch *input_data* to its modality handler."""
        handler = self._handlers.get(input_data.modality)
        if handler is None:
            raise ValueError(
                f"No handler registered for modality {input_data.modality.value!r}."
            )
        return handler.handle(input_data)
ModalityRouter.detect method · python · L245-L265 (21 LOC)
src/aumai_modality/core.py
    def detect(self, raw_content: bytes | str) -> Modality:
        """
        Heuristically detect the modality of *raw_content*.

        Rules (in order):
        1. If it is valid JSON -> structured
        2. Otherwise -> text
        """
        text = (
            raw_content.decode("utf-8", errors="replace")
            if isinstance(raw_content, bytes)
            else raw_content
        )
        stripped = text.strip()
        if stripped and stripped[0] in ("{", "["):
            try:
                json.loads(stripped)
                return Modality.structured
            except (json.JSONDecodeError, ValueError):
                pass
        return Modality.text