huginn_tts_agent 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0ca5b4cb1928e51baa1263ca347bcfba5df46698f25e7fe45487706485e7e74e
4
+ data.tar.gz: 633ce2dfd0bc804364023c1b6764c9f00768f00b8f2e98c58a49f9846c3c6fde
5
+ SHA512:
6
+ metadata.gz: b3b0a62bad98c0d86c2076cac7414d5a54e16367645cf8d9370c01dac29851771d44783b50fb5728bfed305c7ecf622a0f0dfb64638b4cdf3143fc5d34aed266
7
+ data.tar.gz: e116d8385f6a5a53df6422aed4cf7f6ea775bf5253b5a13d7ac0b44720c7750e2ca2cc26093cef4a6cd15b594cf36057c0deb13f70c5fa957d5c550efd53a4a6
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2023 Nicolas Germain
2
+
3
+ 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:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ 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,176 @@
1
+ module Agents
2
+ class TtsAgent < Agent
3
+ include FormConfigurable
4
+ can_dry_run!
5
+ no_bulk_receive!
6
+ default_schedule 'never'
7
+
8
+ description do
9
+ <<-MD
10
+ The Tts Agent interacts with different providers like Elevenlabs's api.
11
+
12
+ The `provider` can be like elevenlabs.
13
+
14
+ `debug` is used for verbose mode.
15
+
16
+ `api_key` is mandatory for the authentication.
17
+
18
+ `text` is the wanted text to convert.
19
+
20
+ `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
21
+ that you anticipate passing without this Agent receiving an incoming Event.
22
+ MD
23
+ end
24
+
25
+ event_description <<-MD
26
+ Events look like this:
27
+
28
+ {
29
+ "history_item_id": "XXXXXXXXXXXXXXXXXXXX",
30
+ "request_id": "XXXXXXXXXXXXXXXXXXXX",
31
+ "voice_id": "21m00Tcm4TlvDq8ikWAM",
32
+ "model_id": "eleven_multilingual_v2",
33
+ "voice_name": "XXXXXX",
34
+ "voice_category": "premade",
35
+ "text": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
36
+ "date_unix": 1704351242,
37
+ "character_count_change_from": 0,
38
+ "character_count_change_to": XX,
39
+ "content_type": "audio/mpeg",
40
+ "state": "created",
41
+ "settings": {
42
+ "similarity_boost": 0.75,
43
+ "stability": 0.5,
44
+ "style": 0.0,
45
+ "use_speaker_boost": true
46
+ },
47
+ "feedback": null,
48
+ "share_link_id": null
49
+ }
50
+ MD
51
+
52
+ def default_options
53
+ {
54
+ 'type' => 'elevenlabs',
55
+ 'api_key' => '',
56
+ 'text' => '',
57
+ 'debug' => 'false',
58
+ 'expected_receive_period_in_days' => '2',
59
+ }
60
+ end
61
+
62
+ form_configurable :type, type: :array, values: ['elevenlabs']
63
+ form_configurable :api_key, type: :string
64
+ form_configurable :text, type: :string
65
+ form_configurable :debug, type: :boolean
66
+ form_configurable :expected_receive_period_in_days, type: :string
67
+ def validate_options
68
+ errors.add(:base, "type has invalid value: should be 'elevenlabs'") if interpolated['type'].present? && !%w(elevenlabs).include?(interpolated['type'])
69
+
70
+ unless options['api_key'].present? || !['elevenlabs'].include?(options['type'])
71
+ errors.add(:base, "api_key is a required field")
72
+ end
73
+
74
+ unless options['text'].present?
75
+ errors.add(:base, "text is a required field")
76
+ end
77
+
78
+ if options.has_key?('debug') && boolify(options['debug']).nil?
79
+ errors.add(:base, "if provided, debug must be true or false")
80
+ end
81
+
82
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
83
+ errors.add(:base, "Please provide 'expected_receive_period_in_days' to indicate how many days can pass before this Agent is considered to be not working")
84
+ end
85
+ end
86
+
87
+ def working?
88
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
89
+ end
90
+
91
+ def receive(incoming_events)
92
+ incoming_events.each do |event|
93
+ interpolate_with(event) do
94
+ log event
95
+ trigger_action
96
+ end
97
+ end
98
+ end
99
+
100
+ def check
101
+ trigger_action
102
+ end
103
+
104
+ private
105
+
106
+ def set_credential(name, value)
107
+ c = user.user_credentials.find_or_initialize_by(credential_name: name)
108
+ c.credential_value = value
109
+ c.save!
110
+ end
111
+
112
+ def log_curl_output(code,body)
113
+
114
+ log "request status : #{code}"
115
+
116
+ if interpolated['debug'] == 'true'
117
+ log "request status : #{code}"
118
+ log "body"
119
+ log body
120
+ end
121
+
122
+ end
123
+
124
+ def elevenlabs()
125
+
126
+ uri = URI.parse("https://api.elevenlabs.io/v1/text-to-speech/21m00Tcm4TlvDq8ikWAM")
127
+ request = Net::HTTP::Post.new(uri)
128
+ request.content_type = "application/json"
129
+ request["Xi-Api-Key"] = interpolated['api_key']
130
+ request.body = JSON.dump({
131
+ "model_id" => "eleven_multilingual_v2",
132
+ "text" => interpolated['text']
133
+ })
134
+
135
+ req_options = {
136
+ use_ssl: uri.scheme == "https",
137
+ }
138
+
139
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
140
+ http.request(request)
141
+ end
142
+
143
+ log_curl_output(response.code,response.body)
144
+
145
+ uri = URI.parse("https://api.elevenlabs.io/v1/history")
146
+ request = Net::HTTP::Get.new(uri)
147
+ request["Xi-Api-Key"] = interpolated['api_key']
148
+
149
+ req_options = {
150
+ use_ssl: uri.scheme == "https",
151
+ }
152
+
153
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
154
+ http.request(request)
155
+ end
156
+
157
+ log_curl_output(response.code,response.body)
158
+
159
+ payload = JSON.parse(response.body)
160
+
161
+ last_item = payload['last_history_item_id']
162
+ result_hash = payload['history'].find { |hash| hash["history_item_id"] == last_item }
163
+ create_event payload: result_hash
164
+ end
165
+
166
+ def trigger_action
167
+
168
+ case interpolated['type']
169
+ when "elevenlabs"
170
+ elevenlabs()
171
+ else
172
+ log "Error: type has an invalid value (#{type})"
173
+ end
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_tts_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_tts_agent/tts_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::TtsAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::TtsAgent.new.default_options
7
+ @checker = Agents::TtsAgent.new(:name => "TtsAgent", :options => @valid_options)
8
+ @checker.user = users(:bob)
9
+ @checker.save!
10
+ end
11
+
12
+ pending "add specs here"
13
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huginn_tts_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Germain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-01-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 12.3.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 12.3.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: huginn_agent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Write a longer description or delete this line.
56
+ email:
57
+ - ngermain@hihouhou.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE.txt
63
+ - lib/huginn_tts_agent.rb
64
+ - lib/huginn_tts_agent/tts_agent.rb
65
+ - spec/tts_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_tts_agent
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.3.3
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Write a short summary, because Rubygems requires one.
89
+ test_files:
90
+ - spec/tts_agent_spec.rb