Tirath5504 commited on
Commit
be90814
1 Parent(s): 3c9ee04

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import gradio as gr
3
+ from nltk.sentiment.vader import SentimentIntensityAnalyzer
4
+ import nltk
5
+ import numpy as np
6
+ nltk.download('vader_lexicon')
7
+ from deep_translator import (GoogleTranslator)
8
+ from langdetect import detect
9
+
10
+ zero_shot_classifier = pipeline("zero-shot-classification" , model='roberta-large-mnli')
11
+
12
+ spam_detector = pipeline("text-classification", model="madhurjindal/autonlp-Gibberish-Detector-492513457")
13
+
14
+ issues = ["Misconduct" , "Negligence" , "Discrimination" , "Corruption" , "Violation of Rights" , "Inefficiency" ,
15
+ "Unprofessional Conduct", "Response Time" , "Use of Firearms" , "Property Damage"]
16
+
17
+ apprecn = ["Tech-Savvy Staff" , "Co-operative Staff" , "Well-Maintained Premises" , "Responsive Staff"]
18
+
19
+ def translate(input_text):
20
+ source_lang = detect(input_text)
21
+ translated = GoogleTranslator(source=source_lang, target='en').translate(text=input_text)
22
+ return translated
23
+
24
+ def spam_detection(input_text):
25
+
26
+ return spam_detector(input_text)[0]['label'] == 'clean'
27
+
28
+ def sentiment_analysis(input_text):
29
+
30
+ score = SentimentIntensityAnalyzer().polarity_scores(input_text)
31
+
32
+
33
+ del score['compound']
34
+
35
+ label = list(filter(lambda x: score[x] == max(score.values()), score))[0]
36
+
37
+
38
+
39
+
40
+ if label == 'neg':
41
+
42
+ return ["Negative Feedback" , score['neg']]
43
+
44
+ elif label == 'pos':
45
+
46
+ return ["Positive Feedback" , -1]
47
+
48
+ else:
49
+
50
+ return ["Neutral Feedback" , -1]
51
+
52
+ def positive_zero_shot(input_text):
53
+
54
+ return zero_shot_classifier(input_text , candidate_labels = apprecn , multi_label = False)['labels'][0]
55
+
56
+
57
+ def negative_zero_shot(input_text):
58
+
59
+ return zero_shot_classifier(input_text , candidate_labels = issues , multi_label = False)['labels'][0]
60
+
61
+ def pipeline(input_text):
62
+
63
+ input_text = translate(input_text)
64
+
65
+ if spam_detection(input_text):
66
+
67
+ if sentiment_analysis(input_text)[0] == "Positive Feedback":
68
+
69
+ return "Positive Feedback" , -1 , positive_zero_shot(input_text)
70
+
71
+ elif sentiment_analysis(input_text)[0] == "Negative Feedback":
72
+
73
+ return "Negative Feedback" , sentiment_analysis(input_text)[1] , negative_zero_shot(input_text)
74
+
75
+ else:
76
+
77
+ return "Neutral Feedback" , -1 , ""
78
+ else:
79
+ return "Spam" , ""
80
+
81
+ iface = gr.Interface(fn = pipeline , inputs=['text'] , outputs=['text' , 'text' , 'text'])
82
+ iface.launch(share=True)