Safety issues with exec or subproccess

I am doing a small college project. The aim is to make a web application for generating python code with chatGPT model. Also this code should also be executed in Streamlit web app. Currently I am executing generated code locally via subproccesses, but I came across an idea of using exec or eval. There are a lot of concerns about the security issues. What are the possible solutions to limit the environment or creating some kind of a sandbox to safely launch generated code though exec?