File size: 1,614 Bytes
eaf68f0 4bce5f9 eaf68f0 4bce5f9 eaf68f0 bb4d297 eaf68f0 adcfa2b eaf68f0 4bce5f9 eaf68f0 b9e38dc eaf68f0 b9e38dc 4bce5f9 4609d8f eaf68f0 b9e38dc eaf68f0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 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"]
|