mlbench123 commited on
Commit
075a1f5
·
verified ·
1 Parent(s): b98f926

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -87
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
- "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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()