File size: 26,865 Bytes
79f9b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
"""
HS Code Classifier Web App

FastAPI backend with:
- Real-time HS code prediction from text input
- Document upload with OCR (Tesseract) support
- Structured field extraction from trade documents
- HS (6-digit) and HTS (7-10 digit) code support
- Top-5 suggestions with confidence scores
- Latent space visualization with UMAP
- Multilingual support (EN, TH, VI, ZH)
"""

import json
import os
import re
import shutil
import tempfile
import threading
import time
import pickle
import uuid
from pathlib import Path

import numpy as np
import pandas as pd
from fastapi import FastAPI, Request, UploadFile, File, Form
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from sentence_transformers import SentenceTransformer
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import LabelEncoder

from field_extractor import extract_fields, get_all_countries, get_all_currencies
from hs_dataset import get_dataset, get_hts_extensions, get_available_hts_countries

# Paths
PROJECT_DIR = Path(__file__).parent
MODEL_DIR = PROJECT_DIR / "models"
DATA_DIR = PROJECT_DIR / "data"
UPLOAD_DIR = PROJECT_DIR / "uploads"
UPLOAD_DIR.mkdir(exist_ok=True)

# Upload config
MAX_FILE_SIZE = 10 * 1024 * 1024  # 10MB
ALLOWED_EXTENSIONS = {".png", ".jpg", ".jpeg", ".tiff", ".tif", ".bmp", ".pdf"}

# Initialize FastAPI
from starlette.middleware.gzip import GZipMiddleware

app = FastAPI(title="HS Code Classifier", version="2.0.0")
app.add_middleware(GZipMiddleware, minimum_size=1000)
app.mount("/static", StaticFiles(directory=str(PROJECT_DIR / "static")), name="static")
templates = Jinja2Templates(directory=str(PROJECT_DIR / "templates"))

# Global model state
model = None
classifier = None
label_encoder = None
hs_reference = None
training_data = None
embeddings = None
umap_data = None
umap_ready = False
hs_dataset = None
classifier_training_indices = None


def _download_hf_artifacts():
    """Download large artifacts from HF Hub if not present locally."""
    from huggingface_hub import hf_hub_download
    repo_id = os.getenv("HF_ARTIFACT_REPO", "Mead0w1ark/multilingual-e5-small-hs-codes")

    file_map = {
        MODEL_DIR / "embeddings.npy": "embeddings.npy",
        MODEL_DIR / "knn_classifier.pkl": "knn_classifier.pkl",
        MODEL_DIR / "label_encoder.pkl": "label_encoder.pkl",
        MODEL_DIR / "metadata.json": "metadata.json",
        MODEL_DIR / "umap_data.json": "umap_data.json",
        DATA_DIR / "training_data.csv": "training_data.csv",
    }
    for local_path, repo_filename in file_map.items():
        if not local_path.exists():
            print(f"Downloading {repo_filename} from {repo_id}...")
            try:
                downloaded = hf_hub_download(
                    repo_id=repo_id, filename=repo_filename,
                )
                local_path.parent.mkdir(parents=True, exist_ok=True)
                shutil.copy2(downloaded, local_path)
                print(f"  -> {local_path}")
            except Exception as e:
                print(f"  Warning: could not download {repo_filename}: {e}")


