Ejemplo n.º 1
0
def main():
    args = BaseRecognitionParser().parse_args()

    if args.encoding == stt_pb2.RAW_OPUS:
        raise ValueError("RAW_OPUS encoding is not supported by this script")
    with audio_open_read(args.audio_file, args.encoding, args.rate,
                         args.num_channels, args.chunk_size,
                         args.pyaudio_max_seconds) as reader:
        metadata = authorization_metadata(args.api_key,
                                          args.secret_key,
                                          "tinkoff.cloud.stt",
                                          type=dict)
        request = build_recognition_request(args, reader, type="json")
        response = requests.post("http{}://{}/v1/stt:recognize".format(
            "s" if args.endpoint.endswith("443") else "", args.endpoint),
                                 json=request,
                                 headers=metadata)

        if response.status_code != 200:
            print(
                "REST failed with HTTP code {}\nHeaders: {}\nBody: {}".format(
                    response.status_code, response.headers, response.text))
            return
        response = response.json()
        print_recognition_response(response)
Ejemplo n.º 2
0
def main():
    args = StreamingRecognitionParser().parse_args()

    with audio_open_read(args.audio_file, args.encoding, args.rate, args.num_channels, args.chunk_size,
                         args.pyaudio_max_seconds) as reader:
        stub = stt_pb2_grpc.SpeechToTextStub(make_channel(args))
        metadata = authorization_metadata(args.api_key, args.secret_key, "tinkoff.cloud.stt")
        responses = stub.StreamingRecognize(stt_generate_requests(args, reader), metadata=metadata)
        print_streaming_recognition_responses(responses)
Ejemplo n.º 3
0
def main():
    args = BaseRecognitionParser().parse_args()
    if args.encoding == stt_pb2.RAW_OPUS:
        raise ValueError("RAW_OPUS encoding is not supported by this script")
    with audio_open_read(args.audio_file, args.encoding, args.rate,
                         args.num_channels, args.chunk_size,
                         args.pyaudio_max_seconds) as reader:
        stub = stt_pb2_grpc.SpeechToTextStub(make_channel(args))
        metadata = authorization_metadata(args.api_key, args.secret_key,
                                          "tinkoff.cloud.stt")
        response = stub.Recognize(build_recognition_request(args, reader),
                                  metadata=metadata)
        print_recognition_response(response)
Ejemplo n.º 4
0
def main():
    args = BaseRecognitionParser().parse_args()
    total = ''
    if args.encoding == stt_pb2.RAW_OPUS:
        raise ValueError("RAW_OPUS encoding is not supported by this script")
    with audio_open_read(args.audio_file, args.encoding, args.rate,
                         args.num_channels, args.chunk_size,
                         args.pyaudio_max_seconds) as reader:
        stub = stt_pb2_grpc.SpeechToTextStub(make_channel(args))
        metadata = authorization_metadata(args.api_key, args.secret_key,
                                          "tinkoff.cloud.stt")
        response = stub.Recognize(build_recognition_request(args, reader),
                                  metadata=metadata)

        if not isinstance(response, dict):
            # https://developers.google.com/protocol-buffers/docs/proto3#json
            response = MessageToDict(response,
                                     including_default_value_fields=True,
                                     preserving_proto_field_name=True)
        for result in response["results"]:
            for alternative in result["alternatives"]:
                total = total + alternative["transcript"]
    print(total)