NimaKL commited on
Commit
303cf51
1 Parent(s): e4afdb3

Upload app (1).py

Browse files
Files changed (1) hide show
  1. app (1).py +79 -0
app (1).py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ from transformers.models.bert import BertTokenizer
5
+ from transformers import TFBertModel
6
+ import streamlit as st
7
+ import pandas as pd
8
+ from transformers import TFAutoModel
9
+
10
+
11
+
12
+
13
+ hist_loss= [0.1971,0.0732,0.0465,0.0319,0.0232,0.0167,0.0127,0.0094,0.0073,0.0058,0.0049,0.0042]
14
+ hist_acc = [0.9508,0.9811,0.9878,0.9914,0.9936,0.9954,0.9965,0.9973,0.9978,0.9983,0.9986,0.9988]
15
+ hist_val_acc = [0.9804,0.9891,0.9927,0.9956,0.9981,0.998,0.9991,0.9997,0.9991,0.9998,0.9998,0.9998]
16
+ hist_val_loss = [0.0759,0.0454,0.028,0.015,0.0063,0.0064,0.004,0.0011,0.0021,0.00064548,0.0010,0.00042896]
17
+ Epochs = [i for i in range(1,13)]
18
+
19
+ hist_loss[:] = [x * 100 for x in hist_loss]
20
+ hist_acc[:] = [x * 100 for x in hist_acc]
21
+ hist_val_acc[:] = [x * 100 for x in hist_val_acc]
22
+ hist_val_loss[:] = [x * 100 for x in hist_val_loss]
23
+ d = {'val_acc':hist_val_acc, 'acc':hist_acc,'loss':hist_loss, 'val_loss':hist_val_loss, 'Epochs': Epochs}
24
+ chart_data = pd.DataFrame(d)
25
+ chart_data.index = range(1,13)
26
+
27
+ @st.cache(suppress_st_warning=True, allow_output_mutation=True)
28
+ def load_model(show_spinner=True):
29
+ yorum_model = tf.keras.models.load_model('TC32_SavedModel')
30
+ tokenizer = BertTokenizer.from_pretrained('NimaKL/tc32_test')
31
+ return yorum_model, tokenizer
32
+
33
+ st.set_page_config(layout='wide', initial_sidebar_state='expanded')
34
+ col1, col2= st.columns(2)
35
+ with col1:
36
+ st.title("TC32 Multi-Class Text Classification")
37
+ st.subheader('Model Loss and Accuracy')
38
+ st.markdown("<br>", unsafe_allow_html=True)
39
+ st.area_chart(chart_data, height=320)
40
+ yorum_model, tokenizer = load_model()
41
+
42
+
43
+ with col2:
44
+ st.title("Sınıfı bulmak için bir şikayet girin. (Ctrl+Enter)")
45
+ st.subheader("Enter complaint (in Turkish) to find the class.")
46
+ #st.subheader("Şikayet")
47
+ text = st.text_area("", "Bebeğim haftada bir kutu mama bitiriyor. Geçen hafta 135 tl'ye aldığım mama bugün 180 tl olmuş. Ben de artık aptamil almayacağım. Tüketici haklarına şikayet etmemiz gerekiyor. Yazıklar olsun.", height=285)
48
+
49
+ def prepare_data(input_text, tokenizer):
50
+ token = tokenizer.encode_plus(
51
+ input_text,
52
+ max_length=256,
53
+ truncation=True,
54
+ padding='max_length',
55
+ add_special_tokens=True,
56
+ return_tensors='tf'
57
+ )
58
+ return {
59
+ 'input_ids': tf.cast(token.input_ids, tf.float64),
60
+ 'attention_mask': tf.cast(token.attention_mask, tf.float64)
61
+ }
62
+
63
+ def make_prediction(model, processed_data, classes=['Alışveriş','Anne-Bebek','Beyaz Eşya','Bilgisayar','Cep Telefonu','Eğitim','Elektronik','Emlak ve İnşaat','Enerji','Etkinlik ve Organizasyon','Finans','Gıda','Giyim','Hizmet','İçecek','İnternet','Kamu','Kargo-Nakliyat','Kozmetik','Küçük Ev Aletleri','Medya','Mekan ve Eğlence','Mobilya - Ev Tekstili','Mücevher Saat Gözlük','Mutfak Araç Gereç','Otomotiv','Sağlık','Sigorta','Spor','Temizlik','Turizm','Ulaşım']):
64
+ probs = model.predict(processed_data)[0]
65
+ return classes[np.argmax(probs)]
66
+
67
+
68
+ if text:
69
+ with col1:
70
+ with st.spinner('Wait for it...'):
71
+ processed_data = prepare_data(text, tokenizer)
72
+ result = make_prediction(yorum_model, processed_data=processed_data)
73
+ st.markdown("<br>", unsafe_allow_html=True)
74
+ st.success("Tahmin başarıyla tamamlandı!")
75
+ with col2:
76
+ description = '<table style="border: collapse; padding-top: 1px;"><tr><div style="height: 62px;"></div></tr><tr><p style="border-width: medium; border-color: #aa5e70; border-radius: 10px;padding-top: 1px;padding-left: 20px;background:#20212a;font-family:Courier New; color: white;font-size: 36px; font-weight: boldest;">'+result+'</p></tr><table>'
77
+ st.markdown(description, unsafe_allow_html=True)
78
+
79
+