Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -86,7 +86,7 @@ def encode_image_to_data_url(img_path: str) -> str:
|
|
| 86 |
b64 = base64.b64encode(f.read()).decode()
|
| 87 |
return f"data:{mime};base64,{b64}"
|
| 88 |
|
| 89 |
-
def
|
| 90 |
# 把所有圖片併在同一次對話傳給 GPT-4o
|
| 91 |
messages = [{"role": "system", "content": system_prompt}]
|
| 92 |
for p in image_paths:
|
|
@@ -100,23 +100,29 @@ def call_gpt4o(image_paths):
|
|
| 100 |
}
|
| 101 |
)
|
| 102 |
|
| 103 |
-
|
| 104 |
-
model
|
| 105 |
-
messages
|
| 106 |
-
response_format
|
| 107 |
"type": "json_schema",
|
| 108 |
"json_schema": product_schema
|
| 109 |
-
}
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
# 4o temperature=0,
|
| 113 |
|
| 114 |
# API 已保證回傳一定符合 schema → 直接 loads
|
| 115 |
return json.loads(resp.choices[0].message.content)
|
| 116 |
|
| 117 |
-
def process(images):
|
| 118 |
paths = [img.name for img in images]
|
| 119 |
-
payload =
|
| 120 |
items = payload.get("products", [])
|
| 121 |
|
| 122 |
# (1) JSON pretty print
|
|
@@ -137,11 +143,17 @@ def process(images):
|
|
| 137 |
with gr.Blocks(title="Price-Tag Parser") as demo:
|
| 138 |
gr.Markdown("## 🏷️ 零售標價解析\n上傳一張或多張標價照片 → 取得 JSON 與 Excel")
|
| 139 |
inp = gr.Files(label="上傳圖片 (可多選)", file_types=["image"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
btn = gr.Button("開始解析 🪄")
|
| 141 |
out_json = gr.JSON(label="辨識結果 (JSON)")
|
| 142 |
out_file = gr.File(label="下載 Excel", file_types=[".xlsx"])
|
| 143 |
|
| 144 |
-
btn.click(process, inputs=inp, outputs=[out_json, out_file])
|
| 145 |
|
| 146 |
if __name__ == "__main__":
|
| 147 |
demo.launch()
|
|
|
|
| 86 |
b64 = base64.b64encode(f.read()).decode()
|
| 87 |
return f"data:{mime};base64,{b64}"
|
| 88 |
|
| 89 |
+
def call_gpt_model(model_name, image_paths):
|
| 90 |
# 把所有圖片併在同一次對話傳給 GPT-4o
|
| 91 |
messages = [{"role": "system", "content": system_prompt}]
|
| 92 |
for p in image_paths:
|
|
|
|
| 100 |
}
|
| 101 |
)
|
| 102 |
|
| 103 |
+
params = {
|
| 104 |
+
"model": model_name,
|
| 105 |
+
"messages": messages,
|
| 106 |
+
"response_format": {
|
| 107 |
"type": "json_schema",
|
| 108 |
"json_schema": product_schema
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
# ➕ 如果是 gpt-4o,才加入 temperature = 0.0
|
| 113 |
+
if model_name == "gpt-4o":
|
| 114 |
+
params["temperature"] = 0
|
| 115 |
+
|
| 116 |
+
resp = client.chat.completions.create(**params)
|
| 117 |
|
| 118 |
# 4o temperature=0,
|
| 119 |
|
| 120 |
# API 已保證回傳一定符合 schema → 直接 loads
|
| 121 |
return json.loads(resp.choices[0].message.content)
|
| 122 |
|
| 123 |
+
def process(images, model_name):
|
| 124 |
paths = [img.name for img in images]
|
| 125 |
+
payload = call_gpt_model(model_name, paths) # payload 是 {"products":[ {...}, ... ]}
|
| 126 |
items = payload.get("products", [])
|
| 127 |
|
| 128 |
# (1) JSON pretty print
|
|
|
|
| 143 |
with gr.Blocks(title="Price-Tag Parser") as demo:
|
| 144 |
gr.Markdown("## 🏷️ 零售標價解析\n上傳一張或多張標價照片 → 取得 JSON 與 Excel")
|
| 145 |
inp = gr.Files(label="上傳圖片 (可多選)", file_types=["image"])
|
| 146 |
+
model_selector = gr.Radio(
|
| 147 |
+
choices=["gpt-4o", "o3"],
|
| 148 |
+
value="gpt-4o",
|
| 149 |
+
label="選擇使用的模型"
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
btn = gr.Button("開始解析 🪄")
|
| 153 |
out_json = gr.JSON(label="辨識結果 (JSON)")
|
| 154 |
out_file = gr.File(label="下載 Excel", file_types=[".xlsx"])
|
| 155 |
|
| 156 |
+
btn.click(process, inputs=[inp, model_selector], outputs=[out_json, out_file])
|
| 157 |
|
| 158 |
if __name__ == "__main__":
|
| 159 |
demo.launch()
|