demomern's picture
Update app.py
548542d
raw
history blame
2.29 kB
import re
import emoji
import joblib
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import classification_report, accuracy_score, confusion_matrix, f1_score
import gradio as gr
# load the TF-IDF vectorizer to a file
cv = joblib.load('tfidf_vectorizer.pkl')
# load the MLP classifier to a file
mlp_label = joblib.load('mlpLabel.pkl')
# load the MLP Aspect classifier to a file
mlp_aspect_label = joblib.load('mlpAspectLabel.pkl')
def remove_html(text) :
patt_html = r"<.*?>"
text = re.sub(patt_html, "", text)
return text
def remove_url(text):
patt_url = r"https?://\S+|www\.\S+"
text = re.sub(patt_url, "", text)
return text
def emoji_to_text(text) :
res_str = ""
for ch in text :
if emoji.is_emoji(ch) :
res_str += f" {emoji.demojize(ch)} "
# print(ch, emoji.demojize(ch))
else :
res_str += ch
return res_str
def clean_review_text(text):
# remove HTML Tags
text = remove_html(text)
# remove url to call function remover_url
text = remove_url(text)
# convert text emoji into text
text = emoji_to_text(text)
# convert all text into lower case
text = text.lower()
return text
label = ['negative', 'neutral', 'positive']
aspect_label = ['Card Decks and Challenges', 'Card Play and Board Games',
'Fun and Coin Collecting', 'Game Scores and Features',
'Game Updates and User Desires', 'Gameplay and App Experience',
'Gameplay and Trading', 'Gameplay and User Experience',
'Property and Land Management', 'Subway Adventures']
def return_label_aspect(Review):
review_vec = cv.transform([clean_review_text(Review)])
pred_label = mlp_label.predict_proba(review_vec)[0]
pred_aspect = mlp_aspect_label.predict_proba(review_vec)[0]
pred_label = { label[i]: round(pred_label[i], 2) for i in range(3) }
pred_aspect = { aspect_label[i]: round(pred_aspect[i], 2) for i in range(10) }
return pred_label, pred_aspect
iface = gr.Interface(fn=return_label_aspect, inputs="text", outputs=[gr.Label(), gr.Label()])
iface.launch(inline = False)