Network integration is common in cyber-physical systems (CPS) to allow for remote access, surveillance, and analysis. They have been exposed to cyberattacks because of their integration with an insecure network. In the event of a violation in internet security, an attacker was able to interfere with the system's functions, which might result in catastrophic consequences. As a result, detecting breaches into mission-critical CPS is a top priority. Detecting assaults on CPSs, which are increasingly being targeted by cyber criminals and cyber threats, is becoming increasingly difficult. It is potential that (AI) Artificial Intelligence as well as (ML) Machine Learning will make this the worst of times, but it also has the potential to be the best of times. There are a variety of ways in which AI technology can aid in the growth and profitability of a variety of industries. Such data can be parsed using ML and AI approaches in designed to check attacks on CPSs. We present the new framework for the detection of cyberattacks, which makes use of AI and ML. We begin a process to cleaning up the data in the CPS database by applying normalization to eliminate errors and duplication. The features are obtained by using a technique known as Linear Discriminant Analysis (LDA). We have suggested the SFL-HMM together with HMS-ACO process as a method used for detection of the cyberattacks. A MATLAB simulation used to evaluate the new strategy, and the metrics obtained from that simulation are compared to those obtained from the older methods. According to the findings of several studies, the framework is significantly more effective than conventional methods in maintaining high levels of privacy. In addition, the framework outperforms conventional detection algorithms in words of detection rate, the rate of the false positive, and calculation time, respectively.