Maslov-Artem
commited on
Commit
·
eb91edf
1
Parent(s):
afed7b5
add text generator
Browse files- app.py +0 -44
- finetuned_model/config.json +41 -0
- finetuned_model/generation_config.json +7 -0
- finetuned_model/model.safetensors +3 -0
- pages/review_predictor.py +58 -0
- pages/text_generator.py +27 -0
app.py
CHANGED
|
@@ -1,47 +1,3 @@
|
|
| 1 |
-
import pickle
|
| 2 |
-
|
| 3 |
import streamlit as st
|
| 4 |
|
| 5 |
-
from preprocessing import data_preprocessing
|
| 6 |
-
|
| 7 |
-
# Load preprocessing steps
|
| 8 |
-
with open("vectorizer.pkl", "rb") as f:
|
| 9 |
-
vectorizer = pickle.load(f)
|
| 10 |
-
|
| 11 |
-
# Load trained model
|
| 12 |
-
with open("logreg_model.pkl", "rb") as f:
|
| 13 |
-
logreg = pickle.load(f)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
# Define function for preprocessing input text
|
| 17 |
-
def preprocess_text(text):
|
| 18 |
-
# Apply preprocessing steps (cleaning, tokenization, vectorization)
|
| 19 |
-
clean_text = data_preprocessing(
|
| 20 |
-
text
|
| 21 |
-
) # Assuming data_preprocessing is your preprocessing function
|
| 22 |
-
print("Clean text ", clean_text)
|
| 23 |
-
vectorized_text = vectorizer.transform([" ".join(clean_text)])
|
| 24 |
-
return vectorized_text
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# Define function for making predictions
|
| 28 |
-
def predict_sentiment(text):
|
| 29 |
-
# Preprocess input text
|
| 30 |
-
processed_text = preprocess_text(text)
|
| 31 |
-
print(preprocess_text)
|
| 32 |
-
# Make prediction
|
| 33 |
-
prediction = logreg.predict(processed_text)
|
| 34 |
-
return prediction
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
# Streamlit app code
|
| 38 |
st.title("Sentiment Analysis with Logistic Regression")
|
| 39 |
-
text_input = st.text_input("Enter your review:")
|
| 40 |
-
if st.button("Predict"):
|
| 41 |
-
prediction = predict_sentiment(text_input)
|
| 42 |
-
if prediction == 1:
|
| 43 |
-
st.write("prediction")
|
| 44 |
-
st.write("Отзыв положительный")
|
| 45 |
-
elif prediction == 0:
|
| 46 |
-
st.write("prediction")
|
| 47 |
-
st.write("Отзыв отрицательный")
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
st.title("Sentiment Analysis with Logistic Regression")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
finetuned_model/config.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "sberbank-ai/rugpt3small_based_on_gpt2",
|
| 3 |
+
"activation_function": "gelu_new",
|
| 4 |
+
"architectures": [
|
| 5 |
+
"GPT2LMHeadModel"
|
| 6 |
+
],
|
| 7 |
+
"attn_pdrop": 0.1,
|
| 8 |
+
"bos_token_id": 1,
|
| 9 |
+
"embd_pdrop": 0.1,
|
| 10 |
+
"eos_token_id": 2,
|
| 11 |
+
"gradient_checkpointing": false,
|
| 12 |
+
"id2label": {
|
| 13 |
+
"0": "LABEL_0"
|
| 14 |
+
},
|
| 15 |
+
"initializer_range": 0.02,
|
| 16 |
+
"label2id": {
|
| 17 |
+
"LABEL_0": 0
|
| 18 |
+
},
|
| 19 |
+
"layer_norm_epsilon": 1e-05,
|
| 20 |
+
"model_type": "gpt2",
|
| 21 |
+
"n_ctx": 2048,
|
| 22 |
+
"n_embd": 768,
|
| 23 |
+
"n_head": 12,
|
| 24 |
+
"n_inner": null,
|
| 25 |
+
"n_layer": 12,
|
| 26 |
+
"n_positions": 2048,
|
| 27 |
+
"pad_token_id": 0,
|
| 28 |
+
"reorder_and_upcast_attn": false,
|
| 29 |
+
"resid_pdrop": 0.1,
|
| 30 |
+
"scale_attn_by_inverse_layer_idx": false,
|
| 31 |
+
"scale_attn_weights": true,
|
| 32 |
+
"summary_activation": null,
|
| 33 |
+
"summary_first_dropout": 0.1,
|
| 34 |
+
"summary_proj_to_labels": true,
|
| 35 |
+
"summary_type": "cls_index",
|
| 36 |
+
"summary_use_proj": true,
|
| 37 |
+
"torch_dtype": "float32",
|
| 38 |
+
"transformers_version": "4.38.2",
|
| 39 |
+
"use_cache": true,
|
| 40 |
+
"vocab_size": 50264
|
| 41 |
+
}
|
finetuned_model/generation_config.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 1,
|
| 4 |
+
"eos_token_id": 2,
|
| 5 |
+
"pad_token_id": 0,
|
| 6 |
+
"transformers_version": "4.38.2"
|
| 7 |
+
}
|
finetuned_model/model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fcdf8d066aa4a05109a1867faf91ab3645bfcec52881d0a9572992c20fbe3120
|
| 3 |
+
size 500941440
|
pages/review_predictor.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
from preprocessing import data_preprocessing
|
| 6 |
+
|
| 7 |
+
# Load preprocessing steps
|
| 8 |
+
with open("vectorizer.pkl", "rb") as f:
|
| 9 |
+
logreg_vectorizer = pickle.load(f)
|
| 10 |
+
|
| 11 |
+
# Load trained model
|
| 12 |
+
with open("logreg_model.pkl", "rb") as f:
|
| 13 |
+
logreg_predictor = pickle.load(f)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# Define function for preprocessing input text
|
| 17 |
+
@st.cache
|
| 18 |
+
def preprocess_text(text):
|
| 19 |
+
# Apply preprocessing steps (cleaning, tokenization, vectorization)
|
| 20 |
+
clean_text = data_preprocessing(
|
| 21 |
+
text
|
| 22 |
+
) # Assuming data_preprocessing is your preprocessing function
|
| 23 |
+
print("Clean text ", clean_text)
|
| 24 |
+
vectorized_text = vectorizer.transform([" ".join(clean_text)])
|
| 25 |
+
return vectorized_text
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# Define function for making predictions
|
| 29 |
+
@st.cache
|
| 30 |
+
def predict_sentiment(text):
|
| 31 |
+
# Preprocess input text
|
| 32 |
+
processed_text = preprocess_text(text)
|
| 33 |
+
# Make prediction
|
| 34 |
+
prediction = logreg_predictor.predict(processed_text)
|
| 35 |
+
return prediction
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
st.sidebar.title("Model Selection")
|
| 39 |
+
model_type = st.sidebar.radio("Select Model Type", ["Classic ML", "LSTM", "BERT"])
|
| 40 |
+
st.title("Review Prediction")
|
| 41 |
+
|
| 42 |
+
# Streamlit app code
|
| 43 |
+
st.title("Sentiment Analysis with Logistic Regression")
|
| 44 |
+
text_input = st.text_input("Enter your review:")
|
| 45 |
+
if st.button("Predict"):
|
| 46 |
+
if model_type == "Classic ML":
|
| 47 |
+
prediction = predict_sentiment(text_input)
|
| 48 |
+
elif model_type == "LSTM":
|
| 49 |
+
prediction = 1
|
| 50 |
+
elif model_type == "BERT":
|
| 51 |
+
prediction = 1
|
| 52 |
+
|
| 53 |
+
if prediction == 1:
|
| 54 |
+
st.write("prediction")
|
| 55 |
+
st.write("Отзыв положительный")
|
| 56 |
+
elif prediction == 0:
|
| 57 |
+
st.write("prediction")
|
| 58 |
+
st.write("Отзыв отрицательный")
|
pages/text_generator.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 4 |
+
|
| 5 |
+
model_path = "finetuned_model/"
|
| 6 |
+
model_name = "sberbank-ai/rugpt3small_based_on_gpt2"
|
| 7 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
| 8 |
+
model = GPT2LMHeadModel.from_pretrained(model_path)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
promt = st.text_input("Ask a question")
|
| 12 |
+
generate = st.button("Generate")
|
| 13 |
+
if generate:
|
| 14 |
+
if not promt:
|
| 15 |
+
st.write("42")
|
| 16 |
+
promt = tokenizer.encode(promt, return_tensors="pt")
|
| 17 |
+
model.eval()
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
out = model.generate(
|
| 20 |
+
promt,
|
| 21 |
+
do_sample=True,
|
| 22 |
+
num_beams=2,
|
| 23 |
+
temperature=1.5,
|
| 24 |
+
top_p=0.9,
|
| 25 |
+
)
|
| 26 |
+
out = list(map(tokenizer.decode, out))[0]
|
| 27 |
+
st.write(out)
|