Пример #1
0
 def __init__(self, api_key, secret_key):
     self.client = ClientSTT(api_key, secret_key)
     self.audio_config = {
         "encoding": "LINEAR16",
         "sample_rate_hertz": 8000,
         "num_channels": 1
     }
Пример #2
0
def audio_recognition(**kwargs):
    """ Основная функция """
    client = ClientSTT(API_KEY, SECRET_KEY)
    audio_config = {
        "encoding": "LINEAR16",
        "sample_rate_hertz": 8000,
        "num_channels": 1
    }
    file_path = kwargs.get("filePath")

    try:
        response = client.recognize(file_path, audio_config)
    except ValueError:   # Проверка на правильность пути файла
        log_error('Your file path is wrong')
        sys.exit(-1)
    except exceptions_js.ValidationError:   # Проверка на правильность настроек
        log_error('Your audio config is wrong')
        sys.exit(-1)
    except exceptions_grpc._InactiveRpcError:   # Проверка на правильность API_KEY и SECRET_KEY
        log_error('Your API_KEY or SECRET_KEY is wrong')
        sys.exit(-1)

    # Достаем результат распознования из ответа
    text = response[0]['alternatives'][0]['transcript']
    rec_stage_processing(rec_stage=kwargs.get("recStage"), text=text,
                         phone_number=kwargs.get("phoneNumber"), db_flag=kwargs.get("dbFlag"))
Пример #3
0
def main():
    database = DB()

    file_name, file_dest, tel_number, flag, stage = input().split()

    audio_config = {
        "encoding": "LINEAR16",
        "sample_rate_hertz": 8000,
        "num_channels": 1
    }

    client = ClientSTT(API_KEY, SECRET_KEY)

    responses = client.recognize(file_dest, audio_config)
    print(responses)
    text = responses[0]["alternatives"][0]["transcript"]
    print(text)
    result, person = first_stage(text)
    result2 = 0
    if stage == "2":
        if result:
            result2 = second_stage(text)


    id = uniqueid()
    un_id = next(id)
    logger.info(str(un_id)+' '+person+ ' ' + str(result2) + ' ' + tel_number + ' ' + responses[0]["start_time"] + ' ' + text)

    if flag == '1':
        database.insert(str(datetime.datetime()), id, person, result2, tel_number, responses[0]["start_time"], text)

    os.remove(file_dest)
Пример #4
0
def recognition():
    client = ClientSTT(API_KEY, SECRET_KEY)

    audio_config = {
        'encoding': 'LINEAR16',
        'sample_rate_hertz': 8000,
        'num_channels': 1
    }

    try:
        with open(audio, 'rb') as source:
            responses = client.recognize(source, audio_config)
            for response in responses:
                alter = response['alternatives']
                end_time = response['end_time']
                transcript = alter[0]['transcript']

                if stage == 1:
                    if 'автоответчик' in transcript:
                        return 0, end_time
                    else:
                        return 1, end_time
                elif stage == 2:
                    if any(sub in transcript for sub in sub_yes):
                        return 1, end_time
                    elif any(sub in transcript for sub in sub_no):
                        return 0, end_time
    except:
        MyExseption(f'Файл {audio} не найден')
        return -1, -1
Пример #5
0
def readingSound(path):
    client = ClientSTT(API_KEY, SECRET_KEY)

    try:
        response = client.recognize(path, configs)
    except Exception as err:
        fl.logError(err)

    return response
