Grok 4.1 Fast· xai
Frontier Reasoning Traces
Write a Python program to convert speech to text using SpeechRecognition.
- User
Write a Python program to convert speech to text using SpeechRecognition.
- Assistant
# Speech-to-Text Converter using SpeechRecognition library # # Prerequisites: # 1. Install the library: pip install SpeechRecognition # 2. For microphone input: pip install pyaudio # 3. This uses Google's free speech recognition service (requires internet). import speech_recognition as sr def speech_to_text(): # Initialize the recognizer r = sr.Recognizer() # Use the default microphone as the audio source with sr.Microphone() as source: print("Adjusting for ambient noise... Please wait.") # Adjust for ambient noise (optional but recommended) r.adjust_for_ambient_noise(source, duration=1) print("Say something! (Speak clearly and listen for the beep.)") # Listen for audio with a timeout and phrase time limit audio = r.listen(source, timeout=5, phrase_time_limit=5) try: # Recognize speech using Google Speech Recognition (free, requires internet) text = r.recognize_google(audio) print(f"You said: {text}") return text except sr.UnknownValueError: print("Sorry, I could not understand the audio.") return None except sr.RequestError as e: print(f"Error with the speech recognition service: {e}") return None # Run the function if __name__ == "__main__": speech_to_text()How to Run:
- Save this as
speech_to_text.py. - Run it:
python speech_to_text.py. - Speak into your microphone when prompted. It will transcribe and print the text.
Notes:
- Internet required: Uses Google's Web Speech API.
- Customization:
- Change
recognize_google(audio, language='en-US')for other languages. - For offline use, consider alternatives like
recognize_sphinx(installpocketsphinx). - To loop continuously: Wrap in
while True:withinput("Press Enter to listen again...").
- Change
- Troubleshooting: Ensure your microphone works and PyAudio is installed (may need build tools on some systems).
- Save this as
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.