Zaious commited on
Commit
eb0ade0
·
verified ·
1 Parent(s): 7fb15d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -48
app.py CHANGED
@@ -15,53 +15,55 @@ MAX_TOKENS = 1024
15
  # ---------- ❷ JSON Schema ----------
16
  product_schema = {
17
  "name": "RetailPriceTagSchema",
18
- "description": "A list of products extracted from retail price-tag photos.",
19
- "strict": True, # 讓模型一定要符合 schema,否則自動重試
20
  "schema": {
21
- "type": "array",
22
- "items": {
23
- "type": "object",
24
- "properties": {
25
- "name": {
26
- "type": "string",
27
- "description": "商品完整名稱;若無或無法辨識請輸出『無法辨識』"
28
- },
29
- "list_price": {
30
- "type": "string",
31
- "description": "標價上標示的原價 (未折扣);若無或無法辨識請輸出『無法辨識』"
32
- },
33
- "promo_price": {
34
- "type": "string",
35
- "description": "促銷/特價;若無或無法辨識請輸出『無法辨識』"
36
- },
37
- "weight": {
38
- "type": "string",
39
- "description": "總重量 (g / kg 等);若無或無法辨識請輸出『無法辨識』"
40
- },
41
- "volume": {
42
- "type": "string",
43
- "description": "總量 (ml / L / pcs 等);若無或無法辨識請輸出『無法辨識』"
44
- },
45
- "barcode": {
46
- "type": "string",
47
- "description": "EAN/UPC/13 碼條碼;若無或無法辨識請輸出『無法辨識』"
48
- },
49
- "item_code": {
50
- "type": "string",
51
- "description": "通路自用貨號/PLU;若無或無法辨識請輸出『無法辨識』"
 
 
 
 
 
 
 
 
 
 
52
  }
53
- },
54
- "required": [
55
- "name",
56
- "list_price",
57
- "promo_price",
58
- "weight",
59
- "volume",
60
- "barcode",
61
- "item_code"
62
- ],
63
- "additionalProperties": False
64
- }
65
  }
66
  }
67
 
@@ -106,13 +108,14 @@ def call_gpt4o(image_paths):
106
 
107
  def process(images):
108
  paths = [img.name for img in images]
109
- all_items = call_gpt4o(paths)
 
110
 
111
  # (1) JSON pretty print
112
- json_str = json.dumps(all_items, ensure_ascii=False, indent=2)
113
 
114
  # (2) to Excel
115
- df = pd.DataFrame(all_items)
116
  bio = io.BytesIO()
117
  df.to_excel(bio, index=False, engine="openpyxl")
118
  bio.seek(0)
 
15
  # ---------- ❷ JSON Schema ----------
16
  product_schema = {
17
  "name": "RetailPriceTagSchema",
18
+ "description": "Products extracted from retail price-tag photos.",
19
+ "strict": True,
20
  "schema": {
21
+ "type": "object",
22
+ "properties": {
23
+ "products": {
24
+ "type": "array",
25
+ "items": {
26
+ "type": "object",
27
+ "properties": {
28
+ "name": {
29
+ "type": "string",
30
+ "description": "完整商品名稱;若無或無法辨識請輸出『無法辨識』"
31
+ },
32
+ "list_price": {
33
+ "type": "string",
34
+ "description": "原價;若無或無法辨識請輸出『無法辨識』"
35
+ },
36
+ "promo_price": {
37
+ "type": "string",
38
+ "description": "促銷/特價;若無或無法辨識請輸出『無法辨識』"
39
+ },
40
+ "weight": {
41
+ "type": "string",
42
+ "description": "總重量;若無或無法辨識請輸出『無法辨識』"
43
+ },
44
+ "volume": {
45
+ "type": "string",
46
+ "description": "總量;若無或無法辨識請輸出『無法辨識』"
47
+ },
48
+ "barcode": {
49
+ "type": "string",
50
+ "description": "條碼號;若無或無法辨識請輸出『無法辨識』"
51
+ },
52
+ "item_code": {
53
+ "type": "string",
54
+ "description": "通路自用貨號/PLU;若無或無法辨識請輸出『無法辨識』"
55
+ }
56
+ },
57
+ "required": [
58
+ "name", "list_price", "promo_price",
59
+ "weight", "volume", "barcode", "item_code"
60
+ ],
61
+ "additionalProperties": False
62
  }
63
+ }
64
+ },
65
+ "required": ["products"],
66
+ "additionalProperties": False
 
 
 
 
 
 
 
 
67
  }
68
  }
69
 
 
108
 
109
  def process(images):
110
  paths = [img.name for img in images]
111
+ payload = call_gpt4o(paths) # payload 是 {"products":[ {...}, ... ]}
112
+ items = payload.get("products", [])
113
 
114
  # (1) JSON pretty print
115
+ json_str = json.dumps(items, ensure_ascii=False, indent=2)
116
 
117
  # (2) to Excel
118
+ df = pd.DataFrame(items)
119
  bio = io.BytesIO()
120
  df.to_excel(bio, index=False, engine="openpyxl")
121
  bio.seek(0)