Spaces:
Build error
Build error
Commit ·
fd66c83
1
Parent(s): 0792818
app.py updated
Browse files- README.md +1 -0
- app.py +8 -3
- src/constants.py +2 -2
- src/single_predict.py +15 -2
README.md
CHANGED
|
@@ -4,6 +4,7 @@ emoji: 🌍
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: streamlit
|
|
|
|
| 7 |
app_file: app.py
|
| 8 |
pinned: false
|
| 9 |
license: apache-2.0
|
|
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: streamlit
|
| 7 |
+
sdk_version: 1.17.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
app.py
CHANGED
|
@@ -6,9 +6,14 @@ single = SinglePrediction()
|
|
| 6 |
batch = BatchPrediction()
|
| 7 |
|
| 8 |
def single_predict(text):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def batch_predict(data):
|
| 14 |
if batch.data_validation(data):
|
|
|
|
| 6 |
batch = BatchPrediction()
|
| 7 |
|
| 8 |
def single_predict(text):
|
| 9 |
+
preds, fig = single.predict(text)
|
| 10 |
+
|
| 11 |
+
if preds < 0.5:
|
| 12 |
+
st.success(f'Non Toxic Comment!!! :thumbsup:')
|
| 13 |
+
st.plotly_chart(fig, theme="streamlit", use_container_width=True)
|
| 14 |
+
else:
|
| 15 |
+
st.error(f'Toxic Comment!!! :thumbsup:')
|
| 16 |
+
st.plotly_chart(fig, theme="streamlit", use_container_width=True)
|
| 17 |
|
| 18 |
def batch_predict(data):
|
| 19 |
if batch.data_validation(data):
|
src/constants.py
CHANGED
|
@@ -3,8 +3,8 @@ import os
|
|
| 3 |
ROOT_DIR = os.getcwd()
|
| 4 |
MODEL_DIR_NAME = "serving_model"
|
| 5 |
MODEL_NAME = "roberta-fine-tuned-2"
|
| 6 |
-
MODEL_PATH = os.path.join(ROOT_DIR,
|
| 7 |
TOKENIZER_FILE_NAME = "tokenizer"
|
| 8 |
-
TOKENIZER_PATH = os.path.join(ROOT_DIR,
|
| 9 |
MAX_LEN =192
|
| 10 |
BUFFER_SIZE=2048
|
|
|
|
| 3 |
ROOT_DIR = os.getcwd()
|
| 4 |
MODEL_DIR_NAME = "serving_model"
|
| 5 |
MODEL_NAME = "roberta-fine-tuned-2"
|
| 6 |
+
MODEL_PATH = os.path.join(ROOT_DIR, MODEL_NAME)
|
| 7 |
TOKENIZER_FILE_NAME = "tokenizer"
|
| 8 |
+
TOKENIZER_PATH = os.path.join(ROOT_DIR, TOKENIZER_FILE_NAME)
|
| 9 |
MAX_LEN =192
|
| 10 |
BUFFER_SIZE=2048
|
src/single_predict.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import numpy as np
|
| 3 |
import tensorflow as tf
|
|
|
|
| 4 |
import transformers
|
| 5 |
from transformers import AutoTokenizer
|
| 6 |
import os
|
|
@@ -22,12 +23,24 @@ class SinglePrediction:
|
|
| 22 |
return_tensors="tf",
|
| 23 |
return_token_type_ids = False)
|
| 24 |
return dict(tokens)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def predict(self, text:str):
|
| 27 |
try:
|
| 28 |
text = re.sub('\n',' ',text).strip()
|
| 29 |
input = self.tokenizer(text)
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
print(e)
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import numpy as np
|
| 3 |
import tensorflow as tf
|
| 4 |
+
import plotly.express as px
|
| 5 |
import transformers
|
| 6 |
from transformers import AutoTokenizer
|
| 7 |
import os
|
|
|
|
| 23 |
return_tensors="tf",
|
| 24 |
return_token_type_ids = False)
|
| 25 |
return dict(tokens)
|
| 26 |
+
|
| 27 |
+
def plot(self, pred):
|
| 28 |
+
fig = px.bar(x=[round(pred), round(1-pred)],
|
| 29 |
+
y=['toxic', 'non-toxic'],
|
| 30 |
+
width=500, height=250,
|
| 31 |
+
template="plotly_dark",
|
| 32 |
+
text_auto='1',
|
| 33 |
+
title="Probabilities(%)")
|
| 34 |
+
fig.update_traces(width=0.3,textfont_size=15, textangle=0, textposition="outside")
|
| 35 |
+
fig.update_layout(yaxis_title=None,xaxis_title=None)
|
| 36 |
+
return fig
|
| 37 |
|
| 38 |
def predict(self, text:str):
|
| 39 |
try:
|
| 40 |
text = re.sub('\n',' ',text).strip()
|
| 41 |
input = self.tokenizer(text)
|
| 42 |
+
pred = self.model.predict(input)[0][0]
|
| 43 |
+
fig = self.plot(pred)
|
| 44 |
+
return pred, fig
|
| 45 |
except Exception as e:
|
| 46 |
print(e)
|