Spaces:
Sleeping
Sleeping
File size: 1,663 Bytes
26290c2 d3d0074 26290c2 d3d0074 26290c2 |
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 |
import re
import streamlit as st
import torch
st.title("film_review")
input_text = st.text_area("Enter your text")
from pages.film_review.model.model_lstm import *
from pages.film_review.model.model_logreg import *
from pages.film_review.model.model_bert import *
import time
class Timer:
def __enter__(self):
self.start_time = time.time()
return self
def __exit__(self, *args):
self.end_time = time.time()
self.execution_time = self.end_time - self.start_time
@st.cache_resource
def get_model():
return torch.load("pages/film_review/model/model_lstm.pt",map_location=torch.device('cpu'))
model = get_model()
model.eval()
dec = {0:'отрицательный',1:'нейтральный',2:'положительный'}
if input_text:
with Timer() as t:
with torch.no_grad():
ans = torch.nn.functional.softmax(model(input_text), dim=1)
idx = torch.argmax(ans, dim=1).item()
st.write(f'LSTM - отзыв: {dec[idx]}, уверенность: { round(ans[0][idx].item(),2)}')
st.write("Время выполнения:", round(t.execution_time*1000, 2), "миллисекунд")
st.write("------------")
with Timer() as t:
st.write(f'Logreg - отзыв: {dec[ predict_tfidf(input_text)[0]]}')
st.write("Время выполнения:", round(t.execution_time*1000, 2), "миллисекунд")
st.write("------------")
with Timer() as t:
st.write(f'Bert - отзыв: {dec[ predict_bert(input_text)]}')
st.write("Время выполнения:", round(t.execution_time*1000, 2), "миллисекунд")
|