run480 commited on
Commit
ab9d3ba
1 Parent(s): 7a2caab

Update app.py

Browse files

Zero-shot text classification

Files changed (1) hide show
  1. app.py +43 -12
app.py CHANGED
@@ -99,20 +99,51 @@
99
  #------------------------------------------------------------------------------------------
100
  # 6. Same model with some tuning with some parameters: num_return_sequences=5, max_length=200, temperature=1.5, num_beams=10
101
 
102
- from transformers import PegasusForConditionalGeneration, PegasusTokenizer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  import gradio as grad
104
 
105
- mdl_name = "google/pegasus-xsum"
106
- pegasus_tkn = PegasusTokenizer.from_pretrained(mdl_name)
107
- mdl = PegasusForConditionalGeneration.from_pretrained(mdl_name)
108
 
109
- def summarize(text):
110
- tokens = pegasus_tkn(text, truncation=True, padding="longest", return_tensors="pt")
111
- translated_txt = mdl.generate(**tokens, num_return_sequences=5, max_length=200, temperature=1.5, num_beams=10)
112
- response = pegasus_tkn.batch_decode(translated_txt, skip_special_tokens=True)
113
- return response
114
 
115
- txt = grad.Textbox(lines=10, label="English", placeholder="English Text here")
116
- out = grad.Textbox(lines=10, label="Summary")
 
117
 
118
- grad.Interface(summarize, inputs=txt, outputs=out).launch()
 
99
  #------------------------------------------------------------------------------------------
100
  # 6. Same model with some tuning with some parameters: num_return_sequences=5, max_length=200, temperature=1.5, num_beams=10
101
 
102
+ # from transformers import PegasusForConditionalGeneration, PegasusTokenizer
103
+ # import gradio as grad
104
+
105
+ # mdl_name = "google/pegasus-xsum"
106
+ # pegasus_tkn = PegasusTokenizer.from_pretrained(mdl_name)
107
+ # mdl = PegasusForConditionalGeneration.from_pretrained(mdl_name)
108
+
109
+ # def summarize(text):
110
+ # tokens = pegasus_tkn(text, truncation=True, padding="longest", return_tensors="pt")
111
+ # translated_txt = mdl.generate(**tokens, num_return_sequences=5, max_length=200, temperature=1.5, num_beams=10)
112
+ # response = pegasus_tkn.batch_decode(translated_txt, skip_special_tokens=True)
113
+ # return response
114
+
115
+ # txt = grad.Textbox(lines=10, label="English", placeholder="English Text here")
116
+ # out = grad.Textbox(lines=10, label="Summary")
117
+
118
+ # grad.Interface(summarize, inputs=txt, outputs=out).launch()
119
+
120
+ #-----------------------------------------------------------------------------------
121
+ # 7. Zero-Shot Learning:
122
+ # Zero-shot learning, as the name implies, is to use a pretrained model , trained on a certain set of data,
123
+ # on a different set of data, which it has not seen during training. This would mean, as an example, to take
124
+ # some model from huggingface that is trained on a certain dataset and use it for inference on examples it has never seen before.
125
+
126
+ # The transformers are where the zero-shot classification implementations are most frequently found by us.
127
+ # There are more than 60 transformer models that function based on the zero-shot classification that are found in the huggingface library.
128
+
129
+ # When we discuss zero-shot text classification , there is one additional thing that springs to mind.
130
+ # In the same vein as zero-shot classification is few-shot classification, which is very similar to zero-shot classification.
131
+ # However, in contrast with zero-shot classification, few-shot classification makes use of very few labeled samples during the training process.
132
+ # The implementation of the few-shot classification methods can be found in OpenAI, where the GPT3 classifier is a well-known example of a few-shot classifier.
133
+
134
+ from transformers import pipeline
135
  import gradio as grad
136
 
137
+ zero_shot_classifier = pipeline("zero-shot-classification")
 
 
138
 
139
+ def classify(text,labels):
140
+     classifer_labels = labels.split(",")
141
+     #["software", "politics", "love", "movies", "emergency", "advertisment","sports"]
142
+     response = zero_shot_classifier(text,classifer_labels)
143
+     return response
144
 
145
+ txt=grad.Textbox(lines=1, label="English", placeholder="text to be classified")
146
+ labels=grad.Textbox(lines=1, label="Labels", placeholder="comma separated labels")
147
+ out=grad.Textbox(lines=1, label="Classification")
148
 
149
+ grad.Interface(classify, inputs=[txt,labels], outputs=out).launch()