sakasegawa
commited on
Commit
•
711ff79
1
Parent(s):
0396a21
init
Browse files- app.py +87 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import base64
|
3 |
+
from openai import OpenAI
|
4 |
+
import pandas as pd
|
5 |
+
import json
|
6 |
+
import io
|
7 |
+
import cv2
|
8 |
+
def estimate_calories(api_key, image):
|
9 |
+
client = OpenAI(api_key=api_key)
|
10 |
+
|
11 |
+
# 画像をbase64エンコードする
|
12 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
13 |
+
_, buffer = cv2.imencode(".jpg", image_rgb)
|
14 |
+
base64_image = base64.b64encode(buffer).decode("utf-8")
|
15 |
+
|
16 |
+
# GPT-4にカロリーを推定させる (JSON-MODE)
|
17 |
+
response = client.chat.completions.create(
|
18 |
+
model="gpt-4o",
|
19 |
+
messages=[
|
20 |
+
{
|
21 |
+
"role": "system",
|
22 |
+
"content": """あなたは画像からカロリーを推定する優秀な栄養士です。提供された食事の画像を分析し、カロリーを推定してください。""",
|
23 |
+
},
|
24 |
+
{
|
25 |
+
"role": "user",
|
26 |
+
"content": [
|
27 |
+
{
|
28 |
+
"type": "image_url",
|
29 |
+
"image_url": {
|
30 |
+
"url": f"data:image/jpg;base64,{base64_image}",
|
31 |
+
"detail": "high",
|
32 |
+
},
|
33 |
+
},
|
34 |
+
{
|
35 |
+
"type": "text",
|
36 |
+
"text": """上記の食事の画像からカロリーを推定してください。料理名は日本語で回答してください。
|
37 |
+
## JSON Schema
|
38 |
+
|
39 |
+
```
|
40 |
+
{
|
41 |
+
"type": "object",
|
42 |
+
"properties": {
|
43 |
+
"step_by_step_estimation": {"type": "string", "description": "料理とカロリーについての推論"},
|
44 |
+
"foods": {
|
45 |
+
"type": "array",
|
46 |
+
"items": {
|
47 |
+
"type": "object",
|
48 |
+
"properties": {
|
49 |
+
"name": {"type": "string", "description": "料理名"},
|
50 |
+
"calorie": {"type": "string", "description": "カロリー"},
|
51 |
+
},
|
52 |
+
"required": ["name", "calorie"],
|
53 |
+
},
|
54 |
+
}
|
55 |
+
},
|
56 |
+
"required": ["step_by_step_estimation", "foods"],
|
57 |
+
}
|
58 |
+
```
|
59 |
+
""",
|
60 |
+
},
|
61 |
+
],
|
62 |
+
},
|
63 |
+
],
|
64 |
+
temperature=1,
|
65 |
+
response_format={"type": "json_object"},
|
66 |
+
)
|
67 |
+
content_json = json.loads(response.choices[0].message.content)
|
68 |
+
step_by_step_estimation = content_json["step_by_step_estimation"]
|
69 |
+
foods = content_json["foods"]
|
70 |
+
|
71 |
+
# foodsをデータフレームに変換する
|
72 |
+
df = pd.DataFrame(foods)
|
73 |
+
return step_by_step_estimation, df
|
74 |
+
|
75 |
+
demo = gr.Interface(
|
76 |
+
fn=estimate_calories,
|
77 |
+
inputs=[gr.Textbox(label="OpenAI API Key"), gr.Image(label="Upload Food Image")],
|
78 |
+
outputs=[
|
79 |
+
gr.Textbox(label="段階的な推論"),
|
80 |
+
gr.DataFrame(headers=["name", "calorie"], label="推定カロリー"),
|
81 |
+
],
|
82 |
+
title="Calorie Predictor",
|
83 |
+
description="食事の画像をアップロードしOpenAIのAPIキーを入力すると、カロリーが推定されます。API使用料にご注意ください。",
|
84 |
+
)
|
85 |
+
|
86 |
+
if __name__ == "__main__":
|
87 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
opencv-python
|
3 |
+
pandas
|