はじめに
GPT-4 Turbo with Vision を使用して、Web ページのスクリーンショットから Next.js × Tailwind CSS のコードを生成してみました。
開発環境
- OS: Windows 11
- 言語: Python 3.11.5
- ライブラリ: Azure OpenAI
実装
では、早速実装していきます!
1. 環境設定
まず、必要なライブラリをインストールします。dotenvとopenaiを使用します。これらは、以下のコマンドでインストールできます。
1pip install python-dotenv openai2. Azure OpenAI の設定
Azure OpenAI を使用するためには、Azure ポータルで Azure OpenAI リソースを作成し、API キーとエンドポイント URL を取得する必要があります。
本記事では省略します。
取得した情報を.envファイルに保存します。
1.env
2AZURE_OPENAI_ENDPOINT=
3AZURE_OPENAI_API_KEY=
4AZURE_OPENAI_API_VERSION=3. 画像の準備
次に、コードを生成したい Web ページのスクリーンショットをwebpage.pngという名前でプロジェクトのルートディレクトリに保存します。
本記事では、以下のスクリーンショットを使用します。
引用元 : https://preline.co/examples/hero-sections.html#hero-with-polygon-bg-element
4. コードの実装
以下の Python スクリプトをtest.pyとして保存し、実行します。
1import os
2from dotenv import load_dotenv
3
4load_dotenv()
5from openai import AzureOpenAI
6import base64
7from mimetypes import guess_type
8
9api_base = os.getenv("AZURE_OPENAI_ENDPOINT")
10api_key = os.getenv("AZURE_OPENAI_API_KEY")
11deployment_name = "gpt-4-vision"
12api_version = os.getenv("AZURE_OPENAI_API_VERSION")
13
14client = AzureOpenAI(
15 api_key=api_key,
16 api_version=api_version,
17 base_url=f"{api_base}/openai/deployments/{deployment_name}",
18)
19
20def local_image_to_data_url(image_path):
21 mime_type, _ = guess_type(image_path)
22 if mime_type is None:
23 mime_type = "application/octet-stream"
24
25 with open(image_path, "rb") as image_file:
26 base64_encoded_data = base64.b64encode(image_file.read()).decode("utf-8")
27
28 return f"data:{mime_type};base64,{base64_encoded_data}"
29
30image_path = "./webpage.png"
31
32response = client.chat.completions.create(
33 model="gpt-4",
34 messages=[
35 {
36 "role": "system",
37 "content": "You are a professional ront-end engineer who is proficient in Next.js (TypeScript)/Tailwind CSS.",
38 },
39 {
40 "role": "user",
41 "content": [
42 {
43 "type": "text",
44 "text": """
45# Task
46 You will receive a "screenshot of a reference web page" from a "user" and build a single page application using Next.js (TypeScript) and Tailwind CSS.
47
48# Constraints
49 - Must have the same look and feel as the "screenshot of a reference web page"
50 - Exactly reproduce the style of the background color, text color, font size, font family, padding, margin, border, etc.
51 - Use the exact text from the screenshot
52 - Accurately reproduce the alignment of elements and line breaks in the screenshot
53 - Provide complete code, without omitting any code content
54 - Provide only the content of the code, without including "```", "```tsx", or a description of the code at the beginning or end
55
56 screenshot of a reference web page:
57 """,
58 },
59 {
60 "type": "image_url",
61 "image_url": {"url": local_image_to_data_url(image_path)},
62 },
63 ],
64 },
65 ],
66 temperature=0,
67 max_tokens=2000,
68)
69
70print(response.choices[0].message.content)localimagetodataurl関数は、ローカルの画像パスをbase64に変換します。
プロンプトは日本語よりも英語の方が効果的な傾向が見られたため、英語にしています。
プロンプトの日本語訳は以下のとおりです。
システムプロンプト:あなたは、Next.js(TypeScript)/Tailwind CSSに精通したフロントエンドエンジニアです。
ユーザープロンプト:
# タスク
ユーザー」から「参照Webページのスクリーンショット」を受け取り、Next.js(TypeScript)とTailwind CSSを使ってシングルページのアプリケーションを構築する。
# 制約事項
– “参照Webページのスクリーンショット “と同じ外観や雰囲気であること
– 背景色、文字色、フォントサイズ、フォントファミリー、パディング、マージン、ボーダーなどのスタイルを正確に再現すること
– スクリーンショットのテキストを正確に使用すること
– スクリーンショット内の要素の配置や改行を正確に再現すること
– コードの内容を省略することなく、完全なコードを提供すること
– コードの先頭や末尾に”“`”や”“tsx”、コードの説明を含めず、コードの内容のみを提供すること
参考Webページのスクリーンショット:
プロンプトは、以下のScreenshot to Code の GitHub リポジトリのシステムプロンプトなどを参考に作成しました。
1import type { NextPage } from 'next';
2import Head from 'next/head';
3
4const Home: NextPage = () => {
5 return (
6
7
8 Let's Build Together
9
10
11
12
13
14 Let's Build Together
15
16
17 Preline UI is an open-source set of prebuilt UI components, ready-to-use examples and
18
19
20 Figma design system based on the utility-first Tailwind CSS framework.
21
22
23
24 Get started
25
26
27 $ npm i preline
28
29
30
31 Package Manager: npm /{' '}
32
33 Installation Guide
34
35
36
37
38 );
39};
40
41export default Home;
42
43/* Tailwind CSS */
44@tailwind base;
45@tailwind components;
46@tailwind utilities;
47
48@layer components {
49 /* Custom styles can be added here */
50}/* Tailwind CSS */より上の部分をNext.jsで作成したプロジェクトのapp/page.tsxに貼り付けて表示した結果は以下のとおりです。
これを更に洗練するプロンプトは難易度が高そうですが、あとは人間が少し手を加えれば再現できそうなレベルです。
フロントエンドの開発に変化が起きそうです。
なお、プロンプトを日本語にして実行した結果は以下のとおりです。
配置などがかなり崩れていることが確認できます。
日本語より英語のほうが効果的なのでしょうか?
以上になります!
最後まで読んでいただき、ありがとうございました!