Пример #6
0
    def recognize_audio(self):
        # Установка аудио конфигурации для дальнейшего распознавания
        audio_config = {
            "encoding": "LINEAR16",
            "sample_rate_hertz": 8000,
            "num_channels": 1
        }
        # Создание клиента
        client = ClientSTT(self.api_k, self.sec_k)
        # Распознвание аудио
        response = client.recognize(self.path, audio_config)
        negative_words = [
            'нет', 'неудобно', 'не могу', 'я занят', 'я сейчас занят'
        ]  # Список негативных слов
        positive_words = ['да конечно', 'говорите',
                          'да удобно']  # Список положительных слов
        result1 = ''
        result2 = ''
        resp_text = response[0]['alternatives'][0][
            'transcript']  # Получение чистого текста аудио

        # Если выбран этап 1 или оба этапа
        if self.rec_step == 1 or self.rec_step == 3:
            # Если это автоответчик
            if resp_text.find('автоответчик') != -1:
                result1 = 0
                self.logger.info(
                    f'Recognized automatic responder in {self.path} file')
            # Если это человек
            else:
                result1 = 1
                self.logger.info(f'Recognized people in {self.path} file')

        # Если выбран этап 2 или оба этапа
        if (self.rec_step == 2 or self.rec_step == 3) and (result1 != 0):
            # Проверка на негативность аудио
            for word in negative_words:
                if resp_text.find(word) != -1:
                    result2 = 0
                    break
            # Проверка на положительность аудио
            for word in positive_words:
                if resp_text.find(word) != -1:
                    result2 = 1
                    break
        # Словарь с распознанной информацией
        info_dict = {
            'result1': result1,
            'result2': result2,
            'duration': response[0]['end_time'],
            'text': resp_text
        }

        return info_dict
Пример #7
0
def parse_file(path_to_file):

    client = ClientSTT(api_key=API_KEY, secret_key=SECRET_KEY)

    audio_config = {
        "encoding": "LINEAR16",
        "sample_rate_hertz": 8000,
        "num_channels": 1
    }

    # recognise method
    response = client.recognize(path_to_file, audio_config)
    return response
Пример #8
0
class STTWrapper:
    """Wrapper class for tinkoff_voicekit_client.ClientSTT"""
    def __init__(self, api_key, secret_key):
        self.client = ClientSTT(api_key, secret_key)
        self.audio_config = {
            "encoding": "LINEAR16",
            "sample_rate_hertz": 8000,
            "num_channels": 1
        }

    def recognize(self, file_path):
        response = self.client.recognize(file_path, self.audio_config)

        best_confidence = -float('inf')
        text = ''

        alternatives = chain.from_iterable(
            map(lambda x: x['alternatives'], response))
        for alternative in alternatives:
            if (alternative['transcript']
                    and best_confidence < alternative['confidence']):
                best_confidence = alternative['confidence']
                text = alternative['transcript']

        return text
Пример #9
0
    def __init__(self, api_key, secret_key, stubs_filepath=None):
        '''
        Initialize ClientSTT by api and secret keys. If stubs is set used them when file is known
        :param api_key: API KEY
        :param secret_key: SECRET KEY
        :param stubs_filepath: path to json file with stubs (which is a dict with keys - filepaths and values - responses)
        '''
        self.client_stt = ClientSTT(api_key=api_key, secret_key=secret_key)

        # Replace api call to using stubs
        if stubs_filepath:
            self._init_stubs_(stubs_filepath)
            self.get_stt = self._get_stt_STUB_
            logger.debug(f"API connected to stubs-file:{stubs_filepath}")
        else:
            self.get_stt = self._get_stt_real_
            logger.debug(f"API used real request to server:{stubs_filepath}")
Пример #10
0
def voice_recognition(filepath: str) -> list:
    """ Отправляем файл на распознавание"""
    try:
        client = ClientSTT(config.API_KEY, config.SECRET_KEY)
    except Exception:
        logger.error(
            "Ошибка при подключении к tinkoff-voicekit. Проверьте интернет-соединение, API-KEY и SECRET-KEY"
        )
        raise
    audio_config = {
        "encoding": "LINEAR16",
        "sample_rate_hertz": 8000,
        "num_channels": 1
    }
    try:
        response = client.recognize(filepath, audio_config)
    except ValueError:
        logger.exception("Ошибка при чтении файла")
        raise
    return response
