glimmer-dsl-libui 0.5.22 → 0.5.23
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 +4 -0
- data/README.md +12 -7
- data/VERSION +1 -1
- data/docs/examples/GLIMMER-DSL-LIBUI-ADVANCED-EXAMPLES.md +167 -2707
- data/examples/gpt2_notepad.rb +107 -0
- data/glimmer-dsl-libui.gemspec +0 -0
- metadata +3 -2
@@ -0,0 +1,107 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'onnxruntime'
|
4
|
+
require 'blingfire'
|
5
|
+
require 'numo/narray'
|
6
|
+
require 'fileutils'
|
7
|
+
require 'glimmer-dsl-libui'
|
8
|
+
|
9
|
+
# GPT-2 model
|
10
|
+
# Transformer-based language model for text generation.
|
11
|
+
# https://github.com/onnx/models/tree/main/text/machine_comprehension/gpt-2
|
12
|
+
class GPT2TextPredictor
|
13
|
+
attr_accessor :text
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@text = ''
|
17
|
+
base_dir = File.join(Dir.home, '.gpt2-notepad')
|
18
|
+
FileUtils.mkdir_p(base_dir)
|
19
|
+
Dir.chdir(__dir__) do
|
20
|
+
downloaded_files = %w[
|
21
|
+
https://github.com/microsoft/BlingFire/raw/master/dist-pypi/blingfire/gpt2.bin
|
22
|
+
https://github.com/microsoft/BlingFire/raw/master/dist-pypi/blingfire/gpt2.i2w
|
23
|
+
https://github.com/onnx/models/raw/main/text/machine_comprehension/gpt-2/model/gpt2-lm-head-10.onnx
|
24
|
+
].map do |url|
|
25
|
+
fname = File.join(base_dir, File.basename(url))
|
26
|
+
next(fname) if File.exist?(fname)
|
27
|
+
|
28
|
+
print "Downloading #{fname}..."
|
29
|
+
require 'open-uri'
|
30
|
+
File.binwrite(fname, URI.open(url).read)
|
31
|
+
puts 'done'
|
32
|
+
fname
|
33
|
+
end
|
34
|
+
@encoder = BlingFire.load_model(downloaded_files[0])
|
35
|
+
@decoder = BlingFire.load_model(downloaded_files[1])
|
36
|
+
@model = OnnxRuntime::Model.new(downloaded_files[2])
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def softmax(y)
|
41
|
+
Numo::NMath.exp(y) / Numo::NMath.exp(y).sum(1, keepdims: true)
|
42
|
+
end
|
43
|
+
|
44
|
+
def predict(a, prob: true)
|
45
|
+
outputs = @model.predict({ input1: [[a]] })
|
46
|
+
logits = Numo::DFloat.cast(outputs['output1'][0])
|
47
|
+
logits = logits[true, -1, true]
|
48
|
+
return logits.argmax unless prob
|
49
|
+
|
50
|
+
log_probs = softmax(logits)
|
51
|
+
idx = log_probs.sort_index
|
52
|
+
i = (log_probs[idx].cumsum < rand).count
|
53
|
+
idx[i]
|
54
|
+
end
|
55
|
+
|
56
|
+
def predict_text(max = 30)
|
57
|
+
a = @encoder.text_to_ids(text)
|
58
|
+
max.times do
|
59
|
+
id = predict(a)
|
60
|
+
a << id
|
61
|
+
break if id == 13 # .
|
62
|
+
end
|
63
|
+
self.text = @decoder.ids_to_text(a)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
class GPT2Notepad
|
68
|
+
include Glimmer::LibUI::Application
|
69
|
+
|
70
|
+
before_body do
|
71
|
+
@text_predictor = GPT2TextPredictor.new
|
72
|
+
end
|
73
|
+
|
74
|
+
body {
|
75
|
+
window('Notepad', 500, 300) {
|
76
|
+
vertical_box {
|
77
|
+
padded true
|
78
|
+
|
79
|
+
horizontal_box {
|
80
|
+
stretchy false
|
81
|
+
|
82
|
+
button('Clear') {
|
83
|
+
on_clicked do
|
84
|
+
@text_predictor.text = ''
|
85
|
+
end
|
86
|
+
}
|
87
|
+
|
88
|
+
button('Continue the sentence(s)') {
|
89
|
+
on_clicked do
|
90
|
+
if @text_predictor.text.empty?
|
91
|
+
msg_box('Empty!', 'Please enter some text first.')
|
92
|
+
else
|
93
|
+
@text_predictor.predict_text
|
94
|
+
end
|
95
|
+
end
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
multiline_entry {
|
100
|
+
text <=> [@text_predictor, :text]
|
101
|
+
}
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
end
|
106
|
+
|
107
|
+
GPT2Notepad.launch
|
data/glimmer-dsl-libui.gemspec
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: glimmer-dsl-libui
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.23
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Maleh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: glimmer
|
@@ -344,6 +344,7 @@ files:
|
|
344
344
|
- examples/form_table3.rb
|
345
345
|
- examples/form_table4.rb
|
346
346
|
- examples/form_table5.rb
|
347
|
+
- examples/gpt2_notepad.rb
|
347
348
|
- examples/grid.rb
|
348
349
|
- examples/histogram.rb
|
349
350
|
- examples/histogram2.rb
|