Download Tagger.py from wmateam/tagging: direct link, hf CLI and curl.
- Browser
- Download file 1.61 kB
-
https://huggingface.co/spaces/wmateam/tagging/resolve/main/Tagger.py
- Command line
-
hf download hf://spaces/wmateam/tagging/Tagger.py
-
curl -L -o Tagger.py https://huggingface.co/spaces/wmateam/tagging/resolve/main/Tagger.py
1.61 kB
| 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"] | |