immosquare-translate 0.1.2 → 0.1.4
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 326589c34a947bb34891fdef41683bf0a0d2bc5dd649c0598d63d7591eac1341
|
4
|
+
data.tar.gz: e28ead0b001c5c3ffea1a322e35ba3293e7c916dbe71a8c677d9484efb86a5c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d74b478dbaa133200e77b2638e38bf986204ecfc5946b2653cb7687b40a8bfbfb28e6e163c772aac679f1c599169bc44c66c14cdb224be8fba830353ad21f69
|
7
|
+
data.tar.gz: bbf947a36c077cfc1b419936cac7a88e4cee75df414c11133f85172aa431cff10e95b5336e202171a18b34036a9964339be5515096ad554fa14a5d271b20e586
|
@@ -20,7 +20,7 @@ module ImmosquareTranslate
|
|
20
20
|
model = OPEN_AI_MODELS.find {|m| m[:name] == model_name }
|
21
21
|
model = OPEN_AI_MODELS.find {|m| m[:name] == "gpt-4-0125-preview" } if model.nil?
|
22
22
|
from_iso = ISO_639.find_by_code(from).english_name.split(";").first
|
23
|
-
to_iso = to.map {|
|
23
|
+
to_iso = to.map {|iso| [iso, ISO_639.find_by_code(iso).english_name.split(";").first] }
|
24
24
|
headers = {
|
25
25
|
"Content-Type" => "application/json",
|
26
26
|
"Authorization" => "Bearer #{ImmosquareTranslate.configuration.openai_api_key}"
|
@@ -28,19 +28,21 @@ module ImmosquareTranslate
|
|
28
28
|
|
29
29
|
prompt_system = "As a sophisticated translation AI, your role is to translate sentences from a specified source language to multiple target languages. " \
|
30
30
|
"It is imperative that you return the translations in a single, pure JSON string format. Use ISO 639-1 language codes for specifying languages. " \
|
31
|
-
"
|
31
|
+
"If string is html, you should return the translated html." \
|
32
32
|
"Ensure that the output does not include markdown (```json) or any other formatting characters. Adhere to the JSON structure meticulously."
|
33
33
|
|
34
34
|
|
35
|
-
prompt = "Translate the following sentences from
|
36
|
-
"
|
37
|
-
"
|
35
|
+
prompt = "Translate the #{text.size} following #{text.size == 1 ? "sentence" : "sentences"} from #{from_iso} (with ISO 639-1 code : #{from}) into the languages #{to_iso.map {|iso, l| "#{l} (with ISO 639-1 code : #{iso})" }.join(", ")}.\n" \
|
36
|
+
"Format the output as a single pure JSON string.\n" \
|
37
|
+
"Follow this array structure: {\"datas\":[{\"locale_iso\":\"Translated Text\"}]}, using the correct ISO 639-1 language codes for each translation. " \
|
38
|
+
"Your response should strictly conform to this JSON structure without any additional characters or formatting.\nSentences to translate are:"
|
38
39
|
|
39
40
|
text.each_with_index do |sentence, index|
|
40
41
|
prompt += "\n#{index + 1}: #{sentence}"
|
41
42
|
end
|
42
43
|
|
43
44
|
|
45
|
+
|
44
46
|
body = {
|
45
47
|
:model => model[:name],
|
46
48
|
:messages => [
|
@@ -74,8 +76,14 @@ module ImmosquareTranslate
|
|
74
76
|
puts("Estimate price => #{input_price.round(3)} + #{output_price.round(3)} = #{price.round(3)} USD")
|
75
77
|
|
76
78
|
|
79
|
+
##============================================================##
|
80
|
+
## On s'assure de ne renvoyer que les locales demandées
|
81
|
+
## car l'API peut renvoyer des locales non demandées...
|
82
|
+
##============================================================##
|
77
83
|
p = JSON.parse(choice["message"]["content"])
|
78
|
-
p["datas"]
|
84
|
+
p["datas"].map! do |my_hash|
|
85
|
+
my_hash.select! {|iso, _value| to.include?(iso.to_s) }
|
86
|
+
end
|
79
87
|
rescue StandardError => e
|
80
88
|
puts(e.message)
|
81
89
|
puts(e.backtrace)
|
data/lib/immosquare-translate.rb
CHANGED
@@ -2,7 +2,7 @@ require_relative "immosquare-translate/configuration"
|
|
2
2
|
require_relative "immosquare-translate/shared_methods"
|
3
3
|
require_relative "immosquare-translate/yml_translator"
|
4
4
|
require_relative "immosquare-translate/translator"
|
5
|
-
|
5
|
+
require_relative "immosquare-translate/railtie" if defined?(Rails)
|
6
6
|
|
7
7
|
|
8
8
|
##===========================================================================##
|
@@ -4,9 +4,9 @@ namespace :immosquare_translate do
|
|
4
4
|
## rake immosquare_translate:translate SOURCE_LOCALE=fr
|
5
5
|
##============================================================##
|
6
6
|
desc "Translate translation files in rails app"
|
7
|
-
task :
|
7
|
+
task :translate_rails_locales => :environment do
|
8
8
|
begin
|
9
|
-
source_locale = ENV.fetch("SOURCE_LOCALE", nil)
|
9
|
+
source_locale = ENV.fetch("SOURCE_LOCALE", nil) || "fr"
|
10
10
|
reset_translations = ENV.fetch("RESET_TRANSLATIONS", nil) || false
|
11
11
|
reset_translations = reset_translations == "true"
|
12
12
|
|
@@ -24,14 +24,4 @@ namespace :immosquare_translate do
|
|
24
24
|
puts(e.message)
|
25
25
|
end
|
26
26
|
end
|
27
|
-
|
28
|
-
##============================================================##
|
29
|
-
## Function to clean translation files in rails app
|
30
|
-
##============================================================##
|
31
|
-
desc "Clean translation files in rails app"
|
32
|
-
task :clean => :environment do
|
33
|
-
Dir.glob("#{Rails.root}/config/locales/**/*.yml").each do |file|
|
34
|
-
ImmosquareYaml.clean(file)
|
35
|
-
end
|
36
|
-
end
|
37
27
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: immosquare-translate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- IMMO SQUARE
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-04-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -71,11 +71,12 @@ extra_rdoc_files: []
|
|
71
71
|
files:
|
72
72
|
- lib/immosquare-translate.rb
|
73
73
|
- lib/immosquare-translate/configuration.rb
|
74
|
+
- lib/immosquare-translate/railtie.rb
|
74
75
|
- lib/immosquare-translate/shared_methods.rb
|
75
76
|
- lib/immosquare-translate/translator.rb
|
76
77
|
- lib/immosquare-translate/version.rb
|
77
78
|
- lib/immosquare-translate/yml_translator.rb
|
78
|
-
- lib/tasks/immosquare-
|
79
|
+
- lib/tasks/immosquare-translate.rake
|
79
80
|
homepage: https://github.com/IMMOSQUARE/immosquare-translate
|
80
81
|
licenses:
|
81
82
|
- MIT
|