Ravilppcd07 commited on
Commit
3a6fb0e
·
verified ·
1 Parent(s): 6b608dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -20
app.py CHANGED
@@ -9,7 +9,7 @@ import os
9
  sentiment_pipeline = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
10
  classification_pipeline = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
11
  ner_pipeline = pipeline("token-classification", model="dslim/bert-base-NER", grouped_entities=True)
12
- emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", top_k=1)
13
  intent_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
14
  summary_generator = pipeline("summarization", model="facebook/bart-large-cnn")
15
 
@@ -72,29 +72,14 @@ def get_emotion(text: str) -> str:
72
  except Exception as e:
73
  return f"Error in emotion detection: {e}"
74
  '''
75
- def get_emotion(text: str) -> str:
 
76
  try:
77
- # Get the classification result
78
- result = emotion_classifier(text)
79
-
80
- # Check if the result is a list and has at least one item
81
- if result and isinstance(result, list) and len(result) > 0:
82
- emotion_label = result[0]['label'] # Access the label of the first result
83
- return emotion_label.capitalize() # Capitalize for readability
84
- return "Unknown emotion" # Fallback if no label found
85
  except Exception as e:
86
  return f"Error in emotion detection: {e}"
87
 
88
 
89
- # Function to handle long conversations for emotion detection
90
- def analyze_long_text_emotion(text: str, chunk_size: int = 512) -> str:
91
- # Split the text into chunks of the specified size
92
- chunks = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
93
- emotions = [get_emotion(chunk) for chunk in chunks]
94
- # Aggregate results by counting the frequency of each emotion
95
- emotion_counts = {label: emotions.count(label) for label in set(emotions)}
96
- return max(emotion_counts, key=emotion_counts.get) # Return the most frequent emotion
97
-
98
  # Function to classify issue
99
  # Function to classify issue with optional user-provided labels
100
  def classify_issue(text, user_labels=None):
@@ -221,7 +206,7 @@ def analyze_conversation(conversation, custom_labels):
221
  issue_category = classify_issue(conversation, user_labels=user_labels)
222
  resolution_status = get_resolution_status(conversation)
223
  entities = extract_entities(conversation)
224
- emotion = analyze_long_text_emotion(conversation)
225
  intent = detect_intent(conversation)
226
  agent_metrics = calculate_agent_metrics(conversation)
227
  topic = generate_topics_with_keybert(conversation)
 
9
  sentiment_pipeline = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
10
  classification_pipeline = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
11
  ner_pipeline = pipeline("token-classification", model="dslim/bert-base-NER", grouped_entities=True)
12
+ emotion_classifier = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion")
13
  intent_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
14
  summary_generator = pipeline("summarization", model="facebook/bart-large-cnn")
15
 
 
72
  except Exception as e:
73
  return f"Error in emotion detection: {e}"
74
  '''
75
+
76
+ def detect_emotion(text):
77
  try:
78
+ return emotion_classifier(text, truncation=True)[0]['label']
 
 
 
 
 
 
 
79
  except Exception as e:
80
  return f"Error in emotion detection: {e}"
81
 
82
 
 
 
 
 
 
 
 
 
 
83
  # Function to classify issue
84
  # Function to classify issue with optional user-provided labels
85
  def classify_issue(text, user_labels=None):
 
206
  issue_category = classify_issue(conversation, user_labels=user_labels)
207
  resolution_status = get_resolution_status(conversation)
208
  entities = extract_entities(conversation)
209
+ emotion = detect_emotion(conversation)
210
  intent = detect_intent(conversation)
211
  agent_metrics = calculate_agent_metrics(conversation)
212
  topic = generate_topics_with_keybert(conversation)