File size: 14,764 Bytes
3505baa
 
 
 
 
 
 
 
 
0085935
3505baa
 
0085935
3505baa
 
 
 
 
 
 
 
0085935
3505baa
0085935
 
57cc5e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e7d703
 
 
 
 
 
 
57cc5e9
 
4e7d703
 
57cc5e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e7d703
57cc5e9
 
 
 
 
 
 
 
 
 
 
 
0085935
3505baa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0085935
 
 
3505baa
0085935
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3505baa
0085935
 
3505baa
0085935
57cc5e9
 
 
 
 
 
0085935
 
57cc5e9
 
 
 
 
 
 
 
a7e6208
 
0085935
 
 
 
 
 
57cc5e9
 
 
 
 
 
 
0085935
57cc5e9
3505baa
0085935
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3505baa
 
0085935
 
 
3505baa
 
0085935
 
3505baa
0085935
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3505baa
 
 
 
 
 
 
57cc5e9
3505baa
 
 
 
9bcf92d
3505baa
0085935
3505baa
 
 
0085935
 
 
 
 
 
 
 
 
 
3505baa
 
 
 
0085935
3505baa
 
 
 
 
 
 
0085935
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
from __future__ import annotations
import os, io, re, json, time, mimetypes, tempfile, string
from typing import List, Union, Tuple, Any, Iterable

from PIL import Image
import pandas as pd
import gradio as gr
import google.generativeai as genai
import requests
import pdfplumber

# ================== CONFIG ==================
DEFAULT_API_KEY = "AIzaSyBbK-1P3JD6HPyE3QLhkOps6_-Xo3wUFbs"

INTERNAL_MODEL_MAP = {
    "Gemini 2.5 Flash": "gemini-2.5-flash",
    "Gemini 2.5 Pro":   "gemini-2.5-pro",
}
EXTERNAL_MODEL_NAME = "prithivMLmods/Camel-Doc-OCR-062825 (External)"

try:
    RESAMPLE = Image.Resampling.LANCZOS
except AttributeError:
    RESAMPLE = Image.LANCZOS

PROMPT_FREIGHT_JSON = """
Please analyze the freight rate table in the file I provide and convert it into JSON in the following structure:
{
  "shipping_line": "...",
  "shipping_line_code": "...",
  "shipping_line_reason": "Why this carrier is chosen?",
  "fee_type": "Air Freight",
  "valid_from": ...,
  "valid_to": ...,
  "charges": [
    {
      "frequency": "...",
      "package_type": "...",
      "aircraft_type": "...",
      "direction": "Export or Import or null",
      "origin": "...",
      "destination": "...",
      "charge_name": "...",
      "charge_code": "...",
      "charge_code_reason": "...",
      "cargo_type": "...",
      "currency": "...",
      "transit": "...",
      "transit_time": "...",
      "weight_breaks": {
        "M": ...,
        "N": ...,
        "+45kg": ...,
        "+100kg": ...,
        "+300kg": ...,
        "+500kg": ...,
        "+1000kg": ...,
        "other": {
          key: value
        },
        "weight_breaks_reason":"Why chosen weight_breaks?"
      },
      "remark": "..."
    }
  ],
  "local_charges": [
    {
      "charge_name": "...",
      "charge_code": "...",
      "unit": "...",
      "amount": ...,
      "remark": "..."
    }
  ]
}
### Date rules
- valid_from format:
  - `DD/MM/YYYY` (if full date)
  - `01/MM/YYYY` (if month+year only)
  - `01/01/YYYY` (if year only)
  - `UFN` if missing
- valid_to:
  - exact `DD/MM/YYYY` if present
  - else `UFN`
STRICT RULES:
- ONLY return a single JSON object as specified above.
- All rates must exactly match the corresponding weight break columns (M,N,45kg, 100kg, 300kg, 500kg, 1000kg, etc.). set null if N/A. No assumptions or interpolations.
- If the table shows "RQ" or similar, set value as "RQST".
- Group same-price destinations into one record separated by "/".
- Always use IATA code for origin and destination.
- Flight number (e.g. ZH118) is not charge code.
- Frequency: D[1-7]; 'Daily' = D1234567. Join multiple (e.g. D3,D4→D34).
- If local charges exist, list them.
- If validity missing, set null.
- Direction: Export if origin is Vietnam (SGN, HAN, DAD...), else Import.
- Provide short plain English reasons for "shipping_line_reason" & "charge_code_reason".
- Replace commas in remarks with semicolons.
- Only return JSON.
"""

# ================== HELPERS ==================
import fitz  # PyMuPDF

def pdf_to_images(pdf_bytes: bytes) -> list[Image.Image]:
    doc = fitz.open(stream=pdf_bytes, filetype="pdf")
    pages = []
    for p in doc:
        pix = p.get_pixmap(dpi=200)
        img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
        pages.append(img)
    return pages

def ensure_rgb(im: Image.Image) -> Image.Image:
    return im.convert("RGB") if im.mode != "RGB" else im

