Orcun2's picture
Upload app.py
3f47019
raw
history blame
1.2 kB
import gradio as gr
import os
import pandas as pd
import tensorflow as tf
import numpy as np
from tensorflow.keras.layers import TextVectorization
df = pd.read_csv(os.path.join('train.csv'))
X = df['comment_text']
columns = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
# # load numpy array from npy file
# from numpy import load
# # load array
# xValues = np.load('forVectorizer.npy', allow_pickle=True)
# SET THE VECTORIZER
#set max features
MAX_FEATURES = 200000
vectorizer = TextVectorization(max_tokens=MAX_FEATURES,
output_sequence_length=1800,
output_mode='int')
vectorizer.adapt(X.values)
model = tf.keras.models.load_model('toxicity.h5')
def score_comment(comment):
vectorized_comment = vectorizer([comment])
results = model.predict(vectorized_comment)
text = ''
for idx, col in enumerate(columns):
text += '{}: {}\n'.format(col, results[0][idx]>0.5)
return text
interface = gr.Interface(fn=score_comment,
inputs=gr.inputs.Textbox(lines=2, placeholder='Comment to score'),
outputs='text')
interface.launch()