Spaces:
Sleeping
Sleeping
| 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() | |