Skip to content

Instantly share code, notes, and snippets.

@justynroberts
Last active November 9, 2024 17:01
Show Gist options
  • Save justynroberts/9b5556be5dabc550e6cbc8e3f663145e to your computer and use it in GitHub Desktop.
Save justynroberts/9b5556be5dabc550e6cbc8e3f663145e to your computer and use it in GitHub Desktop.
OpenAI Image - Bash

Analyze.sh Script Instructions

Steps to Run the Script

  1. Add Key: Ensure that you add your API key to the script for authentication.

  2. Make the Script Executable:

    chmod +x analyze.sh
  3. Install jq for JSON Parsing:

    • macOS: Use Homebrew to install jq:
      brew install jq
  4. Run the Script:

    Use the following format to execute the script:

    ./analyze.sh "Your prompt here" "/path/to/your/image.png"

    Example:

    ./analyze.sh "What's this?" "/Users/justynroberts/work/Uploadpictoopenai/Screenshot 2024-10-17 at 13.59.41.png"

    Sample Output:

    The image appears to be a geographic map showing various locations or points of interest, each marked with unique identifiers or labels (e.g., "VWB1," "CPX Wisblock 1"). The concentric circles likely represent areas of influence or operational ranges related to these points. This type of visualization is often used in networking, logistics, or operational planning to illustrate coverage or relationships between different sites.
    If you have a specific context or application in mind, please provide more details!

#!/bin/bash
# www.fintonlabs.com
export OPENAI_API_KEY="ADD_KEY"
check_requirements() {
for cmd in curl base64 mktemp jq; do
if ! command -v "$cmd" > /dev/null; then
echo "Error: Required command '$cmd' is not installed." >&2
exit 1
fi
done
}
encode_image() {
if [[ ! -f "$1" ]]; then
echo "Error: Image file not found: $1" >&2
exit 1
fi
cat "$1" | base64 | tr -d '\n'
}
make_api_request() {
if [[ -z "$OPENAI_API_KEY" ]]; then
echo "Error: OPENAI_API_KEY environment variable is not set" >&2
exit 1
fi
local temp_file=$(mktemp)
cat > "$temp_file" << EOF
{
"model": "gpt-4o-mini",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "$1"
},
{
"type": "image_url",
"image_url": {
"url": "data:image/jpeg;base64,$2"
}
}
]
}
],
"max_tokens": 300
}
EOF
curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "@$temp_file" \
"https://api.openai.com/v1/chat/completions" | jq -r '.choices[0].message.content'
rm -f "$temp_file"
}
if [[ $# -ne 2 ]]; then
echo "Usage: $0 <prompt> <path_to_image>" >&2
echo "Example: $0 'What colors are in this image?' ./image.jpg" >&2
exit 1
fi
check_requirements
base64_image=$(encode_image "$2")
make_api_request "$1" "$base64_image"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment