Skip to content

Commit

Permalink
fix: make Studio compatible with Pydantic 2 and thus g2p 2
Browse files Browse the repository at this point in the history
Fixes #188
  • Loading branch information
joanise committed Sep 13, 2023
1 parent fba7b3a commit eb03934
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 30 deletions.
55 changes: 29 additions & 26 deletions readalongs/web_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class InputFormat(Enum):
class AssembleRequest(BaseModel):
"""Base request for assemble"""

input_text: str = Field(None, alias="input")
mime_type: InputFormat = Field(None, alias="type")
input_text: Optional[str] = Field(None, alias="input")
mime_type: Optional[InputFormat] = Field(None, alias="type")
text_languages: List[str]
debug: bool = False

Expand All @@ -105,10 +105,10 @@ class AssembleResponse(BaseModel):
text_ids: str # The text ID input for the decoder in plain text
processed_ras: str # The processed RAS XML is returned as a string
input_request: Optional[AssembleRequest] = Field(None, alias="input")
parsed: Optional[str]
tokenized: Optional[str]
g2ped: Optional[str]
log: Optional[str]
parsed: Optional[str] = None
tokenized: Optional[str] = None
g2ped: Optional[str] = None
log: Optional[str] = None


class SupportedLanguage(BaseModel):
Expand Down Expand Up @@ -144,26 +144,28 @@ async def langs() -> List[SupportedLanguage]:
@v1.post("/assemble", response_model=AssembleResponse)
async def assemble(
request: AssembleRequest = Body(
examples={
"text": {
"summary": "A basic example with plain text input",
"value": {
"input": "hej verden",
"type": "text/plain",
"text_languages": ["dan", "und"],
"debug": False,
examples=[
{
"text": {
"summary": "A basic example with plain text input",
"value": {
"input": "hej verden",
"type": "text/plain",
"text_languages": ["dan", "und"],
"debug": False,
},
},
},
"xml": {
"summary": "A basic example with xml input",
"value": {
"input": "<?xml version='1.0' encoding='utf-8'?><read-along version=\"1.0\"><text><p><s>hej verden</s></p></text></read-along>",
"type": "application/readalong+xml",
"text_languages": ["dan", "und"],
"debug": False,
"xml": {
"summary": "A basic example with xml input",
"value": {
"input": "<?xml version='1.0' encoding='utf-8'?><read-along version=\"1.0\"><text><p><s>hej verden</s></p></text></read-along>",
"type": "application/readalong+xml",
"text_languages": ["dan", "und"],
"debug": False,
},
},
},
}
}
]
)
):
"""Create an input RAS from the given text (as plain text or XML).
Expand All @@ -190,7 +192,7 @@ async def assemble(
if request.mime_type == InputFormat.RAS:
try:
parsed = etree.fromstring(
bytes(request.input_text, encoding="utf-8"),
bytes(request.input_text or "", encoding="utf-8"),
parser=etree.XMLParser(resolve_entities=False),
)
except etree.ParseError as e:
Expand Down Expand Up @@ -271,7 +273,7 @@ def create_grammar(xml):
class WordAlignment(BaseModel):
"""Word alignment extracted from RAS"""

word_id: str = Field(None, alias="id")
word_id: Optional[str] = Field(None, alias="id")
start: float
end: float

Expand Down Expand Up @@ -317,6 +319,7 @@ class ConvertRequest(BaseModel):
example=2.01,
gt=0.0,
title="The duration of the audio used to create the alignment, in seconds.",
default=None,
)

ras: str = Field(
Expand Down
3 changes: 2 additions & 1 deletion requirements.dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ black~=22.0
flake8>=4.0.1
gitlint-core==0.17.0
isort>=5.10.1
mypy>=0.941
mypy>=1.4.1
mypy>=1.5.1; python_version >= "3.8"
pre-commit>=2.6.0
types-python-slugify>=5.0.3
types-pyyaml>=6.0.5
Expand Down
8 changes: 5 additions & 3 deletions requirements.min.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ anyio<4.0.0
chevron==0.14.0
click>=8.0.4
coloredlogs==10.0
fastapi==0.78.0
g2p>=1.1.20230822, ==1.*
fastapi>=0.78.0
# Note: when we move g2p to >=2, we will need fastapi>=0.100.0 which requires httpx
g2p>=1.1.20230822, <2.1
httpx>=0.24.1
lxml==4.9.1
networkx>=2.5,<=2.8.4
networkx>=2.6
numpy>=1.16.4
pydub==0.23.1
pympi-ling>=1.69,<2.0
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ensure_newline_before_comments=True

[mypy]
ignore_missing_imports = True
plugins = pydantic.mypy

[flake8]
ignore = E203, E266, E501, W503
Expand Down

0 comments on commit eb03934

Please sign in to comment.