|
import streamlit as st |
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
from sklearn.metrics import precision_recall_curve, auc |
|
|
|
|
|
st.sidebar.title("App Navigation") |
|
page = st.sidebar.radio("Choose a feature", ["Sentiment Analysis", "Model Evaluation"]) |
|
|
|
|
|
if page == "Sentiment Analysis": |
|
|
|
st.title("Twitter Sentiment Analysis App") |
|
|
|
|
|
from transformers import pipeline |
|
sentiment_pipe = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest") |
|
|
|
|
|
user_input = st.text_input("Enter a tweet to analyze:") |
|
|
|
if user_input: |
|
|
|
result = sentiment_pipe(user_input) |
|
st.write("Sentiment Analysis Result:", result) |
|
|
|
|
|
elif page == "Model Evaluation": |
|
st.title("Model Precision-Recall Evaluation") |
|
|
|
|
|
y_true, y_score = None, None |
|
|
|
|
|
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 |
|
|
|
|
|
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) |
|
|
|
|
|
y_score = model.predict_proba(X_test)[:, 1] |
|
y_true = y_test |
|
|
|
elif model_type == "Transformers": |
|
st.write("### Transformers Model") |
|
|
|
from transformers import pipeline |
|
|
|
|
|
model = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest") |
|
|
|
|
|
tweets = ["I love this!", "This is bad...", "I'm not sure about this."] |
|
results = model(tweets) |
|
|
|
|
|
y_true = [1, 0, 1] |
|
y_score = [res["score"] for res in results] |
|
|
|
|
|
if y_true is not None and y_score is not None and len(y_true) > 0 and len(y_score) > 0: |
|
try: |
|
|
|
precision, recall, _ = precision_recall_curve(y_true, y_score) |
|
pr_auc = auc(recall, precision) |
|
|
|
|
|
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() |
|
|
|
|
|
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.") |
|
|