Spaces:
Running
Running
File size: 6,180 Bytes
af1a2b0 27088c4 af1a2b0 27088c4 af1a2b0 27088c4 af1a2b0 | 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | #!/usr/bin/env bash
# Puts the Paystack secret key on both NarratifAI transcription servers
# without the key ever appearing in a file, the shell history, the
# process list or this script.
#
# 1. Hugging Face Space (primary): Space secret PAYSTACK_SECRET_KEY, then restart
# 2. Modal (fallback): its own Modal secret "narratifai-paystack", then redeploy
# 3. Checks both servers can now start a Paystack checkout
#
# Usage (from anywhere):
# bash ~/NarratifAI/asr_system/scripts/set_paystack_key.sh
set -euo pipefail
SPACE="ConsulawTechnologies/NarratifAI"
SPACE_URL="https://consulawtechnologies-narratifai.hf.space"
MODAL_URL="https://consulawtech--narratifai-asr-web-app.modal.run"
ASR_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
say() { printf '\n\033[1m%s\033[0m\n' "$*"; }
ok() { printf ' \033[32mβ\033[0m %s\n' "$*"; }
fail() { printf ' \033[31mβ\033[0m %s\n' "$*"; exit 1; }
for tool in curl python3 modal; do
command -v "$tool" >/dev/null || fail "$tool is not installed"
done
# ββ 1. Paystack key (hidden input) βββββββββββββββββββββββββββββββββββββββββββββ
say "Paystack secret key"
read -r -s -p " Paste it (sk_live_β¦ or sk_test_β¦), then press Enter: " PAYSTACK_KEY; echo
PAYSTACK_KEY="$(printf '%s' "$PAYSTACK_KEY" | tr -d '[:space:]')"
[[ "$PAYSTACK_KEY" =~ ^sk_(live|test)_[A-Za-z0-9]+$ ]] || fail "That doesn't look like a Paystack secret key (it should start with sk_live_ or sk_test_)."
# Ask Paystack whether the key works before putting it anywhere.
status=$(curl -s -o /dev/null -w '%{http_code}' -K - <<<"header = \"Authorization: Bearer $PAYSTACK_KEY\"
url = \"https://api.paystack.co/balance\"")
[[ "$status" == "200" ]] || fail "Paystack rejected the key (HTTP $status). Check you copied the secret key, not the public key."
mode="live"; [[ "$PAYSTACK_KEY" == sk_test_* ]] && mode="test"
ok "Paystack accepted the $mode key"
# ββ 2. Hugging Face token (to set the Space secret) ββββββββββββββββββββββββββββ
say "Hugging Face access"
hf_whoami() {
curl -s -K - <<<"header = \"Authorization: Bearer $1\"
url = \"https://huggingface.co/api/whoami-v2\"" | python3 -c 'import sys,json; print(json.load(sys.stdin).get("name",""))' 2>/dev/null || true
}
HF_TOKEN_VALUE="${HF_TOKEN:-}"
if [[ -z "$HF_TOKEN_VALUE" && -f "$HOME/.cache/huggingface/token" ]]; then
HF_TOKEN_VALUE="$(tr -d '[:space:]' < "$HOME/.cache/huggingface/token")"
fi
who=""
[[ -n "$HF_TOKEN_VALUE" ]] && who="$(hf_whoami "$HF_TOKEN_VALUE")"
if [[ -z "$who" && -n "$HF_TOKEN_VALUE" ]]; then
echo " The Hugging Face login saved on this Mac is no longer valid."
fi
attempt=0
while [[ -z "$who" && $attempt -lt 3 ]]; do
attempt=$((attempt + 1))
echo " Create one at https://huggingface.co/settings/tokens (type: Write) on an account that can edit $SPACE."
read -r -s -p " Paste the Hugging Face token: " HF_TOKEN_VALUE; echo
HF_TOKEN_VALUE="$(printf '%s' "$HF_TOKEN_VALUE" | tr -d '[:space:]')"
who="$(hf_whoami "$HF_TOKEN_VALUE")"
[[ -n "$who" ]] || echo " That token was rejected. Try again."
done
[[ -n "$who" ]] || fail "No working Hugging Face token. Nothing has been changed yet."
ok "Signed in to Hugging Face as $who"
# ββ 3. Space secret + restart ββββββββββββββββββββββββββββββββββββββββββββββββββ
say "Primary server (Hugging Face Space)"
body=$(PAYSTACK_KEY="$PAYSTACK_KEY" python3 -c 'import json,os; print(json.dumps({"key":"PAYSTACK_SECRET_KEY","value":os.environ["PAYSTACK_KEY"],"description":"Paystack secret key for credit purchases"}))')
# The token goes to curl through a config "file" (process substitution) and
# the body through stdin, so neither shows up in the process list.
status=$(printf '%s' "$body" | curl -s -o /dev/null -w '%{http_code}' -X POST \
-H "Content-Type: application/json" \
-K <(printf 'header = "Authorization: Bearer %s"\n' "$HF_TOKEN_VALUE") \
--data-binary @- "https://huggingface.co/api/spaces/$SPACE/secrets")
[[ "$status" == "200" ]] || fail "Could not set the Space secret (HTTP $status)."
ok "Saved PAYSTACK_SECRET_KEY on the Space"
curl -s -o /dev/null -X POST -K <(printf 'header = "Authorization: Bearer %s"\n' "$HF_TOKEN_VALUE") \
"https://huggingface.co/api/spaces/$SPACE/restart" || true
ok "Restarting the Space so it picks up the key"
# ββ 4. Modal secret + redeploy βββββββββββββββββββββββββββββββββββββββββββββββββ
say "Backup server (Modal)"
tmp="$(mktemp)"; chmod 600 "$tmp"
trap 'rm -f "$tmp"' EXIT
printf 'PAYSTACK_SECRET_KEY=%s\n' "$PAYSTACK_KEY" > "$tmp"
modal secret create narratifai-paystack --from-dotenv "$tmp" --force >/dev/null
rm -f "$tmp"
ok "Saved the narratifai-paystack secret on Modal"
(cd "$ASR_DIR" && modal deploy modal_app.py >/dev/null)
ok "Redeployed the Modal server"
unset PAYSTACK_KEY HF_TOKEN_VALUE body
# ββ 5. Check both servers can start a checkout βββββββββββββββββββββββββββββββββ
say "Checking"
check() {
local name="$1" url="$2" tries="${3:-1}" code=""
for ((i = 1; i <= tries; i++)); do
# Missing email on purpose: a configured server answers 400 (bad request),
# an unconfigured one 503 ("Paystack not configured").
code=$(curl -s -m 30 -o /dev/null -w '%{http_code}' -X POST -H 'Content-Type: application/json' \
-d '{"pack_id":"starter"}' "$url/paystack/init" || true)
[[ "$code" == "400" ]] && { ok "$name is ready to take payments"; return 0; }
sleep 20
done
printf ' \033[33m!\033[0m %s answered HTTP %s. If it is still restarting, run this script again in a few minutes.\n' "$name" "$code"
}
check "Backup server (Modal)" "$MODAL_URL" 3
check "Primary server (Hugging Face)" "$SPACE_URL" 15
say "Last step, in the Paystack dashboard"
echo " Settings β API Keys & Webhooks β Webhook URL ($mode mode):"
echo " $SPACE_URL/paystack/webhook"
echo
|