For an application I need to generate some random sentences, i.e. I don't need the output sentences to have any specific link to the prompt other than using the same language. If possible I need this process to be multilingual, i.e. to accept and generate in as many languages as possible. I also need to be able to run this process as many times as I want, this is why a dataset isn't suitable (fixed size).

I tried using mT5 which seems to be the most suitable model for my requirements, right? but I can't get anything out of it. Is it because it needs to be fine-tuned, as I read in different places? If so, any advice how I do this in my case?

Ideally I would also need this not to require too much computation, even at the cost of grammaticality/meaning of sentences.

  • 1
    $\begingroup$ Besides the fact that the generated sentence must match the prompt's language, are there other requirements? Does it need to be a specific length? Otherwise, datasets like tatoeba (huggingface.co/datasets/tatoeba) or Wikipedia (huggingface.co/datasets/wikimedia/wikipedia) might be large enough and cover enough languages for your use case $\endgroup$ Dec 29, 2023 at 9:41
  • 1
    $\begingroup$ @ValentinCalomme thanks for the advice, I might use this. Actually the full application requires generating sentences until a condition is satisfied, this is why I'd prefer generating from a model. $\endgroup$
    – Erwan
    Dec 29, 2023 at 11:52

1 Answer 1


There are a few options there:

  • Different models on HuggingFace

GPT2 should fit your use case. If performance is really critical, I'd give distilGPT2 a try. They only generate English sentences but you should be able to translate things well using something like T5.

  • "Manual" generation

NLTK provides a way to generate sentences using context-free grammar. It might be more time consuming, and might not yield sentences that make sense, but you would definitely do great on performance.

from nltk import CFG
from nltk.parse.generate import generate

grammar = CFG.fromstring(
  S -> NP VP
  VP -> V NP | V NP PP
  PP -> P NP
  V -> "saw" | "ate" | "walked"
  NP -> "John" | "Mary" | "Bob" | Det N | Det N PP
  Det -> "a" | "an" | "the" | "my"
  N -> "man" | "dog" | "cat" | "telescope" | "park"
  P -> "in" | "on" | "by" | "with"

# Generating sentences
for sentence in generate(grammar, n=5):
    print(" ".join(sentence))
  • Build your own model

Back in 2015, I saw this article: https://karpathy.github.io/2015/05/21/rnn-effectiveness/ from Andrej Karpathy. I played around with it and realized that it was relatively easy to generate reasonable text using only a very small RNN. You'd need training data, but your model would be much smaller than a GPT2 or an mT5


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.