Advanced Python for Humanities Students 28: Natural Language Processing 2 (Speech Recognition)

Advanced Python for Humanities Students 28: Natural Language Processing 2 (Speech Recognition)

~01~

Introduction

In the daily lives of humanities students, we are often troubled by the following questions:

※ How long does it take to transcribe a 3-hour oral history from a 90-year-old veteran?

※ How can we quickly extract keywords from folk rhymes scattered across 20 different dialects?

※ How can subtitles be synchronized with speech to ensure both timeliness and accuracy?

Speech recognition technology can convert audio into text in bulk, helping us solve the above problems. In fact, we often use speech recognition technology in our daily lives, such as voice interactions with delivery robots and speech-to-text features in social software. How can we utilize speech recognition technology to assist our research? The simplest method is to purchase professional software or hardware, but a more cost-effective and brain-training approach is to write a program ourselves.

Advanced Python for Humanities Students 28: Natural Language Processing 2 (Speech Recognition)

~02~

Implementation of Speech Recognition Code

There are various open-source models for speech recognition, and this article uses OpenAI’s Whisper model as the tool for speech recognition. For Mandarin Chinese speech recognition, the pre-trained Whisper model is already sufficiently good and does not require fine-tuning. The basic models of Whisper are categorized based on the number of parameters into Whisper-Tiny, Whisper-Small, Whisper-Base, Whisper-Medium, Whisper-Large, and a multilingual version (Whisper-Multilingual). Most of us use personal computers, and humanities students typically only consider document editing needs when purchasing computers, so the hardware configuration may not be suitable for running large models. Therefore, choosing the Whisper-Small model is sufficient.

Speech recognition can be implemented in two steps using Python code.First, install the necessary packages.Run the following command in the terminal:

pip install openai-whisper torch gradio pydub numpy

Second, write the code.

The code is quite simple, as shown below (assuming our goal is to recognize the content spoken in the audio file conversation1.m4a):

import whisper
# Load the model, which will usually download the specified model automatically. Please be patient until the download is complete.
model = whisper.load_model('small')

# Define the speech recognition function
def transcribe_audio_to_text():
    try:
        text = model.transcribe('conversation1.m4a', language='Chinese')
        print("The content spoken in the audio file is: " + text['text'])
        # Write the text to a txt file
        with open('output.txt', 'w', encoding='utf-8', errors='ignore') as f:
            f.write(text['text'])
    except Exception as e:
        print(f"An error occurred: {str(e)}")

if __name__ == "__main__":
    transcribe_audio_to_text()

Running the above code will output the content spoken in the audio file to the screen (see the image below), and it will also be written to a text file named output.txt, saved in the same directory as the program file.

Advanced Python for Humanities Students 28: Natural Language Processing 2 (Speech Recognition)Advanced Python for Humanities Students 28: Natural Language Processing 2 (Speech Recognition)

~03~

Conclusion

The above is the Python code for speech recognition.If you have previously paid for such services, seeing how simple these few lines of code can achieve bulk speech-to-text conversion might make you feel like you were overcharged. However, if you do not understand these, the troubles mentioned at the beginning of the article can indeed drive you crazy. Spending a few dollars to significantly increase your work efficiency is worth it. From now on, you can save that few dollars!

Delegating mechanical labor to algorithms allows researchers to focus their deep thinking on their studies, which is the essence of learning programming languages.

Advanced Python for Humanities Students 28: Natural Language Processing 2 (Speech Recognition)

Statement: Except for the cited sources, all images and text are original works by the author. Please provide accurate attribution when reproducing or quoting.

Note: Advanced content assumes mastery of basic syntax. If you are not yet familiar with the basic syntax, you can read the articles in the “First Round of Basic Syntax” collection of this account.

Leave a Comment