Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,53 +15,55 @@ MAX_TOKENS = 1024
|
|
| 15 |
# ---------- ❷ JSON Schema ----------
|
| 16 |
product_schema = {
|
| 17 |
"name": "RetailPriceTagSchema",
|
| 18 |
-
"description": "
|
| 19 |
-
"strict": True,
|
| 20 |
"schema": {
|
| 21 |
-
"type": "
|
| 22 |
-
"
|
| 23 |
-
"
|
| 24 |
-
|
| 25 |
-
"
|
| 26 |
-
"type": "
|
| 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 |
-
"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 |
-
|
|
|
|
| 110 |
|
| 111 |
# (1) JSON pretty print
|
| 112 |
-
json_str = json.dumps(
|
| 113 |
|
| 114 |
# (2) to Excel
|
| 115 |
-
df = pd.DataFrame(
|
| 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)
|