I don't think I'm getting it right, but isn't it just a matter of checking the character set in the query and determining if it belongs to English or Devnagri part of Unicode and applying transliteration if the query is in English.
If you are looking for a self hosted Devnagri transliteration tool, look into AI4Bharat, a tool developed by IIT Madras.
Did you know there is an extended version of it that exists too. First time I read about it, they talked about how the abridged, director's cut was much better. After watching it a few times, i gave in and watched the extended version and liked it as much too.