baidu_aip_ruby_sdk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 678a506415a759f57b31f644b818059fedcd1204
4
+ data.tar.gz: 55f527301c75ddf15e3a62bdba2f9795fec69e5f
5
+ SHA512:
6
+ metadata.gz: 3cb0855d0383c04e45914f7e29825ad96be3575a1e4fd22e616daa65c0ee878946eed6956b425c36e3c4832398248424610041c37dfcc7071b7cb38e00ac40f4
7
+ data.tar.gz: ce7a3851d685e35a7e172f22a7c8051df689a128f9b34e6d2fe777327d2dbb182121e489d7d3f8047fbc475042d73c67ff43a7d7051fa27c661705939723deef
@@ -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.
@@ -0,0 +1,72 @@
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_aip_ruby_sdk
23
+
24
+ For bundler:
25
+
26
+ gem 'baidu_aip_ruby_sdk'
27
+
28
+
29
+ Quick Start
30
+ -----------
31
+
32
+ ~~~ ruby
33
+ require 'rubygems'
34
+ require 'baidu_aip_ruby_sdk'
35
+
36
+ # BaiduVoiceSynthesis example
37
+ BaiduAiPlatform.api_key = account_info['api_key']
38
+ BaiduAiPlatform.secret_key = account_info['secret_key']
39
+ BaiduAiPlatform::BaiduVoiceSynthesizer::.play_baidu_ai_voice 'QR-00000001', '横琴号十号线, 车辆即将到达创意谷,有 23 名乘客上车, 5 人下车'
40
+
41
+ ~~~
42
+
43
+
44
+ Library Overview
45
+ ----------------
46
+
47
+ ### Connecting ###
48
+
49
+ TLS/SSL is enabled by default:
50
+
51
+ ~~~ ruby
52
+ Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
53
+ ~~~
54
+
55
+ License
56
+ -------
57
+
58
+ The baidu_aip_ruby_sdk ruby gem is licensed under the terms of the MIT license.
59
+ See the file LICENSE for details.
60
+
61
+
62
+ Contact
63
+ -------
64
+
65
+ * Author: LCola
66
+ * Email: developer@lcola.cn
67
+ * Home Page: [LCola]
68
+
69
+
70
+ [LCola]: https://www.lcola.cn/
71
+ [Rubygems]: http://rubygems.org/
72
+ [Bundler]: http://bundler.io/
@@ -0,0 +1,39 @@
1
+ class BaiduAiPlatform::AccessTokenManager
2
+
3
+ require 'singleton'
4
+
5
+ include Singleton
6
+
7
+ def get_access_token
8
+ if @token && !token_expired?
9
+ if BaiduAiPlatform.debug_mode && BaiduAiPlatform.logger
10
+ BaiduAiPlatform.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 BaiduAiPlatform.debug_mode && BaiduAiPlatform.logger
28
+ BaiduAiPlatform.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=#{BaiduAiPlatform.api_key}&client_secret=#{BaiduAiPlatform.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 BaiduAiPlatform::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,3 @@
1
+ module BaiduAiPlatform
2
+ VERSION = '0.0.1'.freeze
3
+ end
@@ -0,0 +1,59 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'find'
5
+ require 'baidu_ai_platform/os/os'
6
+
7
+ class BaiduAiPlatform::VoiceSynthesizer
8
+
9
+ def self.play_ai_voice cuid, text
10
+ file_name = get_baidu_ai_voice cuid, text
11
+ system "#{play_command} #{file_name}"
12
+ File.delete file_name
13
+ end
14
+
15
+ private
16
+
17
+ @voice_dir = Dir.home + '/.baidu_ai_voice/'
18
+
19
+ def self.get_baidu_ai_voice cuid, text
20
+ response = baidu_ai_voice cuid, text
21
+ time = Time.now.strftime '%Y%m%d%H%M%S' + rand(10).to_s
22
+ if !File.directory?@voice_dir
23
+ Dir::mkdir(@voice_dir)
24
+ end
25
+ file_name = @voice_dir + time + ".mp3"
26
+ voice = File.new(file_name, "w+")
27
+ voice.puts(response.body)
28
+ voice.close
29
+ file_name
30
+ end
31
+
32
+ def self.baidu_ai_voice(cuid, text, spd = 5, pit = 5)
33
+ uri = URI.parse("http://tsn.baidu.com/text2audio?")
34
+ if BaiduAiPlatform.debug_mode && BaiduAiPlatform.logger
35
+ BaiduAiPlatform.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" => BaiduAiPlatform::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 BaiduAiPlatform::OS.mac?
54
+ return 'afplay'
55
+ end
56
+ 'play'
57
+ end
58
+
59
+ end
@@ -0,0 +1,13 @@
1
+
2
+ module BaiduAiPlatform
3
+
4
+ @debug_mode = true
5
+
6
+ class<< self
7
+ attr_accessor :logger, :debug_mode, :api_key, :secret_key
8
+ end
9
+
10
+ require 'baidu_ai_platform/assess_token/assess_token_manager'
11
+ require 'baidu_ai_platform/voice_synthesis/voice_synthesizer'
12
+
13
+ end
@@ -0,0 +1,20 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'yaml'
5
+ require 'logger'
6
+
7
+ describe BaiduAiPlatform::VoiceSynthesizer do
8
+
9
+ it 'get_baidu_ai_voice should be successful' do
10
+ account_info = YAML.load_file('./spec/fixtures/baidu_voice_synthesis.yml')
11
+ BaiduAiPlatform.debug_mode = true
12
+ BaiduAiPlatform.logger = Logger.new STDOUT
13
+ BaiduAiPlatform.api_key = account_info['api_key']
14
+ BaiduAiPlatform.secret_key = account_info['secret_key']
15
+ BaiduAiPlatform::VoiceSynthesizer.play_ai_voice 'QR-000000001', '横琴号十号线, 车辆即将到达创意谷,有 23 名乘客上车, 5 人下车'
16
+ BaiduAiPlatform::VoiceSynthesizer.play_ai_voice 'QR-000000001', '横琴号十号线, 车辆即将到达银鑫花园,有 8 名乘客上车, 0 人下车'
17
+ BaiduAiPlatform::VoiceSynthesizer.play_ai_voice 'QR-000000001', '横琴号十号线, 车辆即将到达横琴金融中心,有 6 名乘客上车, 0 人下车'
18
+ end
19
+
20
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: baidu_aip_ruby_sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
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_platform/assess_token/assess_token_manager.rb
36
+ - lib/baidu_ai_platform/os/os.rb
37
+ - lib/baidu_ai_platform/version.rb
38
+ - lib/baidu_ai_platform/voice_synthesis/voice_synthesizer.rb
39
+ - lib/baidu_aip_ruby_sdk.rb
40
+ - spec/baidu_voice_synthesis_spec.rb
41
+ homepage: http://rubygems.org/gems/baidu_aip_ruby_sdk
42
+ licenses:
43
+ - MIT
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.6.14
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Baidu AI ruby sdk
65
+ test_files:
66
+ - spec/baidu_voice_synthesis_spec.rb