def load_models():
    """Load all model artifacts on startup."""
    global model, classifier, label_encoder, hs_reference, training_data, embeddings, umap_data, hs_dataset, classifier_training_indices

    print("Loading models...")
    start = time.time()

    # Download large artifacts from HF Hub if missing locally.
    _download_hf_artifacts()

    # Load sentence transformer:
    # prefer local bundled model, fall back to Hub model when large files are not in repo.
    local_model_dir = MODEL_DIR / "sentence_model"
    has_local_weights = (
        (local_model_dir / "model.safetensors").exists()
        or (local_model_dir / "pytorch_model.bin").exists()
    )
    has_local_tokenizer = (local_model_dir / "tokenizer.json").exists()

    if local_model_dir.exists() and has_local_weights and has_local_tokenizer:
        model = SentenceTransformer(str(local_model_dir))
        print("Loaded local sentence model from models/sentence_model")
    else:
        fallback_model = os.getenv(
            "SENTENCE_MODEL_NAME",
            "intfloat/multilingual-e5-small",
        )
        model = SentenceTransformer(fallback_model)
        print(f"Loaded sentence model from Hugging Face Hub: {fallback_model}")

    # Load HS code reference
    with open(DATA_DIR / "hs_codes_reference.json") as f:
        hs_reference = json.load(f)

    # Load training data
    training_data_path = DATA_DIR / "training_data_indexed.csv"
    if not training_data_path.exists():
        training_data_path = DATA_DIR / "training_data.csv"
    training_data = pd.read_csv(training_data_path)
    training_data["hs_code"] = training_data["hs_code"].astype(str).str.zfill(6)

    classifier_path = MODEL_DIR / "knn_classifier.pkl"
    label_encoder_path = MODEL_DIR / "label_encoder.pkl"
    embeddings_path = MODEL_DIR / "embeddings.npy"
    embeddings_part_paths = sorted(MODEL_DIR.glob("embeddings_part*.npy"))
    core_codes = {str(k).zfill(6) for k in hs_reference.keys()}
    artifacts_exist = (
        classifier_path.exists()
        and label_encoder_path.exists()
        and (embeddings_path.exists() or len(embeddings_part_paths) > 0)
    )

    def load_cached_embeddings():
        if embeddings_path.exists():
            return np.load(embeddings_path)
        part_paths = sorted(MODEL_DIR.glob("embeddings_part*.npy"))
        if part_paths:
            parts = [np.load(p) for p in part_paths]
            return np.concatenate(parts, axis=0)
        return None

    def compute_full_embeddings():
        texts = training_data["text"].fillna("").astype(str).tolist()
        if not texts:
            raise RuntimeError("No training rows available to rebuild classifier.")
        return model.encode(
            [f"passage: {text}" for text in texts],
            normalize_embeddings=True,
            convert_to_numpy=True,
        )

    def rebuild_classifier_on_curated_codes():
        global classifier, label_encoder, classifier_training_indices
        classifier_df = training_data[training_data["hs_code"].isin(core_codes)].copy()
        if classifier_df.empty:
            classifier_df = training_data

        clf_indices = classifier_df.index.to_numpy()
        clf_embeddings = embeddings[clf_indices]
        hs_labels = classifier_df["hs_code"].tolist()
        label_encoder = LabelEncoder()
        y = label_encoder.fit_transform(hs_labels)

        classifier = KNeighborsClassifier(
            n_neighbors=min(5, len(classifier_df)),
            metric="cosine",
            weights="distance",
        )
        classifier.fit(clf_embeddings, y)
        classifier_training_indices = clf_indices
        print(
            f"Rebuilt classifier on {len(classifier_df)} rows "
            f"across {len(set(hs_labels))} curated HS codes"
        )

        try:
            np.save(embeddings_path, embeddings)
            with open(classifier_path, "wb") as f:
                pickle.dump(classifier, f)
            with open(label_encoder_path, "wb") as f:
                pickle.dump(label_encoder, f)
            print("Saved rebuilt classifier artifacts to models/")
        except Exception as e:
            print(f"Warning: could not cache rebuilt artifacts: {e}")

    if artifacts_exist:
        with open(classifier_path, "rb") as f:
            classifier = pickle.load(f)
        with open(label_encoder_path, "rb") as f:
            label_encoder = pickle.load(f)
        embeddings = load_cached_embeddings()
        print("Loaded classifier artifacts from models/")

        if embeddings is None or len(embeddings) != len(training_data):
            print(
                f"Embeddings size mismatch (embeddings={len(embeddings) if embeddings is not None else 0}, "
                f"data={len(training_data)}). "
                "Recomputing embeddings..."
            )
            embeddings = compute_full_embeddings()

        artifact_codes = {str(c).zfill(6) for c in getattr(label_encoder, "classes_", [])}
        invalid_artifacts = (
            not artifact_codes
            or not artifact_codes.issubset(core_codes)
            or len(artifact_codes) > len(core_codes)
        )
        if invalid_artifacts:
            print("Classifier artifacts not aligned with curated HS set; rebuilding classifier...")
            rebuild_classifier_on_curated_codes()
        else:
            # Map KNN fit row indices back to full training_data row indices for latent neighbors.
            classifier_df = training_data[training_data["hs_code"].isin(artifact_codes)].copy()
            classifier_training_indices = classifier_df.index.to_numpy()
            n_fit = int(getattr(classifier, "n_samples_fit_", 0))
            if n_fit <= 0:
                fit_x = getattr(classifier, "_fit_X", None)
                n_fit = int(fit_x.shape[0]) if fit_x is not None else 0

            if n_fit > 0 and len(classifier_training_indices) == n_fit:
                print(f"Mapped classifier indices to {len(classifier_training_indices)} training rows")
            else:
                print(
                    "Classifier index mapping mismatch "
                    f"(mapped={len(classifier_training_indices)}, fit={n_fit}); rebuilding classifier..."
                )
                rebuild_classifier_on_curated_codes()
    else:
        print("Classifier artifacts missing; rebuilding from training data...")
        embeddings = compute_full_embeddings()
        rebuild_classifier_on_curated_codes()
    
    # Load HS dataset (official harmonized-system data)
    hs_dataset = get_dataset()
    
    # UMAP data is loaded/computed in a background thread so the server
    # can start immediately and pass the HF Space health check.
    umap_data = []

    elapsed = time.time() - start
    print(f"All models loaded in {elapsed:.1f}s")


