Skip to content

Instantly share code, notes, and snippets.

@ericlewis
Last active June 29, 2022 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericlewis/65f7752a4a840181e5d2472e809c02ad to your computer and use it in GitHub Desktop.
Save ericlewis/65f7752a4a840181e5d2472e809c02ad to your computer and use it in GitHub Desktop.
Example of having Morgan Freeman explain what code is doing using OpenAI Codex DaVinci & Uberduck's style transfer APIs.
import { Configuration, OpenAIApi } from "openai";
import download from "download";
import ora from "ora";
import soundPlayer from "play-sound"
import prompts from "prompts";
// Get your own keys, nerd.
const kOpenAI = "YOUR_API_KEY";
const kUberDuck = "PRIVATE_KEY:PUBLIC_KEY";
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
const configuration = new Configuration({
apiKey: kOpenAI,
});
const openai = new OpenAIApi(configuration);
const player = soundPlayer();
(async () => {
const {code} = await prompts({
type: 'text',
name: 'code',
message: 'Code to Morgan Freemanize?'
});
let spinner = ora("Understanding code...").start();
const {data: {choices: [{text}]}} = await openai.createCompletion({
model: "code-davinci-002",
prompt: `${code}\n\n\"\"\"\nHere's what the above function is doing:\n1.`,
temperature: 0,
max_tokens: 84,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
stop: ["\"\"\""],
});
const answer = "1." + text;
spinner.stop();
spinner = ora("Morgan Freemanizing...").start();
const credentials = btoa(kUberDuck);
const auth = { "Authorization" : `Basic ${credentials}` };
const res = await fetch("https://api.uberduck.ai/speak", {
body: JSON.stringify({
speech: answer,
voice: "morgan-freeman",
pace: 1
}),
method: "POST",
headers: {
...auth,
'Content-Type': 'application/json'
}
});
spinner.stop();
spinner = ora("Processing Audio...").start();
const {uuid} = await res.json();
const url = `https://uberduck-audio-outputs.s3-us-west-2.amazonaws.com/${uuid}/audio.wav`;
// I am lazy
await sleep(10000);
spinner.stop();
spinner = ora("Downloading Audio...").start();
await download(url, 'out');
spinner.stop();
spinner = ora("Speaking...").start();
await new Promise((resolve) => {
player.play("out/audio.wav", function() {
resolve();
});
});
spinner.stop();
})();
{
"name": "morgan-freeman-explainer",
"version": "1.0.0",
"description": "Morgan Freeman explains your code.",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node --no-warnings index.js"
},
"author": "Eric Lewis",
"license": "MIT",
"dependencies": {
"download": "^8.0.0",
"openai": "^3.0.0",
"ora": "^6.1.2",
"play-sound": "^1.1.5",
"prompts": "^2.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment