It looks like from the code that you are using speech_recognition module but you are missing the import. Include the below-mentioned line at the beginning of your script and see if it works:
import speech_recognition as sr
To know more about this you can have a look at the following video:-