Skip to content

Commit f37093e

Browse files
chore: autopublish 2023-09-23T21:42:55Z
1 parent 4114d51 commit f37093e

File tree

3 files changed

+83
-57
lines changed

3 files changed

+83
-57
lines changed

dist/lyrics_openai_hyphenation.lua

Lines changed: 81 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4504,7 +4504,6 @@ package.preload["library.openai"] = package.preload["library.openai"] or functio
45044504
local API_VERSION = 'v1'
45054505
local OPEN_AI_URL = ORIGIN .. "/" ..API_VERSION
45064506
local COMPLETION_URL = OPEN_AI_URL .. "/chat/completions"
4507-
local CHAT_URL = OPEN_AI_URL .. "/chat"
45084507
openai_api_key = openai_api_key or "invalid key"
45094508
local openai = {}
45104509
local function call_openai(url, body, callback_or_timeout)
@@ -4521,7 +4520,6 @@ package.preload["library.openai"] = package.preload["library.openai"] or functio
45214520
jsresult = result
45224521
end
45234522
end
4524-
print("openai returned: "..result)
45254523
if type(callback_or_timeout) == "function" then
45264524
callback_or_timeout(success, jsresult)
45274525
else
@@ -4549,17 +4547,6 @@ package.preload["library.openai"] = package.preload["library.openai"] or functio
45494547
}
45504548
return call_openai(COMPLETION_URL, body, callback_or_timeout)
45514549
end
4552-
4553-
function openai.create_chat(model, prompt, temperature, max_tokens, callback_or_timeout)
4554-
callback_or_timeout = callback_or_timeout or 5.0
4555-
local body = {
4556-
model = model,
4557-
prompt = prompt,
4558-
temperature = temperature,
4559-
max_tokens = max_tokens
4560-
}
4561-
return call_openai(CHAT_URL, body, callback_or_timeout)
4562-
end
45634550
return openai
45644551
end
45654552
package.preload["library.utils"] = package.preload["library.utils"] or function()
@@ -4892,7 +4879,7 @@ function plugindef()
48924879
Check the pricing at the OpenAI site.
48934880
]]
48944881
finaleplugin.HashURL = "https://raw.githubusercontent.com/finale-lua/lua-scripts/master/hash/lyrics_openai_hyphenation.hash"
4895-
return "Lyrics Hyphenation", "Lyrics Hyphenation",
4882+
return "Lyrics Hyphenation...", "Lyrics Hyphenation",
48964883
"Add or correct lyrics hypenation using your OpenAI account."
48974884
end
48984885
local mixin = require("library.mixin")
@@ -4907,10 +4894,11 @@ local config =
49074894
Hyphenate the following text, delimiting words with spaces and syllables with hyphens.
49084895
If a word has multiple options for hyphenation, choose the one with the most syllables.
49094896
If words are already hyphenated, correct any mistakes found.
4910-
Exclude Text:
4911-
^font(...)
4912-
^size(...)
4913-
^nfx(...)
4897+
Do not modify text with the following patterns (where [TEXT_PLACEHOLDER] is any sequence of characters):
4898+
^font([TEXT_PLACEHOLDER])
4899+
^Font([TEXT_PLACEHOLDER])
4900+
^size([TEXT_PLACEHOLDER])
4901+
^nfx([TEXT_PLACEHOLDER])
49144902
Special Processing:
49154903
Do not modify line endings.
49164904
Identify the language. If it is a language that does not use spaces, nevertheless separate each word with a space and each pronounced syllable inside each word with a hyphen.
@@ -4919,10 +4907,11 @@ Input:
49194907
remove_hyphens_prompt = [[
49204908
Remove hyphens from the following text that has been used for musical text underlay.
49214909
If a word should be hyphenated according to non-musical usage, leave those hyphens in place.
4922-
Exclude Text:
4923-
^font(...)
4924-
^size(...)
4925-
^nfx(...)
4910+
Do not modify text with the following patterns (where [TEXT_PLACEHOLDER] is any sequence of characters):
4911+
^font([TEXT_PLACEHOLDER])
4912+
^Font([TEXT_PLACEHOLDER])
4913+
^size([TEXT_PLACEHOLDER])
4914+
^nfx([TEXT_PLACEHOLDER])
49264915
Special Processing:
49274916
Do not remove any punctuation other than hyphens.
49284917
Do not modify line endings.
@@ -4946,8 +4935,10 @@ local lyrics_prefs =
49464935
}
49474936
config.use_edit_control = config.use_edit_control and (finenv.UI():IsOnMac() or finale.FCCtrlEditText)
49484937
local use_edit_text
4938+
local use_active_lyric = finale.FCActiveLyric ~= nil
49494939
https_session = nil
49504940
update_automatically = true
4941+
global_timer_id = 1
49514942
local function fixup_line_endings(input_str)
49524943
local replacement = "\r"
49534944
if finenv:UI():IsOnWindows() then
@@ -4967,7 +4958,26 @@ local function fixup_line_endings(input_str)
49674958
end
49684959
return result
49694960
end
4970-
local function update_dlg_text(lyrics_box, itemno, type)
4961+
local function update_to_active_lyric(edit_type, popup)
4962+
if not use_active_lyric then return end
4963+
local selected_text = finale.FCString()
4964+
popup:GetText(selected_text)
4965+
name = selected_text.LuaString
4966+
finenv.StartNewUndoBlock("Update Current Lyric to "..name.." "..edit_type:GetInteger(), false)
4967+
local active_lyric = finale.FCActiveLyric()
4968+
if active_lyric:Load() then
4969+
if active_lyric.BlockType ~= popup:GetSelectedItem() + 1 or active_lyric.TextBlockID ~= edit_type:GetInteger() then
4970+
active_lyric.BlockType = popup:GetSelectedItem() + 1
4971+
active_lyric.TextBlockID = edit_type:GetInteger()
4972+
active_lyric.Syllable = 1
4973+
active_lyric:Save()
4974+
end
4975+
end
4976+
finenv.EndUndoBlock(true)
4977+
end
4978+
local function update_dlg_text(lyrics_box, edit_type, popup)
4979+
local itemno = edit_type:GetInteger()
4980+
local type = popup:GetSelectedItem() + 1
49714981
local lyrics_instance = lyrics_classes[type]()
49724982
if lyrics_instance:Load(itemno) then
49734983
local fcstr = lyrics_instance:CreateString()
@@ -4994,11 +5004,17 @@ local function update_dlg_text(lyrics_box, itemno, type)
49945004
lyrics_box:SetText("")
49955005
end
49965006
end
5007+
update_to_active_lyric(edit_type, popup)
49975008
end
4998-
local function update_document(lyrics_box, itemno, type, name)
5009+
local function update_document(lyrics_box, edit_type, popup)
49995010
if https_session then
50005011
return
50015012
end
5013+
local itemno = edit_type:GetInteger()
5014+
local type = popup:GetSelectedItem() + 1
5015+
local selected_text = finale.FCString()
5016+
popup:GetText(selected_text)
5017+
name = selected_text.LuaString
50025018
finenv.StartNewUndoBlock("Update "..name.." "..itemno.." Lyrics", false)
50035019
local lyrics_instance = lyrics_classes[type]()
50045020
local loaded = lyrics_instance:Load(itemno)
@@ -5045,7 +5061,7 @@ local function hyphenate_dlg_text(lyrics_box, popup, edit_type, auto_update, deh
50455061
if auto_update then
50465062
local selected_text = finale.FCString()
50475063
popup:GetText(selected_text)
5048-
update_document(lyrics_box, edit_type:GetInteger(), popup:GetSelectedItem() + 1, selected_text.LuaString)
5064+
update_document(lyrics_box, edit_type, popup)
50495065
end
50505066
else
50515067
finenv.UI():AlertError(result, "OpenAI")
@@ -5059,7 +5075,7 @@ local function hyphenate_dlg_text(lyrics_box, popup, edit_type, auto_update, deh
50595075
local lyrics_text = finale.FCString()
50605076
lyrics_box:GetText(lyrics_text)
50615077
else
5062-
update_dlg_text(lyrics_box, edit_type:GetInteger(), popup:GetSelectedItem() + 1)
5078+
update_dlg_text(lyrics_box, edit_type, popup)
50635079
lyrics_text.LuaString = lyrics_box.LuaString
50645080
end
50655081
lyrics_text:TrimWhitespace()
@@ -5074,6 +5090,22 @@ local function hyphenate_dlg_text(lyrics_box, popup, edit_type, auto_update, deh
50745090
https_session = openai.create_completion(config.api_model, prompt, config.temperature, callback)
50755091
end
50765092
end
5093+
local function update_from_active_lyric(lyrics_box, edit_type, popup)
5094+
if not use_active_lyric then return end
5095+
local active_lyric = finale.FCActiveLyric()
5096+
if active_lyric:Load() then
5097+
if active_lyric.BlockType ~= popup:GetSelectedItem() + 1 or active_lyric.TextBlockID ~= edit_type:GetInteger() then
5098+
if update_automatically and use_edit_text then
5099+
local selected_text = finale.FCString()
5100+
popup:GetText(selected_text)
5101+
update_document(lyrics_box, edit_type, popup)
5102+
end
5103+
popup:SetSelectedItem(active_lyric.BlockType - 1)
5104+
edit_type:SetInteger(active_lyric.TextBlockID)
5105+
update_dlg_text(lyrics_box, edit_type, popup)
5106+
end
5107+
end
5108+
end
50775109
local function create_dialog_box()
50785110
dlg = mixin.FCXCustomLuaWindow()
50795111
:SetTitle("Lyrics OpenAI Hyphenator")
@@ -5089,7 +5121,7 @@ local function create_dialog_box()
50895121
:SetWidth(25)
50905122
:SetInteger(1)
50915123
local lyrics_box
5092-
local yoff = 35
5124+
local yoff = 45
50935125
if config.use_edit_control then
50945126
if use_edit_text then
50955127
lyrics_box = dlg:CreateEditText(10, yoff)
@@ -5122,7 +5154,7 @@ local function create_dialog_box()
51225154
dlg:RegisterHandleControlEvent(update, function(control)
51235155
local selected_text = finale.FCString()
51245156
popup:GetText(selected_text)
5125-
update_document(lyrics_box, lyric_num:GetInteger(), popup:GetSelectedItem() + 1, selected_text.LuaString)
5157+
update_document(lyrics_box, lyric_num, popup)
51265158
end)
51275159
dlg:RegisterHandleControlEvent(auto_update, function(control)
51285160
update_automatically = control:GetCheck() ~= 0
@@ -5133,20 +5165,38 @@ local function create_dialog_box()
51335165

51345166

51355167
dlg.OkButtonCanClose = true
5168+
if use_active_lyric then
5169+
dlg:SetTimer(global_timer_id, 100)
5170+
end
51365171
end)
5172+
if use_active_lyric then
5173+
dlg:RegisterHandleTimer(function(dialog, timer_id)
5174+
if timer_id ~= global_timer_id then return end
5175+
update_from_active_lyric(lyrics_box, lyric_num, popup)
5176+
end)
5177+
end
51375178
dlg:RegisterHandleControlEvent(popup, function(control)
5138-
update_dlg_text(lyrics_box, lyric_num:GetInteger(), popup:GetSelectedItem() + 1)
5179+
update_dlg_text(lyrics_box, lyric_num, popup)
51395180
end)
51405181
dlg:RegisterHandleControlEvent(lyric_num, function(control)
5141-
update_dlg_text(lyrics_box, lyric_num:GetInteger(), popup:GetSelectedItem() + 1)
5182+
update_dlg_text(lyrics_box, lyric_num, popup)
51425183
end)
51435184
dlg:RegisterHandleControlEvent(hyphenate, function(control)
51445185
hyphenate_dlg_text(lyrics_box, popup, lyric_num, update_automatically, false)
51455186
end)
51465187
dlg:RegisterHandleControlEvent(dehyphenate, function(control)
51475188
hyphenate_dlg_text(lyrics_box, popup, lyric_num, update_automatically, true)
51485189
end)
5149-
update_dlg_text(lyrics_box, lyric_num:GetInteger(), popup:GetSelectedItem() + 1)
5190+
dlg:RegisterCloseWindow(function()
5191+
if finenv.UI():IsOnMac() and use_active_lyric then
5192+
finenv.RetainLuaState = false
5193+
end
5194+
end)
5195+
if use_active_lyric then
5196+
update_from_active_lyric(lyrics_box, lyric_num, popup)
5197+
else
5198+
update_dlg_text(lyrics_box, lyric_num, popup)
5199+
end
51505200
return dlg
51515201
end
51525202
local function openai_hyphenation()

docs/library/openai.md

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ okay as long as the timeout is reasonably short.
4040
## Functions
4141

4242
- [create_completion(model, prompt, temperature, callback_or_timeout)](#create_completion)
43-
- [create_chat(model, prompt, temperature, max_tokens, callback_or_timeout)](#create_chat)
4443

4544
### create_completion
4645

4746
```lua
4847
openai.create_completion(model, prompt, temperature, callback_or_timeout)
4948
```
5049

51-
[View source](https://github.com/finale-lua/lua-scripts/tree/refs/heads/master/src/library/openai.lua#L107)
50+
[View source](https://github.com/finale-lua/lua-scripts/tree/refs/heads/master/src/library/openai.lua#L105)
5251

5352
Sends a request to the OpenAI API to generate a completion for a given prompt.
5453

@@ -63,26 +62,3 @@ Sends a request to the OpenAI API to generate a completion for a given prompt.
6362
| ----------- | ----------- |
6463
| `boolean` | if synchronous call. See above for asynchronous. |
6564
| `string` | if synchronous call. See above for asynchronous. |
66-
67-
### create_chat
68-
69-
```lua
70-
openai.create_chat(model, prompt, temperature, max_tokens, callback_or_timeout)
71-
```
72-
73-
[View source](https://github.com/finale-lua/lua-scripts/tree/refs/heads/master/src/library/openai.lua#L132)
74-
75-
Sends a request to the OpenAI API to generate a completion for a given prompt.
76-
77-
| Input | Type | Description |
78-
| ----- | ---- | ----------- |
79-
| `model` | `string` | The model to use, e.g., "gpt-3.5-turbo" |
80-
| `prompt` | `string` | The prompt to send |
81-
| `temperature` | `number` | A value between 0.0 and 1.0. See the OpenAI documentation for information. |
82-
| `max_tokens` | `number` | The maximum number of tokens to generate |
83-
| `callback_or_timeout` (optional) | `function or number` | Defaults to 5.0. |
84-
85-
| Return type | Description |
86-
| ----------- | ----------- |
87-
| `boolean` | if synchronous call. See above for asynchronous. |
88-
| `string` | if synchronous call. See above for asynchronous. |

hash/lyrics_openai_hyphenation.hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
db4f615f8f48942fbd6510dffb7fb0db2ce005205b40f44198125d3fdd66dbd1fa1994e25db54afeace3f9d9b3cf3e9ba27aff14773cb7c2d031873a86224bea lyrics_openai_hyphenation.lua
1+
01742a41d2130ee165d1d314f8972e38d2a12ea5ce12c29ad8f4a4efe629cb794676723b67936e17f9fddd07b8be29cb8fb06c9203f3e13c7d9761242f7d9b9b lyrics_openai_hyphenation.lua

0 commit comments

Comments
 (0)