from transformers import pipeline | |
class ArabicTextGenerator: | |
def __init__(self): | |
self.model_loaded = False | |
def load_model(self): | |
if not self.model_loaded: | |
self.generator = pipeline( | |
'text-generation', | |
model='aubmindlab/aragpt2-base', | |
device=-1 # Force CPU | |
) | |
self.model_loaded = True | |
def generate(self, prompt, max_length=100): | |
self.load_model() | |
output = self.generator( | |
prompt, | |
max_length=max_length, | |
num_return_sequences=1, | |
pad_token_id=50256 # eos_token_id for aragpt2 | |
) | |
return output[0]['generated_text'] |