Пример #11
0
def process_file(call: "CallManager", file_path: str) -> None:
    """
    Sends a file for speech recognition

    :param call:      call manager
    :param file_path: path to recording file
    """
    client = ClientSTT(call.api_key, call.secret_key)

    audio_config = {
        "encoding": "LINEAR16",
        "sample_rate_hertz": 8000,
        "num_channels": 1,
    }

    stream_config = {"config": audio_config}

    responses = client.streaming_recognize(file_path, stream_config)
    for response in responses:
        call.result = response[0]["recognition_result"]["alternatives"][0][
            "transcript"
        ]

        if call.result.find("автоответчик") >= 0:
            break

        call.ao = 1

        for negative in NEGATIVE_ANSWERS:
            if call.result.find(negative) >= 0:
                call.positive = 0.2
                break

        for positive in POSITIVE_ANSWERS:
            if call.result.find(positive) >= 0:
                call.positive = 1
                break

        break

    os.remove(file_path)
Пример #12
0
from tinkoff_voicekit_client import ClientSTT
import re
import datetime
import random
import psycopg2
import os

API_KEY = ""
SECRET_KEY = ""

client = ClientSTT(API_KEY, SECRET_KEY)

audio_config = {
    "encoding": "LINEAR16",
    "sample_rate_hertz": 8000,
    "num_channels": 1
}

while True:
    try:
        global entering_path
        entering_path = input(str("Введите путь к wav-файлу: "))
        check1 = open(entering_path, 'r')
        check1.close()
        break

    except FileNotFoundError:
        print("Ошибка. Введите корректный путь!\n")
        check2 = open(' ', 'a') #Вставьте свой путь для логирования ошибок
        check2.write("Ошибка FileNotFoundError - файл не найден\n")
        check2.close()
Пример #13
0
class ApiClient:
    def __init__(self, api_key, secret_key, stubs_filepath=None):
        '''
        Initialize ClientSTT by api and secret keys. If stubs is set used them when file is known
        :param api_key: API KEY
        :param secret_key: SECRET KEY
        :param stubs_filepath: path to json file with stubs (which is a dict with keys - filepaths and values - responses)
        '''
        self.client_stt = ClientSTT(api_key=api_key, secret_key=secret_key)

        # Replace api call to using stubs
        if stubs_filepath:
            self._init_stubs_(stubs_filepath)
            self.get_stt = self._get_stt_STUB_
            logger.debug(f"API connected to stubs-file:{stubs_filepath}")
        else:
            self.get_stt = self._get_stt_real_
            logger.debug(f"API used real request to server:{stubs_filepath}")

    def _get_stt_real_(self, filepath):
        # Do response to real server
        logger.debug(f"API send to real server this file:{filepath}")
        filepath = os.path.abspath(filepath)
        response = self.client_stt.recognize(filepath, DEFAULT_AUDIO_CONFIG)
        return response

    def _init_stubs_(self, filepath):
        # Load stubs
        import json

        with open(filepath, 'rt') as stubs_handler:
            self.STUBS = json.load(stubs_handler)
        logger.debug("Stubs loaded")

    def _save_stubs_(self):
        # Save changes in stubs
        import json
        with open('stubs.txt', 'wt') as stubs_handler:
            json.dump(self.STUBS, stubs_handler)
        logger.debug("Stub file updated.")

    def _get_stt_STUB_(self, filepath):
        # Try to get response for filepath from stubs
        filepath = os.path.abspath(filepath)
        response = self.STUBS.get(filepath, None)
        # Response not found in stubs
        if response is None:
            logger.debug("File not in stubs! Add it and do request using API")
            response = self._get_stt_real_(filepath)
            self.STUBS[filepath] = response
            # Save new file and its response
            self._save_stubs_()
        return response

    def recognize_wav(self, filepath, stage):
        '''
        Get response about filepath from api and analyze it
        :param filepath: path to wav file
        :param stage: stage to analyze
        :return: data about recognizing
        '''
        response = self.get_stt(filepath)
        if len(response) == 0:
            raise ApiClientError("Something went wrong during request API")
        analyzer = TranscriptionAnalyzer(response)
        return {
            "stage_number": stage,
            "answer": analyzer.analyze_by_stage(stage),
            "transcription": analyzer.get_transcription(),
            "duration": analyzer.get_duration()
        }
