Spaces:
Sleeping
Sleeping
version 1
Browse files- app.py +62 -0
- models/best_model.joblib +3 -0
- models/random_forest_model.joblib +3 -0
- models/trained_knn_model.joblib +3 -0
- models/vectorizer.joblib +3 -0
- requirements.txt +0 -0
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import joblib
|
3 |
+
|
4 |
+
# Load models
|
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()
|
models/best_model.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:32c6f8bb6849d5feac2973b3a7aa0c21aff2f66fe5f099b3f19c6d3eb1e19ed1
|
3 |
+
size 12191
|
models/random_forest_model.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a4e926055ba3fcc2e850ac763a320d485b3339dd417ca9dddef507838e4f8204
|
3 |
+
size 193449
|
models/trained_knn_model.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:cdcbeb9d497a325a93fdcf5aa6f8ec0abd60f4496ecf38931c0406d653e34912
|
3 |
+
size 49052
|
models/vectorizer.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ff59e97a4cb1106ae015d35f8166d6fee0eb6a00cb2adcc4fb91808ff1108f30
|
3 |
+
size 17468
|
requirements.txt
ADDED
Binary file (62 Bytes). View file
|
|