-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsession.py
89 lines (63 loc) · 1.96 KB
/
session.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from asr import ASRPipe
from shortid import ShortId
import logging
# Generate a key for this session
SHORT_ID = ShortId()
# A map of sessions. TODO: handle this better
SESSION_MAP = {}
logger = logging.getLogger("REServe")
def generate_shortid() -> str:
"""Generate a short ID
:return:
"""
return SHORT_ID.generate()
class Session:
"""A session object. Currently only includes the ASR object and a pause variable"""
def __init__(self, key: str, asr: ASRPipe):
"""Initialize our session. We need an ID and the ASR pipe
:param key:
:param asr:
"""
self.key = key
self.asr = asr
self.pause_asr_flag = False
def fill_asr_buffer(self, audio_in):
"""The ASR buffer is for input data, we want to allow filling this buffer unless we are paused
:param audio_in:
:return:
"""
if not self.pause_asr_flag:
self.asr.fill_buffer(audio_in)
def get_asr_transcript(self):
"""Proxy for accessing ASR transcript
:return:
"""
return self.asr.get_transcript()
def pause_asr(self):
"""Signal to stop collecting audio input to the ASR buffer. Sent packets will be dropped
:return:
"""
self.pause_asr_flag = True
def unpause_asr(self):
"""Signal to start collecting audio input from the ASR Buffer.
:return:
"""
self.pause_asr_flag = False
class SessionManager:
"""Generates and manages the dialog state for each session"""
@staticmethod
def create_session(asr: ASRPipe):
"""Create a new session
:param asr:
:return:
"""
key = generate_shortid()
session = Session(key, asr)
SESSION_MAP[key] = session
return session
@staticmethod
def get_session(key: str):
return SESSION_MAP.get(key)
@staticmethod
def delete_session(key: str):
del SESSION_MAP[key]