Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import joblib
|
|
| 3 |
import numpy as np
|
| 4 |
from sklearn.feature_extraction.text import CountVectorizer
|
| 5 |
|
| 6 |
-
# Load models (
|
| 7 |
logreg_model = joblib.load('best_lr_model.pkl')
|
| 8 |
knn_model = joblib.load('best_knn_model.pkl')
|
| 9 |
svc_model = joblib.load('best_svc_model.pkl') # Ensure this model has 'probability=True' if needed
|
|
@@ -15,7 +15,7 @@ vectorizer = joblib.load('vectorizer.pkl')
|
|
| 15 |
# Define the function to predict sentiment
|
| 16 |
def predict_sentiment(review_text, model_name):
|
| 17 |
# Preprocess the review text using the same vectorizer used during training
|
| 18 |
-
transformed_text = vectorizer.transform([review_text]) #
|
| 19 |
|
| 20 |
# Convert sparse matrix to dense (necessary for SVM)
|
| 21 |
transformed_text_dense = transformed_text.toarray() # Convert sparse matrix to dense array
|
|
@@ -46,24 +46,19 @@ def predict_sentiment(review_text, model_name):
|
|
| 46 |
|
| 47 |
# Step 6.4: Put all components together in Gradio's interface
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
output_probability = gr.Textbox(label="Predicted Probability", placeholder="Probabilities will appear here...", elem_id="probability-output")
|
| 61 |
-
|
| 62 |
# Define the action when the button is clicked
|
| 63 |
submit_button.click(fn=predict_sentiment, inputs=[review_input, model_dropdown], outputs=[output_sentiment, output_probability])
|
| 64 |
|
| 65 |
-
# Reset inputs when clear button is clicked
|
| 66 |
-
clear_button.click(fn=lambda: ("", "", ""), inputs=[], outputs=[review_input, output_sentiment, output_probability])
|
| 67 |
-
|
| 68 |
# Launch the interface
|
| 69 |
demo.launch()
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from sklearn.feature_extraction.text import CountVectorizer
|
| 5 |
|
| 6 |
+
# Load models (change paths to where your .pkl files are stored)
|
| 7 |
logreg_model = joblib.load('best_lr_model.pkl')
|
| 8 |
knn_model = joblib.load('best_knn_model.pkl')
|
| 9 |
svc_model = joblib.load('best_svc_model.pkl') # Ensure this model has 'probability=True' if needed
|
|
|
|
| 15 |
# Define the function to predict sentiment
|
| 16 |
def predict_sentiment(review_text, model_name):
|
| 17 |
# Preprocess the review text using the same vectorizer used during training
|
| 18 |
+
transformed_text = vectorizer.transform([review_text]) # This is sparse matrix
|
| 19 |
|
| 20 |
# Convert sparse matrix to dense (necessary for SVM)
|
| 21 |
transformed_text_dense = transformed_text.toarray() # Convert sparse matrix to dense array
|
|
|
|
| 46 |
|
| 47 |
# Step 6.4: Put all components together in Gradio's interface
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
+
# Input components
|
| 50 |
+
review_input = gr.Textbox(label="Review Comment", lines=2, placeholder="Enter your review comment here...")
|
| 51 |
+
model_dropdown = gr.Dropdown(choices=["Logistic Regression", "K-Nearest Neighbors", "Random Forest", "SVM"], label="Select Model")
|
| 52 |
+
|
| 53 |
+
# Output components
|
| 54 |
+
output_sentiment = gr.Textbox(label="Predicted Sentiment Class", placeholder="Predicted sentiment will appear here...")
|
| 55 |
+
output_probability = gr.Textbox(label="Predicted Probability", placeholder="Probabilities will appear here...")
|
| 56 |
+
|
| 57 |
+
# Submit Button
|
| 58 |
+
submit_button = gr.Button("Submit")
|
| 59 |
+
|
|
|
|
|
|
|
| 60 |
# Define the action when the button is clicked
|
| 61 |
submit_button.click(fn=predict_sentiment, inputs=[review_input, model_dropdown], outputs=[output_sentiment, output_probability])
|
| 62 |
|
|
|
|
|
|
|
|
|
|
| 63 |
# Launch the interface
|
| 64 |
demo.launch()
|