File size: 3,708 Bytes
cb2adb5 eb91edf cb2adb5 eb91edf cb2adb5 eb91edf c747562 cb2adb5 eb91edf c747562 eb91edf c747562 eb91edf cb2adb5 c747562 cb2adb5 c747562 cb2adb5 c747562 cb2adb5 eb91edf cb2adb5 eb91edf c747562 cb2adb5 eb91edf cb2adb5 eb91edf cb2adb5 c747562 cb2adb5 eb91edf cb2adb5 eb91edf cb2adb5 eb91edf cb2adb5 eb91edf cb2adb5 |
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
import json
import pickle
import pandas as pd
import streamlit as st
import torch
import torch.nn as nn
import transformers
from model.funcs import (create_model_and_tokenizer, execution_time,
load_model, predict_sentiment)
from model.model import LSTMConcatAttentionEmbed
from preprocessing.preprocessing import data_preprocessing
from preprocessing.rnn_preprocessing import preprocess_single_string
@st.cache_resource
def load_logreg():
with open("vectorizer.pkl", "rb") as f:
logreg_vectorizer = pickle.load(f)
with open("logreg_model.pkl", "rb") as f:
logreg_predictor = pickle.load(f)
return logreg_vectorizer, logreg_predictor
logreg_vectorizer, logreg_predictor = load_logreg()
@st.cache_resource
def load_lstm():
with open("model/vocab.json", "r") as f:
vocab_to_int = json.load(f)
with open("model/int_vocab.json", "r") as f:
int_to_vocab = json.load(f)
model_concat_embed = LSTMConcatAttentionEmbed()
model_concat_embed.load_state_dict(torch.load("model/model_weights.pt"))
return vocab_to_int, int_to_vocab, model_concat_embed
vocab_to_int, int_to_vocab, model_concat_embed = load_lstm()
@st.cache_resource
def load_bert():
model_class = transformers.AutoModel
tokenizer_class = transformers.AutoTokenizer
pretrained_weights = "cointegrated/rubert-tiny2"
weights_path = "model/best_bert_weights.pth"
model = load_model(model_class, pretrained_weights, weights_path)
tokenizer = tokenizer_class.from_pretrained(pretrained_weights)
return model, tokenizer
model, tokenizer = load_bert()
@execution_time
def plot_and_predict(review: str, SEQ_LEN: int, model: nn.Module):
inp = preprocess_single_string(review, SEQ_LEN, vocab_to_int)
model.eval()
with torch.inference_mode():
pred, _ = model(inp.long().unsqueeze(0))
pred = pred.sigmoid().item()
return 1 if pred > 0.75 else 0
def preprocess_text_logreg(text):
# Apply preprocessing steps (cleaning, tokenization, vectorization)
clean_text = data_preprocessing(
text
) # Assuming data_preprocessing is your preprocessing function
vectorized_text = logreg_vectorizer.transform([" ".join(clean_text)])
return vectorized_text
# Define function for making predictions
@execution_time
def predict_sentiment_logreg(text):
# Preprocess input text
processed_text = preprocess_text_logreg(text)
# Make prediction
prediction = logreg_predictor.predict(processed_text)
return prediction
metrics = {
"Models": ["Logistic Regression", "LSTM + attention", "ruBERTtiny2"],
"f1-macro score": [0.94376, 0.93317, 0.94070],
}
col1, col2 = st.columns([1, 3])
df = pd.DataFrame(metrics)
df.set_index("Models", inplace=True)
df.index.name = "Model"
st.sidebar.title("Model Selection")
model_type = st.sidebar.radio("Select Model Type", ["Classic ML", "LSTM", "BERT"])
st.title("Review Prediction")
# Streamlit app code
st.title("Sentiment Analysis with Logistic Regression")
text_input = st.text_input("Enter your review:")
if st.button("Predict"):
if model_type == "Classic ML":
prediction = predict_sentiment_logreg(text_input)
elif model_type == "LSTM":
prediction = plot_and_predict(
review=text_input, SEQ_LEN=25, model=model_concat_embed
)
elif model_type == "BERT":
prediction = predict_sentiment(text_input, model, tokenizer, "cpu")
if prediction == 1:
st.write("prediction")
st.write("Отзыв положительный")
elif prediction == 0:
st.write("prediction")
st.write("Отзыв отрицательный")
st.write(df)
|