gridflowai
commited on
Commit
·
c24562e
1
Parent(s):
4f5ab04
Upload 7 files
Browse files- app.py +107 -0
- dl_model.h5 +3 -0
- logistic_model.pkl +3 -0
- mnb_model.pkl +3 -0
- requirements.txt +6 -0
- svm_model.pkl +3 -0
- tfidf_vectorizer.pkl +3 -0
app.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
import re
|
5 |
+
#importing skearn and joblib modules
|
6 |
+
import joblib
|
7 |
+
import sklearn
|
8 |
+
|
9 |
+
# setting the joblib
|
10 |
+
sklearn.externals.joblib = joblib# Load the models
|
11 |
+
|
12 |
+
lr_model = joblib.load("logistic_model.pkl")
|
13 |
+
svm_model = joblib.load("svm_model.pkl")
|
14 |
+
nn_model = load_model("dl_model.h5")
|
15 |
+
mnb_model = joblib.load("mnb_model.pkl")
|
16 |
+
|
17 |
+
TAG_RE = re.compile(r'<[^>]+>')
|
18 |
+
|
19 |
+
def remove_tags(text):
|
20 |
+
return TAG_RE.sub('', text)
|
21 |
+
|
22 |
+
# Load other necessary files like vectorizers or scalers
|
23 |
+
tfidf_vectorizer = joblib.load("tfidf_vectorizer.pkl")
|
24 |
+
|
25 |
+
def preprocess_text(sen):
|
26 |
+
# Removing html tags
|
27 |
+
sentence = remove_tags(sen)
|
28 |
+
|
29 |
+
# Remove punctuations and numbers
|
30 |
+
sentence = re.sub('[^a-zA-Z]', ' ', sentence)
|
31 |
+
|
32 |
+
# Single character removal
|
33 |
+
sentence = re.sub(r"\s+[a-zA-Z]\s+", ' ', sentence)
|
34 |
+
|
35 |
+
# Removing multiple spaces
|
36 |
+
sentence = re.sub(r'\s+', ' ', sentence)
|
37 |
+
|
38 |
+
return sentence
|
39 |
+
|
40 |
+
# Function to predict sentiment using Logistic Regression
|
41 |
+
# Function to predict sentiment using Logistic Regression
|
42 |
+
def predict_lr(text):
|
43 |
+
preprocessed_text = preprocess_text(text)
|
44 |
+
vectorized_text = tfidf_vectorizer.transform([preprocessed_text])
|
45 |
+
dense_vectorized_text = vectorized_text.toarray() # Convert to dense array
|
46 |
+
prediction = int(lr_model.predict(dense_vectorized_text)[0])
|
47 |
+
return prediction
|
48 |
+
|
49 |
+
|
50 |
+
# Function to predict sentiment using SVM
|
51 |
+
def predict_svm(text):
|
52 |
+
preprocessed_text = preprocess_text(text)
|
53 |
+
vectorized_text = tfidf_vectorizer.transform([preprocessed_text])
|
54 |
+
dense_vectorized_text = vectorized_text.toarray() # Convert to dense array
|
55 |
+
prediction = int(svm_model.predict(dense_vectorized_text)[0])
|
56 |
+
return prediction
|
57 |
+
|
58 |
+
# Function to predict sentiment using Neural Network
|
59 |
+
def predict_nn(text):
|
60 |
+
preprocessed_text = preprocess_text(text)
|
61 |
+
vectorized_text = tfidf_vectorizer.transform([preprocessed_text])
|
62 |
+
dense_vectorized_text = vectorized_text.toarray() # Convert to dense array
|
63 |
+
|
64 |
+
prediction_probs = nn_model.predict(dense_vectorized_text)[0]
|
65 |
+
prediction = int(np.argmax(prediction_probs))
|
66 |
+
return prediction
|
67 |
+
|
68 |
+
# Function to predict sentiment using Multinomial Naive Bayes
|
69 |
+
def predict_mnb(text):
|
70 |
+
preprocessed_text = preprocess_text(text)
|
71 |
+
vectorized_text = tfidf_vectorizer.transform([preprocessed_text])
|
72 |
+
dense_vectorized_text = vectorized_text.toarray() # Convert to dense array
|
73 |
+
prediction = int(mnb_model.predict(dense_vectorized_text)[0])
|
74 |
+
return prediction
|
75 |
+
|
76 |
+
# Set the function based on the selected model
|
77 |
+
def sentiment_prediction(text, model):
|
78 |
+
if model == "Logistic Regression":
|
79 |
+
prediction = predict_lr(text)
|
80 |
+
elif model == "SVM":
|
81 |
+
prediction = predict_svm(text)
|
82 |
+
elif model == "Neural Network":
|
83 |
+
prediction = predict_nn(text)
|
84 |
+
elif model == "Multinomial Naive Bayes":
|
85 |
+
prediction = predict_mnb(text)
|
86 |
+
threshold = 0.5
|
87 |
+
|
88 |
+
# Logic for determining positive or negative sentiment based on the model's prediction
|
89 |
+
if prediction >= threshold:
|
90 |
+
return "Positive"
|
91 |
+
else:
|
92 |
+
return "Negative"
|
93 |
+
# Create the Gradio interface
|
94 |
+
iface = gr.Interface(
|
95 |
+
fn=sentiment_prediction,
|
96 |
+
inputs=[gr.Textbox(type="text", label="Enter Text"), gr.Dropdown(["Logistic Regression", "SVM", "Neural Network", "Multinomial Naive Bayes"], label="Select Model")],
|
97 |
+
outputs=gr.Label(),
|
98 |
+
live=True,
|
99 |
+
title="Sentiment Analysis with Model Selection",
|
100 |
+
description="Enter a text and choose a model for sentiment prediction.",
|
101 |
+
)
|
102 |
+
|
103 |
+
# Launch the Gradio interface
|
104 |
+
iface.launch()
|
105 |
+
|
106 |
+
|
107 |
+
iface.launch()
|
dl_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:25b02d3dddb9af940fb51c0b2496a1e5d06ce76041fe0ea337aeba17705bd189
|
3 |
+
size 795072
|
logistic_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5453e622e1018c776ce9f829efe6a8d705f635920c8d07acc67d932c0b9f7834
|
3 |
+
size 19642619
|
mnb_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5453e622e1018c776ce9f829efe6a8d705f635920c8d07acc67d932c0b9f7834
|
3 |
+
size 19642619
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
scikit-learn==1.2.2
|
3 |
+
numpy
|
4 |
+
nltk
|
5 |
+
tensorflow==2.15.0
|
6 |
+
joblib==1.3.2
|
svm_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5453e622e1018c776ce9f829efe6a8d705f635920c8d07acc67d932c0b9f7834
|
3 |
+
size 19642619
|
tfidf_vectorizer.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1189be4fec99140b4e536076a439f6d71a8dde51ca8071676e78bda39fa01331
|
3 |
+
size 1122210
|