Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import joblib
|
| 3 |
import numpy as np
|
| 4 |
-
from
|
| 5 |
-
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score
|
| 6 |
-
from sklearn.model_selection import train_test_split
|
| 7 |
-
from sklearn.svm import SVC
|
| 8 |
-
from sklearn.neighbors import KNeighborsClassifier
|
| 9 |
-
from sklearn.linear_model import LogisticRegression
|
| 10 |
-
from sklearn.ensemble import RandomForestClassifier
|
| 11 |
|
| 12 |
-
# Load your
|
| 13 |
-
logreg_model =
|
| 14 |
-
knn_model =
|
| 15 |
-
svc_model =
|
| 16 |
-
rf_model =
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
vectorizer = joblib.load('vectorizer.pkl') # You must upload this file as well
|
| 20 |
|
| 21 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def predict_sentiment(review_text, model_name):
|
| 23 |
-
# Preprocess the
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
if model_name == "Logistic Regression":
|
| 29 |
model = logreg_model
|
| 30 |
elif model_name == "K-Nearest Neighbors":
|
|
@@ -33,40 +42,20 @@ def predict_sentiment(review_text, model_name):
|
|
| 33 |
model = svc_model
|
| 34 |
elif model_name == "Random Forest":
|
| 35 |
model = rf_model
|
| 36 |
-
|
| 37 |
-
# Use the model to predict sentiment probabilities
|
| 38 |
-
prob = model.predict_proba(review_features) # Use predict_proba to get probabilities
|
| 39 |
-
|
| 40 |
-
# Return positive and negative sentiment probabilities
|
| 41 |
-
return {"Positive Comment": prob[0][1], "Negative Comment": prob[0][0]}
|
| 42 |
-
|
| 43 |
-
# Gradio Interface setup
|
| 44 |
-
def create_interface():
|
| 45 |
-
# Create a dropdown for model selection
|
| 46 |
-
model_dropdown = gr.Dropdown(
|
| 47 |
-
choices=["Logistic Regression", "K-Nearest Neighbors", "Support Vector Machine", "Random Forest"],
|
| 48 |
-
label="Select Model"
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
# Create a textbox for the review input
|
| 52 |
-
review_input = gr.Textbox(
|
| 53 |
-
lines=2, placeholder="Enter your review comment here...", label="Review Comment"
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
# Create a JSON output to display predicted sentiment and probabilities
|
| 57 |
-
output = gr.JSON()
|
| 58 |
-
|
| 59 |
-
# Create the Gradio interface
|
| 60 |
-
interface = gr.Interface(
|
| 61 |
-
fn=predict_sentiment,
|
| 62 |
-
inputs=[review_input, model_dropdown],
|
| 63 |
-
outputs=output,
|
| 64 |
-
live=True, # Optionally update in real-time
|
| 65 |
-
description="Sentiment Analysis Model",
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
return interface
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
from joblib import load
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Step 6.1: Load your trained models
|
| 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 |
+
# Step 6.2: Define different input components
|
|
|
|
| 12 |
|
| 13 |
+
# a. define textbox input for review comment (as per your example)
|
| 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 NaiveBayes, Logistic Regression, etc.)
|
| 17 |
+
model_dropdown = gr.Dropdown(
|
| 18 |
+
choices=["Logistic Regression", "K-Nearest Neighbors", "Support Vector Machine", "Random Forest"],
|
| 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 review_text using vectorizer (replace with your preprocessing logic)
|
| 29 |
+
# For example, use TfidfVectorizer or CountVectorizer as needed
|
| 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":
|
|
|
|
| 42 |
model = svc_model
|
| 43 |
elif model_name == "Random Forest":
|
| 44 |
model = rf_model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Make prediction using the model
|
| 47 |
+
prob = model.predict_proba(transformed_text) # Assuming models use `predict_proba`
|
| 48 |
+
sentiment = model.predict(transformed_text)[0] # Get the predicted class
|
| 49 |
+
|
| 50 |
+
# Return the predicted sentiment and the probabilities
|
| 51 |
+
return sentiment, f"Positive: {prob[0][1]*100:.2f}%, Negative: {prob[0][0]*100:.2f}%"
|
| 52 |
+
|
| 53 |
+
# Step 6.5: Put all components together in Gradio's interface
|
| 54 |
+
gr.Interface(
|
| 55 |
+
fn=predict_sentiment,
|
| 56 |
+
inputs=[review_input, model_dropdown],
|
| 57 |
+
outputs=[output_sentiment, output_probability],
|
| 58 |
+
live=True, # Enable live updates as the user types
|
| 59 |
+
description="Sentiment Analysis Model",
|
| 60 |
+
theme="compact" # Use a more compact layout for the interface
|
| 61 |
+
).launch()
|