Durganihantri commited on
Commit
1eb01dd
Β·
verified Β·
1 Parent(s): e1881c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -16
app.py CHANGED
@@ -2,10 +2,9 @@ import streamlit as st
2
  import nltk
3
  import spacy
4
  import matplotlib.pyplot as plt
5
- from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
6
  import random
7
  import subprocess
8
- import torch
9
 
10
  # Ensure spaCy model is installed
11
  try:
@@ -19,14 +18,12 @@ nltk.download("vader_lexicon")
19
  from nltk.sentiment import SentimentIntensityAnalyzer
20
  sia = SentimentIntensityAnalyzer()
21
 
22
- # Load multilingual emotion detection model
23
- model_name = "joeddav/xlm-roberta-large-xnli"
24
- tokenizer = AutoTokenizer.from_pretrained(model_name)
25
- emotion_model = AutoModelForSequenceClassification.from_pretrained(model_name)
26
-
27
  # Load English emotion detection pipeline
28
  emotion_pipeline = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
29
 
 
 
 
30
  # Sample texts
31
  sample_texts = [
32
  "The digital world is transforming the way we read and engage with text.",
@@ -96,21 +93,16 @@ if st.button("Analyze Engagement"):
96
  # Sentiment Analysis
97
  sentiment_score = sia.polarity_scores(text)
98
 
99
- # English Emotion Detection
100
  if selected_language == "English":
101
  emotion_results = emotion_pipeline(text)
102
  top_emotion = max(emotion_results[0], key=lambda x: x['score'])['label']
103
  emotion_scores = {e['label']: e['score'] for e in emotion_results[0]}
104
  else:
105
  # Multilingual Sentiment Analysis
106
- inputs = tokenizer(text, return_tensors="pt", truncation=True)
107
- with torch.no_grad():
108
- outputs = emotion_model(**inputs)
109
- probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
110
- top_emotion_idx = torch.argmax(probabilities).item()
111
- emotion_labels = ["neutral", "entailment", "contradiction"]
112
- top_emotion = emotion_labels[top_emotion_idx]
113
- emotion_scores = {top_emotion: probabilities[0][top_emotion_idx].item()}
114
 
115
  # Display Sentiment
116
  st.subheader("πŸ“Š Sentiment Analysis")
 
2
  import nltk
3
  import spacy
4
  import matplotlib.pyplot as plt
5
+ from transformers import pipeline
6
  import random
7
  import subprocess
 
8
 
9
  # Ensure spaCy model is installed
10
  try:
 
18
  from nltk.sentiment import SentimentIntensityAnalyzer
19
  sia = SentimentIntensityAnalyzer()
20
 
 
 
 
 
 
21
  # Load English emotion detection pipeline
22
  emotion_pipeline = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
23
 
24
+ # Load a better multilingual sentiment analysis model
25
+ multilingual_pipeline = pipeline("sentiment-analysis", model="cardiffnlp/twitter-xlm-roberta-base-sentiment")
26
+
27
  # Sample texts
28
  sample_texts = [
29
  "The digital world is transforming the way we read and engage with text.",
 
93
  # Sentiment Analysis
94
  sentiment_score = sia.polarity_scores(text)
95
 
96
+ # Emotion Detection
97
  if selected_language == "English":
98
  emotion_results = emotion_pipeline(text)
99
  top_emotion = max(emotion_results[0], key=lambda x: x['score'])['label']
100
  emotion_scores = {e['label']: e['score'] for e in emotion_results[0]}
101
  else:
102
  # Multilingual Sentiment Analysis
103
+ multilingual_results = multilingual_pipeline(text)
104
+ top_emotion = multilingual_results[0]["label"]
105
+ emotion_scores = {top_emotion: multilingual_results[0]["score"]}
 
 
 
 
 
106
 
107
  # Display Sentiment
108
  st.subheader("πŸ“Š Sentiment Analysis")