from transformers import pipeline import json class Tagger(): def __init__(self, model_name): self.model_name = model_name self.pipe = None with open("tags.json", "r", encoding="utf-8") as f: self.tags_list = json.load(f) self.prompt = f""" You are an MCAT subject classification expert. You will be given a text. Your task is to determine which of the following official MCAT content tags the text belongs to: {self.tags_list} Instructions: - Select only from the subjects listed above. - You can return 1, 2, 3, 4 or 5 subjects. - Do NOT modify, rename, merge, or invent any new tags. - Return your answer only as a JSON array and nothing else. - Not even a extra character, just answer in JSON array NOTHING MORE. Example output format: ["Literature", "Identifying Author's Tone/Opinion/Voice"] - Just the answer! Text: """ def tag(self, text): prompt = self.prompt + text messages = [ {"role": "system", "content": "You are an MCAT exam assistant."}, {"role": "user", "content": prompt} ] if self.pipe is None: self.pipe = pipeline("text-generation", model=self.model_name, device=0) response = self.pipe(messages, pad_token_id=self.pipe.tokenizer.eos_token_id) return response[0]["generated_text"][-1]["content"]