def _read_file_bytes(upload: Union[str, os.PathLike, dict, object] | None) -> bytes:
    if upload is None:
        raise ValueError("No file uploaded.")
    if isinstance(upload, (str, os.PathLike)):
        with open(upload, "rb") as f:
            return f.read()
    if isinstance(upload, dict) and "path" in upload:
        with open(upload["path"], "rb") as f:
            return f.read()
    if hasattr(upload, "read"):
        return upload.read()
    raise TypeError(f"Unsupported file object: {type(upload)}")

def _guess_name_and_mime(file, file_bytes: bytes) -> Tuple[str, str]:
    if isinstance(file, (str, os.PathLike)):
        filename = os.path.basename(str(file))
    elif isinstance(file, dict) and "name" in file:
        filename = os.path.basename(file["name"])
    elif isinstance(file, dict) and "path" in file:
        filename = os.path.basename(file["path"])
    else:
        filename = "upload.bin"
    mime, _ = mimetypes.guess_type(filename)
    if not mime:
        if len(file_bytes) >= 4 and file_bytes[:4] == b"%PDF":
            mime = "application/pdf"
            if not filename.lower().endswith(".pdf"):
                filename += ".pdf"
        else:
            mime = "image/png"
    return filename, mime

# ================== PDF CHECK STEP ==================
def check_pdf_structure(file_bytes: bytes) -> str:
    """Kiểm tra nhanh file PDF có phải bảng nhiều cột, nhiều trang không."""
    try:
        with pdfplumber.open(io.BytesIO(file_bytes)) as pdf:
            if len(pdf.pages) <= 2:
                return "không"
            table_pages = 0
            for page in pdf.pages[:3]:
                tables = page.find_tables()
                if tables and len(tables) > 0:
                    table_pages += 1
            if table_pages >= 1:
                return "có"
            text = "\n".join([(p.extract_text() or "") for p in pdf.pages[:2]])
            num_tokens = sum(ch.isdigit() for ch in text)
            line_count = len(text.splitlines())
            if num_tokens > 100 and line_count > 20:
                return "có"
        return "không"
    except Exception as e:
        print("PDF check error:", e)
        return "không"

# ================== OCR CORE (Gemini) ==================
def run_process_internal_base_v2(file_bytes, filename, mime, question, model_choice, temperature, top_p, batch_size=3):
    api_key = os.environ.get("GOOGLE_API_KEY", DEFAULT_API_KEY)
    if not api_key:
        return "ERROR: Missing GOOGLE_API_KEY.", None
    genai.configure(api_key=api_key)
    model_name = INTERNAL_MODEL_MAP.get(model_choice, "gemini-2.5-flash")
    model = genai.GenerativeModel(model_name=model_name,
                                  generation_config={"temperature": float(temperature), "top_p": float(top_p)})

    if file_bytes[:4] == b"%PDF":
        pages = pdf_to_images(file_bytes)
    else:
        pages = [Image.open(io.BytesIO(file_bytes))]

    user_prompt = (question or "").strip() or PROMPT_FREIGHT_JSON
    all_json_results, all_text_results = [], []
    previous_header_json = None

    def _safe_text(resp):
        try:
            return resp.text
        except:
            return ""

    for i in range(0, len(pages), batch_size):
        batch = pages[i:i+batch_size]
        uploaded = []
        for im in batch:
            with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
                im.save(tmp.name)
                up = genai.upload_file(path=tmp.name, mime_type="image/png")
                up = genai.get_file(up.name)
                uploaded.append(up)

        context_prompt = user_prompt
        resp = model.generate_content([context_prompt] + uploaded)
        text = _safe_text(resp)
        all_text_results.append(text)
        for up in uploaded:
            try:
                genai.delete_file(up.name)
            except:
                pass

    return "\n\n".join(all_text_results), None

# ================== EXTERNAL API (nếu có) ==================
def run_process_external(file_bytes, filename, mime, question, api_url, temperature, top_p):
    if not api_url:
        return "ERROR: Missing external API endpoint.", None
    data = {"prompt": question or "", "temperature": str(temperature), "top_p": str(top_p)}
    files = {"file": (filename, file_bytes, mime)}
    r = requests.post(api_url, files=files, data=data, timeout=60)
    if r.status_code >= 400:
        return f"ERROR: External API HTTP {r.status_code}: {r.text[:200]}", None
    return r.text, None

