Update app.py
Browse files
app.py
CHANGED
|
@@ -12,12 +12,10 @@ MODEL = "gpt-5.1"
|
|
| 12 |
client = OpenAI(api_key=API_KEY)
|
| 13 |
|
| 14 |
|
| 15 |
-
# ---------------- PDF Upload ----------------
|
| 16 |
def upload_pdf(path):
|
| 17 |
return client.files.create(file=open(path, "rb"), purpose="assistants").id
|
| 18 |
|
| 19 |
|
| 20 |
-
# ---------------- Prompt ----------------
|
| 21 |
def prompt():
|
| 22 |
return (
|
| 23 |
"Extract structured JSON from the attached logistics document. Return ONLY valid JSON.\n"
|
|
@@ -51,83 +49,80 @@ def prompt():
|
|
| 51 |
"}\n\n"
|
| 52 |
|
| 53 |
"SHIP FROM RULES:\n"
|
| 54 |
-
"- If
|
| 55 |
-
"- If
|
| 56 |
-
" From: Name <email>\n"
|
| 57 |
-
" then ship_from_name = Name, ship_from_email = email.\n"
|
| 58 |
-
"- If only email exists, set both fields to email.\n"
|
| 59 |
-
"- If both Origin and email sender exist, name = Origin and email under custom_fields.\n"
|
| 60 |
"- Priority: Origin β Email Name β Mill β Sender block β null.\n\n"
|
| 61 |
|
| 62 |
-
"
|
| 63 |
-
"-
|
| 64 |
-
"- 'Mark' must never be used as the railcar number.\n\n"
|
| 65 |
|
| 66 |
"INVENTORY RULES:\n"
|
| 67 |
-
"- Each dimension group
|
| 68 |
-
"-
|
| 69 |
-
"- total_pcs = sum of pieces.\n"
|
| 70 |
-
"- total_fbm = sum of fbm.\n\n"
|
| 71 |
-
|
| 72 |
-
"TOTAL QUANTITY RULE:\n"
|
| 73 |
-
"- Use explicit totals. If no total, leave null.\n\n"
|
| 74 |
-
|
| 75 |
-
"CUSTOM FIELDS RULE:\n"
|
| 76 |
-
"- Capture leftover meaningful text.\n\n"
|
| 77 |
|
| 78 |
"Return ONLY the JSON."
|
| 79 |
)
|
| 80 |
|
| 81 |
|
| 82 |
-
# ---------------- Extraction ----------------
|
| 83 |
def extract(file):
|
| 84 |
path = Path(file.name)
|
| 85 |
-
|
| 86 |
|
| 87 |
-
if
|
| 88 |
fid = upload_pdf(path)
|
| 89 |
-
|
| 90 |
{"type": "text", "text": prompt()},
|
| 91 |
{"type": "file", "file": {"file_id": fid}}
|
| 92 |
]
|
| 93 |
else:
|
| 94 |
b64 = base64.b64encode(path.read_bytes()).decode()
|
| 95 |
-
|
| 96 |
{"type": "text", "text": prompt()},
|
| 97 |
{
|
| 98 |
"type": "image_url",
|
| 99 |
-
"image_url": {"url": f"data:image/{
|
| 100 |
}
|
| 101 |
]
|
| 102 |
|
| 103 |
r = client.chat.completions.create(
|
| 104 |
model=MODEL,
|
| 105 |
-
messages=[{"role": "user", "content":
|
| 106 |
)
|
| 107 |
|
| 108 |
-
|
| 109 |
-
s =
|
| 110 |
-
e =
|
| 111 |
-
return
|
| 112 |
|
| 113 |
|
| 114 |
def ui(file):
|
| 115 |
return extract(file)
|
| 116 |
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
| 120 |
["IMG_0001.jpg"],
|
| 121 |
["IMG_0002.jpg"]
|
| 122 |
]
|
| 123 |
|
| 124 |
|
| 125 |
-
|
| 126 |
-
gr.
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
client = OpenAI(api_key=API_KEY)
|
| 13 |
|
| 14 |
|
|
|
|
| 15 |
def upload_pdf(path):
|
| 16 |
return client.files.create(file=open(path, "rb"), purpose="assistants").id
|
| 17 |
|
| 18 |
|
|
|
|
| 19 |
def prompt():
|
| 20 |
return (
|
| 21 |
"Extract structured JSON from the attached logistics document. Return ONLY valid JSON.\n"
|
|
|
|
| 49 |
"}\n\n"
|
| 50 |
|
| 51 |
"SHIP FROM RULES:\n"
|
| 52 |
+
"- If header shows 'From: Name <email>' β ship_from_name, ship_from_email.\n"
|
| 53 |
+
"- If 'Origin:' exists, use that as ship_from_name.\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
"- Priority: Origin β Email Name β Mill β Sender block β null.\n\n"
|
| 55 |
|
| 56 |
+
"EQUIPMENT RULE:\n"
|
| 57 |
+
"- 'Equipment id' = rail_car_number. Never use 'Mark'.\n\n"
|
|
|
|
| 58 |
|
| 59 |
"INVENTORY RULES:\n"
|
| 60 |
+
"- Each dimension group is separate.\n"
|
| 61 |
+
"- Do NOT merge table rows.\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
"Return ONLY the JSON."
|
| 64 |
)
|
| 65 |
|
| 66 |
|
|
|
|
| 67 |
def extract(file):
|
| 68 |
path = Path(file.name)
|
| 69 |
+
ext = path.suffix.lower()
|
| 70 |
|
| 71 |
+
if ext == ".pdf":
|
| 72 |
fid = upload_pdf(path)
|
| 73 |
+
payload = [
|
| 74 |
{"type": "text", "text": prompt()},
|
| 75 |
{"type": "file", "file": {"file_id": fid}}
|
| 76 |
]
|
| 77 |
else:
|
| 78 |
b64 = base64.b64encode(path.read_bytes()).decode()
|
| 79 |
+
payload = [
|
| 80 |
{"type": "text", "text": prompt()},
|
| 81 |
{
|
| 82 |
"type": "image_url",
|
| 83 |
+
"image_url": {"url": f"data:image/{ext[1:]};base64,{b64}"}
|
| 84 |
}
|
| 85 |
]
|
| 86 |
|
| 87 |
r = client.chat.completions.create(
|
| 88 |
model=MODEL,
|
| 89 |
+
messages=[{"role": "user", "content": payload}]
|
| 90 |
)
|
| 91 |
|
| 92 |
+
output = r.choices[0].message.content
|
| 93 |
+
s = output.find("{")
|
| 94 |
+
e = output.rfind("}")
|
| 95 |
+
return output[s:e+1]
|
| 96 |
|
| 97 |
|
| 98 |
def ui(file):
|
| 99 |
return extract(file)
|
| 100 |
|
| 101 |
|
| 102 |
+
### -----------------------------------------
|
| 103 |
+
### REAL IMAGE PREVIEW (your two sample imgs)
|
| 104 |
+
### -----------------------------------------
|
| 105 |
+
sample_images = [
|
| 106 |
["IMG_0001.jpg"],
|
| 107 |
["IMG_0002.jpg"]
|
| 108 |
]
|
| 109 |
|
| 110 |
|
| 111 |
+
with gr.Blocks() as demo:
|
| 112 |
+
gr.Markdown("# **Logistics OCR Data Extractor (GPT-5.1)**")
|
| 113 |
+
|
| 114 |
+
file_input = gr.File(label="Upload PDF or Image")
|
| 115 |
+
output_json = gr.JSON(label="Extracted JSON")
|
| 116 |
+
|
| 117 |
+
submit_btn = gr.Button("Submit")
|
| 118 |
+
|
| 119 |
+
submit_btn.click(fn=ui, inputs=file_input, outputs=output_json)
|
| 120 |
+
|
| 121 |
+
gr.Examples(
|
| 122 |
+
examples=sample_images,
|
| 123 |
+
inputs=file_input,
|
| 124 |
+
label="Sample Inputs",
|
| 125 |
+
examples_per_page=2
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
demo.launch(share=True)
|