mechtnet commited on
Commit
099abd2
·
verified ·
1 Parent(s): f771410

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -4
app.py CHANGED
@@ -33,7 +33,7 @@ def analyze_text():
33
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
34
  emotion_classifier = pipeline("text-classification", tokenizer=tokenizer, model=model, return_all_scores=True)
35
 
36
- line_emotions = analyze_lines_emotions(lines, emotion_classifier, threshold=0.5)
37
 
38
  # 4. Выбор лучших цитат по настроению
39
  quotes_by_mood = get_best_quotes_by_mood(line_emotions)
@@ -65,7 +65,7 @@ def split_into_lines(text):
65
  lines = text.strip().split('\n')
66
  return lines
67
 
68
- def analyze_lines_emotions(lines, emotion_classifier, threshold=0.5):
69
  # Словарь для перевода эмоций на русский язык
70
  emotion_translation = {
71
  "admiration": "восхищение",
@@ -94,10 +94,29 @@ def analyze_lines_emotions(lines, emotion_classifier, threshold=0.5):
94
  "relief": "облегчение",
95
  "remorse": "раскаяние",
96
  "sadness": "печаль",
97
- "surprise": "удивление",
98
- "neutral": "нейтрально"
99
  }
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  results = []
102
  for line in lines:
103
  if not line.strip():
@@ -107,7 +126,12 @@ def analyze_lines_emotions(lines, emotion_classifier, threshold=0.5):
107
  for pred in predictions:
108
  emotion_label = pred['label']
109
  emotion_label_ru = emotion_translation.get(emotion_label, emotion_label)
 
 
 
110
  emotions[emotion_label_ru] = round(float(pred['score']), 3)
 
 
111
  # Определяем доминирующую эмоцию
112
  dominant_emotion = max(emotions, key=emotions.get)
113
  dominant_score = emotions[dominant_emotion]
 
33
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
34
  emotion_classifier = pipeline("text-classification", tokenizer=tokenizer, model=model, return_all_scores=True)
35
 
36
+ line_emotions = analyze_lines_emotions(lines, emotion_classifier, threshold=0.3)
37
 
38
  # 4. Выбор лучших цитат по настроению
39
  quotes_by_mood = get_best_quotes_by_mood(line_emotions)
 
65
  lines = text.strip().split('\n')
66
  return lines
67
 
68
+ def analyze_lines_emotions(lines, emotion_classifier, threshold=0.3):
69
  # Словарь для перевода эмоций на русский язык
70
  emotion_translation = {
71
  "admiration": "восхищение",
 
94
  "relief": "облегчение",
95
  "remorse": "раскаяние",
96
  "sadness": "печаль",
97
+ "surprise": "удивление"
 
98
  }
99
 
100
+ # Список эмоций для исключения
101
+ excluded_emotions = [
102
+ "развлечение",
103
+ "одобрение",
104
+ "забота",
105
+ "замешательство",
106
+ "любопытство",
107
+ "желание",
108
+ "неодобрение",
109
+ "отвращение",
110
+ "смущение",
111
+ "волнение",
112
+ "благодарность",
113
+ "нервозность",
114
+ "гордость",
115
+ "осознание",
116
+ "облегчение",
117
+ "раскаяние"
118
+ ]
119
+
120
  results = []
121
  for line in lines:
122
  if not line.strip():
 
126
  for pred in predictions:
127
  emotion_label = pred['label']
128
  emotion_label_ru = emotion_translation.get(emotion_label, emotion_label)
129
+ # Пропускаем эмоции из списка исключения
130
+ if emotion_label_ru in excluded_emotions or emotion_label == "neutral":
131
+ continue
132
  emotions[emotion_label_ru] = round(float(pred['score']), 3)
133
+ if not emotions:
134
+ continue # Если нет эмоций для рассмотрения, пропускаем строку
135
  # Определяем доминирующую эмоцию
136
  dominant_emotion = max(emotions, key=emotions.get)
137
  dominant_score = emotions[dominant_emotion]