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("