Skip to content

Commit

Permalink
feat: add user agent string for http request
Browse files Browse the repository at this point in the history
  • Loading branch information
nl8590687 committed May 26, 2022
1 parent 69db49f commit 1508639
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 10 deletions.
3 changes: 1 addition & 2 deletions asrt_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@
from .speech_recognizer import BaseSpeechRecognizer, HttpSpeechRecognizer
from .speech_recognizer import get_speech_recognizer
from .utils import read_wav_datas

__version__ = '1.1.1'
from .version import __version__
38 changes: 38 additions & 0 deletions asrt_sdk/network/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2016-2099 Ailemon.net
#
# This file is part of ASRT Speech Recognition Tool Python SDK.
#
# ASRT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# ASRT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ASRT. If not, see <https://www.gnu.org/licenses/>.
# ============================================================================

'''@package processing
ASRT is a high-level deep learning API for speech recognition,
written in Python and capable of running on top of
Keras, TensorFlow, or MxNet.
Use ASRT if you need a deep learning library that:
- Allows for easy and fast prototyping for speech recognition
(through user friendliness, modularity, and extensibility).
- Supports both Keras and other Deep learning framework(on future).
- Runs seamlessly on CPU and GPU.
- Contains a api server module for developers to test models easily.
Read the documentation at: https://github.com/nl8590687/ASRT_SpeechRecognition/wiki
For a detailed overview of what makes ASRT special, see:
https://asrt.ailemon.me
ASRT is compatible with Python 3.0-3.9
and is distributed under the GPL v3.0 license.
'''

from .http import get_http_session
41 changes: 41 additions & 0 deletions asrt_sdk/network/http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2016-2099 Ailemon.net
#
# This file is part of ASRT Speech Recognition Tool Python SDK.
#
# ASRT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# ASRT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ASRT. If not, see <https://www.gnu.org/licenses/>.
# ============================================================================

"""
@author: nl8590687
ASRT语音识别Python SDK HTTP库模块
"""

import platform
import requests
from ..version import __version__


_HTTP_USER_AGENT = '%s%s%s%s%s' % ("ASRT-SDK client/", __version__,
" (python", platform.python_version(), ") (https://asrt.ailemon.net/)")


def get_http_session():
'''
Get a http request session
'''
sess=requests.Session()
sess.headers.update({"User-Agent":_HTTP_USER_AGENT})
return sess
11 changes: 7 additions & 4 deletions asrt_sdk/speech_recognizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"""

import json
import requests
from .utils import AsrtApiSpeechRequest, AsrtApiLanguageRequest, AsrtApiResponse
from .utils import read_wav_datas
from .network import get_http_session

def get_speech_recognizer(host:str, port:str, protocol:str):
'''
Expand Down Expand Up @@ -105,7 +105,8 @@ def recognite(self, wav_data, frame_rate:int, channels:int, byte_width:int) -> A
'''
request_body = AsrtApiSpeechRequest(wav_data, frame_rate, channels, byte_width)
headers = {'Content-Type': 'application/json'}
response_object = requests.post(self._url_ + self.sub_path + '/all',
http_request = get_http_session()
response_object = http_request.post(self._url_ + self.sub_path + '/all',
headers=headers,
data=request_body.to_json())
response_body_dict = json.loads(response_object.text)
Expand All @@ -119,7 +120,8 @@ def recognite_speech(self, wav_data, frame_rate, channels, byte_width):
'''
request_body = AsrtApiSpeechRequest(wav_data, frame_rate, channels, byte_width)
headers = {'Content-Type': 'application/json'}
response_object = requests.post(self._url_ + self.sub_path + '/speech',
http_request = get_http_session()
response_object = http_request.post(self._url_ + self.sub_path + '/speech',
headers=headers,
data=request_body.to_json())
response_body_dict = json.loads(response_object.text)
Expand All @@ -133,7 +135,8 @@ def recognite_language(self, sequence_pinyin):
'''
request_body = AsrtApiLanguageRequest(sequence_pinyin)
headers = {'Content-Type': 'application/json'}
response_object = requests.post(self._url_ + self.sub_path + '/language',
http_request = get_http_session()
response_object = http_request.post(self._url_ + self.sub_path + '/language',
headers=headers,
data=request_body.to_json())
response_body_dict = json.loads(response_object.text)
Expand Down
38 changes: 38 additions & 0 deletions asrt_sdk/version/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2016-2099 Ailemon.net
#
# This file is part of ASRT Speech Recognition Tool Python SDK.
#
# ASRT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# ASRT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ASRT. If not, see <https://www.gnu.org/licenses/>.
# ============================================================================

'''@package processing
ASRT is a high-level deep learning API for speech recognition,
written in Python and capable of running on top of
Keras, TensorFlow, or MxNet.
Use ASRT if you need a deep learning library that:
- Allows for easy and fast prototyping for speech recognition
(through user friendliness, modularity, and extensibility).
- Supports both Keras and other Deep learning framework(on future).
- Runs seamlessly on CPU and GPU.
- Contains a api server module for developers to test models easily.
Read the documentation at: https://github.com/nl8590687/ASRT_SpeechRecognition/wiki
For a detailed overview of what makes ASRT special, see:
https://asrt.ailemon.me
ASRT is compatible with Python 3.0-3.9
and is distributed under the GPL v3.0 license.
'''

__version__ = '1.1.2'
7 changes: 3 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from distutils.core import setup

from setuptools import find_packages
from asrt_sdk.version import __version__ as asrt_sdk_version

LONG_DESCRIPTION = '''
ASRT_SDK is a client sdk package for ASRT.
Expand All @@ -49,7 +50,7 @@
'''

setup(name='asrt_sdk',
version='1.1.1',
version=asrt_sdk_version,
description='A python sdk for ASRT Speech Recognition Toolkit',
long_description=LONG_DESCRIPTION,
long_description_content_type = 'text/markdown',
Expand Down Expand Up @@ -81,8 +82,6 @@
('License :: OSI Approved :: '
'GNU General Public License v3 or later (GPLv3+)'),
"Operating System :: OS Independent",
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7'
'Programming Language :: Python :: 3'
]
)

0 comments on commit 1508639

Please sign in to comment.