kaushikbar commited on
Commit
ccfea75
1 Parent(s): 08887e8

word attributes

Browse files
Files changed (2) hide show
  1. app.py +23 -0
  2. requirements.txt +2 -0
app.py CHANGED
@@ -4,6 +4,8 @@ from huggingface_hub import hf_hub_download
4
  from langdetect import detect, DetectorFactory, detect_langs
5
  import fasttext
6
  from transformers import pipeline
 
 
7
 
8
  models = {'en': 'Narsil/deberta-large-mnli-zero-cls', #'microsoft/deberta-xlarge-mnli' # English
9
  'de': 'Sahajtomar/German_Zeroshot', # German
@@ -38,6 +40,10 @@ classifiers = {'en': pipeline("zero-shot-classification", hypothesis_template=hy
38
 
39
  fasttext_model = fasttext.load_model(hf_hub_download("julien-c/fasttext-language-id", "lid.176.bin"))
40
 
 
 
 
 
41
  def prep_examples():
42
  example_text1 = "Coronavirus disease (COVID-19) is an infectious disease caused by the SARS-CoV-2 virus. Most \
43
  people who fall sick with COVID-19 will experience mild to moderate symptoms and recover without special treatment. \
@@ -156,6 +162,23 @@ def sequence_to_classify(sequence, labels):
156
  str(datetime.datetime.now()),
157
  sequence,
158
  predicted_labels))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
 
160
  return clean_output
161
 
 
4
  from langdetect import detect, DetectorFactory, detect_langs
5
  import fasttext
6
  from transformers import pipeline
7
+ from transformers_interpret import ZeroShotClassificationExplainer
8
+ import string, nltk
9
 
10
  models = {'en': 'Narsil/deberta-large-mnli-zero-cls', #'microsoft/deberta-xlarge-mnli' # English
11
  'de': 'Sahajtomar/German_Zeroshot', # German
 
40
 
41
  fasttext_model = fasttext.load_model(hf_hub_download("julien-c/fasttext-language-id", "lid.176.bin"))
42
 
43
+ _ = nltk.download('stopwords', quiet=True)
44
+ _ = nltk.download('wordnet', quiet=True)
45
+ _ = nltk.download('punkt', quiet=True)
46
+
47
  def prep_examples():
48
  example_text1 = "Coronavirus disease (COVID-19) is an infectious disease caused by the SARS-CoV-2 virus. Most \
49
  people who fall sick with COVID-19 will experience mild to moderate symptoms and recover without special treatment. \
 
162
  str(datetime.datetime.now()),
163
  sequence,
164
  predicted_labels))
165
+
166
+ # Explain word attributes
167
+ stop_words = nltk.corpus.stopwords.words('english')
168
+ puncts = list(string.punctuation)
169
+
170
+ model_expl = ZeroShotClassificationExplainer(classifier.model, classifier.tokenizer)
171
+ response_expl = model_expl(sequence, labels, hypothesis_template="This example is {}.")
172
+
173
+ if len(labels_pred) == 1:
174
+ response_expl = response_expl[model_expl.predicted_label]
175
+
176
+ for key in response_expl:
177
+ for idx, elem in enumerate(response_expl[key]):
178
+ if elem[0] in stop_words:
179
+ del response_expl[key][idx]
180
+
181
+ print(response_expl)
182
 
183
  return clean_output
184
 
requirements.txt CHANGED
@@ -3,4 +3,6 @@ sentence-transformers
3
  torch
4
  langdetect
5
  fasttext
 
 
6
 
 
3
  torch
4
  langdetect
5
  fasttext
6
+ transformers_interpret
7
+ nltk
8