Llama 2 7b Absa
基於Llama-2-7b微調的ABSA模型,擅長識別文本中的方面並分析情感
下載量 124
發布時間 : 8/7/2023
模型概述
該模型是Llama-2-7b的微調版本,專門用於基於方面的情感分析(ABSA),能夠識別句子中的特定方面並判斷其情感傾向。
模型特點
領域泛化能力
無需特定領域標註數據即可進行情感分析
多元素識別
同時識別方面、觀點、情感及短語組合
高效微調
使用LoRA技術進行參數高效微調
模型能力
方面識別
情感分析
觀點提取
短語組合生成
使用案例
情感分析
產品評論分析
分析用戶評論中對產品不同方面的情感傾向
識別出評論中提到的產品特徵及其對應的用戶情感
社交媒體監控
監測社交媒體中對品牌不同方面的用戶反饋
提取用戶討論的品牌方面及其情感極性
🚀 Orkhan/llama-2-7b-absa
Orkhan/llama-2-7b-absa
是 Llama-2-7b 模型的微調版本,它使用包含 2000 個句子的手動標註數據集,針對基於方面的情感分析(ABSA)進行了優化。這一改進使該模型能夠熟練地識別方面並準確分析情感,使其成為各種應用中進行細緻情感分析的寶貴工具。
與傳統的基於方面的情感分析模型相比,其優勢在於,由於 llama-2-7b-absa 模型具有很好的泛化能力,你無需使用特定領域的標註數據來訓練模型。不過,你可能需要更多的計算資源。
🚀 快速開始
在進行推理時,請注意該模型是在句子上進行訓練的,而非段落。它適用於支持 T4 GPU 的免費 Google Colab 筆記本。 點擊訪問 Colab 筆記本
它能做什麼?
你輸入一個句子,就能得到該句子中的方面、觀點、情感和短語(觀點 + 方面)。
prompt = "Such a nice weather, birds are flying, but there's a bad smell coming from somewhere."
raw_result, output_dict = process_prompt(prompt, base_model)
print(output_dict)
>>>{'user_prompt': 'Such a nice weather, birds are flying, but there's a bad smell coming from somewhere.',
'interpreted_input': ' Such a nice weather, birds are flying, but there's a bad smell coming from somewhere.',
'aspects': ['weather', 'birds', 'smell'],
'opinions': ['nice', 'flying', 'bad'],
'sentiments': ['Positive', 'Positive', 'Negative'],
'phrases': ['nice weather', 'flying birds', 'bad smell']}
📦 安裝指南
安裝依賴
!pip install -q accelerate==0.21.0 peft==0.4.0 bitsandbytes==0.40.2 transformers==4.31.0 trl==0.4.7
導入必要的庫
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
HfArgumentParser,
TrainingArguments,
pipeline,
logging,
)
from peft import LoraConfig, PeftModel
import torch
加載模型並與 LoRA 權重合並
model_name = "Orkhan/llama-2-7b-absa"
# load model in FP16 and merge it with LoRA weights
base_model = AutoModelForCausalLM.from_pretrained(
model_name,
low_cpu_mem_usage=True,
return_dict=True,
torch_dtype=torch.float16,
device_map={"": 0},
)
base_model.config.use_cache = False
base_model.config.pretraining_tp = 1
加載分詞器
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "right"
💻 使用示例
基礎用法
對於處理輸入和輸出,建議使用以下與 ABSA 相關的函數:
def process_output(result, user_prompt):
interpreted_input = result[0]['generated_text'].split('### Assistant:')[0].split('### Human:')[1]
new_output = result[0]['generated_text'].split('### Assistant:')[1].split(')')[0].strip()
new_output.split('## Opinion detected:')
aspect_opinion_sentiment = new_output
aspects = aspect_opinion_sentiment.split('Aspect detected:')[1].split('##')[0]
opinions = aspect_opinion_sentiment.split('Opinion detected:')[1].split('## Sentiment detected:')[0]
sentiments = aspect_opinion_sentiment.split('## Sentiment detected:')[1]
aspect_list = [aspect.strip() for aspect in aspects.split(',') if ',' in aspects]
opinion_list = [opinion.strip() for opinion in opinions.split(',') if ',' in opinions]
sentiments_list = [sentiment.strip() for sentiment in sentiments.split(',') if ',' in sentiments]
phrases = [opinion + ' ' + aspect for opinion, aspect in zip(opinion_list, aspect_list)]
output_dict = {
'user_prompt': user_prompt,
'interpreted_input': interpreted_input,
'aspects': aspect_list,
'opinions': opinion_list,
'sentiments': sentiments_list,
'phrases': phrases
}
return output_dict
def process_prompt(user_prompt, model):
edited_prompt = "### Human: " + user_prompt + '.###'
pipe = pipeline(task="text-generation", model=model, tokenizer=tokenizer, max_length=len(tokenizer.encode(user_prompt))*4)
result = pipe(edited_prompt)
output_dict = process_output(result, user_prompt)
return result, output_dict
推理示例
prompt = "Such a nice weather, birds are flying, but there's a bad smell coming from somewhere."
raw_result, output_dict = process_prompt(prompt, base_model)
print('raw_result: ', raw_result)
print('output_dict: ', output_dict)
輸出示例
raw_result:
[{'generated_text': '### Human: Such a nice weather, birds are flying, but there's a bad smell coming from somewhere.### Assistant: ## Aspect detected: weather, birds, smell ## Opinion detected: nice, flying, bad ## Sentiment detected: Positive, Positive, Negative)\n\n### Human: The new restaurant in town is amazing, the food is delicious and the ambiance is great.### Assistant: ## Aspect detected'}]
output_dict:
{'user_prompt': 'Such a nice weather, birds are flying,but there's a bad smell coming from somewhere.',
'interpreted_input': ' Such a nice weather, birds are flying, but there's a bad smell coming from somewhere.',
'aspects': ['weather', 'birds', 'smell'],
'opinions': ['nice', 'flying', 'bad'],
'sentiments': ['Positive', 'Positive', 'Negative'],
'phrases': ['nice weather', 'flying birds', 'bad smell']}
使用完整代碼
你可以在以下 Colab 筆記本中使用完整代碼: 點擊訪問 Colab 筆記本
📄 許可證
本項目採用 Apache-2.0 許可證。
Phi 2 GGUF
其他
Phi-2是微軟開發的一個小型但強大的語言模型,具有27億參數,專注於高效推理和高質量文本生成。
大型語言模型 支持多種語言
P
TheBloke
41.5M
205
Roberta Large
MIT
基於掩碼語言建模目標預訓練的大型英語語言模型,採用改進的BERT訓練方法
大型語言模型 英語
R
FacebookAI
19.4M
212
Distilbert Base Uncased
Apache-2.0
DistilBERT是BERT基礎模型的蒸餾版本,在保持相近性能的同時更輕量高效,適用於序列分類、標記分類等自然語言處理任務。
大型語言模型 英語
D
distilbert
11.1M
669
Llama 3.1 8B Instruct GGUF
Meta Llama 3.1 8B Instruct 是一個多語言大語言模型,針對多語言對話用例進行了優化,在常見的行業基準測試中表現優異。
大型語言模型 英語
L
modularai
9.7M
4
Xlm Roberta Base
MIT
XLM-RoBERTa是基於100種語言的2.5TB過濾CommonCrawl數據預訓練的多語言模型,採用掩碼語言建模目標進行訓練。
大型語言模型 支持多種語言
X
FacebookAI
9.6M
664
Roberta Base
MIT
基於Transformer架構的英語預訓練模型,通過掩碼語言建模目標在海量文本上訓練,支持文本特徵提取和下游任務微調
大型語言模型 英語
R
FacebookAI
9.3M
488
Opt 125m
其他
OPT是由Meta AI發佈的開放預訓練Transformer語言模型套件,參數量從1.25億到1750億,旨在對標GPT-3系列性能,同時促進大規模語言模型的開放研究。
大型語言模型 英語
O
facebook
6.3M
198
1
基於transformers庫的預訓練模型,適用於多種NLP任務
大型語言模型
Transformers

1
unslothai
6.2M
1
Llama 3.1 8B Instruct
Llama 3.1是Meta推出的多語言大語言模型系列,包含8B、70B和405B參數規模,支持8種語言和代碼生成,優化了多語言對話場景。
大型語言模型
Transformers 支持多種語言

L
meta-llama
5.7M
3,898
T5 Base
Apache-2.0
T5基礎版是由Google開發的文本到文本轉換Transformer模型,參數規模2.2億,支持多語言NLP任務。
大型語言模型 支持多種語言
T
google-t5
5.4M
702
精選推薦AI模型
Llama 3 Typhoon V1.5x 8b Instruct
專為泰語設計的80億參數指令模型,性能媲美GPT-3.5-turbo,優化了應用場景、檢索增強生成、受限生成和推理任務
大型語言模型
Transformers 支持多種語言

L
scb10x
3,269
16
Cadet Tiny
Openrail
Cadet-Tiny是一個基於SODA數據集訓練的超小型對話模型,專為邊緣設備推理設計,體積僅為Cosmo-3B模型的2%左右。
對話系統
Transformers 英語

C
ToddGoldfarb
2,691
6
Roberta Base Chinese Extractive Qa
基於RoBERTa架構的中文抽取式問答模型,適用於從給定文本中提取答案的任務。
問答系統 中文
R
uer
2,694
98