Spaces:
Sleeping
Sleeping
File size: 1,575 Bytes
a298a35 29da466 a298a35 29da466 a298a35 c0c7da3 a298a35 29da466 a298a35 29da466 |
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 |
import streamlit as st
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, classification_report
# Load the dataset
@st.cache
def load_data():
df = pd.read_csv("tweet_emotions.csv")
return df
df = load_data()
# Train a Naive Bayes classifier
@st.cache
def train_classifier(data):
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(data['content'])
y = data['sentiment']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
naive_bayes_model = MultinomialNB()
naive_bayes_model.fit(X_train, y_train)
return naive_bayes_model, vectorizer, X_test, y_test
naive_bayes_model, vectorizer, X_test, y_test = train_classifier(df)
# Streamlit UI
st.title("Emotion Detection App")
# User input
tweet = st.text_area("Enter a tweet:")
# Emotion detection
if st.button("Detect Emotion"):
tweet_vectorized = vectorizer.transform([tweet])
prediction = naive_bayes_model.predict(tweet_vectorized)
st.success(f"Predicted Emotion: {prediction[0]}")
# Display the dataset
st.subheader("Dataset Preview:")
st.write(df.head())
# Model evaluation
st.subheader("Model Evaluation:")
y_pred = naive_bayes_model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
classification_rep = classification_report(y_test, y_pred)
st.write(f"Accuracy: {accuracy:.4f}")
st.write("Classification Report:\n", classification_rep)
|