Adding a custom function in the python bindings

I am trying to add a function to the python API that would allow me to pass a reference to a buffer and an integer.
I have defined the function in native_client/deepspeech.h as follows

/**
 * @brief Return Logits
 *
 * @param aSctx A streaming state pointer returned by {@link DS_CreateStream()}.
 * @param buf Buffer where logits will be stored
 * @param numLostFrames Number of frames lost
 * @param numElements The number of logits to return
 * 
 * @return Number of logit frames that were lost.
 */
DEEPSPEECH_EXPORT
int DS_FetchLogits(const StreamingState* aSctx,
                   vector< vector<float> >* buf,
                   int* numLostFrames,
                   int numElements);

The corresponding function definition in native_client/python/__init__.py is

class Stream(object):

    ...
    ...

    def fetchLogits(self, cpm_buffer, num_lost_frames, num_elements):
        if not self._impl:
            raise RuntimeError("Stream object is not valid. Trying to decode an already finished stream?")
        return deepspeech.impl.FetchLogits(self._impl, cpm_buffer, num_lost_frames, num_elements)

I put the function definition in native_client/python/impl.i by adding the line

%newobject DS_FetchLogits;

What else do I have to add in order to make the function available as a part of the python API?

I think you got it covered completely. I would suggest to copy/inspire from our other API entry points, to make sure you miss nothing, but given what you shared I suspect this is what you did already.

That might require some special SWIG-level magic

1 Like

Like, for reference, this is what @josh_meyer did for his Hot Word PR: enable hot-word boosting (#3297) · mozilla/DeepSpeech@1eb155e · GitHub

That’s on the ctcdecoder bindings, but the idea is likely the same.

1 Like

Thanks for the help. So the only thing left is to map this in SWIG right?
Because currently, when I build the Python API and use it, I get this error

>>> import deepspeech
Segmentation fault (Core Dumped).

Again, I can’t tell for sure, what you have looks right, but that’s all I can say.

Without a stack and more details, hard to do anything. You need to gdb it to get more insights.

1 Like

Alright. Thanks for all the help.