keentomato's picture
Upload app.py
5b78e2e
import joblib
import gradio as gr
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import warnings, string
warnings.filterwarnings('ignore')
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
import nltk
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestClassifier
# from sklearn.tree import DecisionTreeClassifier
# from sklearn.neighbors import KNeighborsClassifier
# from sklearn.svm import SVC
# from sklearn.linear_model import LogisticRegression
from nltk.corpus import stopwords
import string, nltk
from nltk import word_tokenize
from nltk.stem import PorterStemmer
from nltk.stem import WordNetLemmatizer
nltk.download('wordnet')
nltk.download('omw-1.4')
lemmatizer = WordNetLemmatizer()
stemmer = PorterStemmer()
def clean_text(text):
nopunc = [w for w in text if w not in string.punctuation]
nopunc = ''.join(nopunc)
return ' '.join([word for word in nopunc.split() if word.lower() not in stopwords.words('english')])
def preprocess(text):
return ' '.join([word for word in word_tokenize(text) if word not in stopwords.words('english') and not word.isdigit() and word not in string.punctuation])
def stem_words(text):
return ' '.join([stemmer.stem(word) for word in text.split()])
def lemmatize_words(text):
return ' '.join([lemmatizer.lemmatize(word) for word in text.split()])
def text_process(review):
nopunc = [char for char in review if char not in string.punctuation]
nopunc = ''.join(nopunc)
return [word for word in nopunc.split() if word.lower() not in stopwords.words('english')]
### IMPORT ML MODEL; best use for multiple reviews involving the same reviewer or user
print("Running prototype")
loaded_pipeline = joblib.load("trained_fake_review_detector.pkl")
print(loaded_pipeline)
iter_test = ["Hello World"]
print(loaded_pipeline.predict(iter_test))
initialise_gradio = True
if initialise_gradio:
def classify_review(x):
iter_x = list(x)
result = loaded_pipeline.predict(iter_x)
return result[0]
iface = gr.Interface(fn = classify_review,
inputs = 'textbox',
outputs = 'label',
title = 'Fake Review Detection',
description = 'Input any text that you believe to be fake, this will return an output',
article =
'''<div>
<p> Hit submit after putting your fake review text to see the results.</p>
</div>''',
share = True)
#gr.Markdown("## Text Examples")
# Adding of examples
#gr.Examples([["hi", "Adam"], ["hello", "Eve"]], test_fn, cache_examples=True)
iface.launch(share = True)