from transformers import GPT2Tokenizer, TFGPT2LMHeadModel import tensorflow as tf model_name = "gpt2" def load(): global model global tokenizer model = TFGPT2LMHeadModel.from_pretrained(model_name) tokenizer = GPT2Tokenizer.from_pretrained(model_name) def generate(input_text): # Tokenize the input text input_ids = tokenizer.encode(input_text, return_tensors="pt", truncation=True) # Generate output using the model output_ids = model.generate(input_ids, num_beams=3, no_repeat_ngram_size=2, max_new_tokens=200, eos_token_id=tokenizer.eos_token_id) return tokenizer.decode(output_ids[0], skip_special_tokens=True)