-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsentiment.py
77 lines (67 loc) · 2.18 KB
/
sentiment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from audio_gen import user_audio
from text_gen import text_gen
import numpy as np
import wave
import pyaudio
import deepspeech
import datetime
from transformers import AutoTokenizer, AutoModelWithLMHead
from transformers import pipeline
# tokenizer and model for emotion detection
tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-emotion")
model = AutoModelWithLMHead.from_pretrained("mrm8488/t5-base-finetuned-emotion")
# Sentiment analysis model
classifier = pipeline('sentiment-analysis')
class sentiment:
def __init__(self):
self.classifier = classifier
# if you have just used speech to text you can use this
def from_text(self, text):
sentiment = self.classifier(text)
return sentiment[0]
# generates the sentiment analysis of the last recording/file for which the speech to text was done
def recent(self):
file = open('temp.txt', 'r')
lines = file.readlines()
arr = []
for line in lines:
arr.append(line.strip())
output = []
for i in range(len(arr)):
output.append(self.from_text(arr[i]))
return output
# live sentiment analysis from audio recording
# model : 'deepspeech' or 'wav2vec'
def from_audio(self, model=None):
recording = text_gen(model=model)
text = recording.text_from_recording()
return self.from_text(text)
# Emotion Detection Module
class emotion:
def __init__(self):
self.tokenizer = tokenizer
self.model = model
# getting the emotion from text
def from_text(self, text):
input_ids = self.tokenizer.encode(text + '</s>', return_tensors='pt')
output = self.model.generate(input_ids=input_ids,max_length=2)
dec = [self.tokenizer.decode(ids) for ids in output]
label = dec[0]
return label
# Emotion Detection from direct audio file
# model : 'deepspeech' or 'wav2vec'
def from_audio(self, model=None):
recording = text_gen(model=model)
text = recording.text_from_recording()
return self.from_text(text)
# Emotion Detection from the last done audio transcription
def recent(self):
file = open('temp.txt', 'r')
lines = file.readlines()
arr = []
for line in lines:
arr.append(line.strip())
output = []
for i in range(len(arr)):
output.append(self.from_text(arr[i]))
return output