【OpenAI/ChatGPT】API リクエストしたら「You exceeded your current quota, please check your plan and billing details.」

無料で3ヶ月間、18ドル分使えるというので。


openai.api_key = os.getenv("OPENAI_API_KEY")
try:
  response = openai.Completion.create(
    engine="davinci",
    # engine="gpt-3.5-turbo",
    prompt="Hello, world!",
    max_tokens=5
  )
  print(response.choices[0].text)
except Exception as e:
  print(f"{e=}, {type(e)=}")

NGでした。


e=RateLimitError(message='You exceeded your current quota, please check your plan and billing details.', http_status=429, request_id=None), type(e)=<class 'openai.error.RateLimitError'>

利用量の確認をします。

Below you'll find a summary of API usage for your organization. All dates and times are UTC-based, and data may be delayed up to 5 minutes.
👉 Account - OpenAI API hatena-bookmark

利用の記録はないです。

無料18ドル分も付与されてない雰囲気。

「アカウント作成から3ヶ月以内。」ということですがそれは満たしている。

どういうことなんすかね!?

👉 How can I get Free Trial Credits? - General API discussion - OpenAI API Community Forum hatena-bookmark


【スマホ・カメラ不要】Mac上でQRコードを スクリーンキャプチャ→テキスト化 するスクリプト

パソコンの画面に表示されたQRコードを読み取るときの


スマホを手に取る

  ↓

カメラや読み取りアプリを起動

  ↓

カメラをパソコン画面に向ける

  ↓

読み取り

  ↓

テキストを確認する

  ↓

...
 

だるいですね!

 

■ ChatGPT に聞いてみる

以下は、Macで選択範囲をコピーして、その中に含まれるQRコードを読み出し、テキストとQRコードを表示するBashスクリプトです。QRコードの読み出しにはzbarコマンドを使用しています。


#!/bin/bash

# 選択範囲をクリップボードにコピーする
screencapture -i -c -o

# クリップボードにコピーされた画像を一時ファイルに保存する
tmp_file=$(mktemp -t qr_code.XXXXXXXXXX).png
pbpaste -Prefer "public.png" > "$tmp_file"

# zbarを使用してQRコードを読み出す
barcode=$(zbarimg --quiet --raw "$tmp_file")

if [ -n "$barcode" ]; then
    # QRコードのテキストを表示する
    echo "QR Code Text: $barcode"
    # QRコードを作成して表示する
    qrencode -o - "$barcode" | display -
else
    echo "No QR code found in selection."
fi

# 一時ファイルを削除する
rm "$tmp_file"

このスクリプトでは、qrencodeコマンドを使用して、QRコード画像を作成して表示しています。QRコードが検出されなかった場合には、「No QR code found in selection.」と表示します。

このスクリプトをファイルに保存し、実行可能な権限を与えてください。また、zbarコマンドとqrencodeコマンドがインストールされている必要があります。QRコードを表示するためには、ImageMagickパッケージが必要です。

実行すると、クリップボードに範囲指定で画面キャプチャはできます。

しかし、pbpaste でクリップボード内画像データから正しく画像ファイルを生成できませんでした。

 

■ クリップボード内画像データからファイルを生成する

以下の記事を参考に一時ファイルの生成を行います。


osascript <<EOF
set png_data to the clipboard as «class PNGf»
set the_file to open for access POSIX path of (POSIX file "/tmp/image.png") with write permission
write png_data to the_file
close access the_file
EOF

👉 MacOSでクリップボード上の画像を保存するスクリプト - 神和電子 hatena-bookmark

これで、クリップボードにコピーした画像PNGファイルデータから画像ファイルを作成できるようになりました。

 

■ QRコードの解析してテキスト化する

続いて、QRコード読み込みしてテキスト化ツールをインストールします。

👉 ZBar/ZBar: Clone of the mercurial repository http://zbar.hg.sourceforge.net:8000/hgroot/zbar/zbar hatena-bookmark
👉 mchehab/zbar: ZBar is an open source software suite for reading bar codes from various sources, including webcams. As its development stopped in 2012, I took the task of keeping it updated with the V4L2 API. This is the main repository for it. There's a clone at at LinuxTV.org, and another one at gitlab. hatena-bookmark


❯ brew install zbar


❯ zbarimg -h
usage: zbarimg [options] <image>...

scan and decode bar codes from one or more image files

options:
    -h, --help      display this help text
    --version       display version information and exit
    -q, --quiet     minimal output, only print decoded symbol data
    -v, --verbose   increase debug output level
    --verbose=N     set specific debug output level
    -d, --display   enable display of following images to the screen
    -D, --nodisplay disable display of following images (default)
    --xml, --noxml  enable/disable XML output format
    --raw           output decoded symbol data without converting charsets
    -1, --oneshot   exit after scanning one bar code
    -S<CONFIG>[=<VALUE>], --set <CONFIG>[=<VALUE>]
                    set decoder/scanner <CONFIG> to <VALUE> (or 1)

ファイルから読みとるようなので以下のサンプルQRコードファイル hello.png を読ませて出力を確認しておきます。


❯ zbarimg --quiet hello.png
QR-Code:こんにちは、QRコード!

❯ zbarimg --quiet --raw hello.png
こんにちは、QRコード!

簡単で便利ですね。

 

■ 修正する

以上を組み合わせて、前述の ChatGPT によるコードを修正しました。



いけました!

Mac では、ショートカット.app から使うと便利です。


 

■ まとめ

ChatGPT て少しデータが古い感じはありますが、動かないとしても、最初からググるよりも、明らかに早くコードを作成できます。

👉 画像ファイルをMacのコマンドラインで表示する - ごずろぐ hatena-bookmark


Excel / Spreadsheet / Numbers の列の文字列を一括でQRコードで表示する方法

Google Chart API を使ったりプラグインやエクステンションを使ったりする方法もありますが、通信を使わずに完全にローカル処理のみで表示します。

パスワードやシークレットキーなどセキュリティ的に最重要な文字列をQRコードで表示したいこともありますよね。

 

🔑 準備

Pure python QR Code generator を使います。


pip install qrcode

👉 lincolnloop/python-qrcode: Python QR Code image generator hatena-bookmark

今回は、サンプルデータとして以下のCSVファイルを作成しました。


title,url
blog,https://android.benigumo.com
facebook,https://www.facebook.com/androidbenigumocom
twitter,https://twitter.com/maochanz

 

🔑 作成する

CSVファイルをExcel系アプリ(Apple Numbers)で開きます。

Excel / Spreadsheet / Numbers の列の文字列を一括でQRコードで表示する方法

QRコードで表示したい列をコピーします。

Excel / Spreadsheet / Numbers の列の文字列を一括でQRコードで表示する方法

以下をターミナルで実行すると連続で表示されます。


for uri in $(pbpaste); do qr "$uri";echo "$uri\n\n"; done


Excel / Spreadsheet / Numbers の列の文字列を一括でQRコードで表示する方法

以上です。

次回は、これをパスワードマネージャーデータの移行に使います。