Spaces:
Runtime error
Runtime error
Commit ·
5b78e2e
1
Parent(s): 5c77a48
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import warnings, string
|
| 8 |
+
warnings.filterwarnings('ignore')
|
| 9 |
+
from sklearn.model_selection import train_test_split, GridSearchCV
|
| 10 |
+
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
|
| 11 |
+
import nltk
|
| 12 |
+
from nltk.corpus import stopwords
|
| 13 |
+
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
|
| 14 |
+
from sklearn.naive_bayes import MultinomialNB
|
| 15 |
+
from sklearn.pipeline import Pipeline
|
| 16 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 17 |
+
# from sklearn.tree import DecisionTreeClassifier
|
| 18 |
+
# from sklearn.neighbors import KNeighborsClassifier
|
| 19 |
+
# from sklearn.svm import SVC
|
| 20 |
+
# from sklearn.linear_model import LogisticRegression
|
| 21 |
+
from nltk.corpus import stopwords
|
| 22 |
+
import string, nltk
|
| 23 |
+
from nltk import word_tokenize
|
| 24 |
+
from nltk.stem import PorterStemmer
|
| 25 |
+
from nltk.stem import WordNetLemmatizer
|
| 26 |
+
|
| 27 |
+
nltk.download('wordnet')
|
| 28 |
+
nltk.download('omw-1.4')
|
| 29 |
+
|
| 30 |
+
lemmatizer = WordNetLemmatizer()
|
| 31 |
+
stemmer = PorterStemmer()
|
| 32 |
+
|
| 33 |
+
def clean_text(text):
|
| 34 |
+
nopunc = [w for w in text if w not in string.punctuation]
|
| 35 |
+
nopunc = ''.join(nopunc)
|
| 36 |
+
return ' '.join([word for word in nopunc.split() if word.lower() not in stopwords.words('english')])
|
| 37 |
+
def preprocess(text):
|
| 38 |
+
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])
|
| 39 |
+
|
| 40 |
+
def stem_words(text):
|
| 41 |
+
return ' '.join([stemmer.stem(word) for word in text.split()])
|
| 42 |
+
|
| 43 |
+
def lemmatize_words(text):
|
| 44 |
+
return ' '.join([lemmatizer.lemmatize(word) for word in text.split()])
|
| 45 |
+
|
| 46 |
+
def text_process(review):
|
| 47 |
+
nopunc = [char for char in review if char not in string.punctuation]
|
| 48 |
+
nopunc = ''.join(nopunc)
|
| 49 |
+
return [word for word in nopunc.split() if word.lower() not in stopwords.words('english')]
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
### IMPORT ML MODEL; best use for multiple reviews involving the same reviewer or user
|
| 53 |
+
print("Running prototype")
|
| 54 |
+
loaded_pipeline = joblib.load("trained_fake_review_detector.pkl")
|
| 55 |
+
print(loaded_pipeline)
|
| 56 |
+
iter_test = ["Hello World"]
|
| 57 |
+
print(loaded_pipeline.predict(iter_test))
|
| 58 |
+
|
| 59 |
+
initialise_gradio = True
|
| 60 |
+
|
| 61 |
+
if initialise_gradio:
|
| 62 |
+
def classify_review(x):
|
| 63 |
+
iter_x = list(x)
|
| 64 |
+
result = loaded_pipeline.predict(iter_x)
|
| 65 |
+
|
| 66 |
+
return result[0]
|
| 67 |
+
iface = gr.Interface(fn = classify_review,
|
| 68 |
+
inputs = 'textbox',
|
| 69 |
+
outputs = 'label',
|
| 70 |
+
title = 'Fake Review Detection',
|
| 71 |
+
description = 'Input any text that you believe to be fake, this will return an output',
|
| 72 |
+
article =
|
| 73 |
+
'''<div>
|
| 74 |
+
<p> Hit submit after putting your fake review text to see the results.</p>
|
| 75 |
+
</div>''',
|
| 76 |
+
share = True)
|
| 77 |
+
|
| 78 |
+
#gr.Markdown("## Text Examples")
|
| 79 |
+
|
| 80 |
+
# Adding of examples
|
| 81 |
+
#gr.Examples([["hi", "Adam"], ["hello", "Eve"]], test_fn, cache_examples=True)
|
| 82 |
+
|
| 83 |
+
iface.launch(share = True)
|