Spaces:
Sleeping
Sleeping
PRASHANTH REDDY
commited on
Commit
·
e8ae043
1
Parent(s):
77064f4
Added all files
Browse files- .DS_Store +0 -0
- app.py +61 -0
- count_vectorizer.joblib +3 -0
- knn_model.joblib +3 -0
- logistic_regression_model.joblib +3 -0
- requirements.txt +8 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
from nltk.tokenize import word_tokenize
|
| 3 |
+
from nltk.corpus import stopwords
|
| 4 |
+
from nltk.stem import WordNetLemmatizer
|
| 5 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
count_vectorizer = joblib.load("count_vectorizer.joblib")
|
| 9 |
+
best_logistic_model = joblib.load("logistic_regression_model.joblib")
|
| 10 |
+
best_knn_model = joblib.load("knn_model.joblib")
|
| 11 |
+
|
| 12 |
+
knn_test_accuracy =0.635
|
| 13 |
+
logistic_test_accuracy = 0.735
|
| 14 |
+
|
| 15 |
+
def preprocess_text(text):
|
| 16 |
+
tokens = word_tokenize(text)
|
| 17 |
+
tokens = [word for word in tokens if word.isalnum()]
|
| 18 |
+
tokens = [word.lower() for word in tokens]
|
| 19 |
+
stop_words = set(stopwords.words("english"))
|
| 20 |
+
tokens = [word for word in tokens if word not in stop_words]
|
| 21 |
+
lemmatizer = WordNetLemmatizer()
|
| 22 |
+
tokens = [lemmatizer.lemmatize(word) for word in tokens]
|
| 23 |
+
preprocessed_text = " ".join(tokens)
|
| 24 |
+
transformed_text = count_vectorizer.transform([preprocessed_text])
|
| 25 |
+
return transformed_text
|
| 26 |
+
|
| 27 |
+
def predict_sentiment(text, model_name):
|
| 28 |
+
preprocessed_text = preprocess_text(text)
|
| 29 |
+
if model_name == "Logistic Regression":
|
| 30 |
+
prediction = best_logistic_model.predict(preprocessed_text)[0]
|
| 31 |
+
accuracy = logistic_test_accuracy
|
| 32 |
+
|
| 33 |
+
elif model_name == "K-nearest Neighbors":
|
| 34 |
+
prediction = best_knn_model.predict(preprocessed_text)[0]
|
| 35 |
+
accuracy = knn_test_accuracy
|
| 36 |
+
else:
|
| 37 |
+
return "Invalid model selection", None
|
| 38 |
+
if prediction==1:
|
| 39 |
+
sentiment="Positive"
|
| 40 |
+
pos_accuracy=accuracy
|
| 41 |
+
neg_accuracy=1-pos_accuracy
|
| 42 |
+
else:
|
| 43 |
+
sentiment="Negative"
|
| 44 |
+
neg_accuracy=accuracy
|
| 45 |
+
pos_accuracy=1-neg_accuracy
|
| 46 |
+
accuracy_dict = {"Positive": pos_accuracy, "Negative": neg_accuracy}
|
| 47 |
+
|
| 48 |
+
return accuracy_dict
|
| 49 |
+
examples = [["Point your finger at any item on the menu, order it and you won't be disappointed","K-nearest Neighbors"], ["Similarly, the delivery man did not say a word of apology when our food was 45 minutes late.","Logistic Regression"],["I vomited in the bathroom mid lunch.","K-nearest Neighbors"]]
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
interface = gr.Interface(
|
| 53 |
+
fn=predict_sentiment,
|
| 54 |
+
inputs=[gr.Textbox(lines=7, label="Input Text"),
|
| 55 |
+
gr.Dropdown(["Logistic Regression", "K-nearest Neighbors"], label="Select Model")],
|
| 56 |
+
outputs=gr.Label(dict),
|
| 57 |
+
title="Sentiment Prediction",
|
| 58 |
+
description="Select a model and enter a text to predict the sentiment (positive or negative) and show the accuracy score.",
|
| 59 |
+
examples=examples
|
| 60 |
+
)
|
| 61 |
+
interface.launch()
|
count_vectorizer.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:09392663edcf224e458e08526e637a68c1ade848b35ca2df8fed9df264d8fee4
|
| 3 |
+
size 19497
|
knn_model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:60aa9ce605f7c6ededde4406460c7393ed9efcbde01575b287d69ba8a554ed44
|
| 3 |
+
size 62820
|
logistic_regression_model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:068209aa656a54ddfdefa54733c7c3c5a3b6b2355db93ad309b39b5aff7ba241
|
| 3 |
+
size 13583
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
numpy
|
| 3 |
+
seaborn
|
| 4 |
+
wordcloud
|
| 5 |
+
nltk
|
| 6 |
+
scikit-learn==1.2.2
|
| 7 |
+
joblib
|
| 8 |
+
gradio
|