SicheateR99 commited on
Commit
cbca6a6
·
1 Parent(s): 78d8df7

update model

Browse files
Files changed (2) hide show
  1. app.py +51 -2
  2. penerjemah_kategori.pkl +2 -2
app.py CHANGED
@@ -22,13 +22,62 @@ app.add_middleware(
22
  class ItemRequest(BaseModel):
23
  items: list[str]
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  model = None
26
  encoder = None
27
 
28
  def get_model():
29
  global model, encoder
30
  if model is None:
31
- model = tf.keras.models.load_model('model_strukly.keras')
32
  with open('penerjemah_kategori.pkl', 'rb') as file:
33
  encoder = pickle.load(file)
34
  return model, encoder
@@ -94,7 +143,7 @@ def classify_items(request: ItemRequest):
94
  "success": True,
95
  "class_id": best_cat_id,
96
  "category_name": best_cat_name,
97
- "confidence": round(best_confidence * 100, 2)
98
  }
99
 
100
  except Exception as err:
 
22
  class ItemRequest(BaseModel):
23
  items: list[str]
24
 
25
+ class CustomDense(layers.Layer):
26
+
27
+ def __init__(self, units, activation=None, dropout_rate=0.0, **kwargs):
28
+ super(CustomDense, self).__init__(**kwargs)
29
+
30
+ self.units = units
31
+ self.activation = tf.keras.activations.get(activation)
32
+ self.dropout_rate = dropout_rate
33
+ self.dropout = layers.Dropout(dropout_rate)
34
+
35
+ def build(self, input_shape):
36
+
37
+ self.w = self.add_weight(
38
+ shape=(input_shape[-1], self.units),
39
+ initializer='he_normal',
40
+ trainable=True,
41
+ name='kernel'
42
+ )
43
+
44
+ self.b = self.add_weight(
45
+ shape=(self.units,),
46
+ initializer='zeros',
47
+ trainable=True,
48
+ name='bias'
49
+ )
50
+
51
+ def call(self, inputs, training=False):
52
+
53
+ x = tf.matmul(inputs, self.w) + self.b
54
+
55
+ if self.activation is not None:
56
+ x = self.activation(x)
57
+
58
+ x = self.dropout(x, training=training)
59
+
60
+ return x
61
+
62
+ def get_config(self):
63
+
64
+ config = super().get_config()
65
+
66
+ config.update({
67
+ "units": self.units,
68
+ "activation": tf.keras.activations.serialize(self.activation),
69
+ "dropout_rate": self.dropout_rate
70
+ })
71
+
72
+ return config
73
+
74
  model = None
75
  encoder = None
76
 
77
  def get_model():
78
  global model, encoder
79
  if model is None:
80
+ model = tf.keras.models.load_model('model_strukly.keras', custom_objects={'CustomDense': CustomDense})
81
  with open('penerjemah_kategori.pkl', 'rb') as file:
82
  encoder = pickle.load(file)
83
  return model, encoder
 
143
  "success": True,
144
  "class_id": best_cat_id,
145
  "category_name": best_cat_name,
146
+ "confidence": round(best_confidence, 2)
147
  }
148
 
149
  except Exception as err:
penerjemah_kategori.pkl CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:3853757276ca5dc3cd90da9a2cdbb56339be10ddbb38b492831a9ff7773fe8ee
3
- size 389
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3db024b74a86b6da104ce0044712d118b7b0bce5fefa68617a0827f4b4e01876
3
+ size 363