def _compute_umap_background():
    """Load UMAP data from cache or compute in background.

    Sets the global ``umap_data`` list and ``umap_ready`` flag when done.
    """
    global umap_data, umap_ready

    cache_path = MODEL_DIR / "umap_data.json"
    if cache_path.exists():
        try:
            with open(cache_path, encoding="utf-8") as f:
                cached = json.load(f)
            has_category_fields = (
                isinstance(cached, list)
                and len(cached) > 0
                and "chapter_name" in cached[0]
            )
            if isinstance(cached, list) and len(cached) == len(training_data) and has_category_fields:
                umap_data = cached
                umap_ready = True
                print(f"Loaded cached UMAP data: {len(umap_data)} points")
                return
            else:
                print(
                    f"Cached UMAP size mismatch (cache={len(cached)}, data={len(training_data)}). "
                    "Recomputing UMAP projection..."
                )
        except Exception as e:
            print(f"Warning: could not read UMAP cache: {e}")

    print("Computing UMAP projection (background)...")
    try:
        import umap
        reducer = umap.UMAP(
            n_neighbors=30,
            min_dist=0.0,
            n_components=2,
            metric='cosine',
            random_state=42,
        )
        umap_coords = reducer.fit_transform(embeddings)

        points = []
        for i, row in training_data.iterrows():
            hs_code = str(row["hs_code"]).zfill(6)
            chapter = row["hs_chapter"]
            chapter_name = str(row.get("hs_chapter_name", "")).strip()
            if not chapter_name or re.match(r"^HS\s\d{2}$", chapter_name):
                chapter_name = str(chapter).split(";")[0].strip()
            desc = hs_reference.get(hs_code, {}).get("desc", "Unknown")
            points.append({
                "x": float(umap_coords[i, 0]),
                "y": float(umap_coords[i, 1]),
                "text": row["text"][:80],
                "hs_code": hs_code,
                "chapter": chapter,
                "chapter_name": chapter_name,
                "hs_desc": desc,
                "language": row["language"],
            })

        with open(cache_path, "w", encoding="utf-8") as f:
            json.dump(points, f, ensure_ascii=False)

        umap_data = points
        umap_ready = True
        print(f"UMAP projection computed for {len(umap_data)} points")
    except Exception as e:
        print(f"UMAP computation failed: {e}")
        umap_ready = True  # mark ready so endpoints stop saying "computing"


@app.on_event("startup")
async def startup():
    load_models()
    threading.Thread(target=_compute_umap_background, daemon=True).start()


@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
    """Main page."""
    metadata = {}
    try:
        with open(MODEL_DIR / "metadata.json") as f:
            metadata = json.load(f)
    except:
        pass
    
    countries = get_all_countries()
    currencies = get_all_currencies()
    hts_countries = get_available_hts_countries()
    
    return templates.TemplateResponse("index.html", {
        "request": request,
        "metadata": metadata,
        "countries": countries,
        "currencies": currencies,
        "hts_countries": hts_countries,
    })


