Spaces:
Sleeping
Sleeping
File size: 1,540 Bytes
fe98a14 |
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 |
import pandas as pd
import streamlit as st
import warnings
warnings.filterwarnings("ignore")
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.pipeline import Pipeline
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
# Load and clean data
df = pd.read_csv(r"New_emotions.csv")
print(df.columns)
df.drop_duplicates(inplace=True)
x = df["sentence"]
y = df["emotion"]
# Split data
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=29)
# Build model
model = Pipeline([
("vectorizer", CountVectorizer()),
("classifier", MultinomialNB(alpha=2))
])
model.fit(x, y)
y_predict = model.predict(x_test)
# Streamlit App
st.title("Emotion Detection from Text π")
st.write("Model Accuracy:", accuracy_score(y_test, y_predict))
# User input
sentence = st.text_input("Enter a sentence:")
if st.button("Predict Emotion"):
if sentence:
prediction = model.predict([sentence])[0]
st.write("Predicted Emotion:", prediction)
# Show emoji
emojis = {
"sad": "π’π’",
"love": "β€οΈβ€οΈ",
"surprise": "π¦π¦",
"joy": "ππ",
"anger": "π π ",
"fear": "π¨π¨"
}
st.write("Emoji:", emojis.get(prediction, "π€"))
user_data = pd.DataFrame([[sentence, prediction]], columns=["sentence", "predicted_emotion"])
st.write(user_data)
|