SicheateR99 commited on
Commit
9b8c56c
·
verified ·
1 Parent(s): be83dd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -6
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import os
2
  import re
3
  import pickle
 
 
4
  import numpy as np
5
  import tensorflow as tf
6
  from fastapi import FastAPI
@@ -38,15 +40,54 @@ class TextRequest(BaseModel):
38
  text: str
39
 
40
  # =========================================================
41
- # LAZY LOAD SANGAT BERSIH (Tanpa Bypass)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  # =========================================================
43
  def get_ner_model():
44
  global ner_model, vocab
45
  if ner_model is None:
46
  with open("vocab.pkl", "rb") as f:
47
  vocab = pickle.load(f)
48
- # Langsung load utuh! Keras 3 sudah aktif.
49
- ner_model = tf.keras.models.load_model("receipt_ner_model.keras", compile=False)
50
  return ner_model, vocab
51
 
52
  def get_clf_model():
@@ -54,8 +95,8 @@ def get_clf_model():
54
  if clf_model is None:
55
  with open('penerjemah_kategori.pkl', 'rb') as file:
56
  clf_encoder = pickle.load(file)
57
- # Langsung load utuh! Kamus TextVectorization akan otomatis ikut termuat.
58
- clf_model = tf.keras.models.load_model('model_strukly.keras', compile=False)
59
  return clf_model, clf_encoder
60
 
61
  def normalize_token(token: str):
@@ -89,7 +130,6 @@ def predict_ner(request: TextRequest):
89
  X[0, i] = vcb.get(norm_tokens[i], vcb.get(UNK_TOKEN, 0))
90
  mask[0, i] = 1.0
91
 
92
- # Kembali menggunakan mask karena model di-load utuh
93
  probs = model([X, mask], training=False).numpy()
94
  labels = [IDX2LABEL[int(np.argmax(probs[0, i]))] for i in range(length)]
95
 
 
1
  import os
2
  import re
3
  import pickle
4
+ import json
5
+ import zipfile
6
  import numpy as np
7
  import tensorflow as tf
8
  from fastapi import FastAPI
 
40
  text: str
41
 
42
  # =========================================================
43
+ # OPERASI BEDAH FILE .KERAS (Solusi Definitif Error JSON)
44
+ # =========================================================
45
+ def patch_keras_model(original_path):
46
+ patched_path = "patched_" + original_path
47
+ # Jika sudah pernah dibedah, langsung gunakan hasilnya
48
+ if os.path.exists(patched_path):
49
+ return patched_path
50
+
51
+ print(f"Membedah {original_path} untuk menghapus quantization_config...")
52
+ try:
53
+ with zipfile.ZipFile(original_path, 'r') as zin:
54
+ with zipfile.ZipFile(patched_path, 'w') as zout:
55
+ for item in zin.infolist():
56
+ content = zin.read(item.filename)
57
+ if item.filename == 'config.json':
58
+ # Bongkar JSON-nya
59
+ config_dict = json.loads(content.decode('utf-8'))
60
+
61
+ # Fungsi rekursif untuk mencari dan menghapus quantization_config di semua layer
62
+ def hapus_biang_kerok(d):
63
+ if isinstance(d, dict):
64
+ d.pop('quantization_config', None)
65
+ for k, v in d.items():
66
+ hapus_biang_kerok(v)
67
+ elif isinstance(d, list):
68
+ for i in d:
69
+ hapus_biang_kerok(i)
70
+
71
+ hapus_biang_kerok(config_dict)
72
+ # Bungkus kembali jadi JSON
73
+ content = json.dumps(config_dict).encode('utf-8')
74
+
75
+ zout.writestr(item, content)
76
+ return patched_path
77
+ except Exception as e:
78
+ print(f"Gagal membedah model: {e}")
79
+ return original_path # Fallback ke file asli jika gagal
80
+
81
+ # =========================================================
82
+ # LAZY LOAD MENGGUNAKAN MODEL YANG SUDAH DIBEDAH
83
  # =========================================================
84
  def get_ner_model():
85
  global ner_model, vocab
86
  if ner_model is None:
87
  with open("vocab.pkl", "rb") as f:
88
  vocab = pickle.load(f)
89
+ patched_file = patch_keras_model("receipt_ner_model.keras")
90
+ ner_model = tf.keras.models.load_model(patched_file, compile=False)
91
  return ner_model, vocab
92
 
93
  def get_clf_model():
 
95
  if clf_model is None:
96
  with open('penerjemah_kategori.pkl', 'rb') as file:
97
  clf_encoder = pickle.load(file)
98
+ patched_file = patch_keras_model("model_strukly.keras")
99
+ clf_model = tf.keras.models.load_model(patched_file, compile=False)
100
  return clf_model, clf_encoder
101
 
102
  def normalize_token(token: str):
 
130
  X[0, i] = vcb.get(norm_tokens[i], vcb.get(UNK_TOKEN, 0))
131
  mask[0, i] = 1.0
132
 
 
133
  probs = model([X, mask], training=False).numpy()
134
  labels = [IDX2LABEL[int(np.argmax(probs[0, i]))] for i in range(length)]
135