Update app.py
Browse files
app.py
CHANGED
|
@@ -47,82 +47,116 @@ def prompt():
|
|
| 47 |
" ],\n"
|
| 48 |
" \"custom_fields\": {}\n"
|
| 49 |
"}\n\n"
|
| 50 |
-
|
| 51 |
"SHIP FROM RULES:\n"
|
| 52 |
-
"- If
|
| 53 |
-
"- If
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
)
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
def
|
|
|
|
| 68 |
path = Path(file.name)
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 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":
|
| 90 |
)
|
| 91 |
|
| 92 |
-
|
| 93 |
-
s =
|
| 94 |
-
e =
|
| 95 |
-
return
|
| 96 |
|
| 97 |
|
| 98 |
-
def
|
| 99 |
-
|
|
|
|
| 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 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
|
| 118 |
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
gr.Examples(
|
| 122 |
-
examples=
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
)
|
| 127 |
|
| 128 |
demo.launch(share=True)
|
|
|
|
| 47 |
" ],\n"
|
| 48 |
" \"custom_fields\": {}\n"
|
| 49 |
"}\n\n"
|
|
|
|
| 50 |
"SHIP FROM RULES:\n"
|
| 51 |
+
"- If explicit fields like 'Origin', 'Ship From' exist, extract that value.\n"
|
| 52 |
+
"- If the document is an email-style inbound notice (header block) and shows:\n"
|
| 53 |
+
" From: Name <email>\n"
|
| 54 |
+
" then ship_from_name = Name, ship_from_email = email.\n"
|
| 55 |
+
"- If only an email exists and no human name, set both fields to that email.\n"
|
| 56 |
+
"- If both Origin and an email sender exist, use Origin for ship_from_name and still capture the email under ship_from_email.\n"
|
| 57 |
"- Priority: Origin β Email Name β Mill β Sender block β null.\n\n"
|
| 58 |
+
"CARRIER / EQUIPMENT RULE:\n"
|
| 59 |
+
"- If the table contains:\n"
|
| 60 |
+
" Equipment id = <value>\n"
|
| 61 |
+
" Mark = <value>\n"
|
| 62 |
+
" then ALWAYS treat 'Equipment id' as the railcar number.\n"
|
| 63 |
+
"- NEVER use 'Mark' as railcar number.\n"
|
| 64 |
+
"- Carrier type must match the carrier text exactly (e.g., CHICAGO RAIL LINK).\n\n"
|
| 65 |
+
"INVENTORY RULES:\n"
|
| 66 |
+
"- Do not merge length groups. Each unique length or dimension is its own variant.\n"
|
| 67 |
+
"- Extract pcs_per_pkg, packages, pieces, fbm exactly as written.\n"
|
| 68 |
+
"- total_pcs = sum of pieces.\n"
|
| 69 |
+
"- total_fbm = sum of fbm.\n\n"
|
| 70 |
+
"TOTAL QUANTITY RULE:\n"
|
| 71 |
+
"- Use explicit totals if they appear.\n"
|
| 72 |
+
"- If no explicit total quantity appears, leave null.\n\n"
|
| 73 |
+
"CUSTOM FIELDS RULE:\n"
|
| 74 |
+
"- Capture all meaningful leftover fields not part of main schema.\n\n"
|
| 75 |
+
"Return ONLY the JSON."
|
| 76 |
+
)
|
| 77 |
|
|
|
|
|
|
|
| 78 |
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
def extract_image(img):
|
| 81 |
+
"""Process image input"""
|
| 82 |
+
ext = "png" # always PNG internally from Gradio
|
| 83 |
+
|
| 84 |
+
b64 = base64.b64encode(img).decode()
|
| 85 |
+
|
| 86 |
+
content = [
|
| 87 |
+
{"type": "text", "text": prompt()},
|
| 88 |
+
{
|
| 89 |
+
"type": "image_url",
|
| 90 |
+
"image_url": {"url": f"data:image/{ext};base64,{b64}"}
|
| 91 |
+
}
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
r = client.chat.completions.create(
|
| 95 |
+
model=MODEL,
|
| 96 |
+
messages=[{"role": "user", "content": content}]
|
| 97 |
)
|
| 98 |
|
| 99 |
+
text = r.choices[0].message.content
|
| 100 |
+
s = text.find("{")
|
| 101 |
+
e = text.rfind("}")
|
| 102 |
+
return text[s:e+1]
|
| 103 |
+
|
| 104 |
|
| 105 |
+
def extract_pdf(file):
|
| 106 |
+
"""Process PDF input"""
|
| 107 |
path = Path(file.name)
|
| 108 |
+
fid = upload_pdf(path)
|
| 109 |
+
|
| 110 |
+
content = [
|
| 111 |
+
{"type": "text", "text": prompt()},
|
| 112 |
+
{"type": "file", "file": {"file_id": fid}}
|
| 113 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
r = client.chat.completions.create(
|
| 116 |
model=MODEL,
|
| 117 |
+
messages=[{"role": "user", "content": content}]
|
| 118 |
)
|
| 119 |
|
| 120 |
+
text = r.choices[0].message.content
|
| 121 |
+
s = text.find("{")
|
| 122 |
+
e = text.rfind("}")
|
| 123 |
+
return text[s:e+1]
|
| 124 |
|
| 125 |
|
| 126 |
+
def process(image_input, pdf_input):
|
| 127 |
+
if image_input is not None:
|
| 128 |
+
return extract_image(image_input)
|
| 129 |
|
| 130 |
+
if pdf_input is not None:
|
| 131 |
+
return extract_pdf(pdf_input)
|
| 132 |
|
| 133 |
+
return "{}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
|
| 136 |
with gr.Blocks() as demo:
|
| 137 |
gr.Markdown("# **Logistics OCR Data Extractor (GPT-5.1)**")
|
| 138 |
|
| 139 |
+
with gr.Row():
|
| 140 |
+
image_input = gr.Image(type="bytes", label="Upload Image")
|
| 141 |
+
pdf_input = gr.File(type="file", label="Upload PDF")
|
| 142 |
+
|
| 143 |
+
output = gr.JSON(label="Extracted JSON")
|
| 144 |
|
| 145 |
+
submit = gr.Button("Submit")
|
| 146 |
|
| 147 |
+
submit.click(
|
| 148 |
+
fn=process,
|
| 149 |
+
inputs=[image_input, pdf_input],
|
| 150 |
+
outputs=output
|
| 151 |
+
)
|
| 152 |
|
| 153 |
gr.Examples(
|
| 154 |
+
examples=[
|
| 155 |
+
["IMG_0001.jpg", None],
|
| 156 |
+
["IMG_0002.jpg", None]
|
| 157 |
+
],
|
| 158 |
+
inputs=[image_input, pdf_input],
|
| 159 |
+
label="Sample Images",
|
| 160 |
)
|
| 161 |
|
| 162 |
demo.launch(share=True)
|