Spaces:
No application file
No application file
Commit ·
a59fdee
0
Parent(s):
Initial commit: Fine-tuning BERT on ABSA project
Browse files- .gitattributes +1 -0
- README.md +4 -0
- Restaurants_Train_v2.xml +0 -0
- absa_webapp/app.py +58 -0
- absa_webapp/fine_tuned_bert_absa/config.json +37 -0
- absa_webapp/fine_tuned_bert_absa/model.safetensors +3 -0
- absa_webapp/fine_tuned_bert_absa/special_tokens_map.json +7 -0
- absa_webapp/fine_tuned_bert_absa/tokenizer_config.json +57 -0
- absa_webapp/fine_tuned_bert_absa/vocab.txt +0 -0
- absa_webapp/requirements.txt.txt +3 -0
.gitattributes
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
absa_webapp/fine_tuned_bert_absa/model.safetensors filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Fine-Tuning-Bert-on-ABSA
|
| 2 |
+
In this project I have fine-tuned BERT on SemEval 2014 Task 4 dataset so it can work on ABSA and in the end I have deployed the model.
|
| 3 |
+
|
| 4 |
+
I have attached the .ipynb file in this project and a saved model of this project so you can integrate it with streamlit or any other library to deploy it. I have also attached the streamlit source code file.
|
Restaurants_Train_v2.xml
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
absa_webapp/app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load the fine-tuned model and tokenizer
|
| 6 |
+
@st.cache_resource
|
| 7 |
+
def load_model():
|
| 8 |
+
model = BertForSequenceClassification.from_pretrained('./fine_tuned_bert_absa') # Path to your saved model
|
| 9 |
+
tokenizer = BertTokenizer.from_pretrained('./fine_tuned_bert_absa')
|
| 10 |
+
return model, tokenizer
|
| 11 |
+
|
| 12 |
+
model, tokenizer = load_model()
|
| 13 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 14 |
+
model.to(device)
|
| 15 |
+
|
| 16 |
+
# Streamlit app title
|
| 17 |
+
st.title("Aspect-Based Sentiment Analysis Web App")
|
| 18 |
+
|
| 19 |
+
# Input fields for sentence and aspect
|
| 20 |
+
sentence = st.text_area("Enter the review or sentence:")
|
| 21 |
+
aspect = st.text_input("Enter the aspect to analyze (e.g., 'food', 'service'):")
|
| 22 |
+
|
| 23 |
+
# Function to predict sentiment
|
| 24 |
+
def predict_sentiment(sentence, aspect):
|
| 25 |
+
if not sentence or not aspect:
|
| 26 |
+
return "Please enter both a sentence and an aspect."
|
| 27 |
+
|
| 28 |
+
# Preprocess the input
|
| 29 |
+
inputs = tokenizer.encode_plus(
|
| 30 |
+
aspect,
|
| 31 |
+
sentence,
|
| 32 |
+
add_special_tokens=True,
|
| 33 |
+
max_length=128,
|
| 34 |
+
padding='max_length',
|
| 35 |
+
truncation=True,
|
| 36 |
+
return_tensors='pt'
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
input_ids = inputs['input_ids'].to(device)
|
| 40 |
+
attention_mask = inputs['attention_mask'].to(device)
|
| 41 |
+
|
| 42 |
+
# Get predictions
|
| 43 |
+
model.eval()
|
| 44 |
+
with torch.no_grad():
|
| 45 |
+
outputs = model(input_ids, attention_mask=attention_mask)
|
| 46 |
+
|
| 47 |
+
logits = outputs.logits
|
| 48 |
+
predicted_class = torch.argmax(logits, dim=1).item()
|
| 49 |
+
|
| 50 |
+
# Map class index to sentiment
|
| 51 |
+
label_map = {0: "Negative", 1: "Neutral", 2: "Positive"}
|
| 52 |
+
return label_map[predicted_class]
|
| 53 |
+
|
| 54 |
+
# Button for prediction
|
| 55 |
+
if st.button("Analyze Sentiment"):
|
| 56 |
+
result = predict_sentiment(sentence, aspect)
|
| 57 |
+
st.subheader(f"Aspect: {aspect}")
|
| 58 |
+
st.write(f"Predicted Sentiment: **{result}**")
|
absa_webapp/fine_tuned_bert_absa/config.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "bert-base-uncased",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"BertForSequenceClassification"
|
| 5 |
+
],
|
| 6 |
+
"attention_probs_dropout_prob": 0.1,
|
| 7 |
+
"classifier_dropout": null,
|
| 8 |
+
"gradient_checkpointing": false,
|
| 9 |
+
"hidden_act": "gelu",
|
| 10 |
+
"hidden_dropout_prob": 0.1,
|
| 11 |
+
"hidden_size": 768,
|
| 12 |
+
"id2label": {
|
| 13 |
+
"0": "LABEL_0",
|
| 14 |
+
"1": "LABEL_1",
|
| 15 |
+
"2": "LABEL_2"
|
| 16 |
+
},
|
| 17 |
+
"initializer_range": 0.02,
|
| 18 |
+
"intermediate_size": 3072,
|
| 19 |
+
"label2id": {
|
| 20 |
+
"LABEL_0": 0,
|
| 21 |
+
"LABEL_1": 1,
|
| 22 |
+
"LABEL_2": 2
|
| 23 |
+
},
|
| 24 |
+
"layer_norm_eps": 1e-12,
|
| 25 |
+
"max_position_embeddings": 512,
|
| 26 |
+
"model_type": "bert",
|
| 27 |
+
"num_attention_heads": 12,
|
| 28 |
+
"num_hidden_layers": 12,
|
| 29 |
+
"pad_token_id": 0,
|
| 30 |
+
"position_embedding_type": "absolute",
|
| 31 |
+
"problem_type": "single_label_classification",
|
| 32 |
+
"torch_dtype": "float32",
|
| 33 |
+
"transformers_version": "4.46.2",
|
| 34 |
+
"type_vocab_size": 2,
|
| 35 |
+
"use_cache": true,
|
| 36 |
+
"vocab_size": 30522
|
| 37 |
+
}
|
absa_webapp/fine_tuned_bert_absa/model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5ac521812c19b03803d45354d5dedc338466f6687a5d71fd762b2239796c3e24
|
| 3 |
+
size 437961724
|
absa_webapp/fine_tuned_bert_absa/special_tokens_map.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cls_token": "[CLS]",
|
| 3 |
+
"mask_token": "[MASK]",
|
| 4 |
+
"pad_token": "[PAD]",
|
| 5 |
+
"sep_token": "[SEP]",
|
| 6 |
+
"unk_token": "[UNK]"
|
| 7 |
+
}
|
absa_webapp/fine_tuned_bert_absa/tokenizer_config.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"0": {
|
| 4 |
+
"content": "[PAD]",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"100": {
|
| 12 |
+
"content": "[UNK]",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"101": {
|
| 20 |
+
"content": "[CLS]",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"102": {
|
| 28 |
+
"content": "[SEP]",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
},
|
| 35 |
+
"103": {
|
| 36 |
+
"content": "[MASK]",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false,
|
| 41 |
+
"special": true
|
| 42 |
+
}
|
| 43 |
+
},
|
| 44 |
+
"clean_up_tokenization_spaces": true,
|
| 45 |
+
"cls_token": "[CLS]",
|
| 46 |
+
"do_basic_tokenize": true,
|
| 47 |
+
"do_lower_case": true,
|
| 48 |
+
"mask_token": "[MASK]",
|
| 49 |
+
"model_max_length": 512,
|
| 50 |
+
"never_split": null,
|
| 51 |
+
"pad_token": "[PAD]",
|
| 52 |
+
"sep_token": "[SEP]",
|
| 53 |
+
"strip_accents": null,
|
| 54 |
+
"tokenize_chinese_chars": true,
|
| 55 |
+
"tokenizer_class": "BertTokenizer",
|
| 56 |
+
"unk_token": "[UNK]"
|
| 57 |
+
}
|
absa_webapp/fine_tuned_bert_absa/vocab.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
absa_webapp/requirements.txt.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
transformers
|
| 3 |
+
torch
|