@app.post("/predict")
async def predict(request: Request):
    """Predict HS code for a product description with optional structured context."""
    body = await request.json()
    query_text = body.get("text", "").strip()
    made_in = body.get("made_in", "")
    ship_to = body.get("ship_to", "")
    item_price = body.get("item_price", None)
    currency = body.get("currency", "")
    
    if not query_text:
        return JSONResponse({"error": "No text provided"}, status_code=400)
    
    start = time.time()
    
    # Build enriched query using structured fields
    enriched_query = query_text
    context_parts = []
    if made_in:
        context_parts.append(f"origin: {made_in}")
    if ship_to:
        context_parts.append(f"destination: {ship_to}")
    if item_price and currency:
        context_parts.append(f"value: {currency} {item_price}")
    
    if context_parts:
        enriched_query = f"{query_text} ({', '.join(context_parts)})"
    
    # Encode query with e5 prefix
    query_emb = model.encode(
        [f"query: {enriched_query}"],
        normalize_embeddings=True,
        convert_to_numpy=True
    )
    
    # Get predictions with probabilities
    probs = classifier.predict_proba(query_emb)[0]
    top_k = 5
    top_indices = np.argsort(probs)[-top_k:][::-1]
    
    predictions = []
    for idx in top_indices:
        hs_code = label_encoder.classes_[idx]
        hs_code_padded = str(hs_code).zfill(6)
        confidence = float(probs[idx])
        if confidence < 0.01:
            continue
        
        info = hs_reference.get(hs_code_padded, {})
        chapter_code = hs_code_padded[:2]
        heading_code = hs_code_padded[:4]
        
        # Get official description from HS dataset if available
        official = hs_dataset.lookup(hs_code_padded) if hs_dataset else None
        official_desc = official['description'] if official else None
        
        # Validate against official dataset
        validation = hs_dataset.validate_hs_code(hs_code_padded) if hs_dataset else None
        
        predictions.append({
            "hs_code": hs_code_padded,
            "confidence": confidence,
            "description": info.get("desc", official_desc or "No description available"),
            "official_description": official_desc,
            "chapter": info.get("chapter", "Unknown"),
            "chapter_code": chapter_code,
            "heading_code": heading_code,
            "validated": validation['valid'] if validation else None,
        })
    
    # Find nearest training examples
    sims = embeddings @ query_emb.T
    top_sim_idx = np.argsort(sims.flatten())[-3:][::-1]
    similar_examples = []
    for idx in top_sim_idx:
        if idx < len(training_data):
            similar_examples.append({
                "text": training_data.iloc[idx]["text"],
                "hs_code": str(training_data.iloc[idx]["hs_code"]).zfill(6),
                "similarity": float(sims[idx][0]),
            })
    
    elapsed = time.time() - start
    
    return JSONResponse({
        "query": query_text,
        "enriched_query": enriched_query,
        "predictions": predictions,
        "similar_examples": similar_examples,
        "inference_time_ms": round(elapsed * 1000, 1),
    })