Пример #14
0
def main():
    params = sys.argv
    if len(params) < 5:
        print("need path_to_wav phone_number write_to_db stage_recognizing")
        return
    path_to_wav = params[1]
    phone_number = params[2]
    write_to_db = params[3] == "1"
    stage_recognizing = 1 if params[4] == "1" else 2

    client = ClientSTT(API_KEY, SECRET_KEY)

    audio_config = {
        "encoding": "LINEAR16",
        "sample_rate_hertz": 8000,
        "num_channels": 1
    }
    stream_config = {"config": audio_config}

    recognizing_result = ""
    result = 0
    duration = get_duration_wave_file(path_to_wav)

    # делаем запрос на распознование
    with open(path_to_wav, "rb") as source:
        responses = client.streaming_recognize(source, stream_config)
        for response in responses:
            text_result = response[0]['recognition_result']['alternatives'][0][
                'transcript']
            text_words = set(text_result.lower().split())

            if len(text_words) == 0:
                continue

            recognizing_result = text_result

            if stage_recognizing == 1:
                if find_word_in_text(ao_words, text_words):
                    result = 0
                    # debug_echo("автоответчик")
                else:
                    result = 1
                    # debug_echo("человек")
            else:
                if find_word_in_text(negative_words, text_words):
                    result = 0
                    # debug_echo("негатив")
                elif find_word_in_text(positive_words, text_words):
                    result = 1
                    # debug_echo("позитив")

    now = datetime.datetime.now()
    uniq_id = now.strftime('%s')  # уникальный id для лога и БД это таймстамп
    result_data = [
        now.strftime('%Y-%m-%d'),
        now.strftime('%H:%M:%S'), uniq_id, result, phone_number, duration,
        recognizing_result
    ]
    recognizing_log.info('\t'.join(str(x) for x in result_data))

    if write_to_db:
        write_result_to_db(result_data)

    # удаляем переданный файл
    if delete_wav_file:
        os.remove(path_to_wav)
Пример #15
0
def client_stt(params):
    return ClientSTT(params.API_KEY, params.SECRET_KEY)
Пример #16
0
from os import remove

setlocale(LC_ALL, 'ru_RU.UTF-8')


class ScriptFailure(Exception):
    # this overrides initialization method to write down log of errors
    def __init__(self, message, errors):
        with open("errors.log", 'w', encoding='utf-8',
                  newline='\r\n') as f_out:
            f_out.writelines(errors)
        super().__init__(message)


# dancing with interface of STT service
client = ClientSTT(API_KEY, SECRET_KEY)
audio_config = {
    "encoding": "LINEAR16",
    "sample_rate_hertz": 8000,
    "num_channels": 1
}

# =================================== STEP 1 ===================================
# input string format:
# filepath: str, phone number: str, recognition phase: str, database flag: bool

# creating unique number for this operation
oper_id = uuid4().hex
# single-line input
args = input().split()
# multi-line input
Пример #17
0
            '\nPlease, check audio configuration parameters')
    except _channel._InactiveRpcError as err:
        logger.error(err)
        raise AuthException(
            '\nPlease, check API KEY and SECRETE KEY in config.py')


if __name__ == '__main__':
    # создаст необходимые таблицы в БД, если их нет
    try:
        _execute_queries(initial_sql_operators)
    except OperationalError as err:
        logger.error(err)
        raise DBConfigException(
            '\nPlease, check values in db configuration (config.py/db_config)')

    client = ClientSTT(API_KEY, SECRET_KEY)

    response = get_response_with_recognized_file(
        audio_config=audio_config, path=parameters_from_terminal.path)
    # Извлекает распознанный текст для дальнейшей записи
    text_body = response[0]['alternatives'][0]['transcript']

    _, call_result = _check_audio_stage(
        text_body=text_body, stage_value=parameters_from_terminal.call_stage)
    _record_recognized_audio_in_file(response=response,
                                     call_result=call_result,
                                     parameters=parameters_from_terminal,
                                     text_body=text_body)
    _delete_wav_file(path_to_file=parameters_from_terminal.path)
Пример #18
0
import psycopg2
from config import API_KEY, SECRET_KEY
from tinkoff_voicekit_client import ClientSTT
from time import strftime
from os import remove

client = ClientSTT(API_KEY, SECRET_KEY)

name_audio = input('Введите имя wav файла: ')
number = input('Введите номер телефона: ')
call_db = input('Хотите чтобы произошла запись в базу данных? 1-да 0-нет: ')

audio_config = {
    "encoding": "LINEAR16",
    "sample_rate_hertz": 8000,
    "num_channels": 1
}

# идентификациоонный номер
with open('log.log', 'rt') as num:
    id_num = len(num.readlines())
#    print(id_num)

# recognise method call
response = client.recognize(name_audio + '.wav', audio_config)
#print(response)
d = dict(response[0])['alternatives'][0].get('transcript')
print('Текст из файла: ' + d)
s = 'автоответчик'
negative_word = ['нет', 'неудобно']
good_word = ['говорите', 'да кончено']
Пример #19
0
               "'" + objToFile["time"] + "'", "'" + objToFile["option"] + "'",
               objToFile["tel"], float_duration,
               "'" + objToFile["audio_result"] + "'"))
        dbconn.commit()
        dbconn.close()


#UI - User Interfase
mock_arr["audio"] = input("Пожалуйста, введите путь к аудио файлу: ")
mock_arr["tel"] = input("Номер телефона: ")
mock_arr["db"] = input("Нужно ли записывать в базу данных 1 - да, 0 - нет: ")
mock_arr["stage"] = input("Проход 1 или 2: ")
#UI - User Interfase

#Connect tinkoff_voicekit_client
client = ClientSTT(API_KEY, SECRET_KEY)

# recognise method call
response = client.recognize(mock_arr["audio"], audio_config)

if mock_arr["stage"] == "1":
    parse_first = parse_dict_first(response)
    print(parse_first)
    log_writter(response, mock_arr, parse_first)
else:
    parse_second = parse_dict_second(response)
    print(parse_second)
    log_writter(response, mock_arr, parse_second)
    #delete audio file after 2 stage
    try:
        os.remove(mock_arr["audio"])
Пример #20
0
    def recognize(self):  # Метод получения значения сообщения
        def checkAnswer(
            message
        ):  # Метод проверки на Положительное или Отрицательное значение
            notAM = ['нет', 'До свидания']
            aM = ['Говорите', 'Да', 'Удобно', 'Слушаю']
            for answer in aM:
                if re.search(r'(?i)\b{}\b'.format(answer),
                             message) is not None:
                    return 1
            for answer in notAM:
                if re.search(r'(?i)\b{}\b'.format(answer),
                             message) is not None:
                    return 0

        def checkAnswMachine(message):  # Метод проверки на Человека и АО
            if (re.search('Автоответчик'.lower(), message.lower())):
                return 0
            else:
                return 1

        def addToDB(result):  # Метод добавления данных в БД
            now = datetime.datetime.now()
            try:
                conn = psycopg2.connect(dbname='database',
                                        user='******',
                                        password=1,
                                        host='127.0.0.1',
                                        port="5432")
            except:
                print('Не удалось подключится к бд')
                with open('logErr.csv', 'a', newline='') as csv_file:
                    writer = csv.writer(csv_file, delimiter=',')
                    data = [{
                        'ID': str(uuid.uuid4()),
                        'Error': 'Не удалось подключиться к СУБД'
                    }]
                    writer.writerow(data)
                    sys.exit()
            cursor = conn.cursor()

            date = str(now.day) + "/" + str(now.month) + "/" + str(now.year)
            timeD = str(now.hour) + ":" + str(now.minute) + ":" + str(
                now.second)
            if self.stage == 1:
                cursor.execute(
                    'INSERT INTO public."Answers"("Date", "Time", "Result", "Phone", "Duration", "Message")VALUES (\'{0}\', \'{1}\', \'{2}\', \'{3}\', \'{4}\', \'{5}\');'
                    .format(date, timeD,
                            "АО" if result['stage 1'] == 0 else "Человек",
                            self.phoneNumber, float(result['duration']),
                            result['message']))

            if self.stage == 2:
                cursor.execute(
                    'INSERT INTO public."Answers"("Date", "Time", "Result", "Phone", "Duration", "Message") VALUES (\'{0}\', \'{1}\', \'{2}\', \'{3}\', \'{4}\', \'{5}\');'
                    .format(
                        date, timeD, "Отрицательно" if result['stage 2'] == 0
                        else "Положительно", self.phoneNumber,
                        (result['duration']), result['message']))
            conn.commit()

        def addToLogFile(result):  #Добавление записей в лог файл
            now = datetime.datetime.now()

            if self.stage == 1:
                data = [{
                    "ID":
                    str(uuid.uuid4()),
                    "Date":
                    str(now.day) + "." + str(now.month) + "." + str(now.year),
                    "Time":
                    str(now.hour) + ":" + str(now.minute) + ":" +
                    str(now.second),
                    "Result":
                    "АО" if result['stage 1'] == 0 else "Человек",
                    "Phone":
                    self.phoneNumber,
                    "Duration":
                    result['duration'],
                    "Message":
                    result['message']
                }]
            elif self.stage == 2:

                data = [{
                    "ID":
                    str(uuid.uuid4()),
                    "Date":
                    str(now.day) + "." + str(now.month) + "." + str(now.year),
                    "Time":
                    str(now.hour) + ":" + str(now.minute) + ":" +
                    str(now.second),
                    "Result":
                    "Отрицательно"
                    if result['stage 2'] == 0 else "Положительно",
                    "Phone":
                    self.phoneNumber,
                    "Duration":
                    result['duration'],
                    "Message":
                    result['message']
                }]

            with open('log.csv', "a", newline='') as csv_file:
                writer = csv.writer(csv_file, delimiter=',')
                writer.writerow(data)

        client = ClientSTT(self.API_KEY, self.SECRET_KEY)
        response = client.recognize(self.pathToFile, self.audio_config)
        time = float(re.search(r'\d*\.\d*', response[0]['end_time']).group(0))
        recText = response[0]['alternatives'][0]['transcript']
        if ((self.stage == 1) and (self.DB)):
            checkMachine = checkAnswMachine(recText)
            addToDB({
                "stage 1": checkMachine,
                "duration": time,
                "message": recText
            })
            os.remove(self.pathToFile)
        elif ((self.stage == 1) and (self.DB == False)):
            checkMachine = checkAnswMachine(recText)
            addToLogFile({
                "stage 1": checkMachine,
                "duration": time,
                "message": recText
            })
            os.remove(self.pathToFile)
        elif ((self.stage == 2) and (self.DB)):
            if (checkAnswMachine(recText) == 0):
                print(
                    'Обнаружен автоответчик используйте 1 этап распознования')
            else:
                checkAns = checkAnswer(
                    response[0]['alternatives'][0]['transcript'])
                addToDB({
                    "stage 2": checkAns,
                    "duration": time,
                    "message": recText
                })
                os.remove(self.pathToFile)
        elif ((self.stage == 2) and (self.DB == False)):
            if (checkAnswMachine(recText) == 0):
                print(
                    'Обнаружен автоответчик используйте 1 этап распознования')
            else:
                checkAns = checkAnswer(
                    response[0]['alternatives'][0]['transcript'])
                addToLogFile({
                    "stage 2": checkAns,
                    "duration": time,
                    "message": recText
                })
                os.remove(self.pathToFile)
Пример #21
0
import logging
import os
import re
from datetime import datetime
import time
import socket
import librosa
import psycopg2
from tinkoff_voicekit_client import ClientSTT

#Подключаемся к тинькоф библиотеке
API_KEY = "Введите свой ключ"
SECRET_KEY = "Введите свой ключ"

client = ClientSTT(API_KEY, SECRET_KEY)

audio_config = {
    "encoding": "LINEAR16",
    "sample_rate_hertz": 8000,
    "num_channels": 1
}

#Принимаем информацию из консоли.
print(
    'Приветствую, укажите путь к файлу .wav в формате C:/Files/audiofile.wav')
pat_h = input()
if pat_h.split('.')[-1] != 'wav':
    print('Файл должен быть формата .wav!')
    exit()
else:
    print('good')