khadija3818 commited on
Commit
29da466
1 Parent(s): c0c7da3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -11
app.py CHANGED
@@ -1,21 +1,55 @@
1
  import streamlit as st
2
- from transformers import pipeline
 
 
 
 
3
 
4
- def detect_emotion(sentence):
5
- emotion_analyzer = pipeline("text-classification")
6
- result = emotion_analyzer(sentence)
7
- emotion = result[0]['label']
8
- return emotion
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # Streamlit UI
11
  st.title("Emotion Detection App")
12
 
13
  # User input
14
- sentence = st.text_area("Enter a sentence:")
15
- if not sentence:
16
- st.warning("Please enter a sentence.")
17
 
18
  # Emotion detection
19
  if st.button("Detect Emotion"):
20
- emotion = detect_emotion(sentence)
21
- st.success(f"Emotion detected: {emotion.capitalize()}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import pandas as pd
3
+ from sklearn.model_selection import train_test_split
4
+ from sklearn.feature_extraction.text import CountVectorizer
5
+ from sklearn.naive_bayes import MultinomialNB
6
+ from sklearn.metrics import accuracy_score, classification_report
7
 
8
+ # Load the dataset
9
+ @st.cache
10
+ def load_data():
11
+ df = pd.read_csv("tweet_emotions.csv")
12
+ return df
13
+
14
+ df = load_data()
15
+
16
+ # Train a Naive Bayes classifier
17
+ @st.cache
18
+ def train_classifier(data):
19
+ vectorizer = CountVectorizer()
20
+ X = vectorizer.fit_transform(data['content'])
21
+ y = data['sentiment']
22
+
23
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
24
+
25
+ naive_bayes_model = MultinomialNB()
26
+ naive_bayes_model.fit(X_train, y_train)
27
+
28
+ return naive_bayes_model, vectorizer, X_test, y_test
29
+
30
+ naive_bayes_model, vectorizer, X_test, y_test = train_classifier(df)
31
 
32
  # Streamlit UI
33
  st.title("Emotion Detection App")
34
 
35
  # User input
36
+ tweet = st.text_area("Enter a tweet:")
 
 
37
 
38
  # Emotion detection
39
  if st.button("Detect Emotion"):
40
+ tweet_vectorized = vectorizer.transform([tweet])
41
+ prediction = naive_bayes_model.predict(tweet_vectorized)
42
+ st.success(f"Predicted Emotion: {prediction[0]}")
43
+
44
+ # Display the dataset
45
+ st.subheader("Dataset Preview:")
46
+ st.write(df.head())
47
+
48
+ # Model evaluation
49
+ st.subheader("Model Evaluation:")
50
+ y_pred = naive_bayes_model.predict(X_test)
51
+ accuracy = accuracy_score(y_test, y_pred)
52
+ classification_rep = classification_report(y_test, y_pred)
53
+
54
+ st.write(f"Accuracy: {accuracy:.4f}")
55
+ st.write("Classification Report:\n", classification_rep)