Skip to content

Instantly share code, notes, and snippets.

@Frando
Last active April 20, 2024 00:14
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 Frando/8e9ab18a767b2c70b33dcd8c5f1aaf8a to your computer and use it in GitHub Desktop.
Save Frando/8e9ab18a767b2c70b33dcd8c5f1aaf8a to your computer and use it in GitHub Desktop.
iroh fog discord bot prompt generator
// deployed via deno deploy (free plan) and call with
// https://posh-eel-46.deno.dev/?q=https://url.to/some-media.mp3
const image = "ghcr.io/ggerganov/whisper.cpp:main";
const model = "tiny"; // see https://github.com/ggerganov/whisper.cpp/blob/master/models/download-ggml-model.sh#L28
const maxDuration = 1000 * 10; // 10s
function build(url: string) {
const prepare = "mkdir /models";
const fetchModel =
`./models/download-ggml-model.sh ${model} /models > /dev/null 2> /dev/null`;
const curl = `curl -s -o /uploads/media.mp3 '${url}'`;
const ffmpeg = `ffmpeg
-hide_banner -loglevel error
-i /uploads/media.mp3
-ar 16000 -ac 1 -c:a pcm_s16le
/tmp/media.wav`;
const transcribe = `./main
-m /models/ggml-${model}.bin
--language auto
--output-txt
--no-timestamps
--duration ${maxDuration}
-f /tmp/media.wav
-of /uploads/transcript.txt`;
const result = `cat /uploads/transcript.txt`;
const debugMsg = `transcribed ${url} (first ${maxDuration} seconds)`;
const debug = `echo '${debugMsg}' >&2`;
const command = [prepare, fetchModel, curl, ffmpeg, transcribe, result, debug]
.join(" && ")
.replace("\n", " ")
.replace(/\s+/g, " ");
return `/docker image:${image} command:"${command}"`;
}
Deno.serve((req: Request) => {
const url = new URL(req.url);
const q = url.searchParams.get("q");
if (!q) {
return new Response("Missing `q` query parameter with media URL", {
status: 400,
});
}
const res = build(q);
return new Response(res);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment