|
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""" |
|
<style> |
|
[data-testid="stAppViewContainer"] > .main {{ |
|
background-image: url("https://i.ibb.co/kH8bcr4/1368432.jpg"); |
|
background-size: 115%; |
|
background-position: top left; |
|
background-repeat: no-repeat; |
|
background-attachment: local; |
|
}} |
|
|
|
[data-testid="stSidebar"] > div:first-child {{ |
|
background-image: url("https://ibb.co/ZBkdJRg"); |
|
background-size: 115%; |
|
background-position: center; |
|
background-repeat: no-repeat; |
|
background-attachment: fixed; |
|
}} |
|
|
|
[data-testid="stHeader"] {{ |
|
background: rgba(0,0,0,0); |
|
}} |
|
|
|
[data-testid="stToolbar"] {{ |
|
right: 2rem; |
|
}} |
|
|
|
div.css-1n76uvr.e1tzin5v0 {{ |
|
background-color: rgba(238, 238, 238, 0.5); |
|
border: 10px solid #EEEEEE; |
|
padding: 5% 5% 5% 10%; |
|
border-radius: 5px; |
|
}} |
|
|
|
</style> |
|
""" |
|
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 |
|
|
|
|
|
model = transformers.DistilBertModel.from_pretrained( |
|
"distilbert-base-uncased", |
|
output_attentions = False, |
|
output_hidden_states = False |
|
) |
|
|
|
tokenizer = transformers.DistilBertTokenizer.from_pretrained("distilbert-base-uncased") |
|
|
|
|
|
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): |
|
|
|
ans = {0: "NEGATIVE", 1: "POSITIVE"} |
|
last_hidden_states = model(**input_tokens) |
|
|
|
vectors = last_hidden_states[0][:,0,:].detach().cpu().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") |
|
|
|
|
|
if text_input: |
|
input_tokens = preprocess_text(text_input, max_len, tokenizer) |
|
output = predict_sentiment(model, input_tokens) |
|
st.write(output) |
|
|
|
|
|
|
|
|
|
|
|
|