Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,6 +38,9 @@ clf_encoder = None
|
|
| 38 |
class TextRequest(BaseModel):
|
| 39 |
text: str
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
def build_manual_model(vocab_size=7929, max_len=64, num_classes=8):
|
| 42 |
inputs = tf.keras.Input(shape=(max_len,), dtype='int32', name='tokens')
|
| 43 |
x = tf.keras.layers.Embedding(input_dim=vocab_size, output_dim=64, mask_zero=True, name='embedding')(inputs)
|
|
@@ -70,12 +73,39 @@ def get_ner_model():
|
|
| 70 |
ner_model.load_weights("receipt_ner_model.keras")
|
| 71 |
return ner_model, vocab
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
def get_clf_model():
|
| 74 |
global clf_model, clf_encoder
|
| 75 |
if clf_model is None:
|
| 76 |
with open('penerjemah_kategori.pkl', 'rb') as file:
|
| 77 |
clf_encoder = pickle.load(file)
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
return clf_model, clf_encoder
|
| 80 |
|
| 81 |
def normalize_token(token: str):
|
|
@@ -95,7 +125,7 @@ def predict_ner(request: TextRequest):
|
|
| 95 |
if not raw_text.strip():
|
| 96 |
return {"success": True, "entities": entities, "classes": kategori_struk}
|
| 97 |
|
| 98 |
-
# --- PROSES NER ---
|
| 99 |
model, vcb = get_ner_model()
|
| 100 |
tokens = [t for line in raw_text.split("\n") for t in line.strip().split() if t]
|
| 101 |
|
|
@@ -125,7 +155,7 @@ def predict_ner(request: TextRequest):
|
|
| 125 |
span = []
|
| 126 |
if current_type: entities[current_type].append(" ".join(span))
|
| 127 |
|
| 128 |
-
# --- PROSES KLASIFIKASI ---
|
| 129 |
items_list = entities.get("ITEM", [])
|
| 130 |
teks_untuk_klasifikasi = " ".join(items_list)
|
| 131 |
|
|
@@ -138,12 +168,12 @@ def predict_ner(request: TextRequest):
|
|
| 138 |
indeks_tertinggi = np.argmax(hasil_prediksi[0])
|
| 139 |
kategori_struk = c_encoder.inverse_transform([indeks_tertinggi])[0]
|
| 140 |
except Exception as e:
|
| 141 |
-
print(f"Error Klasifikasi: {e}")
|
| 142 |
|
| 143 |
return {
|
| 144 |
"success": True,
|
| 145 |
"entities": entities,
|
| 146 |
-
"classes": kategori_struk
|
| 147 |
}
|
| 148 |
|
| 149 |
except Exception as e:
|
|
|
|
| 38 |
class TextRequest(BaseModel):
|
| 39 |
text: str
|
| 40 |
|
| 41 |
+
# =========================================================
|
| 42 |
+
# LAZY LOAD NER MODEL (Manual Architecture Bypass)
|
| 43 |
+
# =========================================================
|
| 44 |
def build_manual_model(vocab_size=7929, max_len=64, num_classes=8):
|
| 45 |
inputs = tf.keras.Input(shape=(max_len,), dtype='int32', name='tokens')
|
| 46 |
x = tf.keras.layers.Embedding(input_dim=vocab_size, output_dim=64, mask_zero=True, name='embedding')(inputs)
|
|
|
|
| 73 |
ner_model.load_weights("receipt_ner_model.keras")
|
| 74 |
return ner_model, vocab
|
| 75 |
|
| 76 |
+
# =========================================================
|
| 77 |
+
# LAZY LOAD CLASSIFICATION MODEL (Manual Architecture Bypass)
|
| 78 |
+
# =========================================================
|
| 79 |
+
def build_manual_clf_model():
|
| 80 |
+
# Meniru arsitektur dari log error Anda
|
| 81 |
+
inputs = tf.keras.Input(shape=(1,), dtype=tf.string, name='input_layer')
|
| 82 |
+
|
| 83 |
+
x = tf.keras.layers.TextVectorization(
|
| 84 |
+
max_tokens=5000,
|
| 85 |
+
output_sequence_length=50,
|
| 86 |
+
name='text_vectorization'
|
| 87 |
+
)(inputs)
|
| 88 |
+
|
| 89 |
+
x = tf.keras.layers.Embedding(input_dim=5000, output_dim=64, name='embedding')(x)
|
| 90 |
+
x = tf.keras.layers.GlobalAveragePooling1D(name='global_average_pooling1d')(x)
|
| 91 |
+
x = tf.keras.layers.Dense(64, activation='relu', name='dense')(x)
|
| 92 |
+
x = tf.keras.layers.Dropout(0.2, name='dropout')(x)
|
| 93 |
+
|
| 94 |
+
# Output 7 classes
|
| 95 |
+
outputs = tf.keras.layers.Dense(7, activation='softmax', name='dense_1')(x)
|
| 96 |
+
|
| 97 |
+
return tf.keras.Model(inputs=inputs, outputs=outputs, name='functional')
|
| 98 |
+
|
| 99 |
def get_clf_model():
|
| 100 |
global clf_model, clf_encoder
|
| 101 |
if clf_model is None:
|
| 102 |
with open('penerjemah_kategori.pkl', 'rb') as file:
|
| 103 |
clf_encoder = pickle.load(file)
|
| 104 |
+
|
| 105 |
+
# Bypass JSON yang rusak, langsung load bobotnya!
|
| 106 |
+
clf_model = build_manual_clf_model()
|
| 107 |
+
clf_model.load_weights('model_strukly.keras')
|
| 108 |
+
|
| 109 |
return clf_model, clf_encoder
|
| 110 |
|
| 111 |
def normalize_token(token: str):
|
|
|
|
| 125 |
if not raw_text.strip():
|
| 126 |
return {"success": True, "entities": entities, "classes": kategori_struk}
|
| 127 |
|
| 128 |
+
# --- 1. PROSES NER ---
|
| 129 |
model, vcb = get_ner_model()
|
| 130 |
tokens = [t for line in raw_text.split("\n") for t in line.strip().split() if t]
|
| 131 |
|
|
|
|
| 155 |
span = []
|
| 156 |
if current_type: entities[current_type].append(" ".join(span))
|
| 157 |
|
| 158 |
+
# --- 2. PROSES KLASIFIKASI ---
|
| 159 |
items_list = entities.get("ITEM", [])
|
| 160 |
teks_untuk_klasifikasi = " ".join(items_list)
|
| 161 |
|
|
|
|
| 168 |
indeks_tertinggi = np.argmax(hasil_prediksi[0])
|
| 169 |
kategori_struk = c_encoder.inverse_transform([indeks_tertinggi])[0]
|
| 170 |
except Exception as e:
|
| 171 |
+
print(f"Error Klasifikasi internal: {e}")
|
| 172 |
|
| 173 |
return {
|
| 174 |
"success": True,
|
| 175 |
"entities": entities,
|
| 176 |
+
"classes": kategori_struk
|
| 177 |
}
|
| 178 |
|
| 179 |
except Exception as e:
|