from transformers import Pipeline from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing.text import tokenizer_from_json from tensorflow.keras.preprocessing.sequence import pad_sequences import numpy as np import json class NewsClassifierPipeline(Pipeline): def __init__(self): super().__init__() self.model = load_model('./news_classifier.h5') with open('./tokenizer.json', 'r') as f: tokenizer_data = json.load(f) self.tokenizer = tokenizer_from_json(tokenizer_data) def preprocess(self, inputs): sequences = self.tokenizer.texts_to_sequences([inputs]) return pad_sequences(sequences, maxlen=128) def _forward(self, inputs): processed = self.preprocess(inputs) predictions = self.model.predict(processed) label = "foxnews" if predictions[0][0] > 0.5 else "nbc" score = predictions[0][0] if label == "foxnews" else 1 - predictions[0][0] return [{"label": label, "score": float(score)}] def postprocess(self, outputs): return outputs