#!/bin/bash ## ## Get stuff done with chatgpt! ## CMD=$(basename $0) TMP=$HOME/.tmp/$CMD ; mkdir -p $TMP model=gpt-5.5 usage() { me=$(basename $0) echo "Usage: $me -a generate-image -p prompt [-v]" echo "Usage: $me -a ask -p prompt [-f file] [-m model] [-v]" echo "Usage: $me -a say -p text > speech.mp3" echo "Usage: $me -a transcribe -f file [-m model] [-p context] [-v] # model defaults to whisper-1; context hints vocabulary" exit 1 } while getopts "a:p:f:m:hv" o; do case "$o" in p) prompt=$OPTARG ;; a) action=$OPTARG ;; f) file=$OPTARG ;; m) model=$OPTARG ; transcribe_model=$OPTARG ;; v) VERBOSE=yes ;; * | h) usage ;; esac done if [ -z "$OPENAI_API_KEY" ] ; then echo "No OPENAI_API_KEY env variable set." exit 1 fi chatgpt_req() { curl -s https://api.openai.com/$1 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d "$2" } result() { if [ "$VERBOSE" = "yes" ] ; then jq . else jq "$@" fi } case "$action" in generate-image) if [ -z "$prompt" ]; then echo "Missing -p prompt" exit 1 fi body=$(jq -n --arg prompt "$prompt" \ '{"model":"dall-e-3","prompt":$prompt,"n":1,"size":"1024x1024"}') chatgpt_req "v1/images/generations" "$body" | result -r .data[0].url ;; ask) if [ -z "$prompt" ]; then echo "Missing -p prompt" exit 1 fi if [ -n "$file" ]; then if [ ! -f "$file" ]; then echo "-f $file doesn't exist" exit 1 fi mt=$(file --mime-type "$file" | sed 's/^.*: *//') case "$mt" in image/*) b64=$(mktemp "$TMP/img.XXXXXX") base64assist -a encode -f "$file" > "$b64" body=$(jq -n --arg prompt "$prompt" --arg mt "$mt" --arg model "$model" --rawfile data "$b64" \ '{"model":$model,"messages":[{"role":"user","content":[ {"type":"text","text":$prompt}, {"type":"image_url","image_url":{"url":("data:" + $mt + ";base64," + $data)}} ]}]}') rm -f "$b64" ;; *) content=$(printf '%s\n\n%s' "$prompt" "$(cat "$file")") body=$(jq -n --arg content "$content" --arg model "$model" \ '{"model":$model,"messages":[{"role":"user","content":$content}]}') ;; esac else body=$(jq -n --arg prompt "$prompt" --arg model "$model" \ '{"model":$model,"messages":[{"role":"user","content":$prompt}]}') fi curl -s https://api.openai.com/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d "$body" | result -r '.choices[0].message.content' ;; say) if [ -z "$prompt" ]; then echo "Missing -p prompt" exit 1 fi body=$(jq -n --arg prompt "$prompt" \ '{"model":"tts-1","input":$prompt,"voice":"alloy"}') chatgpt_req "v1/audio/speech" "$body" ;; transcribe) if [ -z "$file" ]; then echo "Missing -f file" exit 1 fi if [ ! -f "$file" ]; then echo "-f $file doesn't exist" exit 1 fi command -v ffmpeg >/dev/null || { echo "ffmpeg not installed"; exit 1; } # Whisper rejects some containers that carry a friendly extension (a phone # .m4a that's really 3GPP, say). Re-encoding every input up front sidesteps # the guessing game. to_whisper_m4a() { ffmpeg -nostdin -y -i "$1" -c:a aac -b:a 32k "$2" -loglevel error } whisper_upload() { local f="$1" local resp fields=(-F "file=@$f" -F "model=${transcribe_model:-whisper-1}") [ -n "$prompt" ] && fields+=(--form-string "prompt=$prompt") resp=$(curl -s https://api.openai.com/v1/audio/transcriptions \ -H "Authorization: Bearer $OPENAI_API_KEY" \ "${fields[@]}") if [ "$VERBOSE" = "yes" ]; then echo "$resp" | jq . >&2 fi if echo "$resp" | jq -e '.error' >/dev/null 2>&1; then echo "$resp" | jq -r '.error.message // .error' >&2 return 1 fi echo "$resp" | jq -r '.text // empty' } LIMIT=$((24 * 1024 * 1024)) # 24MB — stay safely under 25MB API limit # Upload work_file, first splitting into chunks if it's over the size cap. # Prints the transcript on stdout; returns non-zero on any whisper error. whisper_transcribe() { local work_file="$1" file_size file_size=$(wc -c < "$work_file") if [ "$file_size" -le "$LIMIT" ]; then whisper_upload "$work_file" return $? fi local duration seg_secs seg_dir full text duration=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$work_file" 2>/dev/null) seg_secs=$(awk "BEGIN { printf \"%d\", $duration * ($LIMIT / $file_size) }") seg_dir="$work_dir/segs" ; mkdir -p "$seg_dir" echo "Splitting into ~${seg_secs}s chunks..." >&2 ffmpeg -nostdin -y -i "$work_file" -f segment -segment_time "$seg_secs" \ -c copy "$seg_dir/seg%03d.m4a" -loglevel error full="" for seg in "$seg_dir"/seg*.m4a; do text=$(whisper_upload "$seg") || { rm -rf "$seg_dir"; return 1; } full="${full}${text} " done rm -rf "$seg_dir" echo "${full% }" } work_dir=$(mktemp -d "$TMP/transcribe.XXXXXX") work_file="$work_dir/audio.m4a" to_whisper_m4a "$file" "$work_file" || { echo "Conversion failed: $file" >&2; rm -rf "$work_dir"; exit 1; } transcript=$(whisper_transcribe "$work_file") rc=$? rm -rf "$work_dir" [ "$rc" -eq 0 ] && printf '%s\n' "$transcript" exit $rc ;; *) usage ;; esac