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
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)
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
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"))
def readingSound(path): client = ClientSTT(API_KEY, SECRET_KEY) try: response = client.recognize(path, configs) except Exception as err: fl.logError(err) return response
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
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
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
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)
# multi-line input # args = [] # for _ in range(4): # args.append(input()) # check for correct input errors = check_input(oper_id, args) # it had been decided to process and log all input errors before interrupt the script if errors: raise ScriptFailure("Step 1 failure. Check errors.log", errors) print("Step 1 success. Input accepted") # =================================== STEP 2 =================================== # as we saw from the examples the response list is already sorted from max (absolute) confidence, # so the first entry in list is the one we need try: response = client.recognize(args[0], audio_config) text = response[0]['alternatives'][0]['transcript'] # check if we have any text to process if not text: errors.append(f"{oper_id}. No speech has been recognized") raise ScriptFailure("Step 2 failure. Check errors.log", errors) else: print("Step 2 success. STT service responded") except Exception as e: errors.append(f"{oper_id}. {e.args[0]}\n") raise ScriptFailure("Step 2 failure. Check errors.log", errors) # =================================== STEP 3 =================================== try: # phase 1 if args[2] == 1:
if pat_h.split('.')[-1] != 'wav': print('Файл должен быть формата .wav!') exit() else: print('good') print('Укажите номер телефона') phone_number = input() print('Нужно ли делать запись в базу данных?') choice_db = input() if choice_db == "да": go_bd = True else: go_bd = False #Распознаем слова из аудио response = client.recognize(pat_h, audio_config) del response[1:] droblenie_1 = response[0].pop('alternatives') droblenie_2 = droblenie_1[0] result = droblenie_2.pop('transcript') print(result) # Делаем из строк со словами - список со словами + задаем слова на распознавание автоответчика и положительной/отрицательной реакции wordList = re.sub("[^\w]", " ", result).split() avtoWords = [ 'автоответчик', 'авто', 'ответчик', 'сообщение', 'после', 'оставьте' 'сигнала', 'вас', 'приветствует' ] yesWords = [
check1 = open(entering_path, 'r') check1.close() break except FileNotFoundError: print("Ошибка. Введите корректный путь!\n") check2 = open(' ', 'a') #Вставьте свой путь для логирования ошибок check2.write("Ошибка FileNotFoundError - файл не найден\n") check2.close() pass entering_telnum = input("Введите номер телефона: ") entering_flag = input("Сохранить в базу данных? (да/нет): ") print() response = client.recognize(entering_path, audio_config) elem = response[0]["alternatives"][0]["transcript"] time = response[0]["end_time"] result = [] def func1(): if "автоответчик" in elem: return 0 elif "автоответчик" not in elem: return 1 def func2(): if re.search(r'нет|неудобно|до свидания', elem): return 0
"'" + 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"]) except OSError as e: err_logger("Ошибка удаления файла: %s : %s" % (file_path, e.strerror))
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 = ['говорите', 'да кончено'] l = [] date = strftime('%Y%m%d') time = strftime('%H%M%S') l.append(date) l.append(time) l.append(str(id_num)) # если в аудио записи распознан автоответчик возвращает 0, если человек возвращает 1. if set(s.split()) & set(d.split()): print('Распознан автоответчик 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() }