baidu_ai 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.md +9 -0
- data/README.md +71 -0
- data/lib/baidu_ai.rb +12 -0
- data/lib/baidu_ai/assess_token/assess_token_manager.rb +39 -0
- data/lib/baidu_ai/os/os.rb +23 -0
- data/lib/baidu_ai/speech_synthesis/speech_synthesizer.rb +59 -0
- data/lib/baidu_ai/version.rb +3 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6faa1255eece4642632f47df4934068537fefc8d
|
4
|
+
data.tar.gz: 9931636b01d234df652fcb8996f8ebc789c79813
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 193cd18461ddbeda5b90b2c9682187092c2028093c9c274bd6f0cc2db4209277f1d8fe9d2360e2b4268534ac82189610f7df5a5f5a11fccc2ecc9bade3b28c4c
|
7
|
+
data.tar.gz: 74dcdb1e3c47baaa71eb1117c05d5d64b6f7cb83c2e4a35dbc395e12c3ecb30291288a5740d2941a35c7179aa2b6fe2bab24404fea8fe2c92b1d781c6c0bebe5
|
data/LICENSE.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2009-2013 Nicholas J Humfrey
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
[![Build Status](https://rubygems.org//baidu_aip_ruby_sdk.svg)](https://rubygems.org//baidu_aip_ruby_sdk)
|
2
|
+
|
3
|
+
baidu_aip_ruby_sdk
|
4
|
+
=========
|
5
|
+
|
6
|
+
Baidu AI Platform ruby sdk, unofficial.
|
7
|
+
|
8
|
+
Table of Contents
|
9
|
+
-----------------
|
10
|
+
* [Installation](#installation)
|
11
|
+
* [Quick Start](#quick-start)
|
12
|
+
* [Library Overview](#library-overview)
|
13
|
+
* [License](#license)
|
14
|
+
* [Contact](#contact)
|
15
|
+
|
16
|
+
|
17
|
+
Installation
|
18
|
+
------------
|
19
|
+
|
20
|
+
You may get the latest stable version from [Rubygems]:
|
21
|
+
|
22
|
+
$ gem install baidu_ai
|
23
|
+
|
24
|
+
For bundler:
|
25
|
+
|
26
|
+
gem 'baidu_ai'
|
27
|
+
|
28
|
+
|
29
|
+
Quick Start
|
30
|
+
-----------
|
31
|
+
|
32
|
+
~~~ ruby
|
33
|
+
require 'rubygems'
|
34
|
+
require 'baidu_ai'
|
35
|
+
|
36
|
+
# SpeechSynthesis example
|
37
|
+
BaiduAi.api_key = 'your api key'
|
38
|
+
BaiduAi.secret_key = 'your secret key'
|
39
|
+
BaiduAi::SpeechSynthesizer.speak 'QR-00000001', '横琴号十号线, 车辆即将到达创意谷,有 23 名乘客上车, 5 人下车'
|
40
|
+
~~~
|
41
|
+
|
42
|
+
|
43
|
+
Library Overview
|
44
|
+
----------------
|
45
|
+
|
46
|
+
### Connecting ###
|
47
|
+
|
48
|
+
TLS/SSL is enabled by default:
|
49
|
+
|
50
|
+
~~~ ruby
|
51
|
+
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
|
52
|
+
~~~
|
53
|
+
|
54
|
+
License
|
55
|
+
-------
|
56
|
+
|
57
|
+
The baidu_aip_ruby_sdk ruby gem is licensed under the terms of the MIT license.
|
58
|
+
See the file LICENSE for details.
|
59
|
+
|
60
|
+
|
61
|
+
Contact
|
62
|
+
-------
|
63
|
+
|
64
|
+
* Author: LCola
|
65
|
+
* Email: developer@lcola.cn
|
66
|
+
* Home Page: [LCola]
|
67
|
+
|
68
|
+
|
69
|
+
[LCola]: https://www.lcola.cn/
|
70
|
+
[Rubygems]: http://rubygems.org/
|
71
|
+
[Bundler]: http://bundler.io/
|
data/lib/baidu_ai.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
class BaiduAi::AccessTokenManager
|
2
|
+
|
3
|
+
require 'singleton'
|
4
|
+
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
def get_access_token
|
8
|
+
if @token && !token_expired?
|
9
|
+
if BaiduAi.debug_mode && BaiduAi.logger
|
10
|
+
BaiduAi.logger.debug "Using existing access-token #{@token}"
|
11
|
+
end
|
12
|
+
return @token
|
13
|
+
end
|
14
|
+
get_and_save_new_token
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def token_expired?
|
20
|
+
Time.now - @created_time > @expires_in
|
21
|
+
end
|
22
|
+
|
23
|
+
def save_token response
|
24
|
+
@token = response['access_token']
|
25
|
+
@expires_in = response['expires_in']
|
26
|
+
@created_time = Time.now
|
27
|
+
if BaiduAi.debug_mode && BaiduAi.logger
|
28
|
+
BaiduAi.logger.debug "Saving acess-token #{@token}, expires_in #{@expires_in} seconds."
|
29
|
+
end
|
30
|
+
@token
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_and_save_new_token
|
34
|
+
url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=#{BaiduAi.api_key}&client_secret=#{BaiduAi.secret_key}"
|
35
|
+
response = JSON.parse(Net::HTTP.get(URI(url)))
|
36
|
+
save_token response
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module BaiduAi::OS
|
2
|
+
|
3
|
+
def self.windows?
|
4
|
+
(/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.mac?
|
8
|
+
(/darwin/ =~ RUBY_PLATFORM) != nil
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.unix?
|
12
|
+
!OS.windows?
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.linux?
|
16
|
+
OS.unix? and not OS.mac?
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.jruby?
|
20
|
+
RUBY_ENGINE == 'jruby'
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
require 'json'
|
4
|
+
require 'find'
|
5
|
+
require 'baidu_ai/os/os'
|
6
|
+
|
7
|
+
class BaiduAi::SpeechSynthesizer
|
8
|
+
|
9
|
+
def self.speak cuid, text
|
10
|
+
file_name = get_baidu_ai_speech cuid, text
|
11
|
+
system "#{play_command} #{file_name}"
|
12
|
+
File.delete file_name
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
@speech_dir = Dir.home + '/.baidu_ai_speech/'
|
18
|
+
|
19
|
+
def self.get_baidu_ai_speech cuid, text
|
20
|
+
response = baidu_ai_speech cuid, text
|
21
|
+
time = Time.now.strftime '%Y%m%d%H%M%S' + rand(10).to_s
|
22
|
+
if !File.directory?@speech_dir
|
23
|
+
Dir::mkdir(@speech_dir)
|
24
|
+
end
|
25
|
+
file_name = @speech_dir + time + ".mp3"
|
26
|
+
speech = File.new(file_name, "w+")
|
27
|
+
speech.puts(response.body)
|
28
|
+
speech.close
|
29
|
+
file_name
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.baidu_ai_speech(cuid, text, spd = 5, pit = 5)
|
33
|
+
uri = URI.parse("http://tsn.baidu.com/text2audio?")
|
34
|
+
if BaiduAi.debug_mode && BaiduAi.logger
|
35
|
+
BaiduAi.logger.debug "Requesting uri #{uri}..."
|
36
|
+
end
|
37
|
+
req = Net::HTTP::Post.new(uri)
|
38
|
+
res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
|
39
|
+
req.set_form_data({
|
40
|
+
"lan" => "zh",
|
41
|
+
"ctp" => "1",
|
42
|
+
"cuid" => cuid,
|
43
|
+
"tok" => BaiduAi::AccessTokenManager.instance.get_access_token,
|
44
|
+
"tex" => text,
|
45
|
+
"spd" => spd,
|
46
|
+
"pit" => pit
|
47
|
+
})
|
48
|
+
http.request req
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.play_command
|
53
|
+
if BaiduAi::OS.mac?
|
54
|
+
return 'afplay'
|
55
|
+
end
|
56
|
+
'play'
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: baidu_ai
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- LCola
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-03-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: An unofficial simple baidu ai ruby gem
|
28
|
+
email: developer@lcola.cn
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- LICENSE.md
|
34
|
+
- README.md
|
35
|
+
- lib/baidu_ai.rb
|
36
|
+
- lib/baidu_ai/assess_token/assess_token_manager.rb
|
37
|
+
- lib/baidu_ai/os/os.rb
|
38
|
+
- lib/baidu_ai/speech_synthesis/speech_synthesizer.rb
|
39
|
+
- lib/baidu_ai/version.rb
|
40
|
+
homepage: http://rubygems.org/gems/baidu_ai
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata: {}
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 2.6.14
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: Baidu AI ruby sdk
|
64
|
+
test_files: []
|