0
0

【生成AIアプリ100チャレンジ】(12)画像解析アプリ VISION API

Posted at

公開サイト

https://ai.nuevo.jp/vision/
※ユーザー限定

開発環境

Server lightSail
Language Python3.11
Framework Django
DB sqlite3

ローカル環境ではPythonのvenvを使用。エディタはvs codeです。

目的

画像解析の生成AIであるvison apiをつかったアプリ。アップした画像の説明をしてくれる。
最初はプロンプトの規定は「日本語で返して」という設定のみ。このプロンプトを設定すれば、「なんの食材か」「なんの機材か」のみレスポンスを返すようになるのかは、後日チャレンジしたい。

コード

qiita.rb
    OPENAI_API_KEY = os.environ['OPENAI_API_IMAGE_KEY']
    domain = request.build_absolute_uri('/')
    file_path = 'demo.jpg'
    if default_storage.exists(file_path):
        default_storage.delete(file_path)
    img_results = ""
    image_path = ""
    if request.method == "POST":
        form = ChatForm(request.POST, request.FILES)
        if form.is_valid():
            image_file = request.FILES['image']
            image_content = image_file.read()
            default_storage.save('demo.jpg', ContentFile(image_content))
            client = OpenAI(
                api_key = OPENAI_API_KEY,
            )
            image_path = settings.BASE_DIR / "uploads/demo.jpg"
            print(image_path)
            def encode_image(image_path):
                with open(image_path, "rb") as image_file:
                    return base64.b64encode(image_file.read()).decode('utf-8')
            base64_image = encode_image(image_path)
            headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {OPENAI_API_KEY}"
            }
            payload = {
            "model": "gpt-4-vision-preview",
            "messages": [
                {
                "role": "user",
                "content": [
                    {
                    "type": "text",
                    "text": "What’s in this image? 日本語で答えてください"
                    },
                    {
                    "type": "image_url",
                    "image_url": {
                        "url": f"data:image/jpeg;base64,{base64_image}"
                    }
                    }
                ]
                }
            ],
            "max_tokens": 300
            }
            response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
            img_results = (response.json().get("choices")[0]["message"]["content"])
    else:
        form = ChatForm()  

アプリ画面

スクリーンショット 2024-04-05 12.45.02.png

ソニックユースのキムゴードンの最新アルバムのジャケットを解析しました。

感想

画像解析はプロンプトによって、いろんなアプリが作れるのではないかと思っている。LLMやファインチューニングなどのテクニックを使う必要もあるのかもしれない。色々と試していきたい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0