Update app.py
Browse files
app.py
CHANGED
@@ -2,68 +2,64 @@ import gradio as gr
|
|
2 |
import pickle
|
3 |
import numpy as np
|
4 |
|
5 |
-
# Load all the
|
6 |
-
with open(
|
7 |
-
random_forest_model = pickle.load(
|
8 |
|
9 |
-
with open(
|
10 |
-
logistic_model = pickle.load(
|
11 |
|
12 |
-
with open(
|
13 |
-
knn_yelp_model = pickle.load(
|
14 |
|
15 |
-
with open(
|
16 |
-
svm_linear = pickle.load(
|
17 |
|
18 |
-
with open(
|
19 |
-
svm_poly = pickle.load(
|
20 |
|
21 |
-
with open(
|
22 |
-
svm_rbf = pickle.load(
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
"Random Forest": random_forest_model,
|
27 |
-
"Logistic Regression": logistic_model,
|
28 |
-
"KNN": knn_yelp_model,
|
29 |
-
"SVM Linear": svm_linear,
|
30 |
-
"SVM Polynomial": svm_poly,
|
31 |
-
"SVM RBF": svm_rbf
|
32 |
-
}
|
33 |
|
34 |
# Function to predict class probabilities
|
35 |
-
def
|
36 |
-
#
|
37 |
-
|
38 |
-
with open('vectorizer.pkl', 'rb') as file:
|
39 |
-
vectorizer = pickle.load(file)
|
40 |
-
|
41 |
-
# Transform the review text into a vector
|
42 |
-
review_vector = vectorizer.transform([review_text])
|
43 |
|
44 |
# Select the model based on user input
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
return prob
|
51 |
-
else:
|
52 |
-
return "Model not found."
|
53 |
|
54 |
-
# Gradio
|
55 |
-
|
56 |
-
fn=
|
57 |
inputs=[
|
58 |
-
gr.Textbox(label="Enter
|
59 |
gr.Dropdown(
|
60 |
-
|
61 |
-
|
62 |
-
)
|
63 |
],
|
64 |
-
outputs=
|
65 |
live=True
|
66 |
)
|
67 |
|
68 |
# Launch the interface
|
69 |
-
|
|
|
2 |
import pickle
|
3 |
import numpy as np
|
4 |
|
5 |
+
# Load all models and the vectorizer
|
6 |
+
with open("random_forest_model.pkl", "rb") as f:
|
7 |
+
random_forest_model = pickle.load(f)
|
8 |
|
9 |
+
with open("logistic_model.pkl", "rb") as f:
|
10 |
+
logistic_model = pickle.load(f)
|
11 |
|
12 |
+
with open("knn_yelp_model.pkl", "rb") as f:
|
13 |
+
knn_yelp_model = pickle.load(f)
|
14 |
|
15 |
+
with open("svm_linear.pkl", "rb") as f:
|
16 |
+
svm_linear = pickle.load(f)
|
17 |
|
18 |
+
with open("svm_poly.pkl", "rb") as f:
|
19 |
+
svm_poly = pickle.load(f)
|
20 |
|
21 |
+
with open("svm_rbf.pkl", "rb") as f:
|
22 |
+
svm_rbf = pickle.load(f)
|
23 |
|
24 |
+
with open("vectorizer.pkl", "rb") as f:
|
25 |
+
vectorizer = pickle.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Function to predict class probabilities
|
28 |
+
def predict_sentiment(review, model_name):
|
29 |
+
# Vectorize the input review
|
30 |
+
review_vec = vectorizer.transform([review])
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# Select the model based on user input
|
33 |
+
if model_name == 'Random Forest':
|
34 |
+
model = random_forest_model
|
35 |
+
elif model_name == 'Logistic Regression':
|
36 |
+
model = logistic_model
|
37 |
+
elif model_name == 'KNN':
|
38 |
+
model = knn_yelp_model
|
39 |
+
elif model_name == 'SVM Linear':
|
40 |
+
model = svm_linear
|
41 |
+
elif model_name == 'SVM Poly':
|
42 |
+
model = svm_poly
|
43 |
+
elif model_name == 'SVM RBF':
|
44 |
+
model = svm_rbf
|
45 |
|
46 |
+
# Predict probabilities for each class (assuming binary classification)
|
47 |
+
probabilities = model.predict_proba(review_vec)[0]
|
48 |
+
return {"Negative": probabilities[0], "Positive": probabilities[1]}
|
|
|
|
|
|
|
49 |
|
50 |
+
# Set up the Gradio interface
|
51 |
+
iface = gr.Interface(
|
52 |
+
fn=predict_sentiment,
|
53 |
inputs=[
|
54 |
+
gr.Textbox(label="Enter Your Review"),
|
55 |
gr.Dropdown(
|
56 |
+
choices=["Random Forest", "Logistic Regression", "KNN", "SVM Linear", "SVM Poly", "SVM RBF"],
|
57 |
+
label="Select Model"
|
58 |
+
),
|
59 |
],
|
60 |
+
outputs=gr.Label(num_top_classes=2),
|
61 |
live=True
|
62 |
)
|
63 |
|
64 |
# Launch the interface
|
65 |
+
iface.launch()
|