Add KeyboardInterrupt in generate()