mlbench123 commited on
Commit
b98f926
·
verified ·
1 Parent(s): 11a5a6b

Upload 4 files

Browse files
Files changed (5) hide show
  1. .gitattributes +2 -0
  2. IMG_0001.jpg +3 -0
  3. IMG_0002.jpg +3 -0
  4. app.py +87 -0
  5. requirements.txt +2 -0
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ IMG_0001.jpg filter=lfs diff=lfs merge=lfs -text
37
+ IMG_0002.jpg filter=lfs diff=lfs merge=lfs -text
IMG_0001.jpg ADDED

Git LFS Details

  • SHA256: 5e1453024c82634b9c55e8a0b33459fa963afd71c4ca3ed3fa8906cde0350b1e
  • Pointer size: 131 Bytes
  • Size of remote file: 625 kB
IMG_0002.jpg ADDED

Git LFS Details

  • SHA256: e581324771c23452371a3e830f468585922b833234307de3b6c32d25f15219d9
  • Pointer size: 131 Bytes
  • Size of remote file: 487 kB
app.py ADDED
@@ -0,0 +1,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
+ "Sample 1 (IMG_0001.jpg)": SAMPLE_DIR / "IMG_0001.jpg",
15
+ "Sample 2 (IMG_0002.jpg)": SAMPLE_DIR / "IMG_0002.jpg",
16
+ "None": None
17
+ }
18
+
19
+ def upload_pdf(p):
20
+ f = client.files.create(file=open(p, "rb"), purpose="assistants")
21
+ return f.id
22
+
23
+ def prompt():
24
+ return (
25
+ "Extract structured JSON:\n"
26
+ "{"
27
+ "\"po_number\":string|null,"
28
+ "\"ship_from\":string|null,"
29
+ "\"carrier_type\":string|null,"
30
+ "\"rail_car_number\":string|null,"
31
+ "\"total_quantity\":number|null,"
32
+ "\"inventories\":[{\"productName\":string,\"productCode\":string|null,"
33
+ "\"pcs\":number|null,\"dimensions\":string|null}],"
34
+ "\"custom_fields\":{}"
35
+ "}\n"
36
+ "Use only PDF/image content."
37
+ )
38
+
39
+ def extract_from_path(path: Path):
40
+ suffix = path.suffix.lower()
41
+
42
+ if suffix == ".pdf":
43
+ fid = upload_pdf(path)
44
+ msg = [
45
+ {"type": "text", "text": prompt()},
46
+ {"type": "file", "file": {"file_id": fid}}
47
+ ]
48
+ else:
49
+ b64 = base64.b64encode(path.read_bytes()).decode()
50
+ msg = [
51
+ {"type": "text", "text": prompt()},
52
+ {
53
+ "type": "image_url",
54
+ "image_url": {"url": f"data:image/{suffix[1:]};base64,{b64}"}
55
+ }
56
+ ]
57
+
58
+ response = client.chat.completions.create(
59
+ model=MODEL,
60
+ messages=[{"role": "user", "content": msg}]
61
+ )
62
+
63
+ raw = response.choices[0].message.content
64
+ start = raw.find("{")
65
+ end = raw.rfind("}")
66
+ return raw[start:end+1]
67
+
68
+ def run_extraction(uploaded_file, sample_name):
69
+ if uploaded_file is not None:
70
+ return extract_from_path(Path(uploaded_file.name))
71
+
72
+ if sample_name != "None":
73
+ sample_path = SAMPLES[sample_name]
74
+ return extract_from_path(sample_path)
75
+
76
+ return "Upload a file or select a sample."
77
+
78
+ gr.Interface(
79
+ fn=run_extraction,
80
+ inputs=[
81
+ gr.File(label="Upload PDF or Image (optional)"),
82
+ gr.Dropdown(list(SAMPLES.keys()), value="None", label="Or choose a sample image")
83
+ ],
84
+ outputs=gr.JSON(label="Extracted JSON"),
85
+ title="Logistics OCR Text Extraction (OpenAI GPT-5.1)",
86
+ description="Upload your own PDF/image or choose a sample to test the extraction."
87
+ ).launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ openai>=1.0.0
2
+ gradio