Skip to content

Commit c5e5965

Browse files
authored
Merge pull request #326 from tencentyun/feature_flynnzzhang_d1fa4343
ci add asr_hot_vocabulary_table sdk
2 parents f3888f8 + d94dde2 commit c5e5965

File tree

4 files changed

+350
-5
lines changed

4 files changed

+350
-5
lines changed

demo/ci_speech_recognition.py

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding=utf-8
22

33
from qcloud_cos import CosConfig
4-
from qcloud_cos import CosS3Client
4+
from qcloud_cos.intelligent_speech import IntelligentSpeechClient
55

66
import os
77
import sys
@@ -22,7 +22,7 @@
2222

2323
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key,
2424
Token=token) # 获取配置对象
25-
client = CosS3Client(config)
25+
client = IntelligentSpeechClient(config)
2626

2727
bucket_name = 'examplebucket-1250000000'
2828

@@ -181,6 +181,79 @@ def ci_delete_asr_template():
181181
print(response)
182182
return response
183183

184+
def ci_create_asr_hot_vocabulary_table():
185+
# 创建语音识别热词表
186+
body = {
187+
"TableName": "test",
188+
"TableDescription": "test",
189+
"VocabularyWeights": {
190+
"Vocabulary": "abc",
191+
"Weight": "10"
192+
},
193+
# "VocabularyWeightStr": ""
194+
}
195+
response, data = client.ci_create_asr_hot_vocabulary_table(
196+
Bucket=bucket_name,
197+
Body=body,
198+
ContentType='application/xml'
199+
)
200+
print(response)
201+
print(data)
202+
return response, data
203+
204+
205+
def ci_update_asr_hot_vocabulary_table():
206+
# 更新语音识别热词表
207+
body = {
208+
"TableId": "08417b95c91xxxxxxxxxxxxx",
209+
"TableName": "test1",
210+
"TableDescription": "test1",
211+
"VocabularyWeights": {
212+
"Vocabulary": "abc",
213+
"Weight": "8"
214+
},
215+
# "VocabularyWeightStr": ""
216+
}
217+
response, data = client.ci_update_asr_hot_vocabulary_table(
218+
Bucket=bucket_name,
219+
Body=body,
220+
ContentType='application/xml'
221+
)
222+
print(response)
223+
print(data)
224+
return response, data
225+
226+
227+
def ci_get_asr_hot_vocabulary_table():
228+
# 获取指定语音识别热词表
229+
response, data = client.ci_get_asr_hot_vocabulary_table(
230+
Bucket=bucket_name,
231+
TableId='08417b95c91xxxxxxxxxxxxx',
232+
)
233+
print(response)
234+
print(data)
235+
return response, data
236+
237+
238+
def ci_list_asr_hot_vocabulary_table():
239+
# 获取语音识别热词表列表
240+
response, data = client.ci_list_asr_hot_vocabulary_table(
241+
Bucket=bucket_name,
242+
)
243+
print(response)
244+
print(data)
245+
return response, data
246+
247+
def ci_delete_asr_hot_vocabulary_table():
248+
# 删除指定语音识别热词表
249+
response, data = client.ci_delete_asr_hot_vocabulary_table(
250+
Bucket=bucket_name,
251+
TableId='08417b95c91xxxxxxxxxxxxx',
252+
)
253+
print(response)
254+
print(data)
255+
return response, data
256+
184257

185258
if __name__ == '__main__':
186259
# ci_get_asr_bucket()
@@ -195,4 +268,9 @@ def ci_delete_asr_template():
195268
# ci_list_asr_jobs()
196269
# ci_close_asr_bucket()
197270
# ci_open_asr_bucket()
198-
ci_get_asr_bucket()
271+
# ci_get_asr_bucket()
272+
# ci_create_asr_hot_vocabulary_table()
273+
# ci_update_asr_hot_vocabulary_table()
274+
# ci_get_asr_hot_vocabulary_table()
275+
# ci_list_asr_hot_vocabulary_table()
276+
ci_delete_asr_hot_vocabulary_table()

