-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtripo-image.sh
More file actions
executable file
·59 lines (54 loc) · 2.93 KB
/
Copy pathtripo-image.sh
File metadata and controls
executable file
·59 lines (54 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env bash
# Generate a 2D image with Tripo (text-to-image) and download the PNG.
#
# export TRIPO_API_KEY='tsk_...' # never commit this; single-quote the WHOLE token
# ./tripo-image.sh "a prompt" out.png # basic model (5 credits)
# TRIPO_MODEL=gpt_image_2 ./tripo-image.sh "a prompt" out.png # high-end (10 credits)
#
# Basic : type=text_to_image -> 5 credits.
# High-end: type=generate_image + model -> 10 credits. Models: gpt_image_2, gpt_image_1.5,
# midjourney, gemini_3_pro_image_preview, flux.1_kontext_pro, flux.1_dev, gpt_4o, ...
# Output image URL lives at .data.output.generated_image. Checks balance first; refuses at 0.
# Set the key VERBATIM (keys contain '-'/'_', ~47 chars); a partial key fails with code 1002.
set -euo pipefail
PROMPT="${1:?usage: tripo-image.sh \"<prompt>\" <output.png>}"
OUT="${2:?usage: tripo-image.sh \"<prompt>\" <output.png>}"
KEY="${TRIPO_API_KEY:?set TRIPO_API_KEY in your environment (do not commit it)}"
MODEL="${TRIPO_MODEL:-}" # empty -> basic text_to_image (5cr); else high-end (10cr)
NEG="${TRIPO_NEG:-}"
API="https://api.tripo3d.ai/v2/openapi"
auth=(-H "Authorization: Bearer $KEY")
cget() { curl -s --retry 5 --retry-delay 2 --retry-all-errors "$@"; }
resp=$(cget "${auth[@]}" "$API/user/balance")
code=$(echo "$resp" | jq -r '.code // "?"')
[ "$code" = "0" ] || { echo "Tripo API error (code $code): $(echo "$resp" | jq -r '.message // .')"; exit 1; }
bal=$(echo "$resp" | jq -r '.data.balance')
echo "Tripo balance: $bal credits"
[ "$bal" -gt 0 ] 2>/dev/null || { echo "No credits, aborting (no charge made)."; exit 1; }
# Build the request: high-end uses generate_image+model_version, basic uses text_to_image.
if [ -n "$MODEL" ]; then
body=$(jq -nc --arg p "$PROMPT" --arg n "$NEG" --arg m "$MODEL" \
'{type:"generate_image", model_version:$m, prompt:$p} + (if $n=="" then {} else {negative_prompt:$n} end)')
else
body=$(jq -nc --arg p "$PROMPT" --arg n "$NEG" \
'{type:"text_to_image", prompt:$p} + (if $n=="" then {} else {negative_prompt:$n} end)')
fi
echo "Generating (${MODEL:-text_to_image}): $PROMPT"
tid=$(cget -X POST "$API/task" "${auth[@]}" -H "Content-Type: application/json" -d "$body" | jq -r '.data.task_id // empty')
[ -n "$tid" ] || { echo "Failed to create task (likely no charge)"; exit 1; }
echo "task: $tid"
url=""
for i in $(seq 1 60); do
r=$(cget "${auth[@]}" "$API/task/$tid" || true)
st=$(echo "$r" | jq -r '.data.status'); pr=$(echo "$r" | jq -r '.data.progress // 0')
echo " [$i] $st $pr%"
case "$st" in
success) url=$(echo "$r" | jq -r '.data.output.generated_image // empty'); break ;;
failed|banned|cancelled|expired|unknown) echo "Generation $st"; exit 1 ;;
esac
sleep 5
done
[ -n "$url" ] || { echo "Timed out"; exit 1; }
mkdir -p "$(dirname "$OUT")"
cget "$url" -o "$OUT"
echo "Saved $OUT ($(wc -c < "$OUT") bytes). Remaining: $(cget "${auth[@]}" "$API/user/balance" | jq -r '.data.balance') cr"