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

Transformer: Add TextGenPipeline + FeaturizePipeline #141

Open
Lundez opened this issue Aug 27, 2022 · 0 comments
Open

Transformer: Add TextGenPipeline + FeaturizePipeline #141

Lundez opened this issue Aug 27, 2022 · 0 comments

Comments

@Lundez
Copy link
Member

Lundez commented Aug 27, 2022

Some code I did during first implementation of Transformers,

class FeatureizeTranslator(val tokenizer: HuggingFaceTokenizerWrapper) :
    Translator<String, NDArray> {
    override fun processInput(ctx: TranslatorContext, input: String): NDList {
        val inputEncoding = tokenizer.encode(input)
        val ids = ctx.ndManager.create(inputEncoding.ids)
        val attention = ctx.ndManager.create(inputEncoding.attentionMask)

        return NDList(ids, attention)
    }

    override fun processOutput(ctx: TranslatorContext, list: NDList): NDArray {
        return list[0]
    }
}
class FeaturizePipeline(model: Criteria<String, NDArray>) : TransformerPipeline<String, NDArray>(model) {
    companion object {
        fun create(
            modelName: String,
            engine: Engine = Engine.ONNX,
            device: Device = Device.cpu(),
        ): TransformerPipeline<String, NDArray> {
            val model = HuggingFaceModelHub.load(modelName, engine)
            val translator = FeatureizeTranslator(HuggingFaceTokenizerWrapper(modelName))
            val criteria = baseCriteria(translator, model.localPath, engine, device)

            return FeaturizePipeline(criteria)
        }

        @JvmStatic
        fun main(args: Array<String>) {
            create("optimum/bert-base-NER").use { pipeline ->
                println(pipeline.predict("My name is Sarah and I live in London"))
            }
        }
    }
}
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

No branches or pull requests

1 participant