File size: 1,997 Bytes
d15a7ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import catboost
from catboost import CatBoostClassifier
import re
import string
from nltk.corpus import stopwords
from pymystem3 import Mystem
from joblib import load  
import nltk
nltk.download('stopwords')
import time

def data_preprocessing(text):
    stop_words = set(stopwords.words('russian'))
    text = text.lower()
    text = re.sub("<.*?>", "", text)
    text = re.sub(r'http\S+', " ", text)
    text = re.sub(r'@\w+', ' ', text)
    text = re.sub(r'#\w+', ' ', text)
    text = re.sub(r'\d+', ' ', text)
    text = "".join([c for c in text if c not in string.punctuation])
    return " ".join([word for word in text.split() if word not in stop_words])

def lemmatize_text(text):
    mystem = Mystem()
    lemmas = mystem.lemmatize(text)
    return ' '.join(lemmas)

model = CatBoostClassifier()
model.load_model('Weights/cat_model4.cbm')

tfidf_vectorizer = load('Weights/tfidf_vectorizer.joblib')

def classic_ml_page():
    st.title("Классификация отзывов")
    user_review = st.text_area("Введите ваш отзыв здесь:")

    if st.button("Классифицировать"):
        if user_review:
            preprocessed_review = data_preprocessing(user_review)
            lemmatized_review = lemmatize_text(preprocessed_review)
            vectorized_review = tfidf_vectorizer.transform([lemmatized_review])
            start_time = time.time()
            prediction = model.predict(vectorized_review)
            end_time = time.time()
            execution_time = end_time - start_time
            if prediction[0] == 1:
                st.write("Позитивный отзыв 😀")
            else:
                st.write("Негативный отзыв 😟")
            st.write(f'Время предсказания: {execution_time:.4f} секунд')
        else:
            st.write("Пожалуйста, введите отзыв для классификации.")