1. #Ver1
  2. from flask import Flask, request, render_template_string
  3. from openai import OpenAI
  4. import base64
  5. app = Flask(__name__)
  6. client = OpenAI(api_key="あなたのOpenAI keyをここに入力”) # ←ここに自分のOpenAIキーを貼る
  7. # HTMLテンプレート
  8. html = """
  9. <!doctype html>
  10. <html>
  11. <head>
  12.     <meta charset="utf-8">
  13.     <title>パーソナルカラー診断AI(手の甲)</title>
  14. </head>
  15. <body style="font-family:sans-serif; text-align:center; margin-top:50px;">
  16.     <h2>パーソナルカラー診断AI(手の甲で診断)??</h2>
  17.     <p>手の甲の写真をアップロードすると、AIがあなたの肌トーンからパーソナルカラーを推定します。</p>
  18.     <form action="/analyze" method="post" enctype="multipart/form-data" style="margin-top:20px;">
  19.         <input type="file" name="image" accept="image/*" required>
  20.         <br><br>
  21.         <button type="submit">診断する</button>
  22.     </form>
  23.     {% if result %}
  24.         <hr>
  25.         <h3>診断結果 ?</h3>
  26.         <div style="max-width:600px; margin:auto; text-align:left; font-size:18px;">
  27.             {{ result | safe }}
  28.         </div>
  29.     {% endif %}
  30. </body>
  31. </html>
  32. """
  33. @app.route("/", methods=["GET"])
  34. def index():
  35.     return render_template_string(html)
  36. @app.route("/analyze", methods=["POST"])
  37. def analyze():
  38.     image_file = request.files["image"]
  39.     image_bytes = image_file.read()
  40.     image_base64 = base64.b64encode(image_bytes).decode("utf-8")
  41.     # OpenAIに画像を送信
  42.     response = client.chat.completions.create(
  43.         model="gpt-4o-mini", # 画像認識対応モデル
  44.         messages=[
  45.             {
  46.                 "role": "user",
  47.                 "content": [
  48.                     {
  49.                         "type": "text",
  50.                         "text": (
  51.                             "以下の画像は手の甲です。この画像から肌の色味・明るさ・血色などを分析し、"
  52.                             "4つのパーソナルカラータイプ(スプリング、サマー、オータム、ウィンター)の中から最も近いタイプを推定してください。"
  53.                             "そのうえで:\n"
  54.                             "1. 選ばれたタイプ名(例:スプリングタイプ)\n"
  55.                             "2. 診断理由(肌トーン・血色感などから見た根拠)\n"
  56.                             "3. 似合う色(服・メイク・アクセサリーなど)\n"
  57.                             "4. 避けた方がいい色(理由付き)\n"
  58.                             "を日本語でわかりやすく説明してください。"
  59.                         ),
  60.                     },
  61.                     {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}}
  62.                 ],
  63.             }
  64.         ],
  65.     )
  66.     result = response.choices[0].message.content
  67.     return render_template_string(html, result=result)
  68. if __name__ == "__main__":
  69.     app.run(debug=True)