Jiahuita commited on
Commit
7532efb
1 Parent(s): de435e1

Attempt to fix pipeline inference issue

Browse files
Files changed (2) hide show
  1. config.json +2 -2
  2. pipeline.py +15 -21
config.json CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:81c482c90c48b46c5f596d6668d2156f438723e160ffe30628880b28fdfc38ab
3
- size 241
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:81be9128702e1663bf6780233ee168b71b7299c2e42438fbb4a9129bcda41b18
3
+ size 265
pipeline.py CHANGED
@@ -1,35 +1,29 @@
1
  from transformers import Pipeline
2
- import tensorflow as tf
3
  from tensorflow.keras.models import load_model
4
  from tensorflow.keras.preprocessing.text import tokenizer_from_json
5
  from tensorflow.keras.preprocessing.sequence import pad_sequences
6
- import json
7
  import numpy as np
 
 
8
 
9
  class NewsClassifierPipeline(Pipeline):
10
- def __init__(self):
11
  super().__init__()
12
- self.model = load_model('./news_classifier.h5')
13
- with open('tokenizer.json') as f:
14
  tokenizer_data = json.load(f)
15
  self.tokenizer = tokenizer_from_json(tokenizer_data)
16
 
17
- def preprocess(self, text):
18
- sequence = self.tokenizer.texts_to_sequences([text])
19
- padded = pad_sequences(sequence)
20
- return padded
21
 
22
- def _forward(self, texts):
23
- processed = self.preprocess(texts)
24
- predictions = self.model.predict(processed)
25
- scores = tf.nn.softmax(predictions, axis=1)
26
-
27
- predicted_class = np.argmax(predictions)
28
- score = float(np.max(scores))
29
-
30
- label = 'foxnews' if predicted_class == 0 else 'nbc'
31
-
32
- return [{'label': label, 'score': score}]
33
 
34
  def postprocess(self, model_outputs):
35
- return model_outputs
 
1
  from transformers import Pipeline
 
2
  from tensorflow.keras.models import load_model
3
  from tensorflow.keras.preprocessing.text import tokenizer_from_json
4
  from tensorflow.keras.preprocessing.sequence import pad_sequences
 
5
  import numpy as np
6
+ import tensorflow as tf
7
+ import json
8
 
9
  class NewsClassifierPipeline(Pipeline):
10
+ def __init__(self, model_path="news_classifier.h5", tokenizer_path="tokenizer.json"):
11
  super().__init__()
12
+ self.model = load_model(model_path)
13
+ with open(tokenizer_path, "r") as f:
14
  tokenizer_data = json.load(f)
15
  self.tokenizer = tokenizer_from_json(tokenizer_data)
16
 
17
+ def preprocess(self, inputs):
18
+ sequences = self.tokenizer.texts_to_sequences([inputs])
19
+ return pad_sequences(sequences, maxlen=128)
 
20
 
21
+ def _forward(self, inputs):
22
+ preprocessed = self.preprocess(inputs)
23
+ predictions = self.model.predict(preprocessed)
24
+ scores = tf.nn.softmax(predictions, axis=1).numpy()
25
+ label = np.argmax(scores)
26
+ return [{"label": "foxnews" if label == 0 else "nbc", "score": float(scores[0, label])}]
 
 
 
 
 
27
 
28
  def postprocess(self, model_outputs):
29
+ return model_outputs