| # Test the HuggingFace Inference Endpoint with a local image file | |
| # Usage: ./test_inference.sh <image_file> | |
| set -e | |
| if [ -z "$1" ]; then | |
| echo "Usage: $0 <image_file>" | |
| exit 1 | |
| fi | |
| IMAGE_FILE="$1" | |
| if [ ! -f "$IMAGE_FILE" ]; then | |
| echo "Error: File not found: $IMAGE_FILE" | |
| exit 1 | |
| fi | |
| # Required environment variables | |
| if [ -z "$HF_ENDPOINT" ]; then | |
| echo "Error: HF_ENDPOINT not set" | |
| echo "Export it first: export HF_ENDPOINT=https://xxx.endpoints.huggingface.cloud" | |
| exit 1 | |
| fi | |
| if [ -z "$HF_TOKEN" ]; then | |
| echo "Error: HF_TOKEN not set" | |
| echo "Export it first: export HF_TOKEN=hf_xxx" | |
| exit 1 | |
| fi | |
| # Convert image to base64 and create JSON payload in temp file | |
| echo "Converting $IMAGE_FILE to base64..." | |
| TEMP_FILE=$(mktemp) | |
| trap "rm -f $TEMP_FILE" EXIT | |
| printf '{"inputs": "' > "$TEMP_FILE" | |
| base64 -i "$IMAGE_FILE" | tr -d '\n' >> "$TEMP_FILE" | |
| printf '"}' >> "$TEMP_FILE" | |
| echo "Sending request to $HF_ENDPOINT..." | |
| curl -s -X POST "$HF_ENDPOINT" \ | |
| -H "Authorization: Bearer $HF_TOKEN" \ | |
| -H "Content-Type: application/json" \ | |
| -d @"$TEMP_FILE" | python3 -m json.tool | |