Commit
·
645c754
1
Parent(s):
47f38d5
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,8 +13,32 @@ classifier_emotions = ['positive', 'neutral', 'negative']
|
|
| 13 |
|
| 14 |
classifier = pipeline('text-classification', model=classifier_model_name)
|
| 15 |
|
| 16 |
-
def
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
gr_interface = gradio.Interface(
|
| 20 |
fn = my_inference_function,
|
|
|
|
| 13 |
|
| 14 |
classifier = pipeline('text-classification', model=classifier_model_name)
|
| 15 |
|
| 16 |
+
def find_emotional_sentences(text, emotions, threshold):
|
| 17 |
+
sentences_by_emotion = {}
|
| 18 |
+
for e in emotions:
|
| 19 |
+
sentences_by_emotion[e]=[]
|
| 20 |
+
sentences = nltk.sent_tokenize(text)
|
| 21 |
+
print(f'Document has {len(text)} characters and {len(sentences)} sentences.')
|
| 22 |
+
for s in sentences:
|
| 23 |
+
prediction = classifier(s)
|
| 24 |
+
if (prediction[0]['label']!='neutral' and prediction[0]['score']>threshold):
|
| 25 |
+
#print (f'Sentence #{sentences.index(s)}: {prediction} {s}')
|
| 26 |
+
sentences_by_emotion[prediction[0]['label']].append(s)
|
| 27 |
+
for e in emotions:
|
| 28 |
+
print(f'{e}: {len(sentences_by_emotion[e])} sentences')
|
| 29 |
+
return sentences_by_emotion
|
| 30 |
+
|
| 31 |
+
def summarize_sentences(sentences_by_emotion, min_length, max_length):
|
| 32 |
+
for k in sentences_by_emotion.keys():
|
| 33 |
+
if (len(sentences_by_emotion[k])!=0):
|
| 34 |
+
text = ' '.join(sentences_by_emotion[k])
|
| 35 |
+
summary = summarizer(text, min_length=min_length, max_length=max_length)
|
| 36 |
+
print(f"{k.upper()}: {summary[0]['summary_text']}\n")
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def my_inference_function(text):
|
| 40 |
+
sentences_by_emotion = find_emotional_sentences(text, classifier_emotions, 0.7)
|
| 41 |
+
return sentences_by_emotion
|
| 42 |
|
| 43 |
gr_interface = gradio.Interface(
|
| 44 |
fn = my_inference_function,
|