Commit
•
8f486ee
1
Parent(s):
3b371b8
app.py changes
Browse files- app.py +9 -4
- src/predict.py +5 -4
app.py
CHANGED
@@ -5,12 +5,17 @@ model = ModelLoader()
|
|
5 |
prediction = PredictionServices(model.Model, model.Tokenizer)
|
6 |
|
7 |
def single_predict(text):
|
|
|
8 |
preds = prediction.single_predict(text)
|
9 |
-
|
|
|
|
|
|
|
10 |
|
11 |
-
app = gr.Interface(gr.Textbox(label="Enter Comment"),
|
12 |
-
|
13 |
-
outputs=gr.Label('
|
|
|
14 |
|
15 |
app.launch()
|
16 |
|
|
|
5 |
prediction = PredictionServices(model.Model, model.Tokenizer)
|
6 |
|
7 |
def single_predict(text):
|
8 |
+
print(text)
|
9 |
preds = prediction.single_predict(text)
|
10 |
+
toxic_pred = float(preds)
|
11 |
+
non_toxic_pred = float(1-toxic_pred)
|
12 |
+
rslt = {"Toxic":toxic_pred,"Non Toxic":non_toxic_pred}
|
13 |
+
return rslt
|
14 |
|
15 |
+
app = gr.Interface(inputs=gr.Textbox(label="Enter Comment"),
|
16 |
+
fn=single_predict,
|
17 |
+
outputs=[gr.Label('Probabilities')],
|
18 |
+
title="Toxic Comment Classifier")
|
19 |
|
20 |
app.launch()
|
21 |
|
src/predict.py
CHANGED
@@ -15,7 +15,7 @@ class PredictionServices:
|
|
15 |
self.model = model
|
16 |
self.tokenizer = tokenizer
|
17 |
|
18 |
-
def
|
19 |
tokens = self.tokenizer(text,
|
20 |
max_length=MAX_LEN,
|
21 |
truncation=True,
|
@@ -23,7 +23,8 @@ class PredictionServices:
|
|
23 |
add_special_tokens=True,
|
24 |
return_tensors="tf",
|
25 |
return_token_type_ids = False)
|
26 |
-
|
|
|
27 |
|
28 |
def plot(self, pred):
|
29 |
fig = px.bar(x=[round(pred), round(1-pred)],
|
@@ -49,7 +50,7 @@ class PredictionServices:
|
|
49 |
df = pd.read_csv(data)
|
50 |
df.dropna(inplace=True)
|
51 |
df = df.comment_text.apply(lambda x: re.sub('\n',' ',x).strip())
|
52 |
-
input = self.
|
53 |
preds = self.model.predict(input)
|
54 |
df['probabilities'] = preds
|
55 |
df['toxic'] = np.where(df['probabilities']>0.5, 1, 0)
|
@@ -60,7 +61,7 @@ class PredictionServices:
|
|
60 |
def single_predict(self, text:str):
|
61 |
try:
|
62 |
text = re.sub('\n',' ',text).strip()
|
63 |
-
input = self.
|
64 |
pred = self.model.predict(input)[0][0]
|
65 |
return pred
|
66 |
except Exception as e:
|
|
|
15 |
self.model = model
|
16 |
self.tokenizer = tokenizer
|
17 |
|
18 |
+
def tokenizer_fn(self, text:str):
|
19 |
tokens = self.tokenizer(text,
|
20 |
max_length=MAX_LEN,
|
21 |
truncation=True,
|
|
|
23 |
add_special_tokens=True,
|
24 |
return_tensors="tf",
|
25 |
return_token_type_ids = False)
|
26 |
+
inputs = dict(tokens)
|
27 |
+
return inputs
|
28 |
|
29 |
def plot(self, pred):
|
30 |
fig = px.bar(x=[round(pred), round(1-pred)],
|
|
|
50 |
df = pd.read_csv(data)
|
51 |
df.dropna(inplace=True)
|
52 |
df = df.comment_text.apply(lambda x: re.sub('\n',' ',x).strip())
|
53 |
+
input = self.tokenizer_fn(df.comment_text.values.tolist())
|
54 |
preds = self.model.predict(input)
|
55 |
df['probabilities'] = preds
|
56 |
df['toxic'] = np.where(df['probabilities']>0.5, 1, 0)
|
|
|
61 |
def single_predict(self, text:str):
|
62 |
try:
|
63 |
text = re.sub('\n',' ',text).strip()
|
64 |
+
input = self.tokenizer_fn(text)
|
65 |
pred = self.model.predict(input)[0][0]
|
66 |
return pred
|
67 |
except Exception as e:
|