# ================== MAIN ROUTER (đã thêm STEP CHECK) ==================
def run_process(file, question, model_choice, temperature, top_p, external_api_url):
    """
    Router (có bước kiểm tra PDF/table trước khi xử lý):
      - Nếu PDF nhiều trang/nhiều bảng -> extract trước (pdfplumber)
      - Ngược lại -> OCR trực tiếp Gemini
    """
    try:
        if file is None:
            return "ERROR: No file uploaded.", None

        file_bytes = _read_file_bytes(file)
        filename, mime = _guess_name_and_mime(file, file_bytes)

        # STEP 1️⃣: Check PDF structure
        if mime == "application/pdf" or file_bytes[:4] == b"%PDF":
            check_result = check_pdf_structure(file_bytes)
            print(f"[PDF Check] {filename}: {check_result}")

            if check_result == "có":
                try:
                    print("➡️ PDF có nhiều cột/nhiều trang → dùng pdfplumber extract trước rồi Gemini.")
                    all_dfs = []
                    saved_header = None
            
                    with pdfplumber.open(io.BytesIO(file_bytes)) as pdf:
                        for page_idx, page in enumerate(pdf.pages, start=1):
                            print(f"📄 Đang xử lý trang {page_idx}...")
            
                            table = page.extract_table({
                                "vertical_strategy": "lines",
                                "horizontal_strategy": "text",
                                "snap_tolerance": 3,
                                "intersection_tolerance": 5,
                            })
            
                            if not table or len(table) < 2:
                                print(f"⚠️ Trang {page_idx}: Không phát hiện bảng hợp lệ.")
                                continue
            
                            header = table[0]
                            rows = table[1:]
            
                            # Lưu header đầu tiên
                            if saved_header is None:
                                saved_header = header
                                print(f"✅ Trang {page_idx}: Lưu header đầu tiên: {saved_header}")
            
                            # Nếu trang sau không có header rõ → dùng header cũ
                            if len(header) < len(saved_header) or "REGION" not in header[0]:
                                print(f"↩️ Trang {page_idx}: Không có header rõ ràng, dùng lại header trước.")
                                header = saved_header
                                rows = table
                            else:
                                saved_header = header  # cập nhật header hợp lệ
            
                            if len(rows) == 0:
                                print(f"⚠️ Trang {page_idx}: Không có dữ liệu dưới header.")
                                continue
            
                            try:
                                df = pd.DataFrame(rows, columns=header)
                                all_dfs.append(df)
                                print(f"✅ Trang {page_idx}: {len(df)} dòng được thêm.")
                            except Exception as e:
                                print(f"❌ Lỗi tạo DataFrame ở trang {page_idx}: {e}")
            
                    if all_dfs:
                        final_df = pd.concat(all_dfs, ignore_index=True).dropna(how="all").reset_index(drop=True)
                        print(f"✅ Tổng cộng {len(final_df)} dòng được trích xuất từ PDF.")
            
                        # Xuất ra file tạm (Excel + JSON)
                        base_name = os.path.splitext(filename)[0]
                        tmp_dir = tempfile.gettempdir()
                        # json_path = os.path.join(tmp_dir, f"{base_name}.json")
                        # excel_path = os.path.join(tmp_dir, f"{base_name}.xlsx")
            
                        # final_df.to_json(json_path, orient="records", force_ascii=False, indent=2)
                        # final_df.to_excel(excel_path, index=False)
            
                        # print(f"✅ Xuất JSON:  {json_path}")
                        # print(f"✅ Xuất Excel: {excel_path}")
            
                        # Convert bảng thành CSV text để Gemini đọc tiếp
                        table_text = final_df.to_csv(index=False)
                        print(f"✅ Đang Gen text từ file CSV")
                        question = (
                            f"{PROMPT_FREIGHT_JSON}\n"
                            "Below is the table text extracted from the PDF (CSV format):\n"
                            f"{table_text}\n\n"
                            "Please convert this into valid JSON as per the schema."
                        )
                    else:
                        print("⚠️ Không có bảng hợp lệ để extract bằng pdfplumber.")
            
                except Exception as e:
                    print("❌ pdfplumber extract failed:", e)


        # STEP 2️⃣: Route model
        if model_choice == EXTERNAL_MODEL_NAME:
            return run_process_external(
                file_bytes=file_bytes, filename=filename, mime=mime,
                question=question, api_url=external_api_url,
                temperature=temperature, top_p=top_p
            )

        return run_process_internal_base_v2(
            file_bytes=file_bytes, filename=filename, mime=mime,
            question=question, model_choice=model_choice,
            temperature=temperature, top_p=top_p
        )

    except Exception as e:
        return f"ERROR: {type(e).__name__}: {str(e)}", None

# ================== UI ==================
def main():
    with gr.Blocks(title="OCR Multi-Agent System") as demo:
        file = gr.File(label="Upload PDF/Image")
        question = gr.Textbox(label="Prompt", lines=2)
        model_choice = gr.Dropdown(choices=[*INTERNAL_MODEL_MAP.keys(), EXTERNAL_MODEL_NAME],
                                   value="Gemini 2.5 Flash", label="Model")
        temperature = gr.Slider(0.0, 2.0, value=0.2, step=0.05)
        top_p = gr.Slider(0.0, 1.0, value=0.95, step=0.01)
        external_api_url = gr.Textbox(label="External API URL", visible=False)
        output_text = gr.Code(label="Output", language="json")
        run_btn = gr.Button("🚀 Process")

        run_btn.click(
            run_process,
            inputs=[file, question, model_choice, temperature, top_p, external_api_url],
            outputs=[output_text, gr.State()]
        )

    return demo

demo = main()

if __name__ == "__main__":
    demo.launch()