|
import streamlit as st |
|
from pyparsing import empty |
|
from mymodel import CustomBertForSequenceClassification |
|
from transformers import BertTokenizer, Trainer |
|
|
|
POSITIVE = 0 |
|
NEGATIVE = 1 |
|
NEUTRAL = 2 |
|
|
|
idx_target = {POSITIVE:'positive', NEGATIVE:'negative', NEUTRAL:'neutral'} |
|
|
|
g_selected_model_type = None |
|
g_input_text = ' ' |
|
|
|
def get_model_type(select_model, add_layers): |
|
if (select_model == "BERT") & (add_layers == True): |
|
return "klue/bert-base", "bert_layered" |
|
elif (select_model == "BERT") & (add_layers == False): |
|
return "klue/bert-base", "bert" |
|
elif (select_model == "RoBERTa") & (add_layers == True): |
|
return "klue/roberta-base", "roberta_layered" |
|
elif (select_model == "RoBERTa") & (add_layers == False): |
|
return "klue/roberta-base", "roberta" |
|
|
|
def show_predict_result(model_type, input_text): |
|
target = POSITIVE |
|
show_response_img(target) |
|
show_response_text(target) |
|
|
|
def show_response_img(target = None): |
|
if target == None: |
|
st.con3.write("") |
|
elif target == POSITIVE: |
|
st.con3.write("μλ νμ ") |
|
elif target == NEGATIVE: |
|
st.con3.write("μ°λ νμ ") |
|
elif target == NEUTRAL: |
|
st.con3.write("무λ€λ€ν νμ ") |
|
|
|
def show_response_text(target = None): |
|
if target == None: |
|
st.con4.write("") |
|
elif target == POSITIVE: |
|
st.con4.write("κΈμ μ μΈ λ°μ") |
|
elif target == NEGATIVE: |
|
st.con4.write("λΆμ μ μΈ λ°μ") |
|
elif target == NEUTRAL: |
|
st.con4.write("무λ€λ€ν λ°μ") |
|
|
|
|
|
def show_data(): |
|
st.write("data") |
|
|
|
def show_api_usage(): |
|
with st.container(border=True): |
|
st.write("api μ¬μ©λ²") |
|
|
|
def tab1_page(): |
|
global g_selected_model_type |
|
global g_input_text |
|
st.con1,st.con2 = st.columns([0.3,0.7]) |
|
st.con3,st.con4 = st.columns([0.3,0.7]) |
|
st.con5,empty1 = st.columns([0.9999,0.0001]) |
|
|
|
with st.container(): |
|
with st.con1: |
|
with st.con1.container(border=True): |
|
selected_model = st.selectbox("μμΈ‘ λͺ¨λΈμ μ ννμΈμ.", ["BERT", "RoBERTa"]) |
|
add_layers = st.checkbox('Layer μΆκ°') |
|
g_selected_model_type = get_model_type(selected_model, add_layers) |
|
show_predict_result(g_selected_model_type, g_input_text) |
|
with st.con2: |
|
with st.con2.container(border=True): |
|
input_text = st.text_area("input_text") |
|
submit_button = st.button('νμΈ') |
|
if submit_button : |
|
g_input_text = input_text |
|
show_predict_result(g_selected_model_type, g_input_text) |
|
with st.con3: |
|
with st.con3.container(border=True): |
|
show_response_img() |
|
with st.con4: |
|
with st.con4.container(border=True): |
|
show_response_text() |
|
with st.con5: |
|
with st.con5.container(border=True): |
|
show_data() |
|
with empty1: |
|
empty() |
|
|
|
|
|
def tab2_page(): |
|
show_api_usage() |
|
|
|
|
|
|
|
|
|
|
|
tokenizer = BertTokenizer.from_pretrained("bert_tokenizer_layered") |
|
model = CustomBertForSequenceClassification.from_pretrained("bert_model_layered") |
|
trainer = Trainer().load_model("bert_trainer_layered") |
|
|
|
|
|
|
|
st.title("Semi Project - Sentiment analysis") |
|
|
|
|
|
|
|
tab1, tab2 = st.tabs(['λμ νμΈ', 'API']) |
|
|
|
with tab1: |
|
tab1_page() |
|
|
|
with tab2: |
|
tab2_page() |