Spaces:
Build error
Build error
Upload 6 files
Browse files- Model/config.json +25 -0
- Model/tf_model.h5 +3 -0
- Tokenizer/special_tokens_map.json +7 -0
- Tokenizer/tokenizer_config.json +57 -0
- Tokenizer/vocab.txt +0 -0
- app.py +118 -0
Model/config.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
"initializer_range": 0.02,
|
| 13 |
+
"intermediate_size": 3072,
|
| 14 |
+
"layer_norm_eps": 1e-12,
|
| 15 |
+
"max_position_embeddings": 512,
|
| 16 |
+
"model_type": "bert",
|
| 17 |
+
"num_attention_heads": 12,
|
| 18 |
+
"num_hidden_layers": 12,
|
| 19 |
+
"pad_token_id": 0,
|
| 20 |
+
"position_embedding_type": "absolute",
|
| 21 |
+
"transformers_version": "4.45.2",
|
| 22 |
+
"type_vocab_size": 2,
|
| 23 |
+
"use_cache": true,
|
| 24 |
+
"vocab_size": 30522
|
| 25 |
+
}
|
Model/tf_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a4077865c0fdbde540ae3a4c99f5495340564aae31e646f0a354f1988e66565c
|
| 3 |
+
size 438223128
|
Tokenizer/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 |
+
}
|
Tokenizer/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 |
+
}
|
Tokenizer/vocab.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import TFBertForSequenceClassification, BertTokenizer
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Set layout to wide
|
| 7 |
+
st.set_page_config(layout="wide")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# Load the trained BERT model and tokenizer
|
| 11 |
+
@st.cache(allow_output_mutation=True)
|
| 12 |
+
def load_model():
|
| 13 |
+
model = TFBertForSequenceClassification.from_pretrained('C:/Users/Pranit/PycharmProjects/customer/Model')
|
| 14 |
+
tokenizer = BertTokenizer.from_pretrained('C:/Users/Pranit/PycharmProjects/customer/Tokenizer')
|
| 15 |
+
return model, tokenizer
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
model, tokenizer = load_model()
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# Tokenize and encode the input text
|
| 22 |
+
def encode_input(text, max_length=128):
|
| 23 |
+
encoded_input = tokenizer.encode_plus(
|
| 24 |
+
text,
|
| 25 |
+
add_special_tokens=True,
|
| 26 |
+
max_length=max_length,
|
| 27 |
+
padding='max_length', # Updated for compatibility with TensorFlow
|
| 28 |
+
return_attention_mask=True,
|
| 29 |
+
return_tensors='tf'
|
| 30 |
+
)
|
| 31 |
+
return encoded_input['input_ids'], encoded_input['attention_mask']
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Prediction function
|
| 35 |
+
def predict_sentiment(text):
|
| 36 |
+
input_ids, attention_mask = encode_input(text)
|
| 37 |
+
prediction = model.predict([input_ids, attention_mask])[0]
|
| 38 |
+
pred_label = np.argmax(prediction, axis=1)
|
| 39 |
+
return pred_label[0], prediction[0] # Return prediction scores
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# Apply custom CSS for enhanced UI
|
| 43 |
+
st.markdown("""
|
| 44 |
+
<style>
|
| 45 |
+
/* Background color */
|
| 46 |
+
body {
|
| 47 |
+
background-color: #f0f2f6;
|
| 48 |
+
}
|
| 49 |
+
/* Header font color */
|
| 50 |
+
.stTitle {
|
| 51 |
+
color: #3A3F44;
|
| 52 |
+
}
|
| 53 |
+
/* Text area color and font */
|
| 54 |
+
.stTextArea {
|
| 55 |
+
background-color: #ffffff;
|
| 56 |
+
font-size: 18px;
|
| 57 |
+
}
|
| 58 |
+
/* Button color */
|
| 59 |
+
div.stButton > button {
|
| 60 |
+
background-color: #00A86B;
|
| 61 |
+
color: white;
|
| 62 |
+
border-radius: 8px;
|
| 63 |
+
padding: 10px;
|
| 64 |
+
font-weight: bold;
|
| 65 |
+
}
|
| 66 |
+
/* Custom results style */
|
| 67 |
+
.results {
|
| 68 |
+
font-size: 20px;
|
| 69 |
+
color: #007bff;
|
| 70 |
+
font-weight: bold;
|
| 71 |
+
}
|
| 72 |
+
/* Icon style */
|
| 73 |
+
.icon {
|
| 74 |
+
vertical-align: middle;
|
| 75 |
+
margin-right: 5px;
|
| 76 |
+
}
|
| 77 |
+
</style>
|
| 78 |
+
""", unsafe_allow_html=True)
|
| 79 |
+
|
| 80 |
+
# Streamlit App UI
|
| 81 |
+
st.title("Sentiment Classifier with BERT")
|
| 82 |
+
|
| 83 |
+
# Add icons from Font Awesome
|
| 84 |
+
st.write("""
|
| 85 |
+
<div style='display: flex; align-items: center;'>
|
| 86 |
+
<img src='https://img.icons8.com/ios-filled/50/000000/sentiment-analysis.png' class='icon' />
|
| 87 |
+
<h3>Enter a sentence below and the model will predict whether it's Positive or Negative:</h3>
|
| 88 |
+
</div>
|
| 89 |
+
""", unsafe_allow_html=True)
|
| 90 |
+
|
| 91 |
+
# User input
|
| 92 |
+
user_input = st.text_area("Enter Text:", "")
|
| 93 |
+
|
| 94 |
+
if st.button("🧠 Classify Sentiment"):
|
| 95 |
+
if user_input:
|
| 96 |
+
pred_label, prediction_scores = predict_sentiment(user_input)
|
| 97 |
+
sentiment = "Positive" if pred_label == 1 else "Negative"
|
| 98 |
+
|
| 99 |
+
# Display results
|
| 100 |
+
st.markdown(f"<div class='results'>Predicted Sentiment: **{sentiment}**</div>", unsafe_allow_html=True)
|
| 101 |
+
|
| 102 |
+
# Visualizations
|
| 103 |
+
st.subheader("Text Analysis Results")
|
| 104 |
+
st.write(f"**Word Count:** {len(user_input.split())}")
|
| 105 |
+
st.write(f"**Character Count:** {len(user_input)}")
|
| 106 |
+
|
| 107 |
+
# Display prediction scores
|
| 108 |
+
st.write(f"**Positive Score:** {prediction_scores[1]:.2f}")
|
| 109 |
+
st.write(f"**Negative Score:** {prediction_scores[0]:.2f}")
|
| 110 |
+
|
| 111 |
+
# Visualize the sentiment scores
|
| 112 |
+
st.bar_chart(prediction_scores)
|
| 113 |
+
|
| 114 |
+
else:
|
| 115 |
+
st.write("Please enter text to classify.")
|
| 116 |
+
|
| 117 |
+
st.write("---")
|
| 118 |
+
st.write("BERT Model fine-tuned for Sentiment Classification")
|