@app.post("/upload-document")
async def upload_document(file: UploadFile = File(...)):
    """Upload a document (image/PDF) and extract text via OCR + structured fields."""
    # Validate file
    if not file.filename:
        return JSONResponse({"error": "No file provided"}, status_code=400)
    
    ext = Path(file.filename).suffix.lower()
    if ext not in ALLOWED_EXTENSIONS:
        return JSONResponse(
            {"error": f"Unsupported file type: {ext}. Allowed: {', '.join(ALLOWED_EXTENSIONS)}"},
            status_code=400
        )
    
    # Read file content
    content = await file.read()
    if len(content) > MAX_FILE_SIZE:
        return JSONResponse(
            {"error": f"File too large. Maximum: {MAX_FILE_SIZE // (1024*1024)}MB"},
            status_code=400
        )
    
    # Save to temp file
    file_id = str(uuid.uuid4())[:8]
    temp_path = UPLOAD_DIR / f"{file_id}{ext}"
    with open(temp_path, "wb") as f:
        f.write(content)
    
    try:
        import pytesseract
        from PIL import Image
        
        ocr_text = ""
        
        if ext == ".pdf":
            # Convert PDF to images, then OCR
            try:
                from pdf2image import convert_from_path
                images = convert_from_path(str(temp_path), dpi=300)
                texts = []
                for img in images:
                    texts.append(pytesseract.image_to_string(img))
                ocr_text = "\n\n".join(texts)
            except ImportError:
                return JSONResponse(
                    {"error": "PDF support requires pdf2image and poppler. Install with: pip install pdf2image"},
                    status_code=500
                )
            except Exception as e:
                return JSONResponse(
                    {"error": f"PDF processing error: {str(e)}"},
                    status_code=500
                )
        else:
            # Image OCR
            img = Image.open(temp_path)
            ocr_text = pytesseract.image_to_string(img)
        
        if not ocr_text.strip():
            return JSONResponse({
                "error": "OCR could not extract any text from this document. Please try a clearer image.",
                "raw_text": "",
                "fields": {},
            })
        
        # Extract structured fields
        fields = extract_fields(ocr_text)
        
        return JSONResponse({
            "success": True,
            "file_id": file_id,
            "filename": file.filename,
            "raw_text": ocr_text.strip(),
            "fields": fields,
        })
    
    except Exception as e:
        return JSONResponse(
            {"error": f"OCR processing failed: {str(e)}"},
            status_code=500
        )
    finally:
        # Clean up temp file
        if temp_path.exists():
            temp_path.unlink()


@app.post("/extract-fields")
async def extract_fields_endpoint(request: Request):
    """Extract structured fields from arbitrary text (no OCR needed)."""
    body = await request.json()
    text = body.get("text", "").strip()
    
    if not text:
        return JSONResponse({"error": "No text provided"}, status_code=400)
    
    fields = extract_fields(text)
    return JSONResponse({"fields": fields})


@app.get("/hts-extensions/{hs_code}")
async def get_hts(hs_code: str, country: str = "US"):
    """Get HTS (country-specific) extensions for a 6-digit HS code."""
    result = get_hts_extensions(hs_code, country)
    return JSONResponse(result)


@app.get("/hs-lookup/{hs_code}")
async def hs_lookup(hs_code: str):
    """Look up an HS code in the official dataset."""
    if not hs_dataset:
        return JSONResponse({"error": "HS dataset not loaded"}, status_code=500)
    
    result = hs_dataset.lookup(hs_code)
    if not result:
        # Try search instead
        search_results = hs_dataset.search(hs_code, max_results=5)
        return JSONResponse({
            "found": False,
            "message": f"Code {hs_code} not found. Did you mean one of these?",
            "suggestions": search_results,
        })
    
    return JSONResponse({"found": True, **result})


@app.get("/hs-search")
async def hs_search(q: str = "", limit: int = 20):
    """Search HS codes by description."""
    if not q:
        return JSONResponse({"error": "No query provided"}, status_code=400)
    
    results = hs_dataset.search(q, max_results=limit)
    return JSONResponse({"results": results, "query": q})


@app.get("/hs-validate/{hs_code}")
async def hs_validate(hs_code: str):
    """Validate whether an HS code exists."""
    result = hs_dataset.validate_hs_code(hs_code)
    return JSONResponse(result)


@app.get("/hts-countries")
async def hts_countries():
    """Get list of countries with HTS extensions available."""
    return JSONResponse({"countries": get_available_hts_countries()})


@app.get("/visualization-data")
async def get_visualization_data(request: Request):
    """Return UMAP projection data for visualization.

    Supports ``?max_points=N`` to subsample for faster initial load.
    The subsample is stratified by chapter so every category is represented.
    """
    max_points = int(request.query_params.get("max_points", "0"))

    points = umap_data
    if not points:
        cache_path = MODEL_DIR / "umap_data.json"
        if cache_path.exists():
            with open(cache_path, encoding="utf-8") as f:
                points = json.load(f)

    if not points:
        if not umap_ready:
            return JSONResponse({"points": [], "computing": True})
        return JSONResponse({"points": [], "error": "No UMAP data available"})

    total = len(points)
    if 0 < max_points < total:
        # Stratified subsample: keep proportional representation per chapter
        import random as _rng
        _rng.seed(42)
        by_chapter: dict[str, list] = {}
        for p in points:
            by_chapter.setdefault(p.get("chapter_name", "Other"), []).append(p)
        sampled: list = []
        for ch, ch_pts in by_chapter.items():
            n = max(1, round(len(ch_pts) / total * max_points))
            sampled.extend(_rng.sample(ch_pts, min(n, len(ch_pts))))
        _rng.shuffle(sampled)
        return JSONResponse({"points": sampled, "total": total, "sampled": True})

    return JSONResponse({"points": points, "total": total})


