呼叫中心的合规性监控
#python #speechtotext #callcenter #analysis

确保法律和政策合规是管理和领导呼叫中心操作的人们的关键问题。在以下文章中,我们将研究深集的语音AI平台如何集成到监视和合规性工作流程中。

每当代理商与客户交谈时,实时获取呼叫笔录并检测代理是否符合标准是有帮助的。例如,每个人在致电客户服务时可能听到的常见短语 - 可以记录此通话是出于质量保证的目的。”大多数情况下,法律要求客户服务代理通知客户记录呼叫。

我们将使用Python和Deepgram的语音到文本API,以了解实时接收带有实时流的成绩单是多么简单。我们还将利用某些功能,这些功能会在对话中识别每个发言人,迅速浏览笔录中的短语,并认识到该模型未经训练或经常遇到的单词。 p>

在您从Python中进行合规监视之前

在这篇文章中,我使用Python 3.10,因此,如果您想跟随,请确保已安装该版本。您还需要抓住Deepgram API Key, which you can get here

下一步:创建一个目录,我叫Mine monitor_compliance

然后:转到该目录和create a virtual environment内部,以便所有python库可以安装在此处,而不是在您的计算机上全球安装。要安装虚拟环境在终端中的目录内运行以下命令:python3 -m venv venv。现在通过这样做来激活它:source venv/bin/activate

安装python软件包,以调查与文本的语音监控

您需要在虚拟环境中安装一些Python软件包才能正常工作。您可以使用Python的pip命令来安装这些软件包。确保您的虚拟环境处于活动状态。然后,从您的终端安装以下内容:

  • pip安装pyaudio

  • PIP安装Websockets

您只需要两个python库,PyAudiowebsockets。 Pyaudio库使您可以从计算机的麦克风中获得声音。 Websockets Python库也被使用,因为我们可以使用实时流媒体。 Deepgram也有一个Python SDK,但是在这篇文章中,我们将直接击中API端点。

Python代码依赖性和文件设置

创建一个名为monitor.py的空python文件,并添加以下导入语句:

import pyaudio
import asyncio
import websockets
import os
import json
from pprint import pprint

接下来,添加您的Deepgram API键:

DEEPGRAM_API_KEY=REPLACE_WITH_YOUR_DEEPGRAM_API_KEY

定义Python变量

DEEPGRAM_API_KEY下方,您需要定义一些python变量。常数与Pyaudio相关,Audio_queue是我们在整个代码中使用的异步队列。

FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
CHUNK = 8000

audio_queue = asyncio.Queue()

python回调代码,用于与文本的语音监控的合规性监视

当我们创建Pyaudio对象以获取音频时,我们需要此回调作为参数传递。

def callback(input_data, frame_count, time_info, status_flags):
  # Put an item into the queue without blocking.
   audio_queue.put_nowait(input_data)

   return (input_data, pyaudio.paContinue)

在Python中获取麦克风音频

我们在此异步功能中立即连接到麦克风,创建我们的pyaudio对象并打开流。

async def microphone(): 
   audio = pyaudio.PyAudio()
   stream = audio.open(
       format = FORMAT,
       channels = CHANNELS,
       rate = RATE,
       input = True,
       frames_per_buffer = CHUNK,
       stream_callback = callback

   )

stream.start_stream()
   while stream.is_active():
       await asyncio.sleep(0.1)

   stream.stop_stream()
   stream.close()

打开Websocket并连接到DeepGram实时语音到文本

此代码授权Deepgram并打开Websocket以允许实时音频流。我们正在传递API调用中的一些深集功能,例如:

diarize-捕获成绩单中的每个说话者并给他们一个ID。

search-搜索笔录中的短语“可以录制此通话是为了质量和培训目的”。

keywords-正确识别参与者的姓氏和术语

async def process():
   extra_headers = {
       'Authorization': 'token ' + DEEPGRAM_API_KEY
   }

   async with websockets.connect('wss://api.deepgram.com/v1/listen?encoding=linear16&sample_rate=16000&channels=1&'\
                                   '&punctuate=true' \
                                   '&diarize=true' \
                                   '&search=this+call+may+be+recorded+for+quality+and+training+purposes' \
                                   '&keywords=Warrens:2' \
                                   '&keyword_boost=standard',
                                   extra_headers = extra_headers) as ws:

       async def sender(ws): 
           try:
               while True:
                   data = await audio_queue.get() 
                   await ws.send(data)
           except Exception as e:
               print('Error while sending: ', + str(e))
               raise

       async def receiver(ws): # receives the transcript
           async for msg in ws:
               msg = json.loads(msg)
               pprint(msg)
               transcript = msg['channel']['alternatives'][0]['transcript']
               words = msg['channel']['alternatives'][0]['words']

               for speaker in words:
                   print(f"Speaker {speaker['speaker']}: {transcript} ")

                   break


       await asyncio.gather(sender(ws), receiver(ws))

