natsukantou 0.1.3 → 0.2.1
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 +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +1 -0
- data/lib/natsukantou/chat_gpt.rb +78 -0
- data/lib/natsukantou/handle_ruby_markup.rb +1 -1
- data/lib/natsukantou/version.rb +1 -1
- data/lib/natsukantou.rb +1 -0
- data/natsukantou.gemspec +4 -1
- metadata +19 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d29dec07a03407ea767c53dce9760627262abf2fb35d3f9888659be3c2442efa
|
4
|
+
data.tar.gz: 36b30c5b5fff59a62293acdf660fd7f508c0b8bfc20c1882c3f7139b7ba4a06c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 07cb8f6e26187a971f0ff080767b7662a62ce7f0eb7730c647586ea351925ba94190156400ac37d3012d8bdaa7f848bd7456dc58b0216cb5e5ccb386ba9d4be5
|
7
|
+
data.tar.gz: da43175d1d13f63a0c2255cdc110257e35e54d8164ec65fbbdab1c81c1eff01b146db8e5239cef8312bfea87802e6be9258dcf42401991a72e379f35d5ba4288
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'openai'
|
4
|
+
|
5
|
+
# Machine translator
|
6
|
+
#
|
7
|
+
# ChatGPT
|
8
|
+
module Natsukantou
|
9
|
+
class ChatGpt
|
10
|
+
include UtilityBase
|
11
|
+
|
12
|
+
# @param app [Hash]
|
13
|
+
# @param access_token [String] API access token
|
14
|
+
#
|
15
|
+
# @param translate_by_section [String] CSS selector to translate matched elements one by one.
|
16
|
+
# Specify a css path (e.g. "body>p") to translate in smaller chunks.
|
17
|
+
def initialize(
|
18
|
+
app, access_token:,
|
19
|
+
translate_by_section:
|
20
|
+
)
|
21
|
+
@app = app
|
22
|
+
@access_token = access_token
|
23
|
+
|
24
|
+
### Non request related setting
|
25
|
+
@translate_by_section = translate_by_section
|
26
|
+
end
|
27
|
+
|
28
|
+
attr_reader :env
|
29
|
+
attr_reader :access_token
|
30
|
+
attr_reader :translate_by_section
|
31
|
+
|
32
|
+
def call(env)
|
33
|
+
@env = env
|
34
|
+
|
35
|
+
if translate_by_section
|
36
|
+
env[:dom].css(translate_by_section).each do |node|
|
37
|
+
next if node.text.empty?
|
38
|
+
|
39
|
+
translated_xml = translate(node.to_xml)
|
40
|
+
node.replace(dom_node(translated_xml))
|
41
|
+
end
|
42
|
+
else
|
43
|
+
# TODO
|
44
|
+
end
|
45
|
+
|
46
|
+
env[:dom].lang = env[:lang_to].code
|
47
|
+
|
48
|
+
@app.call(env)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def translate(text)
|
54
|
+
message = "Please help me to translate the XML `#{text}` from #{env.lang_from.code} to #{env.lang_to.code}, maintaining XML structure." # rubocop:disable Layout/LineLength
|
55
|
+
|
56
|
+
response = client.chat(
|
57
|
+
parameters: {
|
58
|
+
model: "gpt-3.5-turbo",
|
59
|
+
messages: [{ role: "user", content: message }],
|
60
|
+
temperature: 0.7,
|
61
|
+
}
|
62
|
+
)
|
63
|
+
|
64
|
+
translated_xml = response.dig("choices", 0, "message", "content")
|
65
|
+
|
66
|
+
translated_xml.strip!
|
67
|
+
|
68
|
+
translated_xml
|
69
|
+
rescue Net::OpenTimeout
|
70
|
+
sleep 10
|
71
|
+
retry
|
72
|
+
end
|
73
|
+
|
74
|
+
def client
|
75
|
+
@client ||= ::OpenAI::Client.new(access_token: access_token)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/lib/natsukantou/version.rb
CHANGED
data/lib/natsukantou.rb
CHANGED
@@ -33,6 +33,7 @@ module Natsukantou
|
|
33
33
|
# Translators
|
34
34
|
autoload_and_register :translator, :DeepL, "natsukantou/deep_l"
|
35
35
|
autoload_and_register :translator, :Minhon, "natsukantou/minhon"
|
36
|
+
autoload_and_register :translator, :ChatGpt, "natsukantou/chat_gpt"
|
36
37
|
|
37
38
|
# Middlewares
|
38
39
|
autoload_and_register :middleware, :SubstitudeGlossary, "natsukantou/substitude_glossary"
|
data/natsukantou.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.email = ["mark@goodlife.tw"]
|
10
10
|
|
11
11
|
spec.summary = "human language translation library for XML documents"
|
12
|
-
spec.description = "human language translation library for XML documents, supporting DeepL"
|
12
|
+
spec.description = "human language translation library for XML documents, supporting DeepL and ChatGPT"
|
13
13
|
spec.homepage = "https://gitlab.com/lulalala/natsukantou"
|
14
14
|
spec.license = "MIT"
|
15
15
|
spec.required_ruby_version = ">= 2.7.0"
|
@@ -42,6 +42,9 @@ Gem::Specification.new do |spec|
|
|
42
42
|
# Minhon
|
43
43
|
spec.add_dependency 'oauth', '~> 0.5.1'
|
44
44
|
|
45
|
+
# ChatGPT
|
46
|
+
spec.add_dependency 'ruby-openai', '~> 3.5.0'
|
47
|
+
|
45
48
|
# For more information and examples about making a new gem, check out our
|
46
49
|
# guide at: https://bundler.io/guides/creating_gem.html
|
47
50
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: natsukantou
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- lulalala
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: middleware
|
@@ -108,7 +108,22 @@ dependencies:
|
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: 0.5.1
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: ruby-openai
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 3.5.0
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 3.5.0
|
111
125
|
description: human language translation library for XML documents, supporting DeepL
|
126
|
+
and ChatGPT
|
112
127
|
email:
|
113
128
|
- mark@goodlife.tw
|
114
129
|
executables:
|
@@ -128,6 +143,7 @@ files:
|
|
128
143
|
- lib/monkey_patch/oga_document.rb
|
129
144
|
- lib/monkey_patch/runner.rb
|
130
145
|
- lib/natsukantou.rb
|
146
|
+
- lib/natsukantou/chat_gpt.rb
|
131
147
|
- lib/natsukantou/deep_l.rb
|
132
148
|
- lib/natsukantou/handle_ruby_markup.rb
|
133
149
|
- lib/natsukantou/minhon.rb
|
@@ -167,7 +183,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
183
|
- !ruby/object:Gem::Version
|
168
184
|
version: '0'
|
169
185
|
requirements: []
|
170
|
-
rubygems_version: 3.4.
|
186
|
+
rubygems_version: 3.4.12
|
171
187
|
signing_key:
|
172
188
|
specification_version: 4
|
173
189
|
summary: human language translation library for XML documents
|