From 94815b79181aaf550e99ee5a349e8f550489aadf Mon Sep 17 00:00:00 2001 From: stuartgo Date: Sun, 7 Dec 2025 15:05:18 +0100 Subject: [PATCH] first commit --- __init__.py | 0 enums.py | 10 ++++++++ pyproject.toml | 20 ++++++++++++++++ schemas.py | 52 +++++++++++++++++++++++++++++++++++++++++ structures/__init__.py | 0 structures/embedding.py | 18 ++++++++++++++ structures/lexeme.py | 22 +++++++++++++++++ structures/phrase.py | 26 +++++++++++++++++++++ structures/symbol.py | 10 ++++++++ structures/word.py | 23 ++++++++++++++++++ 10 files changed, 181 insertions(+) create mode 100644 __init__.py create mode 100644 enums.py create mode 100644 pyproject.toml create mode 100644 schemas.py create mode 100644 structures/__init__.py create mode 100644 structures/embedding.py create mode 100644 structures/lexeme.py create mode 100644 structures/phrase.py create mode 100644 structures/symbol.py create mode 100644 structures/word.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/enums.py b/enums.py new file mode 100644 index 0000000..46b8c26 --- /dev/null +++ b/enums.py @@ -0,0 +1,10 @@ +from enum import Enum +class ModelType(Enum): + EMBEDDING = "embedding" + TRANSCRIPTION = "transcription" + RESPONSE = "response" +class PreprocessorType(Enum): + NORWEGIAN = "norwegian" + DEFAULT = "norwegian" +class SymbolProvider(Enum): + TOBII_DYNAVOX = "tobii_dynavox" \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..b9268bd --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,20 @@ +[project] +name = "shared-types" +version = "0.1.0" +description = "Shared typing models for all services" +authors = [ + { name = "Your Team", email = "team@example.com" } +] +requires-python = ">=3.10" +readme = "README.md" +keywords = ["types", "schemas", "shared"] +classifiers = [ + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.10", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent" +] + +[build-system] +requires = ["setuptools>=65.5.1", "wheel"] +build-backend = "setuptools.build_meta" diff --git a/schemas.py b/schemas.py new file mode 100644 index 0000000..d34448c --- /dev/null +++ b/schemas.py @@ -0,0 +1,52 @@ +from pydantic import BaseModel as PydanticBaseModel +from typing import List, Any, Dict,Union +from structures import Embedding,Word, Symbol,Phrase + + + +class EmbedRequest(PydanticBaseModel): + stemmed_words: List[str] + tokens: dict +class EmbeddingResponse(PydanticBaseModel): + embeddings: List[Embedding] + + +class ResponseRequest(PydanticBaseModel): + text: str + context: Dict[str, Any] + + +class PreprocessRequest(PydanticBaseModel): + text: str + +class PreprocessResponse(PydanticBaseModel): + raw_words: List[str] + tokens: Dict[str, Any] + + +class GetSymbolRequest(PydanticBaseModel): + lexeme_id: int + +class GetSymbolResponse(PydanticBaseModel): + symbol: Symbol + +class GetWordRequest(PydanticBaseModel): + word_text: str + stemmed: bool = False + + +class GetWordResponse(PydanticBaseModel): + word:Union[Word,None] + +class GetSimilarWordRequest(PydanticBaseModel): + embedding: Embedding + +class GetAllPhrasesResponse(PydanticBaseModel): + phrases: List[Phrase] + + + + + + + diff --git a/structures/__init__.py b/structures/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/structures/embedding.py b/structures/embedding.py new file mode 100644 index 0000000..aeb56a3 --- /dev/null +++ b/structures/embedding.py @@ -0,0 +1,18 @@ +from __future__ import annotations +from dataclasses import dataclass +import torch + + +@dataclass(frozen=True) +class Embedding: + data: torch.Tensor + + def __post_init__(self): + if not isinstance(self.data, torch.Tensor): + raise TypeError(f"data must be a torch.Tensor, got {type(self.data)}") + + def compare(self, other: Embedding) -> float: + """ + Compare this embedding with another embedding. + """ + raise NotImplementedError("compare method must be implemented") diff --git a/structures/lexeme.py b/structures/lexeme.py new file mode 100644 index 0000000..082ae71 --- /dev/null +++ b/structures/lexeme.py @@ -0,0 +1,22 @@ +from abc import ABC, abstractmethod +from typing import List, Union +from dataclasses import dataclass +from .embedding import Embedding + + +@dataclass +class Lexeme(ABC): + id: int + + @property + @abstractmethod + def content(self) -> Union[str, List[str]]: + """Text content of the lexeme.""" + pass + + @property + @abstractmethod + def embedding(self) -> Embedding: + """Embedding(s) associated with the lexeme.""" + pass + diff --git a/structures/phrase.py b/structures/phrase.py new file mode 100644 index 0000000..ea5eee4 --- /dev/null +++ b/structures/phrase.py @@ -0,0 +1,26 @@ +from typing import List + +from .lexeme import Lexeme +from .embedding import Embedding +from .word import Word + + +class Phrase(Lexeme): + def __init__(self, id: int, content: str, embedding: Embedding): + self.id = id + self._content = content + self._embedding = embedding + + @property + def content(self) -> List[str]: + return self._content + + @property + def embedding(self) -> Embedding: + return self._embedding + + def __str__(self): + return self.content + + def __repr__(self): + return self.__str__() \ No newline at end of file diff --git a/structures/symbol.py b/structures/symbol.py new file mode 100644 index 0000000..4cfb4e8 --- /dev/null +++ b/structures/symbol.py @@ -0,0 +1,10 @@ +from dataclasses import dataclass +from typing import Optional +from PIL import Image + + +@dataclass +class Symbol: + id: int + label: str + image: Optional[Image.Image] diff --git a/structures/word.py b/structures/word.py new file mode 100644 index 0000000..5e4e791 --- /dev/null +++ b/structures/word.py @@ -0,0 +1,23 @@ +from .lexeme import Lexeme +from .embedding import Embedding + + +class Word(Lexeme): + def __init__(self, word_id: int, content: str, embedding: Embedding): + self.id = word_id + self._content = content + self._embedding = embedding + + @property + def content(self) -> str: + return self._content + + @property + def embedding(self) -> Embedding: + return self._embedding + + def __str__(self): + return self.content + + def __repr__(self): + return self.__str__()