Cricles commited on
Commit
d97910c
·
verified ·
1 Parent(s): 4e2ff5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -46
app.py CHANGED
@@ -23,40 +23,7 @@ def load_model():
23
  model = fasttext.load_model('./model_fasttext.bin')
24
  return model
25
 
26
- model = load_model()
27
-
28
- bin_str = get_image_base64("./билли.png")
29
- page_bg_img = '''
30
- <style>
31
- .stApp{
32
- background-image: linear-gradient(rgba(255, 255, 255, 0.7),
33
- rgba(255, 255, 255, 0.7)),
34
- url("data:image/png;base64,%s");
35
- background-size: cover;
36
- background-position: center;
37
- background-repeat: no-repeat;
38
- background-attachment: fixed;
39
- }
40
- </style>
41
- ''' % bin_str
42
- st.markdown(page_bg_img, unsafe_allow_html=True)
43
-
44
- logo_base64 = get_image_base64("./top_logo1.png")
45
-
46
- # Используем HTML для вставки логотипа в заголовок
47
- st.markdown(
48
- f"""
49
- <div style="display: flex; justify-content: center;">
50
- <img src="data:image/png;base64,{logo_base64}" width="400">
51
- </div>
52
- """,
53
- unsafe_allow_html=True
54
- )
55
-
56
- # Описание
57
- st.write("""<p style='text-align: center; font-size: 24px;'>Это приложение сделает твою речь менее токсичной.
58
- И даже не придётся платить 300 bucks.</p>""", unsafe_allow_html=True)
59
-
60
  class ModelWrapper(object):
61
  MODELS_DIR: str = "./new_models/"
62
  MODEL_NAME: str = "model"
@@ -90,19 +57,40 @@ class ModelWrapper(object):
90
  predicted_id = torch.argmax(predicted, dim=1).numpy()[0]
91
  return self.id2label[predicted_id]
92
 
 
 
93
 
94
- def highlight_obscene_words(text):
95
- label,_=model.predict(text.lower())
96
- if label[0]=='__label__positive':
97
- st.markdown(
98
- "<span style='background:#47916B;'>{}|приемлемо</span>".format(text),
99
- unsafe_allow_html=True
100
- )
101
- else:
102
- st.markdown(
103
- "<span style='background:#ffcccc;'>{}|токсично</span>".format(text),
104
- unsafe_allow_html=True
105
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
  # Боковая панель
108
  with st.sidebar:
@@ -119,6 +107,43 @@ st.write("""<p style='text-align: center;'>Введите текст ниже,
119
 
120
  user_input = st.text_area('',height=200)
121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  if st.button("Проверить текст"):
123
  if user_input.strip():
124
  st.subheader("Результат:")
 
23
  model = fasttext.load_model('./model_fasttext.bin')
24
  return model
25
 
26
+ @st.cache_resource
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  class ModelWrapper(object):
28
  MODELS_DIR: str = "./new_models/"
29
  MODEL_NAME: str = "model"
 
57
  predicted_id = torch.argmax(predicted, dim=1).numpy()[0]
58
  return self.id2label[predicted_id]
59
 
60
+ model_fasttext = load_model()
61
+ model_BERT=ModelWrapper()
62
 
63
+ bin_str = get_image_base64("./билли.png")
64
+ page_bg_img = '''
65
+ <style>
66
+ .stApp{
67
+ background-image: linear-gradient(rgba(255, 255, 255, 0.7),
68
+ rgba(255, 255, 255, 0.7)),
69
+ url("data:image/png;base64,%s");
70
+ background-size: cover;
71
+ background-position: center;
72
+ background-repeat: no-repeat;
73
+ background-attachment: fixed;
74
+ }
75
+ </style>
76
+ ''' % bin_str
77
+ st.markdown(page_bg_img, unsafe_allow_html=True)
78
+
79
+ logo_base64 = get_image_base64("./top_logo1.png")
80
+
81
+ # Используем HTML для вставки логотипа в заголовок
82
+ st.markdown(
83
+ f"""
84
+ <div style="display: flex; justify-content: center;">
85
+ <img src="data:image/png;base64,{logo_base64}" width="400">
86
+ </div>
87
+ """,
88
+ unsafe_allow_html=True
89
+ )
90
+
91
+ # Описание
92
+ st.write("""<p style='text-align: center; font-size: 24px;'>Это приложение сделает твою речь менее токсичной.
93
+ И даже не придётся платить 300 bucks.</p>""", unsafe_allow_html=True)
94
 
95
  # Боковая панель
96
  with st.sidebar:
 
107
 
108
  user_input = st.text_area('',height=200)
109
 
110
+ model_type = st.sidebar.selectbox(
111
+ "Выберите модель",
112
+ ("fasttext", "ru-BERT", "FRIDA")
113
+ )
114
+
115
+ def highlight_obscene_words(text, model_type):
116
+ if model_type=="fasttext":
117
+ label,_=model.predict(text.lower())
118
+ if label[0]=='__label__positive':
119
+ st.markdown(
120
+ "<span style='background:#47916B;'>{}|приемлемо</span>".format(text),
121
+ unsafe_allow_html=True
122
+ )
123
+ else:
124
+ st.markdown(
125
+ "<span style='background:#ffcccc;'>{}|токсично</span>".format(text),
126
+ unsafe_allow_html=True
127
+ )
128
+ elif model_type=="ru-BERT":
129
+ label=model_BERT(text.lower())
130
+ if label=='__label__positive':
131
+ st.markdown(
132
+ "<span style='background:#47916B;'>{}|приемлемо</span>".format(text),
133
+ unsafe_allow_html=True
134
+ )
135
+ else:
136
+ st.markdown(
137
+ "<span style='background:#ffcccc;'>{}|токсично</span>".format(text),
138
+ unsafe_allow_html=True
139
+ )
140
+ else:
141
+ #ЗАГЛУШКА
142
+ st.markdown(
143
+ "<span style='background:#47916B;'>{}|приемлемо</span>".format(text),
144
+ unsafe_allow_html=True
145
+ )
146
+
147
  if st.button("Проверить текст"):
148
  if user_input.strip():
149
  st.subheader("Результат:")