Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Pydantic typing #1104

Closed
wants to merge 2 commits into from
Closed

Add Pydantic typing #1104

wants to merge 2 commits into from

Conversation

extrange
Copy link

Hi, we are using this library extensively at our company, and internally we have added type validation with Pydantic. If this is useful, I can update the documentation as well.

@extrange extrange force-pushed the master branch 2 times, most recently from d2a962d to 3538dc5 Compare October 30, 2024 10:37
@MahmoudAshraf97
Copy link
Collaborator

Hello, I'm not well experienced with pydantic tbh, can you explain how this is useful or helps ease future maintainability? I know there is a lot of code duplication that this reduces but is there other benefits?

@extrange
Copy link
Author

extrange commented Oct 30, 2024

We have a use case where we need to serialize the output tuple[Iterable[Segment], TranscriptionInfo] into JSON, and then deserialize it back into a typed Python object/model.

Namedtuples do not natively allow nested deserialization. For example, using the following JSON:

segments, info = model.transcribe(audio, word_timestamps=True)
out = [s_asdict() for s in segments]
print(json.dumps(out, indent=2))
[
  {
    "id": 1,
    "seek": 2490,
    "start": 0.0,
    "end": 6.0,
    "text": " Ladies and gentlemen, thank you for being here and for your written representations.",
    "tokens": [
      50364,
      //...
    ],
    "avg_logprob": -0.26516544380608725,
    "compression_ratio": 1.597883597883598,
    "no_speech_prob": 0.01361083984375,
    "words": null,
    "temperature": 0.0
  },
//...
]

If we try to convert this back to a list[Segment], the words is not converted:

from faster_whisper.transcribe import Segment
[Segment(**s) for s in parsed]
Segment(
    id=1,
    seek=2490,
    start=0.0,
    end=4.62,
    text=" Ladies and gentlemen, thank you for being here and for your written representations.",
    tokens=[
        50364,
# ...
    ],
    avg_logprob=-0.26516544380608725,
    compression_ratio=1.597883597883598,
    no_speech_prob=0.01361083984375,
    words=[
        [0.0, 0.32, " Ladies", 0.310546875],
        [0.32, 0.54, " and", 0.89453125],
# ...
    ],
    temperature=0.0,
)

Currently, we are recursing through the Segment and TranscriptionInfo objects to convert them to Pydantic models (which natively allow nested deserialization), but it would be nice if the library supported this out of the box.

This should also fix #667.

@MahmoudAshraf97
Copy link
Collaborator

I see, but why not use dataclass? it supports recursive serialization too and will not add an extra dependency or change much in the code

@extrange
Copy link
Author

I think dataclasses are not serializable to JSON, at least not without another library:

import json
from dataclasses import dataclass


@dataclass
class Foo:
    x: str


foo = Foo(x="bar")
json.dumps(foo)

# TypeError: Object of type Foo is not JSON serializable

@extrange
Copy link
Author

extrange commented Nov 1, 2024

Closed in favour of #1105

@extrange extrange closed this Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants