Rai007-is-here commited on
Commit
47c2fe8
1 Parent(s): 5a719e9

initial commit

Browse files
Files changed (1) hide show
  1. grad_creation_sent_analysis_ver_1.py +234 -0
grad_creation_sent_analysis_ver_1.py ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import requests
4
+ import json
5
+
6
+ from transformers import AutoTokenizer, AutoModelForTokenClassification
7
+ from nltk.tokenize import sent_tokenize
8
+ tokenizer = AutoTokenizer.from_pretrained("dslim/bert-base-NER")
9
+ model = AutoModelForTokenClassification.from_pretrained("dslim/bert-base-NER")
10
+ nlp = pipeline("ner", model=model, tokenizer=tokenizer)
11
+ classifier = pipeline("text-classification",model='bhadresh-savani/distilbert-base-uncased-emotion', return_all_scores=True)
12
+
13
+ def detect_emotion(lista, emotion_threshold, minimum_words):
14
+ thresh = emotion_threshold
15
+ mini = minimum_words
16
+ prediction = classifier(lista)
17
+ listb = []
18
+ for i in range(len(prediction)):
19
+ for k in prediction[i]:
20
+ if k["score"]>thresh and len(lista[i].split())>mini:
21
+ a = (i,k["label"],k["score"])
22
+ listb.append(a)
23
+ listc = []
24
+ for i in listb:
25
+ val = (i[0],lista[i[0]],i[1],i[2])
26
+ listc.append(val)
27
+ return listc
28
+
29
+ def detect_entity3(lista):
30
+ ner_results = nlp(lista)
31
+ listb = []
32
+ entity = []
33
+ for i in range(len(ner_results)):
34
+ end = -10
35
+ old = None
36
+ for j in range(len(ner_results[i])):
37
+ if int(ner_results[i][j]["start"])==int(end):
38
+ if j==len(ner_results[i])-1:
39
+ appen = ner_results[i][j]["word"].replace("#", "")
40
+ old["word"] = old["word"] + appen
41
+ if old["score"]< ner_results[i][j]["score"]:
42
+ old["score"] = ner_results[i][j]["score"]
43
+ val = (i,old["word"],old["score"],ner_results[i][j]['entity'],old["start"]) #can do j-1 also here
44
+ entity.append(val)
45
+ else:
46
+ appen = ner_results[i][j]["word"].replace("#", "")
47
+ old["word"] = old["word"] + appen
48
+ if old["score"]< ner_results[i][j]["score"]:
49
+ old["score"] = ner_results[i][j]["score"]
50
+ end = ner_results[i][j]["end"]
51
+ else:
52
+ if old is not None:
53
+ if j==len(ner_results[i])-1:
54
+ val = (i,old["word"],old["score"],ner_results[i][j-1]['entity'],old["start"])
55
+ entity.append(val)
56
+ #print("\n")
57
+ old["word"] = ner_results[i][j]["word"]
58
+ old["score"] = ner_results[i][j]["score"]
59
+ old["start"] = ner_results[i][j]["start"]
60
+ val = (i,old["word"],old["score"],ner_results[i][j]['entity'],old["start"])
61
+ entity.append(val)
62
+ else:
63
+ val = (i,old["word"],old["score"],ner_results[i][j-1]['entity'],old["start"])
64
+ entity.append(val)
65
+ #print("\n")
66
+ old["word"] = ner_results[i][j]["word"]
67
+ old["score"] = ner_results[i][j]["score"]
68
+ old["start"] = ner_results[i][j]["start"]
69
+ end = ner_results[i][j]["end"]
70
+ else:
71
+ old = {}
72
+ old["word"] = ner_results[i][j]["word"]
73
+ old["score"] = ner_results[i][j]["score"]
74
+ old["start"] = ner_results[i][j]["start"]
75
+ end = ner_results[i][j]["end"]
76
+ listc = []
77
+ for i in entity:
78
+ val = (i[0],lista[i[0]],i[1],i[2],i[4])
79
+ listc.append(val)
80
+ return listc
81
+
82
+ def compare_and_print(output1,output2):
83
+ dicta = {}
84
+ for i in output1:
85
+ dicta[i[0]] = "No"
86
+ for i in output2:
87
+ if i[0] in dicta:
88
+ dicta[i[0]] = "Yes"
89
+ flag = 0
90
+ both = []
91
+ for i in dicta:
92
+ if dicta[i]=="Yes":
93
+ flag=1
94
+ both.append(i)
95
+ return both
96
+
97
+ def detect_tam(entity_output, tam_list):
98
+ dicta = {}
99
+ for i in entity_output:
100
+ for j in range(len(tam_list)):
101
+ comp = tam_list[j].split()
102
+ if i[2].lower() == comp[0].lower():
103
+ if i[1][i[4]:i[4]+len(tam_list[j])].lower()==tam_list[j].lower():
104
+ if i[0] not in dicta:
105
+ dicta[i[0]] = []
106
+ dicta[i[0]].append(j)
107
+ else:
108
+ if j in dicta[i[0]]:
109
+ pass
110
+ else:
111
+ dicta[i[0]].append(j)
112
+ return dicta
113
+
114
+ def myFunc(e):
115
+ return e[1]
116
+
117
+ def integrate_all(text,threshold, min_words, max_detection, max_tam_detection):
118
+ out_text = ""
119
+ emotion_threshold = threshold
120
+ minimum_words = min_words
121
+ emotion_number = max_detection #3
122
+ both_number = max_tam_detection #5
123
+ emotion_number = int(emotion_number)
124
+ both_number = int(both_number)
125
+ #tam_number = 4
126
+ lista = sent_tokenize(text)
127
+ emotion_out = detect_emotion(lista,emotion_threshold, minimum_words)
128
+ out_text = out_text + "##Selected based on emotions##"
129
+ out_text = out_text + "\n---------------------------"
130
+ dicta = {}
131
+ for i in emotion_out:
132
+ if i[2] not in dicta:
133
+ dicta[i[2]] = []
134
+ dicta[i[2]].append((i[0],i[3]))
135
+ else:
136
+ dicta[i[2]].append((i[0],i[3]))
137
+ for i in dicta:
138
+ dicta[i].sort(reverse=True,key=myFunc)
139
+ #print(dicta)
140
+ emotion_selected = []
141
+ for i in dicta:
142
+ val_em = 0
143
+ if len(dicta[i])<emotion_number:
144
+ em_loop = len(dicta[i])
145
+ else:
146
+ em_loop = emotion_number
147
+ #em_loop = int(em_loop)
148
+ for j in range(em_loop):
149
+ emotion_selected.append(dicta[i][j])
150
+ for i in emotion_selected:
151
+ if len(lista)>=i[0]-2+8+1:
152
+ sel_val = 8
153
+ else:
154
+ sel_val = len(lista)-1-(i[0]-2+8)
155
+ for j in range(sel_val):
156
+ out_text = out_text + "\n" + str(-2+j) + ", " + str(lista[i[0]-2+j])
157
+ out_text = out_text + "\n---------------------------"
158
+ out_text = out_text + "\n---------------------------"
159
+ out_text = out_text + "\n##Selected based on presence of both named entity then followed by highest emotions##"
160
+ out_text = out_text + "\n---------------------------"
161
+ entity_out = detect_entity3(lista)
162
+ both = compare_and_print(emotion_out,entity_out)
163
+ both_selected = []
164
+ if len(both)<=both_number:
165
+ for i in both:
166
+ both_selected.append((i,None))
167
+ else:
168
+ list_em = []
169
+ for i in both:
170
+ for j in emotion_out:
171
+ if i==j[0]:
172
+ list_em.append((i,j[3]))
173
+ list_em.sort(reverse=True, key=myFunc)
174
+ for i in range(both_number):
175
+ both_selected.append(list_em[i])
176
+ for i in both_selected:
177
+ if len(lista)>=i[0]-2+8+1:
178
+ sel_val = 8
179
+ else:
180
+ sel_val = len(lista)-1-(i[0]-2+8)
181
+ for j in range(sel_val):
182
+ out_text = out_text + "\n" + str(-2+j) + ", " + str(lista[i[0]-2+j])
183
+ out_text = out_text + "\n---------------------------"
184
+ out_text = out_text + "\n---------------------------"
185
+ """print("##Selected based on presence of no. of TAM##")
186
+ print("-------------------------------")
187
+ if len(tam_list)==0:
188
+ print("No TAM list provided hence no selection based on TAM")
189
+ tam_output = detect_tam(entity_out, tam_list)
190
+ no_of_tam = {}
191
+ count = 0
192
+ tam_numbers=[]
193
+ for i in tam_output:
194
+ count = count+1
195
+ if len(tam_output[i]) not in no_of_tam:
196
+ no_of_tam[len(tam_output[i])] = [i]
197
+ tam_numbers.append(len(tam_output[i]))
198
+ else:
199
+ no_of_tam[len(tam_output[i])].append(i)
200
+ tam_numbers.sort(reverse=True)
201
+ if count<tam_number:
202
+ tam_number = count
203
+ tam_output = []
204
+ for i in tam_numbers:
205
+ if tam_number<=0:
206
+ break
207
+ local_lista = no_of_tam[i]
208
+ for i in local_lista:
209
+ tam_output.append(i)
210
+ tam_number = tam_number-1
211
+ for i in tam_output:
212
+ print(lista[i])"""
213
+ return out_text
214
+
215
+ def generateMessage(text, threshold, min_words, max_detection, max_tam_detection):
216
+ text2 = text
217
+ try:
218
+ text1 = integrate_all(text2, threshold, min_words, max_detection, max_tam_detection)
219
+ except:
220
+ text1 = ""
221
+ return text1
222
+
223
+ iface = gr.Interface(fn=generateMessage, inputs=[gr.inputs.Textbox(label="Input Description", default="Ladies and gentlemen, Elon Musk. Thank you, thank you. Thank you very much. It's an honor to be hosting Saturday Night Live. I mean that sometimes after I say something, I have to say I mean that so people really know that I mean it. That's because I don't always have a lot of intonation or variation in how I speak, which I'm told makes for great comedy. I'm actually making history. Not as the first person with Asperger's to host SNL, or at least the first to admit it. So I won't make a lot of eye contact with the cast tonight, but all right, I'm pretty good at running human in emulation mode. I'd first like to share with you my vision for the future. I believe in a renewable energy future. I believe that humanity must become a multi planetary, space bearing civilization. Those seem like exciting goals, don't they? I think if I just posted that on Twitter, I'd be fine. But I also write things like 69 days after 420 again, haha. I don't know. I thought it was funny. That's why I read Haha at the end. Look, I know I sometimes say or post strange things, but that's just how my brain works. To anyone I'm offended, I just want to say I reinvented electric cars and I'm sending people to Mars in a rocket ship. Did you think I was also going to be a chill, normal dude? Now, a lot of times people are reduced to the dumbest thing they ever did. Like one time I smoked weed on Joe Rogan's podcast, and now all the time I hear Elon Musk. All he ever does is smoke weed on podcasts. Like I go from podcast to podcast lighting up joints. It happened once. It's like reducing OJ. Simpson to murderer. That was one time. Fun fact, OJ. Also hosted the show in 79, anna Gain in 96. Killed both times. One reason I've always loved SNL is because it's genuinely live. A lot of people don't realize that we're actually live right now. Which means I could say something truly shocking, like I drive a Prius. SNL is also a great way to learn something new about the host. For example, this is my son's name. It's pronounced cat running across keyboard. Another thing people want to know is what was I like as a kid? The answer is pretty much the same as now, but tomorrow. But we can also ask my mother, who's here tonight. Her name is May, like the month, but with an E at the end. Thanks for spelling my name, Elon. Mom, do you remember when I was twelve and I created my own video game called Blastar about a spaceship that battles aliens? I do. I remember they paid you $500. But you were too young to open a bank account, so I had to open one for you. That's right. Whatever happened to that bank account? That's not important. You turn that video game about space into reality. Unless you consider that our reality might be a video game and we're all just computer simulations being played by a teenager in another planet. That's great, Elon. Well, break your leg tonight. I love you very much. I'll be tomorrow. And I'm excited for my Mother's Day gift. I just hope it's not ghost coin. It is. It sure is. Okay, we've got a great show for you tonight. Miley Cyrus is here, so stick around and we're be right back."),
224
+ gr.inputs.Number(label="Threshold", default=0.995),
225
+ gr.inputs.Number(label="Min no. of words", default=5),
226
+ gr.inputs.Number(label="Max detection from each emotion type", default=3),
227
+ gr.inputs.Number(label="Max TAM detection lines", default=4)
228
+ ], outputs=["textbox"],
229
+ title = "Emotion sensitive lines detection",
230
+ allow_screenshot = False, allow_flagging = False
231
+ )
232
+
233
+
234
+ iface.launch(server_name="0.0.0.0", auth=("vidyo", "pass_qwertyindra"), share=True)