import streamlit as st import base64 import streamlit as st import plotly.express as px df = px.data.iris() @st.cache_data def get_img_as_base64(file): with open(file, "rb") as f: data = f.read() return base64.b64encode(data).decode() page_bg_img = f""" """ st.markdown(page_bg_img, unsafe_allow_html=True) import tensorflow as tf from tensorflow import keras import numpy as np import matplotlib.pyplot as plt ################################################################################################ import torch import numpy as np import transformers import pickle from sklearn.linear_model import LogisticRegression #def load_model(): model = transformers.DistilBertModel.from_pretrained( "distilbert-base-uncased", output_attentions = False, output_hidden_states = False ) #model.load_state_dict(torch.load('model/modelbert.pt', map_location=torch.device('cpu'))) tokenizer = transformers.DistilBertTokenizer.from_pretrained("distilbert-base-uncased") #return model_finetuned, tokenizer def preprocess_text(text_input, max_len, tokenizer): input_tokens = tokenizer( text_input, return_tensors='pt', padding=True, max_length=max_len, truncation = True ) return input_tokens def predict_sentiment(model, input_tokens): #st.write(input_tokens) ans = {0: "NEGATIVE", 1: "POSITIVE"} last_hidden_states = model(**input_tokens) #st.write('last_hidden_states ok') vectors = last_hidden_states[0][:,0,:].detach().cpu().numpy() #st.write('vectors ok') #output = model(**input_tokens).last_hidden_states.detach().numpy() with open('model/modelbert1.pkl', 'rb') as file: cls = pickle.load(file) result = ans[cls.predict(vectors)[0]] return result col1, col2, col3 = st.columns([1,5,1]) with col2: st.title('Text sentiment analysis') col1, col2, col3 = st.columns([2,5,2]) with col2: max_len = st.slider('Maximum word length', 0, 500, 250) text_input = st.text_input("Enter some text") #model, tokenizer = load_model() if text_input: input_tokens = preprocess_text(text_input, max_len, tokenizer) output = predict_sentiment(model, input_tokens) st.write(output) ################################################################################################ #st.markdown("