Upload app.py
Browse files
app.py
CHANGED
@@ -5,37 +5,58 @@ import joblib
|
|
5 |
models = {
|
6 |
"Logistic Regression": joblib.load("models/best_model.joblib"),
|
7 |
"Random Forest": joblib.load("models/random_forest_model.joblib"),
|
8 |
-
"SVM (Linear)": joblib.load("models/svm_model_linear.joblib"),
|
9 |
-
"SVM (Polynomial)": joblib.load("models/svm_model_polynomial.joblib"),
|
10 |
-
"SVM (RBF)": joblib.load("models/svm_model_rbf.joblib"),
|
11 |
"KNN": joblib.load("models/trained_knn_model.joblib"),
|
12 |
}
|
13 |
|
|
|
|
|
|
|
14 |
# Define prediction function
|
15 |
-
def
|
|
|
|
|
|
|
|
|
16 |
model = models[model_name]
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
)
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
if __name__ == "__main__":
|
41 |
interface.launch()
|
|
|
5 |
models = {
|
6 |
"Logistic Regression": joblib.load("models/best_model.joblib"),
|
7 |
"Random Forest": joblib.load("models/random_forest_model.joblib"),
|
|
|
|
|
|
|
8 |
"KNN": joblib.load("models/trained_knn_model.joblib"),
|
9 |
}
|
10 |
|
11 |
+
# Load vectorizer
|
12 |
+
vectorizer = joblib.load("models/vectorizer.joblib")
|
13 |
+
|
14 |
# Define prediction function
|
15 |
+
def predict_sentiment(review, model_name):
|
16 |
+
# Transform the review text using the vectorizer
|
17 |
+
processed_review = vectorizer.transform([review])
|
18 |
+
|
19 |
+
# Select the model
|
20 |
model = models[model_name]
|
21 |
+
|
22 |
+
# Make predictions
|
23 |
+
predicted_class = model.predict(processed_review)[0]
|
24 |
+
probabilities = model.predict_proba(processed_review)[0]
|
25 |
+
|
26 |
+
# Define sentiment labels
|
27 |
+
sentiment_labels = ["Negative Comment", "Positive Comment"]
|
28 |
+
predicted_label = sentiment_labels[predicted_class]
|
29 |
+
|
30 |
+
# Return probabilities as percentages
|
31 |
+
positive_percentage = probabilities[1] * 100
|
32 |
+
negative_percentage = probabilities[0] * 100
|
33 |
+
|
34 |
+
return predicted_label, positive_percentage, negative_percentage
|
35 |
+
|
36 |
+
# Build Gradio interface
|
37 |
+
with gr.Blocks() as interface:
|
38 |
+
gr.Markdown("<h1>Text Classification Models</h1>")
|
39 |
+
gr.Markdown("Choose a model and provide a review to see the sentiment analysis results with probabilities displayed as scales.")
|
40 |
+
|
41 |
+
with gr.Row():
|
42 |
+
with gr.Column():
|
43 |
+
review_input = gr.Textbox(label="Review Comment", placeholder="Type your comment here...")
|
44 |
+
model_selector = gr.Dropdown(
|
45 |
+
choices=list(models.keys()), label="Select Model", value="Logistic Regression"
|
46 |
+
)
|
47 |
+
submit_button = gr.Button("Submit")
|
48 |
+
|
49 |
+
with gr.Column():
|
50 |
+
sentiment_output = gr.Textbox(label="Predicted Sentiment Class", interactive=False)
|
51 |
+
positive_progress = gr.Slider(label="Positive Comment Percentage", minimum=0, maximum=100, interactive=False)
|
52 |
+
negative_progress = gr.Slider(label="Negative Comment Percentage", minimum=0, maximum=100, interactive=False)
|
53 |
+
|
54 |
+
submit_button.click(
|
55 |
+
predict_sentiment,
|
56 |
+
inputs=[review_input, model_selector],
|
57 |
+
outputs=[sentiment_output, positive_progress, negative_progress],
|
58 |
+
)
|
59 |
+
|
60 |
+
# Launch the app
|
61 |
if __name__ == "__main__":
|
62 |
interface.launch()
|