From 150863931bbe15604dcdd4d6a882208ca6f70c4b Mon Sep 17 00:00:00 2001 From: ailemon <3210346136@qq.com> Date: Thu, 26 May 2022 14:34:22 +0800 Subject: [PATCH] feat: add user agent string for http request --- asrt_sdk/__init__.py | 3 +-- asrt_sdk/network/__init__.py | 38 ++++++++++++++++++++++++++++++++ asrt_sdk/network/http.py | 41 +++++++++++++++++++++++++++++++++++ asrt_sdk/speech_recognizer.py | 11 ++++++---- asrt_sdk/version/__init__.py | 38 ++++++++++++++++++++++++++++++++ setup.py | 7 +++--- 6 files changed, 128 insertions(+), 10 deletions(-) create mode 100644 asrt_sdk/network/__init__.py create mode 100644 asrt_sdk/network/http.py create mode 100644 asrt_sdk/version/__init__.py diff --git a/asrt_sdk/__init__.py b/asrt_sdk/__init__.py index d3fad2b..e710ef8 100644 --- a/asrt_sdk/__init__.py +++ b/asrt_sdk/__init__.py @@ -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__ diff --git a/asrt_sdk/network/__init__.py b/asrt_sdk/network/__init__.py new file mode 100644 index 0000000..2183a14 --- /dev/null +++ b/asrt_sdk/network/__init__.py @@ -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 . +# ============================================================================ + +'''@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 diff --git a/asrt_sdk/network/http.py b/asrt_sdk/network/http.py new file mode 100644 index 0000000..5f85198 --- /dev/null +++ b/asrt_sdk/network/http.py @@ -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 . +# ============================================================================ + +""" +@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 diff --git a/asrt_sdk/speech_recognizer.py b/asrt_sdk/speech_recognizer.py index 2434773..744861c 100644 --- a/asrt_sdk/speech_recognizer.py +++ b/asrt_sdk/speech_recognizer.py @@ -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): ''' @@ -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) @@ -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) @@ -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) diff --git a/asrt_sdk/version/__init__.py b/asrt_sdk/version/__init__.py new file mode 100644 index 0000000..5ef4592 --- /dev/null +++ b/asrt_sdk/version/__init__.py @@ -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 . +# ============================================================================ + +'''@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' diff --git a/setup.py b/setup.py index 9d321e1..30267af 100644 --- a/setup.py +++ b/setup.py @@ -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. @@ -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', @@ -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' ] )