Spaces:
Sleeping
Sleeping
File size: 3,282 Bytes
de25aac 67be84f 9ead773 e16daaf de25aac d372470 67be84f e16daaf 67be84f 3035677 67be84f 4534890 67be84f 4ca93fe 67be84f d372470 4534890 4ca93fe ec19d72 cc1d9d5 e16daaf de25aac 4534890 de25aac 67be84f 4b07367 ec19d72 4ca93fe ec19d72 4ca93fe 4b07367 ec19d72 4b07367 67be84f d372470 67be84f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import gradio as gr
import joblib
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
# Load models (change paths to where your .pkl files are stored)
logreg_model = joblib.load('best_lr_model.pkl')
knn_model = joblib.load('best_knn_model.pkl')
svc_model = joblib.load('best_svc_model.pkl') # Ensure this model has 'probability=True' if needed
rf_model = joblib.load('best_rf_model.pkl')
# Preprocessing: Load the same vectorizer you used during training
vectorizer = joblib.load('vectorizer.pkl')
# Define the function to predict sentiment
def predict_sentiment(review_text, model_name, positive_threshold=0.6):
# Preprocess the review text using the same vectorizer used during training
transformed_text = vectorizer.transform([review_text]) # This is sparse matrix
# Convert sparse matrix to dense (necessary for SVM and other models)
transformed_text_dense = transformed_text.toarray() # Convert sparse matrix to dense array
# Choose model based on the dropdown selection
if model_name == "Logistic Regression":
model = logreg_model
elif model_name == "K-Nearest Neighbors":
model = knn_model
elif model_name == "Random Forest":
model = rf_model
elif model_name == "SVM":
model = svc_model
# Make predictions and get probabilities
if hasattr(model, 'predict_proba'):
prob = model.predict_proba(transformed_text_dense) # Use dense matrix here
sentiment_prob = prob[0][1] # Probability of positive sentiment
sentiment = model.predict(transformed_text_dense)[0]
# Apply threshold to determine sentiment class
if sentiment_prob >= positive_threshold:
sentiment_class = "Positive"
probability = f"Positive Comment: {sentiment_prob*100:.2f}%, Negative Comment: {(1-sentiment_prob)*100:.2f}%"
else:
sentiment_class = "Negative"
probability = f"Positive Comment: {sentiment_prob*100:.2f}%, Negative Comment: {(1-sentiment_prob)*100:.2f}%"
else:
sentiment = model.predict(transformed_text_dense)[0]
probability = "Model does not support probability estimation."
# Return the predicted sentiment class and probabilities
sentiment_class = "Positive" if sentiment == 1 else "Negative"
return sentiment_class, probability
# Step 6.4: Put all components together in Gradio's interface
with gr.Blocks() as demo:
# Input components
review_input = gr.Textbox(label="Review Comment", lines=2, placeholder="Enter your review comment here...")
model_dropdown = gr.Dropdown(choices=["Logistic Regression", "K-Nearest Neighbors", "Random Forest", "SVM"], label="Select Model")
# Output components
output_sentiment = gr.Textbox(label="Predicted Sentiment Class", placeholder="Predicted sentiment will appear here...")
output_probability = gr.Textbox(label="Predicted Probability", placeholder="Probabilities will appear here...")
# Submit Button
submit_button = gr.Button("Submit")
# Define the action when the button is clicked
submit_button.click(fn=predict_sentiment, inputs=[review_input, model_dropdown], outputs=[output_sentiment, output_probability])
# Launch the interface
demo.launch()
|