Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,22 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
-
from joblib import load
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
logreg_model = load('best_lr_model.pkl')
|
| 7 |
-
knn_model = load('best_knn_model.pkl')
|
| 8 |
-
svc_model = load('best_svc_model.pkl')
|
| 9 |
-
rf_model = load('best_rf_model.pkl')
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
review_input = gr.Textbox(label="Review Comment", lines=2, placeholder="Enter your review comment here...")
|
| 15 |
-
|
| 16 |
-
# b. define dropdown input for model selection (mimicking the dropdown for models like Logistic Regression, K-Nearest Neighbors, etc.)
|
| 17 |
-
model_dropdown = gr.Dropdown(
|
| 18 |
-
choices=["Logistic Regression", "K-Nearest Neighbors", "Random Forest", "SVM"],
|
| 19 |
-
label="Select Model"
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
# Step 6.3: Define output component for predicted sentiment class and probability
|
| 23 |
-
output_sentiment = gr.Textbox(label="Predicted Sentiment Class", placeholder="Predicted sentiment will appear here...")
|
| 24 |
-
output_probability = gr.Textbox(label="Predicted Probability", placeholder="Probabilities will appear here...")
|
| 25 |
-
|
| 26 |
-
# Step 6.4: Define a function to predict sentiment using the selected model
|
| 27 |
def predict_sentiment(review_text, model_name):
|
| 28 |
-
# Preprocess the
|
| 29 |
-
|
| 30 |
-
# Here I'm assuming you've preprocessed the input text in the same way you did during training
|
| 31 |
|
| 32 |
-
# Dummy text transformation for demonstration purposes
|
| 33 |
-
# Replace with actual vectorizer code (e.g., vectorizer.transform([review_text])) if needed
|
| 34 |
-
transformed_text = np.array([review_text]) # Example transformation
|
| 35 |
-
|
| 36 |
-
# Choose the model based on the selected dropdown
|
| 37 |
if model_name == "Logistic Regression":
|
| 38 |
model = logreg_model
|
| 39 |
elif model_name == "K-Nearest Neighbors":
|
|
@@ -43,19 +26,31 @@ def predict_sentiment(review_text, model_name):
|
|
| 43 |
elif model_name == "SVM":
|
| 44 |
model = svc_model
|
| 45 |
|
| 46 |
-
#
|
| 47 |
prob = model.predict_proba(transformed_text)
|
| 48 |
sentiment = model.predict(transformed_text)[0]
|
| 49 |
-
|
| 50 |
-
# Return the predicted sentiment and
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
|
| 5 |
+
# Load models (change paths to where your .pkl files are stored)
|
| 6 |
+
logreg_model = joblib.load('best_lr_model.pkl')
|
| 7 |
+
knn_model = joblib.load('best_knn_model.pkl')
|
| 8 |
+
svc_model = joblib.load('best_svc_model.pkl')
|
| 9 |
+
rf_model = joblib.load('best_rf_model.pkl')
|
| 10 |
|
| 11 |
+
# Preprocessing: Load the same vectorizer you used during training
|
| 12 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
| 13 |
+
vectorizer = joblib.load('vectorizer.pkl')
|
| 14 |
|
| 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]) # Transform the input review text
|
|
|
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
if model_name == "Logistic Regression":
|
| 21 |
model = logreg_model
|
| 22 |
elif model_name == "K-Nearest Neighbors":
|
|
|
|
| 26 |
elif model_name == "SVM":
|
| 27 |
model = svc_model
|
| 28 |
|
| 29 |
+
# Make predictions and get probabilities
|
| 30 |
prob = model.predict_proba(transformed_text)
|
| 31 |
sentiment = model.predict(transformed_text)[0]
|
| 32 |
+
|
| 33 |
+
# Return the predicted sentiment class and probabilities
|
| 34 |
+
sentiment_class = "Positive" if sentiment == 1 else "Negative"
|
| 35 |
+
probability = f"Positive Comment: {prob[0][1]*100:.2f}%, Negative Comment: {prob[0][0]*100:.2f}%"
|
| 36 |
+
|
| 37 |
+
return sentiment_class, probability
|
| 38 |
+
|
| 39 |
+
# Step 6.4: Put all components together in Gradio's interface
|
| 40 |
+
with gr.Blocks() as demo:
|
| 41 |
+
# Input components
|
| 42 |
+
review_input = gr.Textbox(label="Review Comment", lines=2, placeholder="Enter your review comment here...")
|
| 43 |
+
model_dropdown = gr.Dropdown(choices=["Logistic Regression", "K-Nearest Neighbors", "Random Forest", "SVM"], label="Select Model")
|
| 44 |
+
|
| 45 |
+
# Output components
|
| 46 |
+
output_sentiment = gr.Textbox(label="Predicted Sentiment Class", placeholder="Predicted sentiment will appear here...")
|
| 47 |
+
output_probability = gr.Textbox(label="Predicted Probability", placeholder="Probabilities will appear here...")
|
| 48 |
+
|
| 49 |
+
# Submit Button
|
| 50 |
+
submit_button = gr.Button("Submit")
|
| 51 |
+
|
| 52 |
+
# Define the action when the button is clicked
|
| 53 |
+
submit_button.click(fn=predict_sentiment, inputs=[review_input, model_dropdown], outputs=[output_sentiment, output_probability])
|
| 54 |
+
|
| 55 |
+
# Launch the interface
|
| 56 |
+
demo.launch()
|