Views
No views yet
1if torch.cuda.is_available():
2 DEVICE = "cuda"
3elif torch.backends.mps.is_available():
4 DEVICE = "mps"
5else:
6 DEVICE = "cpu"
7model = AutoModelForCausalLM.from_pretrained(f"{REPO_NAME}-tinyllama-qlora", device_map="cuda")
8tokenizer = AutoTokenizer.from_pretrained(f"{REPO_NAME}-tinyllama-qlora")
9tokenizer.pad_token = tokenizer.eos_token
10tokenizer.padding_side = "left"
11
12IDX2NAME = {0: "negative", 1: "neutral", 2: "positive"}
13def postprocess_sentiment(output_text: str) -> str:
14 """
15 Фильтрует вывод модели и возвращает только метку класса, к которому относится сообщение ('positive', 'negative', 'neutral').
16 Parameters:
17 output_text (str): Текст, сгенерированный моделью.
18
19 Returns:
20 str: тональность текста или пустая строка
21 """
22
23 parts = output_text.split("assistant", 1)
24 text_to_process = parts[1] if len(parts) > 1 else output_text
25
26 match = re.search(rf"\b({'|'.join(IDX2NAME.values())})\b", text_to_process, re.IGNORECASE)
27 return match.group(1).lower() if match else ""
28SYSTEM_PROMPT = "Your task is to look through the provided text and classify the sentiment of it. Possible classes are: positive, negative, neutral. Respond only one word from the possible classes that best describes the sentiment of provided text."
29text = 'I hate playing Minecraft' #здесь может быть ваш текст
30chat = [
31 {'role': 'system', 'content' : SYSTEM_PROMPT},
32 {'role': 'user', 'content' : f"Text to classify: {text}"}
33 ]
34ch_temp = tokenizer.apply_chat_template(chat, tokenize = False)
35input_ids = tokenizer(ch_temp, return_tensors = 'pt').to(model.device)
36output_ids = model.generate(input_ids['input_ids'], max_new_tokens=16)
37generated_text = tokenizer.decode(output_ids[0][len(input_ids[0]) :], skip_special_tokens=True)
38print(generated_text)