运行Python代码以进行合规监视

最后,我们可以运行该项目的代码。为此,添加以下行,从您的终端类型中添加以下命令:python3 monitor.py

async def run():
   await asyncio.gather(microphone(),process())

if __name__ == '__main__':
   asyncio.run(run())

根据所使用的流音频,您可以期望得到以下响应:

Diarization

Speaker 0: Hello. 
Speaker 0: Can you hear me? 
Speaker 0: Hello, and thank you for calling Premier phone service. 
Speaker 0: Be aware that this call may be recorded for quality and training purposes. My name is Beth and will be assisting you today. 
Speaker 0: How are you doing? 
Speaker 1: Not too bad. 
Speaker 1: How are you today? 
Speaker 0: I'm doing well. Thank you. May I please have your name? 
Speaker 1: My name is Blake Warren. 

Search

 'search': [{'hits': [{'confidence': 0.8900703,
                                   'end': 15.27,
                                   'snippet': 'this call may be recorded for '
                                              'quality and training purposes '
                                              'my name is',
                                   'start': 11.962303},
                                  {'confidence': 0.3164375,
                                   'end': 17.060001,
                                   'snippet': 'and training purposes my name '
                                              'is beth and i will be assisting '
                                              'you today',
                                   'start': 13.546514}],
                         'query': 'this call may be recorded for quality and '
                                  'training purposes'}]},

扩展项目合规性监控,语音为文本

希望您在这个项目上工作很开心。用Python和Deepgram监视呼叫中心的合规性可以简单明了。您可以使用Deepgram’s other features的一些流媒体进一步扩展项目。

该项目的最终代码如下:

import pyaudio
import asyncio
import websockets
import os
import json
from pprint import pprint

DEEPGRAM_API_KEY = "YOUR_DEEPGRAM_API_KEY"

FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
CHUNK = 8000

audio_queue = asyncio.Queue()

def callback(input_data, frame_count, time_info, status_flags):
   audio_queue.put_nowait(input_data) 
   return (input_data, pyaudio.paContinue)


async def microphone(): 
   audio = pyaudio.PyAudio()
   stream = audio.open(
       format = FORMAT,
       channels = CHANNELS,
       rate = RATE,
       input = True,
       frames_per_buffer = CHUNK,
       stream_callback = callback
   )

   stream.start_stream()

   while stream.is_active():
       await asyncio.sleep(0.1)


   stream.stop_stream()
   stream.close()

async def process():
   extra_headers = {
       'Authorization': 'token ' + DEEPGRAM_API_KEY
   }

   async with websockets.connect('wss://api.deepgram.com/v1/listen?encoding=linear16&sample_rate=16000&channels=1&'\
                                   '&punctuate=true' \
                                   '&diarize=true' \
                                   '&search=this+call+may+be+recorded+for+quality+and+training+purposes' \
                                   '&keywords=Warrens:2' \
                                   '&keyword_boost=standard',
                                   extra_headers = extra_headers) as ws:

       async def sender(ws): 
           try:
               while True:
                   data = await audio_queue.get() 
                   await ws.send(data)
           except Exception as e:
               print('Error while sending: ', + str(e))
               raise

       async def receiver(ws): 
           async for msg in ws:
               msg = json.loads(msg)
               pprint(msg)
               transcript = msg['channel']['alternatives'][0]['transcript']
               words = msg['channel']['alternatives'][0]['words']

               for speaker in words:
                   print(f"Speaker {speaker['speaker']}: {transcript} ")

                   break


       await asyncio.gather(sender(ws), receiver(ws))



async def run():
   await asyncio.gather(microphone(),process())

if __name__ == '__main__':
   asyncio.run(run())

如果您对此帖子或Deepgram周围的其他任何反馈,我们很乐意收到您的来信。请在我们的GitHub discussions中告诉我们。