Drilling accidents prediction is the important task in well construction. Drilling support software allows observing the drilling parameters for multiple wells at the same time and artificial intelligence helps detecting the drilling accident predecessor ahead the emergency situation. We present machine learning (ML) algorithm for prediction of such accidents as stuck, mud loss, fluid show, washout, break of drill string and shale collar. The model for forecasting the drilling accidents is based on the "Bag-of-features" approach, which implies the use of distributions of the directly recorded data as the main features. Bag-of-features implies the labeling of small parts of data by the particular symbol, named codeword. Building histograms of symbols for the data segment, one could use the histogram as an input for the machine learning algorithm. Fragments of real-time mud log data were used to create the model. We define more than 1000 drilling accident predecessors for more than 60 real accidents and about 2500 normal drilling cases as a training set for ML model. The developed model analyzes real-time mud log data and calculates the probability of accident. The result is presented as a probability curve for each type of accident; if the critical probability value is exceeded, the user is notified of the risk of an accident. The Bag-of-features model shows high performance by validation both on historical data and in real time. The prediction quality does not vary field to field and could be used in different fields without additional training of the ML model. The software utilizing the ML model has microservice architecture and is integrated with the WITSML data server. It is capable of real-time accidents forecasting without human intervention. As a result, the system notifies the user in all cases when the situation in the well becomes similar to the pre-accident one, and the engineer has enough time to take the necessary actions to prevent an accident.