File size: 2,341 Bytes
e8c8b7d 624d086 e8c8b7d 624d086 e8c8b7d 624d086 e8c8b7d |
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 |
import gradio as gr
import joblib
# Load models
models = {
"Logistic Regression": joblib.load("models/best_model.joblib"),
"Random Forest": joblib.load("models/random_forest_model.joblib"),
"KNN": joblib.load("models/trained_knn_model.joblib"),
}
# Load vectorizer
vectorizer = joblib.load("models/vectorizer.joblib")
# Define prediction function
def predict_sentiment(review, model_name):
# Transform the review text using the vectorizer
processed_review = vectorizer.transform([review])
# Select the model
model = models[model_name]
# Make predictions
predicted_class = model.predict(processed_review)[0]
probabilities = model.predict_proba(processed_review)[0]
# Define sentiment labels
sentiment_labels = ["Negative Comment", "Positive Comment"]
predicted_label = sentiment_labels[predicted_class]
# Return probabilities as percentages
positive_percentage = probabilities[1] * 100
negative_percentage = probabilities[0] * 100
return predicted_label, positive_percentage, negative_percentage
# Build Gradio interface
with gr.Blocks() as interface:
gr.Markdown("<h1>Text Classification Models</h1>")
gr.Markdown("Choose a model and provide a review to see the sentiment analysis results with probabilities displayed as scales.")
with gr.Row():
with gr.Column():
review_input = gr.Textbox(label="Review Comment", placeholder="Type your comment here...")
model_selector = gr.Dropdown(
choices=list(models.keys()), label="Select Model", value="Logistic Regression"
)
submit_button = gr.Button("Submit")
with gr.Column():
sentiment_output = gr.Textbox(label="Predicted Sentiment Class", interactive=False)
positive_progress = gr.Slider(label="Positive Comment Percentage", minimum=0, maximum=100, interactive=False)
negative_progress = gr.Slider(label="Negative Comment Percentage", minimum=0, maximum=100, interactive=False)
submit_button.click(
predict_sentiment,
inputs=[review_input, model_selector],
outputs=[sentiment_output, positive_progress, negative_progress],
)
# Launch the app
if __name__ == "__main__":
interface.launch()
|