Last active
October 7, 2024 20:38
-
-
Save RStankov/db591044fe2494692c3193f87571b3e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require 'net/http' | |
require 'json' | |
module OpenAI | |
extend self | |
API_KEY = ENV.fetch('openai_api_key') | |
def completion(system:, user:) | |
uri = URI('https://api.openai.com/v1/chat/completions') | |
request = Net::HTTP::Post.new( | |
uri, | |
'Content-Type' => 'application/json', | |
'Authorization' => "Bearer #{API_KEY}", | |
) | |
request.body = JSON.dump( | |
model: 'gpt-4o', | |
messages: [{ | |
role: 'system', | |
content: system, | |
}, | |
{ | |
role: 'user', | |
content: user, | |
}], | |
temperature: 1, | |
max_tokens: 2000, | |
top_p: 1, | |
frequency_penalty: 0, | |
presence_penalty: 0, | |
) | |
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| | |
http.request(request) | |
end | |
json_response = JSON.parse(response.body) | |
json_response['choices'][0]['message']['content'] | |
# rescue StandardError => e | |
# puts e | |
# pp json_response unless json_response.nil? | |
end | |
end | |
module Clipboard | |
extend self | |
def read | |
`osascript -e 'the clipboard as Unicode text'` | |
end | |
def write(text) | |
escaped_text = text.gsub('\\', '\\\\\\\\').gsub('"', '\\\"').gsub("\n", '\\\\n') | |
`osascript -e 'set the clipboard to "#{escaped_text}"'` | |
end | |
end | |
module Notification | |
extend self | |
def installed? | |
return true if system('which terminal-notifier > /dev/null 2>&1') | |
title = 'Error' | |
message = "terminal-notifier is not installed.\nInstall it via:\nbrew install terminal-notifier" | |
`osascript -e 'display dialog "#{message}" with title "#{title}" buttons {"OK"} default button "OK" with icon stop'` | |
false | |
end | |
def show(title, message, timeout: 0) | |
`terminal-notifier -title "#{title}" -message "#{message}" -group "#{title}" -sender "com.runningwithcrayons.Alfred"` | |
if timeout > 0 | |
pid = Process.spawn("sleep #{timeout}; terminal-notifier -remove '#{title}' > /dev/null") | |
Process.detach(pid) | |
end | |
end | |
end | |
module SpellFix | |
extend self | |
LANGUAGES = { | |
'bg' => ['Bulgarian', /[А-Яа-яЁё]/u], | |
'en' => ['English', /[A-Za-z]/], | |
} | |
def call(lang) | |
return '' unless Notification.installed? | |
language, characters = LANGUAGES.fetch(lang) | |
clipboard = Clipboard.read | |
unless clipboard.to_s.match?(characters) | |
Notification.show('SpellFix', "🚫 Clipboard doesn't contain #{language} text", timeout: 20) | |
return '' | |
end | |
Notification.show('SpellFix', '🚧 Fixing spelling...') | |
system_prompt = <<~PROMPT | |
The text is in #{language} language. | |
Correct all spelling, punctuation, capitalization, and grammatical errors in the text. | |
Maintain the original structure and content without making additional changes. | |
You can add appropriate new lines and whitespace. | |
Just return the corrected text; don't give any explanations or comments. | |
PROMPT | |
system_prompt += "\nKeep all English-spelled words (you can fix their spelling)." if lang != 'en' | |
corrected_text = OpenAI.completion( | |
system: system_prompt, | |
user: clipboard, | |
) | |
Clipboard.write(corrected_text) | |
Notification.show('SpellFix', '✅ Spelling fixed', timeout: 5) | |
return corrected_text | |
rescue StandardError => e | |
Notification.show('SpellFix', "🔴 ERROR: #{e.message}") | |
end | |
end | |
print SpellFix.call(ARGV[0]) | |
print "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment