Update app.py
Browse files
app.py
CHANGED
|
@@ -1,87 +1,105 @@
|
|
| 1 |
-
import base64
|
| 2 |
-
import json
|
| 3 |
-
from pathlib import Path
|
| 4 |
-
import gradio as gr
|
| 5 |
-
from openai import OpenAI
|
| 6 |
-
|
| 7 |
-
API_KEY = "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"
|
| 8 |
-
MODEL = "gpt-5.1"
|
| 9 |
-
|
| 10 |
-
client = OpenAI(api_key=API_KEY)
|
| 11 |
-
|
| 12 |
-
SAMPLE_DIR = Path("samples")
|
| 13 |
-
SAMPLES = {
|
| 14 |
-
"
|
| 15 |
-
"Sample
|
| 16 |
-
"
|
| 17 |
-
}
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 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 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import json
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
|
| 7 |
+
API_KEY = "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"
|
| 8 |
+
MODEL = "gpt-5.1"
|
| 9 |
+
|
| 10 |
+
client = OpenAI(api_key=API_KEY)
|
| 11 |
+
|
| 12 |
+
SAMPLE_DIR = Path("samples")
|
| 13 |
+
SAMPLES = {
|
| 14 |
+
"None": None,
|
| 15 |
+
"Sample 1 - IMG_0001.jpg": SAMPLE_DIR / "IMG_0001.jpg",
|
| 16 |
+
"Sample 2 - IMG_0002.jpg": SAMPLE_DIR / "IMG_0002.jpg"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# ------------------ Upload PDF ------------------
|
| 21 |
+
def upload_pdf(path):
|
| 22 |
+
f = client.files.create(
|
| 23 |
+
file=open(path, "rb"),
|
| 24 |
+
purpose="assistants"
|
| 25 |
+
)
|
| 26 |
+
return f.id
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# ------------------ Prompt ---------------------
|
| 30 |
+
def build_prompt():
|
| 31 |
+
return (
|
| 32 |
+
"Extract structured JSON from this shipping document. "
|
| 33 |
+
"Return ONLY valid JSON:\n"
|
| 34 |
+
"{\n"
|
| 35 |
+
" \"po_number\": string|null,\n"
|
| 36 |
+
" \"ship_from\": string|null,\n"
|
| 37 |
+
" \"carrier_type\": string|null,\n"
|
| 38 |
+
" \"rail_car_number\": string|null,\n"
|
| 39 |
+
" \"total_quantity\": number|null,\n"
|
| 40 |
+
" \"inventories\": [\n"
|
| 41 |
+
" {\"productName\":string,\"productCode\":string|null,\"pcs\":number|null,\"dimensions\":string|null}\n"
|
| 42 |
+
" ],\n"
|
| 43 |
+
" \"custom_fields\": {}\n"
|
| 44 |
+
"}\n"
|
| 45 |
+
"Use ONLY the text visible in the document. Do NOT hallucinate values."
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# ------------------ Extract Core Logic ------------------
|
| 50 |
+
def extract_from_path(path: Path):
|
| 51 |
+
|
| 52 |
+
suffix = path.suffix.lower()
|
| 53 |
+
|
| 54 |
+
if suffix == ".pdf":
|
| 55 |
+
file_id = upload_pdf(path)
|
| 56 |
+
content = [
|
| 57 |
+
{"type": "text", "text": build_prompt()},
|
| 58 |
+
{"type": "file", "file": {"file_id": file_id}}
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
else:
|
| 62 |
+
# image handling
|
| 63 |
+
raw = path.read_bytes()
|
| 64 |
+
b64 = base64.b64encode(raw).decode()
|
| 65 |
+
mime = suffix.replace(".", "")
|
| 66 |
+
content = [
|
| 67 |
+
{"type": "text", "text": build_prompt()},
|
| 68 |
+
{"type": "image_url", "image_url": {"url": f"data:image/{mime};base64,{b64}"}}
|
| 69 |
+
]
|
| 70 |
+
|
| 71 |
+
response = client.chat.completions.create(
|
| 72 |
+
model=MODEL,
|
| 73 |
+
messages=[{"role": "user", "content": content}]
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
out = response.choices[0].message.content
|
| 77 |
+
s = out.find("{")
|
| 78 |
+
e = out.rfind("}")
|
| 79 |
+
|
| 80 |
+
return out[s:e+1]
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# ------------------ UI Logic ------------------
|
| 84 |
+
def run_extraction(uploaded_file, sample_name):
|
| 85 |
+
|
| 86 |
+
if uploaded_file:
|
| 87 |
+
return extract_from_path(Path(uploaded_file.name))
|
| 88 |
+
|
| 89 |
+
if sample_name != "None":
|
| 90 |
+
return extract_from_path(SAMPLES[sample_name])
|
| 91 |
+
|
| 92 |
+
return "Upload a file or choose a sample image."
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# ------------------ Gradio Interface ------------------
|
| 96 |
+
gr.Interface(
|
| 97 |
+
fn=run_extraction,
|
| 98 |
+
inputs=[
|
| 99 |
+
gr.File(label="Upload PDF or Image"),
|
| 100 |
+
gr.Dropdown(list(SAMPLES.keys()), value="None", label="Or choose a sample")
|
| 101 |
+
],
|
| 102 |
+
outputs=gr.JSON(label="Extracted JSON"),
|
| 103 |
+
title="Logistics OCR Text Extraction (GPT-5.1 LLM)",
|
| 104 |
+
description="Upload a PDF/image or select a built-in sample to extract structured logistics data."
|
| 105 |
+
).launch()
|