# `TFLiteElixir.FlatBufferModel`
[🔗](https://github.com/cocoa-xu/tflite_elixir/blob/main/lib/tflite_elixir/flatbuffer_model.ex#L1)

An RAII object that represents a read-only tflite model, copied from disk, or
mmapped.

# `nif_error`

```elixir
@type nif_error() :: {:error, String.t()}
```

# `build_from_buffer`

```elixir
@spec build_from_buffer(binary(), Keyword.t()) ::
  %TFLiteElixir.FlatBufferModel{
    initialized: term(),
    minimum_runtime: term(),
    model: term()
  }
  | nif_error()
```

Build model from caller owned memory buffer

Note that `buffer` will be copied.

# `build_from_file`

```elixir
@spec build_from_file(String.t(), Keyword.t()) ::
  %TFLiteElixir.FlatBufferModel{
    initialized: term(),
    minimum_runtime: term(),
    model: term()
  }
  | nif_error()
```

Build model from a given .tflite file

Note that if the tensorflow-lite library was compiled with `TFLITE_MCU`,
then this function will always have return type `nif_error()`.

##### Keyword parameters
- `error_reporter`: `TFLiteElixir.ErrorReporter`.

  Caller retains ownership of `error_reporter` and must ensure its lifetime
  is longer than the FlatBufferModel instance.

# `build_from_file!`

```elixir
@spec build_from_file!(String.t(), Keyword.t()) :: %TFLiteElixir.FlatBufferModel{
  initialized: term(),
  minimum_runtime: term(),
  model: term()
}
```

Same as `build_from_file/2`, but raises rather than returning `{:error, reason}`.

# `error_reporter`

```elixir
@spec error_reporter(%TFLiteElixir.FlatBufferModel{
  initialized: term(),
  minimum_runtime: term(),
  model: reference()
}) :: %TFLiteElixir.ErrorReporter{ref: term()} | {:error, String.t()}
```

Get the error report of the current FlatBuffer model.

# `get_associated_file`

```elixir
@spec get_associated_file(binary(), [String.t()] | String.t()) ::
  %{required(String.t()) =&gt; String.t() | nif_error()} | String.t() | nif_error()
```

Get associated file(s) from a FlatBuffer model

# `get_minimum_runtime`

```elixir
@spec get_minimum_runtime(%TFLiteElixir.FlatBufferModel{
  initialized: term(),
  minimum_runtime: term(),
  model: term()
}) :: String.t() | nif_error()
```

Returns the minimum runtime version from the flatbuffer. This runtime
version encodes the minimum required interpreter version to run the
flatbuffer model. If the minimum version can't be determined, an empty
string will be returned.

Note that the returned minimum version is a lower-bound but not a strict
lower-bound; ops in the graph may not have an associated runtime version,
in which case the actual required runtime might be greater than the
reported minimum.

# `initialized`

```elixir
@spec initialized(%TFLiteElixir.FlatBufferModel{
  initialized: term(),
  minimum_runtime: term(),
  model: term()
}) :: boolean() | nif_error()
```

Check whether current model has been initialized

# `list_associated_files`

```elixir
@spec list_associated_files(binary()) :: [String.t()] | nif_error()
```

Get a list of all associated file(s) in a TFLite model file

# `read_all_metadata`

```elixir
@spec read_all_metadata(%TFLiteElixir.FlatBufferModel{
  initialized: term(),
  minimum_runtime: term(),
  model: term()
}) :: %{required(String.t()) =&gt; String.t()} | nif_error()
```

Return model metadata as a mapping of name & buffer strings.

See Metadata table in TFLite schema.

# `verify_and_build_from_buffer`

```elixir
@spec verify_and_build_from_buffer(binary(), Keyword.t()) ::
  %TFLiteElixir.FlatBufferModel{
    initialized: term(),
    minimum_runtime: term(),
    model: term()
  }
  | :invalid
  | nif_error()
```

Verify whether the buffer is a legit flatbuffer, then build a model from it

Returns `:invalid` if the buffer does not verify. Note that `buffer` will be copied.

# `verify_and_build_from_file`

```elixir
@spec verify_and_build_from_file(String.t(), Keyword.t()) ::
  %TFLiteElixir.FlatBufferModel{
    initialized: term(),
    minimum_runtime: term(),
    model: term()
  }
  | :invalid
  | {:error, String.t()}
```

Verifies whether the content of the file is legit, then builds a model
based on the file.

##### Keyword parameters
- `error_reporter`: `TFLiteElixir.ErrorReporter`.

  Caller retains ownership of `error_reporter` and must ensure its lifetime
  is longer than the FlatBufferModel instance.

Returns `:invalid` in case of failure.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
