File size: 2,991 Bytes
711ff79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import base64
from openai import OpenAI
import pandas as pd
import json
import io
import cv2
def estimate_calories(api_key, image):
    client = OpenAI(api_key=api_key)

    # 画像をbase64エンコードする
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    _, buffer = cv2.imencode(".jpg", image_rgb)
    base64_image = base64.b64encode(buffer).decode("utf-8")

    # GPT-4にカロリーを推定させる (JSON-MODE)
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {
                "role": "system",
                "content": """あなたは画像からカロリーを推定する優秀な栄養士です。提供された食事の画像を分析し、カロリーを推定してください。""",
            },
            {
                "role": "user",
                "content": [
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": f"data:image/jpg;base64,{base64_image}",
                            "detail": "high",
                        },
                    },
                    {
                        "type": "text",
                        "text": """上記の食事の画像からカロリーを推定してください。料理名は日本語で回答してください。
## JSON Schema

```
{
    "type": "object",
    "properties": {
        "step_by_step_estimation": {"type": "string", "description": "料理とカロリーについての推論"},
        "foods": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "name": {"type": "string", "description": "料理名"},
                    "calorie": {"type": "string", "description": "カロリー"},
                },
                "required": ["name", "calorie"],
            },
        }
    },
    "required": ["step_by_step_estimation", "foods"],
}
```
""",
                    },
                ],
            },
        ],
        temperature=1,
        response_format={"type": "json_object"},
    )
    content_json = json.loads(response.choices[0].message.content)
    step_by_step_estimation = content_json["step_by_step_estimation"]
    foods = content_json["foods"]

    # foodsをデータフレームに変換する
    df = pd.DataFrame(foods)
    return step_by_step_estimation, df

demo = gr.Interface(
    fn=estimate_calories,
    inputs=[gr.Textbox(label="OpenAI API Key"), gr.Image(label="Upload Food Image")],
    outputs=[
        gr.Textbox(label="段階的な推論"),
        gr.DataFrame(headers=["name", "calorie"], label="推定カロリー"),
    ],
    title="Calorie Predictor",
    description="食事の画像をアップロードしOpenAIのAPIキーを入力すると、カロリーが推定されます。API使用料にご注意ください。",
)

if __name__ == "__main__":
    demo.launch()