Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added file-saving option for live transcription. #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

fxjkhr
Copy link

@fxjkhr fxjkhr commented May 20, 2023

I asked gpt-4 for this feature =)


It seems like you would like the transcriptions to be written to the output file as they are being produced, rather than waiting until the end of the transcription process. Below is a modification of your script that includes this feature. This was achieved by opening the output file in 'append' mode ('a') and writing each line of transcription to the file as they are produced.

#! python3.7

import argparse
import io
import os
import speech_recognition as sr
import whisper
import torch

from datetime import datetime, timedelta
from queue import Queue
from tempfile import NamedTemporaryFile
from time import sleep
from sys import platform


def main():
    parser = argparse.ArgumentParser()
    # Same arguments as before...
    parser.add_argument("--output", default=None, help="Output text file path", type=str)
    if 'linux' in platform:
        parser.add_argument("--default_microphone", default='pulse',
                            help="Default microphone name for SpeechRecognition. "
                                 "Run this with 'list' to view available Microphones.", type=str)
    args = parser.parse_args()
    
    # Same setup as before...

    output_file = None
    if args.output:
        output_file = open(args.output, 'a')

    while True:
        try:
            # Same code as before...

                # Write to output file live as transcription is generated
                if output_file is not None:
                    output_file.write(text + '\n')
                    output_file.flush()

                # Same code as before...
        except KeyboardInterrupt:
            break

    if output_file is not None:
        output_file.close()
        print(f"\n\nTranscription saved to {args.output}")
    else:
        print("\n\nTranscription:")
        for line in transcription:
            print(line)


if __name__ == "__main__":
    main()

In this code, an output file is opened in append mode before the main loop starts if an output path is specified. Each transcription is then written to the output file immediately as they are generated. It's important to call output_file.flush() after writing each transcription to ensure that it is actually written to the file immediately, as Python's file output is buffered by default. This buffer is cleared (meaning the data is actually written to the file) when flush() is called. Finally, the output file is closed after the main loop finishes.

Please note that the transcription data will be written to the output file every time a new transcription is generated. This means that the output file will be updated quite frequently, which could potentially result in slower performance if the transcriptions are being generated very quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant