File size: 3,245 Bytes
a8ceb05 |
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
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
#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("<div style='text-align: center; font-size: 25px;'> ", unsafe_allow_html=True)
#st.markdown("<div style='text-align: center; font-size: 25px;'> ", unsafe_allow_html=True) |