About using inference API of this model

#10
by sanjitaa - opened

I am trying to use inference API of this mbart50 model but I am not able to pass target language code in query. I want to translate text to required target language. How can I acheive this through inference API. Can someone help me with it?

Regards,
Sanjita

Please help me out.

Hey @sanjitaa I believe the model is incorrectly deployed using Text2TextGeneration instead of Translation, which means that it currently cannot be used through the InferenceApi with specific target languages.

I'm updating it here: https://huggingface.co/facebook/mbart-large-50-many-to-many-mmt/discussions/11

In the meantime, here's how you can already use it:

import json
import requests

headers = {"Authorization": f"Bearer xxx"}
API_URL = "https://api-inference.huggingface.co/pipeline/translation/facebook/mbart-large-50-many-to-many-mmt"

def query(payload):
    data = json.dumps(payload)
    response = requests.request("POST", API_URL, headers=headers, data=data)
    return json.loads(response.content.decode("utf-8"))

data = query({"inputs": "My name is Sarah Jessica Parker but you can call me Jessica", "parameters": {"src_lang": "en_XX", "tgt_lang": "fr_XX"}})

print(data)

This returns

[{'translation_text': "Mon nom est Sarah Jessica Parker mais vous pouvez m'appeler Jessica"}]

The language codes can be found in the README.md file. Make sure you change "xxx" to your personal token!

@lysandre Thank you.
Is there a restriction in length of input text for this model ? I have a problem in translation of long input texts.

I believe there is a limit of 1024 tokens given the max_position_embeddings field in the config.json

@lysandre Thank you.
but how to achieve this . Is there any other approach for it ? Is there a direct way so that we can translate text of any size ?

Models such as MBART have a maximum size, unfortunately. You could split it into smaller texts and translate in batches. Some models, like the Longformer, can translate very large texts; unfortunately, it is not the case for this model.

@lysandre Can I use this API in production also ? I am using it in my company's project so is it suitable ?

@lysandre Also how can I pass the texts in chunks in the same inference API because this model does not translate long input texts.

I invite you to read the documentation of the inference API, available here: https://huggingface.co/docs/api-inference/quicktour
It contains answers to your questions :)

Thanks!

Sign up or log in to comment