Write a Python script to load an open-source model and use it for text generation while tweaking hyperparameters.
Asked in the Technical Round 1 stage. For deterministic grading, represent the loaded model as a JSON checkpoint whose transitions map each token to candidate next-token logits. Return only generated text, excluding the prompt.
Implement generate_text(model_json, prompt, max_new_tokens, temperature, top_p). The JSON string contains a transitions object. Select tokens using temperature-scaled logits and a top_p nucleus, choosing the highest-probability remaining token at each step. Use <BOS> for an empty prompt and stop when no transition exists.
def generate_text(model_json, prompt, max_new_tokens, temperature, top_p):