qcloud_cos/cos_client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9568,7 +9568,7 @@ def ci_list_asr_jobs(self, Bucket, QueueId=None, StartCreationTime=None, EndCrea
95689568

95699569
def ci_create_asr_template(self, Bucket, Name, EngineModelType, ChannelNum=None,
95709570
ResTextFormat=None, FilterDirty=0, FilterModal=0, ConvertNumMode=0, SpeakerDiarization=0,
9571-
SpeakerNumber=0, FilterPunc=0, OutputFileType='txt', FlashAsr=False, Format=None, FirstChannelOnly=1, WordInfo=0, **kwargs):
9571+
SpeakerNumber=0, FilterPunc=0, OutputFileType='txt', FlashAsr=False, Format=None, FirstChannelOnly=1, WordInfo=0, HotVocabularyTableId=None, **kwargs):
95729572
""" 创建语音识别模板接口 https://cloud.tencent.com/document/product/460/78939
95739573
95749574
:param Bucket(string): 存储桶名称.
@@ -9654,6 +9654,8 @@ def ci_create_asr_template(self, Bucket, Name, EngineModelType, ChannelNum=None,
96549654
body['SpeechRecognition']['Format'] = Format
96559655
body['SpeechRecognition']['FirstChannelOnly'] = FirstChannelOnly
96569656
body['SpeechRecognition']['WordInfo'] = WordInfo
9657+
if HotVocabularyTableId:
9658+
body['SpeechRecognition']['HotVocabularyTableId'] = HotVocabularyTableId
96579659

96589660
xml_config = format_xml(data=body, root='Request')
96599661
path = "/template"
@@ -9678,7 +9680,7 @@ def ci_create_asr_template(self, Bucket, Name, EngineModelType, ChannelNum=None,
96789680

96799681
def ci_update_asr_template(self, Bucket, TemplateId, Name, EngineModelType, ChannelNum,
96809682
ResTextFormat, FilterDirty=0, FilterModal=0, ConvertNumMode=0, SpeakerDiarization=0,
9681-
SpeakerNumber=0, FilterPunc=0, OutputFileType='txt', FlashAsr=False, Format=None, FirstChannelOnly=1, WordInfo=0, **kwargs):
9683+
SpeakerNumber=0, FilterPunc=0, OutputFileType='txt', FlashAsr=False, Format=None, FirstChannelOnly=1, WordInfo=0, HotVocabularyTableId=None, **kwargs):
96829684
""" 更新语音识别模板接口 https://cloud.tencent.com/document/product/460/78942
96839685
96849686
:param Bucket(string): 存储桶名称.
@@ -9766,6 +9768,8 @@ def ci_update_asr_template(self, Bucket, TemplateId, Name, EngineModelType, Chan
97669768
body['SpeechRecognition']['Format'] = Format
97679769
body['SpeechRecognition']['FirstChannelOnly'] = FirstChannelOnly
97689770
body['SpeechRecognition']['WordInfo'] = WordInfo
9771+
if HotVocabularyTableId:
9772+
body['SpeechRecognition']['HotVocabularyTableId'] = HotVocabularyTableId
97699773
xml_config = format_xml(data=body, root='Request')
97709774
path = "/template/" + TemplateId
97719775
url = self._conf.uri(bucket=Bucket, path=path, endpoint=self._conf._endpoint_ci)

qcloud_cos/intelligent_speech.py

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
# -*- coding=utf-8
2+
3+
from qcloud_cos import CosS3Auth
4+
from qcloud_cos.cos_client import logger, CosS3Client
5+
from .cos_comm import *
6+
7+
8+
class IntelligentSpeechClient(CosS3Client):
9+
10+
def _asr_hot_vocabulary_table(self, Bucket, Body=None, Params={}, Path="/asrhotvocabtable", Method="POST", **kwargs):
11+
headers = mapped(kwargs)
12+
final_headers = {}
13+
params = Params
14+
for key in headers:
15+
if key.startswith("response"):
16+
params[key] = headers[key]
17+
else:
18+
final_headers[key] = headers[key]
19+
headers = final_headers
20+
params = format_values(params)
21+
22+
xml_config = None
23+
if Body is not None:
24+
xml_config = format_xml(data=Body, root='Request')
25+
path = Path
26+
url = self._conf.uri(bucket=Bucket, path=path, endpoint=self._conf._endpoint_ci)
27+
logger.info("_asr_hot_vocabulary_table result, url=:{url} ,headers=:{headers}, params=:{params}, xml_config=:{xml_config}".format(
28+
url=url,
29+
headers=headers,
30+
params=params,
31+
xml_config=xml_config))
32+
rt = self.send_request(
33+
method=Method,
34+
url=url,
35+
bucket=Bucket,
36+
data=xml_config,
37+
auth=CosS3Auth(self._conf, path, params=params),
38+
params=params,
39+
headers=headers,
40+
ci_request=True,
41+
cos_request=False)
42+
43+
data = rt.content
44+
response = dict(**rt.headers)
45+
if 'Content-Type' in response and len(data) != 0:
46+
if response['Content-Type'] == 'application/xml':
47+
data = xml_to_dict(rt.content)
48+
format_dict(data, ['Response'])
49+
elif response['Content-Type'].startswith('application/json'):
50+
data = rt.json()
51+
52+
return response, data
53+
54+
def ci_create_asr_hot_vocabulary_table(self, Bucket, Body, **kwargs):
55+
"""
56+
57+
:param Bucket(string): 存储桶名称
58+
:param Body(dict): 创建热词表body
59+
:param kwargs(dict): 设置请求的headers.
60+
:return(dict): 创建成功返回的结果, dict类型.
61+
62+
. code-block:: python
63+
64+
body = {
65+
"TableName": "test",
66+
"TableDescription": "test",
67+
"VocabularyWeights": {
68+
"Vocabulary": "abc",
69+
"Weight": "10"
70+
},
71+
# "VocabularyWeightStr": ""
72+
}
73+
response, data = client.ci_create_asr_hot_vocabulary_table(
74+
Bucket=bucket_name,
75+
Body=body,
76+
ContentType='application/xml'
77+
)
78+
print(response)
79+
print(data)
80+
return response, data
81+
"""
82+
return self._asr_hot_vocabulary_table(Bucket, Body, **kwargs)
83+
84+
def ci_update_asr_hot_vocabulary_table(self, Bucket, Body, **kwargs):
85+
"""
86+
87+
:param Bucket(string): 存储桶名称
88+
:param Body(dict): 更新热词表body
89+
:param kwargs(dict): 设置请求的headers.
90+
:return(dict): 更新成功返回的结果, dict类型.
91+
92+
. code-block:: python
93+
94+
body = {
95+
"TableId": "08417b95c91xxxxxxxxxxxxx",
96+
"TableName": "test1",
97+
"TableDescription": "test1",
98+
"VocabularyWeights": {
99+
"Vocabulary": "abc",
100+
"Weight": "8"
101+
},
102+
# "VocabularyWeightStr": ""
103+
}
104+
response, data = client.ci_update_asr_hot_vocabulary_table(
105+
Bucket=bucket_name,
106+
Body=body,
107+
ContentType='application/xml'
108+
)
109+
print(response)
110+
print(data)
111+
return response, data
112+
"""
113+
return self._asr_hot_vocabulary_table(Bucket=Bucket, Body=Body, Method="PUT", **kwargs)
114+
115+
def ci_get_asr_hot_vocabulary_table(self, Bucket, TableId, **kwargs):
116+
"""
117+
118+
:param Bucket(string): 存储桶名称
119+
:param TableId(string): 热词表ID
120+
:param kwargs(dict): 设置请求的headers.
121+
:return(dict): 查询成功返回的结果, dict类型.
122+
123+
. code-block:: python
124+
125+
response, data = client.ci_get_asr_hot_vocabulary_table(
126+
Bucket=bucket_name,
127+
TableId='08417b95c91xxxxxxxxxxxxx',
128+
)
129+
print(response)
130+
print(data)
131+
return response, data
132+
"""
133+
return self._asr_hot_vocabulary_table(Bucket=Bucket, Method="GET", Path="/asrhotvocabtable/" + TableId, **kwargs)
134+
135+
def ci_list_asr_hot_vocabulary_table(self, Bucket, Offset=0, Limit=10, **kwargs):
136+
"""
137+
138+
:param Bucket(string): 存储桶名称
139+
:param Offset(string): 热词表ID
140+
:param Limit(string): 热词表ID
141+
:param kwargs(dict): 设置请求的headers.
142+
:return(dict): 查询成功返回的结果, dict类型.
143+
144+
. code-block:: python
145+
146+
response, data = client.ci_list_asr_hot_vocabulary_table(
147+
Bucket=bucket_name,
148+
)
149+
print(response)
150+
print(data)
151+
return response, data
152+
"""
153+
param = {}
154+
param["offset"] = Offset
155+
param["limit"] = Limit
156+
return self._asr_hot_vocabulary_table(Bucket=Bucket, Method="GET", Params=param, **kwargs)
157+
158+
def ci_delete_asr_hot_vocabulary_table(self, Bucket, TableId, **kwargs):
159+
"""
160+
161+
:param Bucket(string): 存储桶名称
162+
:param TableId(string): 热词表ID
163+
:param kwargs(dict): 设置请求的headers.
164+
:return(dict): 查询成功返回的结果, dict类型.
165+
166+
. code-block:: python
167+
168+
response, data = client.ci_delete_asr_hot_vocabulary_table(
169+
Bucket=bucket_name,
170+
TableId='08417b95c91xxxxxxxxxxxxx',
171+
)
172+
print(response)
173+
print(data)
174+
return response, data
175+
"""
176+
return self._asr_hot_vocabulary_table(Bucket=Bucket, Method="DELETE", Path="/asrhotvocabtable/" + TableId, **kwargs)

0 commit comments

Comments
 (0)