import streamlit as st import numpy as np import matplotlib.pyplot as plt from sklearn.metrics import precision_recall_curve, auc # Sidebar navigation st.sidebar.title("App Navigation") page = st.sidebar.radio("Choose a feature", ["Sentiment Analysis", "Model Evaluation"]) # Sentiment Analysis Page if page == "Sentiment Analysis": # Set page title st.title("Twitter Sentiment Analysis App") # Load sentiment analysis pipeline from transformers import pipeline sentiment_pipe = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest") # Input box for user to enter a tweet user_input = st.text_input("Enter a tweet to analyze:") if user_input: # Run sentiment analysis result = sentiment_pipe(user_input) st.write("Sentiment Analysis Result:", result) # Model Evaluation Page elif page == "Model Evaluation": st.title("Model Precision-Recall Evaluation") # Initialize default values for y_true and y_score y_true, y_score = None, None # Option to use actual model predictions st.write("### Evaluate Using Actual Model") model_type = st.selectbox("Choose model type:", ["Scikit-learn", "Transformers"]) if model_type == "Scikit-learn": st.write("### Scikit-learn Model") from sklearn.ensemble import RandomForestClassifier from sklearn.datasets import make_classification # Create and train a Scikit-learn model X_train, y_train = make_classification(n_samples=1000, n_features=20, random_state=42) X_test, y_test = make_classification(n_samples=200, n_features=20, random_state=42) model = RandomForestClassifier() model.fit(X_train, y_train) # Generate predictions y_score = model.predict_proba(X_test)[:, 1] # Predicted probabilities for the positive class y_true = y_test # True labels elif model_type == "Transformers": st.write("### Transformers Model") from transformers import pipeline # Load a Transformers model model = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest") # Simulate a batch of tweets for evaluation tweets = ["I love this!", "This is bad...", "I'm not sure about this."] results = model(tweets) # Simulated true labels (manually defined for simplicity) y_true = [1, 0, 1] # Manually defined true labels y_score = [res["score"] for res in results] # Extract probabilities # Validate data and calculate Precision-Recall curve if y_true is not None and y_score is not None and len(y_true) > 0 and len(y_score) > 0: try: # Calculate Precision, Recall, and AUC precision, recall, _ = precision_recall_curve(y_true, y_score) pr_auc = auc(recall, precision) # Plot the PR curve fig, ax = plt.subplots() ax.plot(recall, precision, label=f"PR Curve (AUC = {pr_auc:.2f})") ax.set_xlabel("Recall") ax.set_ylabel("Precision") ax.set_title("Precision-Recall Curve") ax.legend(loc="best") ax.grid() # Display the plot st.pyplot(fig) except Exception as e: st.error(f"An error occurred while generating the PR curve: {e}") else: st.info("Please select a model and ensure it generates valid data.")