Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import tensorflow as tf
|
| 4 |
from fastapi import FastAPI
|
|
@@ -25,15 +26,50 @@ class ItemRequest(BaseModel):
|
|
| 25 |
items_text: str
|
| 26 |
|
| 27 |
# =========================================================
|
| 28 |
-
#
|
| 29 |
# =========================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def get_clf_model():
|
| 31 |
global clf_model
|
| 32 |
if clf_model is None:
|
| 33 |
-
print("
|
| 34 |
-
#
|
| 35 |
-
clf_model =
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
return clf_model
|
| 38 |
|
| 39 |
@app.post("/classify/")
|
|
@@ -41,7 +77,7 @@ def classify_items(request: ItemRequest):
|
|
| 41 |
try:
|
| 42 |
raw_items = request.items_text
|
| 43 |
|
| 44 |
-
# Jika Space A mengirim string kosong
|
| 45 |
if not raw_items.strip():
|
| 46 |
return {"success": True, "class_id": None}
|
| 47 |
|
|
@@ -58,5 +94,5 @@ def classify_items(request: ItemRequest):
|
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
import traceback
|
| 61 |
-
print(f"Error AI Klasifikasi: {traceback.format_exc()}")
|
| 62 |
return {"success": False, "error": str(e), "class_id": None}
|
|
|
|
| 1 |
import os
|
| 2 |
+
import zipfile
|
| 3 |
import numpy as np
|
| 4 |
import tensorflow as tf
|
| 5 |
from fastapi import FastAPI
|
|
|
|
| 26 |
items_text: str
|
| 27 |
|
| 28 |
# =========================================================
|
| 29 |
+
# JURUS PAMUNGKAS: MANUAL BYPASS + ZIP EXTRACT
|
| 30 |
# =========================================================
|
| 31 |
+
def build_manual_clf_model():
|
| 32 |
+
inputs = tf.keras.Input(shape=(1,), dtype=tf.string, name='input_layer')
|
| 33 |
+
|
| 34 |
+
x = tf.keras.layers.TextVectorization(
|
| 35 |
+
max_tokens=5000,
|
| 36 |
+
output_sequence_length=50,
|
| 37 |
+
name='text_vectorization'
|
| 38 |
+
)(inputs)
|
| 39 |
+
|
| 40 |
+
x = tf.keras.layers.Embedding(input_dim=5000, output_dim=64, name='embedding')(x)
|
| 41 |
+
x = tf.keras.layers.GlobalAveragePooling1D(name='global_average_pooling1d')(x)
|
| 42 |
+
x = tf.keras.layers.Dense(64, activation='relu', name='dense')(x)
|
| 43 |
+
x = tf.keras.layers.Dropout(0.2, name='dropout')(x)
|
| 44 |
+
|
| 45 |
+
outputs = tf.keras.layers.Dense(7, activation='softmax', name='dense_1')(x)
|
| 46 |
+
|
| 47 |
+
return tf.keras.Model(inputs=inputs, outputs=outputs, name='functional')
|
| 48 |
+
|
| 49 |
def get_clf_model():
|
| 50 |
global clf_model
|
| 51 |
if clf_model is None:
|
| 52 |
+
print("Membangun arsitektur manual dan memuat bobot...")
|
| 53 |
+
# 1. Bangun arsitektur manual untuk menghindari quantization_config
|
| 54 |
+
clf_model = build_manual_clf_model()
|
| 55 |
+
# 2. Hanya muat bobot angka
|
| 56 |
+
clf_model.load_weights("model_strukly.keras")
|
| 57 |
+
|
| 58 |
+
# 3. Suntikkan kamus TextVectorization langsung dari dalam file .keras
|
| 59 |
+
print("Mengekstrak kamus TextVectorization...")
|
| 60 |
+
tv_layer = clf_model.get_layer('text_vectorization')
|
| 61 |
+
try:
|
| 62 |
+
with zipfile.ZipFile("model_strukly.keras", 'r') as z:
|
| 63 |
+
# Cari file txt di folder assets
|
| 64 |
+
vocab_files = [n for n in z.namelist() if 'assets/' in n and n.endswith('.txt')]
|
| 65 |
+
if vocab_files:
|
| 66 |
+
with z.open(vocab_files[0]) as f:
|
| 67 |
+
vocab_list = f.read().decode('utf-8').splitlines()
|
| 68 |
+
tv_layer.set_vocabulary(vocab_list)
|
| 69 |
+
print(f"Kamus berhasil disuntikkan dari: {vocab_files[0]}")
|
| 70 |
+
except Exception as e:
|
| 71 |
+
print(f"Gagal menyuntikkan kamus: {e}")
|
| 72 |
+
|
| 73 |
return clf_model
|
| 74 |
|
| 75 |
@app.post("/classify/")
|
|
|
|
| 77 |
try:
|
| 78 |
raw_items = request.items_text
|
| 79 |
|
| 80 |
+
# Jika Space A mengirim string kosong
|
| 81 |
if not raw_items.strip():
|
| 82 |
return {"success": True, "class_id": None}
|
| 83 |
|
|
|
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
import traceback
|
| 97 |
+
print(f"Error AI Klasifikasi detail: {traceback.format_exc()}")
|
| 98 |
return {"success": False, "error": str(e), "class_id": None}
|