File size: 5,855 Bytes
402daee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
--[[

ReaSpeechControlsUI.lua - UI elements for configuring ASR services

]]--

ReaSpeechControlsUI = Polo {
  -- Copied from whisper.tokenizer.LANGUAGES
  LANGUAGES = {
    en = 'english', zh = 'chinese', de = 'german',
    es = 'spanish', ru = 'russian', ko = 'korean',
    fr = 'french', ja = 'japanese', pt = 'portuguese',
    tr = 'turkish', pl = 'polish', ca = 'catalan',
    nl = 'dutch', ar = 'arabic', sv = 'swedish',
    it = 'italian', id = 'indonesian', hi = 'hindi',
    fi = 'finnish', vi = 'vietnamese', he = 'hebrew',
    uk = 'ukrainian', el = 'greek', ms = 'malay',
    cs = 'czech', ro = 'romanian', da = 'danish',
    hu = 'hungarian', ta = 'tamil', no = 'norwegian',
    th = 'thai', ur = 'urdu', hr = 'croatian',
    bg = 'bulgarian', lt = 'lithuanian', la = 'latin',
    mi = 'maori', ml = 'malayalam', cy = 'welsh',
    sk = 'slovak', te = 'telugu', fa = 'persian',
    lv = 'latvian', bn = 'bengali', sr = 'serbian',
    az = 'azerbaijani', sl = 'slovenian', kn = 'kannada',
    et = 'estonian', mk = 'macedonian', br = 'breton',
    eu = 'basque', is = 'icelandic', hy = 'armenian',
    ne = 'nepali', mn = 'mongolian', bs = 'bosnian',
    kk = 'kazakh', sq = 'albanian', sw = 'swahili',
    gl = 'galician', mr = 'marathi', pa = 'punjabi',
    si = 'sinhala', km = 'khmer', sn = 'shona',
    yo = 'yoruba', so = 'somali', af = 'afrikaans',
    oc = 'occitan', ka = 'georgian', be = 'belarusian',
    tg = 'tajik', sd = 'sindhi', gu = 'gujarati',
    am = 'amharic', yi = 'yiddish', lo = 'lao',
    uz = 'uzbek', fo = 'faroese', ht = 'haitian creole',
    ps = 'pashto', tk = 'turkmen', nn = 'nynorsk',
    mt = 'maltese', sa = 'sanskrit', lb = 'luxembourgish',
    my = 'myanmar', bo = 'tibetan', tl = 'tagalog',
    mg = 'malagasy', as = 'assamese', tt = 'tatar',
    haw = 'hawaiian', ln = 'lingala', ha = 'hausa',
    ba = 'bashkir', jw = 'javanese', su = 'sundanese'
  },
  LANGUAGE_CODES = {},
  DEFAULT_LANGUAGE = 'en',

  ITEM_WIDTH = 125,
  LARGE_ITEM_WIDTH = 375,
}

function ReaSpeechControlsUI:init()
  self.log_enable = false
  self.log_debug = false

  self.language = self.DEFAULT_LANGUAGE
  self.translate = false
  self.initial_prompt = ''
  self.model_name = nil
end

function ReaSpeechControlsUI:get_request_data()
  return {
    language = self.language,
    translate = self.translate,
    initial_prompt = self.initial_prompt,
    model_name = self.model_name,
  }
end

ReaSpeechControlsUI._init_languages = function ()
  for code, _ in pairs(ReaSpeechControlsUI.LANGUAGES) do
    table.insert(ReaSpeechControlsUI.LANGUAGE_CODES, code)
  end

  table.sort(ReaSpeechControlsUI.LANGUAGE_CODES, function (a, b)
    return ReaSpeechControlsUI.LANGUAGES[a] < ReaSpeechControlsUI.LANGUAGES[b]
  end)

  table.insert(ReaSpeechControlsUI.LANGUAGE_CODES, 1, '')
  ReaSpeechControlsUI.LANGUAGES[''] = 'detect'
end

ReaSpeechControlsUI._init_languages()

function ReaSpeechControlsUI:render()
  --start input table so logo and inputs sit side-by-side
  if ImGui.BeginTable(ctx, 'InputTable', 2) then
    app:trap(function()
      --column settings
      ImGui.TableSetupColumn(ctx, 'Logo', ImGui.TableColumnFlags_WidthFixed())
      ImGui.TableSetupColumn(ctx, 'Inputs', ImGui.TableColumnFlags_WidthFixed())
      -- first column
      ImGui.TableNextColumn(ctx)
      ImGui.SameLine(ctx, -10)
      app.png_from_bytes('reaspeech-logo-small')
      -- second column
      ImGui.TableNextColumn(ctx)
      -- start language selection
      self:render_language_controls()
      ImGui.Dummy(ctx,0, 10)
      self:render_advanced_controls()
    end)
    ImGui.EndTable(ctx)
  end
  -- end input table
  ImGui.SameLine(ctx, ImGui.GetWindowWidth(ctx) - self.ITEM_WIDTH + 65)
  app.png_from_bytes('heading-logo-tech-audio')
end

function ReaSpeechControlsUI:render_language_controls()
  if ImGui.TreeNode(ctx, 'Language Options', ImGui.TreeNodeFlags_DefaultOpen()) then
    app:trap(function()
      ImGui.Dummy(ctx, 0, 25)
      ImGui.SameLine(ctx)
      if ImGui.BeginCombo(ctx, "language", self.LANGUAGES[self.language]) then
        app:trap(function()
          local combo_items = self.LANGUAGE_CODES
          for _, combo_item in pairs(combo_items) do
            local is_selected = (combo_item == self.language)
            if ImGui.Selectable(ctx, self.LANGUAGES[combo_item], is_selected) then
              self.language = combo_item
            end
          end
        end)
        ImGui.EndCombo(ctx)
      end
      local rv, value
      ImGui.SameLine(ctx)
      rv, value = ImGui.Checkbox(ctx, "translate", self.translate)
      if rv then
        self.translate = value
      end
    end)

    ImGui.TreePop(ctx)
  end
end

function ReaSpeechControlsUI:render_advanced_controls()
  local rv, value

  if ImGui.TreeNode(ctx, 'Advanced Options') then
    app:trap(function()
      ImGui.Dummy(ctx, 0, 25)

      ImGui.SameLine(ctx)
      ImGui.PushItemWidth(ctx, self.LARGE_ITEM_WIDTH)
      app:trap(function ()
        rv, value = ImGui.InputText(ctx, 'initial prompt', self.initial_prompt)
        if rv then
          self.initial_prompt = value
        end
      end)
      ImGui.PopItemWidth(ctx)

      ImGui.SameLine(ctx)
      ImGui.PushItemWidth(ctx, 100)
      app:trap(function ()
        rv, value = ImGui.InputTextWithHint(ctx, 'model name', self.model_name or "<default>")
        if rv then
          self.model_name = value
        end
      end)
      ImGui.PopItemWidth(ctx)

      ImGui.SameLine(ctx)
      rv, value = ImGui.Checkbox(ctx, "log", self.log_enable)
      if rv then
        self.log_enable = value
      end

      if self.log_enable then
        ImGui.SameLine(ctx)
        rv, value = ImGui.Checkbox(ctx, "debug", self.log_debug)
        if rv then
          self.log_debug = value
        end
      end
    end)

    ImGui.TreePop(ctx)
    ImGui.Spacing(ctx)
  end
end