Hi, I need an extension that translates selected text from Polish to English using the GPT3turbo API. The selected text should be automatically replaced with the translated version. I am not a developer and don’t know anything about coding. I tried to use the code below (and many similar ones), but it’s not working. Every time I get an error saying “There is no application set to open the URL popclipext://return/…” or the translation button is not clickable.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Actions</key>
<array>
<dict>
<key>Script File</key>
<string>translate.sh</string>
<key>Title</key>
<string>Translate to English</string>
</dict>
</array>
<key>Name</key>
<string>PolishToEnglishTranslator</string>
<key>Required Software Version</key>
<integer>813</integer>
<key>SDK</key>
<integer>3</integer>
<key>Regex</key>
<string>\S</string>
</dict>
</plist>
#!/bin/bash
selected_text="$POPCLIP_TEXT"
api_key="IhadAPIkeyHere"
translated_text=$(curl -s -X POST "https://api.openai.com/v1/engines/davinci-codex/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $api_key" \
-d "{\"prompt\":\"Translate the following Polish text to English: '$selected_text'\",\"n\":1,\"max_tokens\":500,\"temperature\":0.5,\"top_p\":1,\"engine\":\"text-davinci-003.5-turbo\"}" \
| python -c "import sys, json; print(json.load(sys.stdin)['choices'][0]['text'].strip())")
echo $translated_text
I don’t have access to GPT4 but even GPT3.5 Turbo is 100 better than Deep-L.
Update:
It doesn’t work either:
// #popclip
// name: Polish to English Translator
// icon: iconify:eos-icons:translate
// language: javascript
// after: paste-result
// entitlements: [network]
// options: [{identifier: apikey, label: API Key, type: string,
// description: 'Obtain API key from https://platform.openai.com/account/api-keys'}]
const axios = require("axios");
// base object for communicating with OpenAI
const openai = axios.create({
baseURL: 'https://api.openai.com/v1/',
headers: { Authorization: `Bearer ${popclip.options.apikey}` }
});
// use the GPT-3.5-turbo model (note - can change/add other params here)
const data = {
model: 'text-davinci-003.5-turbo',
max_tokens: 100,
temperature: 0.5,
prompt: `Translate the following Polish text to English: '${popclip.input.text}'`
};
// send query to OpenAI's `completions` service
const response = await openai.post('completions', data);
// return the first text in the response
return response.data.choices[0].text.trim();