@app.get("/visualization-density")
async def get_visualization_density():
    """All UMAP points in compact columnar format for density/labels."""
    points = umap_data or []
    if not points:
        cache_path = MODEL_DIR / "umap_data.json"
        if cache_path.exists():
            with open(cache_path, encoding="utf-8") as f:
                points = json.load(f)
    if not points:
        if not umap_ready:
            return JSONResponse({"chapters": {}, "computing": True})
        return JSONResponse({"error": "No data"})

    by_chapter: dict[str, dict[str, list]] = {}
    for p in points:
        ch = p.get("chapter_name", "Unknown")
        if ch not in by_chapter:
            by_chapter[ch] = {"x": [], "y": []}
        by_chapter[ch]["x"].append(round(p["x"], 3))
        by_chapter[ch]["y"].append(round(p["y"], 3))

    return JSONResponse({"chapters": by_chapter})


@app.post("/embed-query")
async def embed_query(request: Request):
    """Get UMAP coordinates for a query."""
    body = await request.json()
    query_text = body.get("text", "").strip()
    
    if not query_text:
        return JSONResponse({"error": "No text provided"}, status_code=400)
    
    query_emb = model.encode(
        [f"query: {query_text}"],
        normalize_embeddings=True,
        convert_to_numpy=True
    )
    
    n_fit = int(getattr(classifier, "n_samples_fit_", 0))
    if n_fit <= 0:
        fit_x = getattr(classifier, "_fit_X", None)
        n_fit = int(fit_x.shape[0]) if fit_x is not None else 0
    if n_fit <= 0:
        return JSONResponse({"error": "Classifier has no fitted rows"}, status_code=500)

    n_neighbors = min(5, n_fit)
    distances, indices = classifier.kneighbors(query_emb, n_neighbors=n_neighbors)
    
    if umap_data and len(umap_data) > 0:
        weights = 1.0 / (distances[0] + 1e-6)
        weights = weights / weights.sum()
        
        mapped_indices = []
        for idx in indices[0]:
            mapped_idx = int(idx)
            if (
                classifier_training_indices is not None
                and mapped_idx < len(classifier_training_indices)
            ):
                mapped_idx = int(classifier_training_indices[mapped_idx])
            mapped_indices.append(mapped_idx)

        x = sum(
            umap_data[idx]["x"] * w
            for idx, w in zip(mapped_indices, weights)
            if 0 <= idx < len(umap_data)
        )
        y = sum(
            umap_data[idx]["y"] * w
            for idx, w in zip(mapped_indices, weights)
            if 0 <= idx < len(umap_data)
        )
        
        neighbors = []
        for idx, dist in zip(mapped_indices, distances[0]):
            if idx < len(umap_data):
                point = umap_data[idx]
                # cosine distance in [0, 2] for normalized vectors; lower is closer
                similarity = max(0.0, min(1.0, 1.0 - float(dist)))
                neighbors.append({
                    **point,
                    "distance": float(dist),
                    "similarity": similarity,
                })
        
        return JSONResponse({
            "x": float(x),
            "y": float(y),
            "neighbors": neighbors,
        })
    
    if not umap_ready:
        return JSONResponse({"error": "UMAP data is still computing", "computing": True})
    return JSONResponse({"error": "No UMAP data for projection"})


@app.get("/health")
async def health():
    """Health check."""
    return {
        "status": "ok",
        "model_loaded": model is not None,
        "hs_dataset_loaded": hs_dataset._loaded if hs_dataset else False,
        "hs_codes_count": len(hs_dataset.subheadings) if hs_dataset else 0,
        "umap_ready": umap_ready,
    }


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)