Spaces:
Runtime error
Runtime error
def transcribe_file(speech_file): | |
"""Transcribe the given audio file asynchronously.""" | |
from google.cloud import speech | |
client = speech.SpeechClient() | |
with open(speech_file, "rb") as audio_file: | |
content = audio_file.read() | |
""" | |
Note that transcription is limited to a 60 seconds audio file. | |
Use a GCS file for audio longer than 1 minute. | |
""" | |
audio = speech.RecognitionAudio(content=content) | |
config = speech.RecognitionConfig( | |
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16, | |
sample_rate_hertz=16000, | |
language_code="en-US", | |
) | |
operation = client.long_running_recognize(config=config, audio=audio) | |
print("Waiting for operation to complete...") | |
response = operation.result(timeout=90) | |
# Each result is for a consecutive portion of the audio. Iterate through | |
# them to get the transcripts for the entire audio file. | |
for result in response.results: | |
# The first alternative is the most likely one for this portion. | |
print("Transcript: {}".format(result.alternatives[0].transcript)) | |
print("Confidence: {}".format(result.alternatives[0].confidence)) | |
transcribe_file('2.wav') | |