AAhad commited on
Commit
d826258
1 Parent(s): 36116d9

updated logic with more examples

Browse files
Files changed (1) hide show
  1. app.py +387 -9
app.py CHANGED
@@ -1,11 +1,389 @@
1
  import streamlit as st
 
 
 
2
 
3
- st.title('🤗 Transformers Library examples')
4
- st.divider()
5
- st.write("Sentiment Analysis")
6
- st.write("'Zero-shot classification")
7
- st.write("Text Generation")
8
- st.write("'Mask Filling")
9
- st.write("Named Entity Recognition")
10
- st.write("Question Answering")
11
- st.write("Translation")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import time
3
+ from transformers import pipeline
4
+ from datasets import load_dataset, Audio
5
 
6
+ st.set_page_config(page_title="🤗 Transformers Library examples",layout="wide")
7
+
8
+ st.title('🤗 :rainbow[Transformers Library examples]')
9
+
10
+ # Done
11
+ # function for Sentiment Analysis or Text classification model
12
+ def sentiment_analysis():
13
+ code = '''
14
+ from transformers import pipeline
15
+
16
+ classifier = pipeline("sentiment-analysis")
17
+ results = classifier("Transformers library is very helpful.")
18
+ '''
19
+ st.code(code, language='python')
20
+ if st.button("Run Test ", type="primary"):
21
+ with st.spinner('Wait for it...'):
22
+ time.sleep(5)
23
+ classifier = pipeline("sentiment-analysis")
24
+ results = classifier("Transformers library is very helpful.")
25
+
26
+
27
+ st.write("Output:")
28
+ st.success(results)
29
+ st.divider()
30
+
31
+ st.subheader("Example: Multiple statements analysis")
32
+ with st.spinner('Wait for it...'):
33
+ time.sleep(5)
34
+
35
+ code = '''
36
+ from transformers import pipeline
37
+
38
+ classifier = pipeline("sentiment-analysis")
39
+ results = classifier([
40
+ "This is quick tutorial site.",
41
+ "I learnt new topics today.",
42
+ "I do not like lengthy tutorials."
43
+ ])
44
+ '''
45
+ st.code(code, language='python')
46
+
47
+ results = classifier([
48
+ "This is quick tutorial site.",
49
+ "I learnt new topics today.",
50
+ "I do not like lengthy tutorials."
51
+ ])
52
+
53
+ st.write("Output:")
54
+ st.success(results)
55
+
56
+
57
+ # function for Sentiment Analysis or Text classification model
58
+ def text_generation():
59
+ code = '''
60
+ from transformers import pipeline
61
+
62
+ classifier = pipeline("sentiment-analysis")
63
+ results = classifier("Transformers library is very helpful.")
64
+ '''
65
+ st.code(code, language='python')
66
+ if st.button("Run Test ", type="primary"):
67
+ with st.spinner('Wait for it...'):
68
+ time.sleep(5)
69
+
70
+ classifier = pipeline("sentiment-analysis")
71
+
72
+
73
+
74
+ # function for Sentiment Analysis or Text classification model
75
+ def summarization():
76
+ code = '''
77
+ from transformers import pipeline
78
+
79
+ classifier = pipeline("sentiment-analysis")
80
+ results = classifier("Transformers library is very helpful.")
81
+ '''
82
+ st.code(code, language='python')
83
+ if st.button("Run Test ", type="primary"):
84
+ with st.spinner('Wait for it...'):
85
+ time.sleep(5)
86
+
87
+ classifier = pipeline("sentiment-analysis")
88
+
89
+ # DONE
90
+ # function for Image Classification model
91
+ def image_classification():
92
+ code = '''
93
+ from transformers import pipeline
94
+
95
+ classifier = pipeline("sentiment-analysis")
96
+ results = classifier("Transformers library is very helpful.")
97
+ '''
98
+ st.code(code, language='python')
99
+ if st.button("Run Test ", type="primary"):
100
+ st.image("./data/dog.jpeg", width=250, use_column_width=100)
101
+ with st.spinner('Wait for it...'):
102
+ time.sleep(8)
103
+ vision_classifier = pipeline(model="google/vit-base-patch16-224")
104
+ preds = vision_classifier(images="./data/dog.jpeg")
105
+ st.success("Output:")
106
+ st.json(preds)
107
+
108
+
109
+ # function for Sentiment Analysis or Text classification model
110
+ def image_segmentation():
111
+ code = '''
112
+ from transformers import pipeline
113
+
114
+ classifier = pipeline("sentiment-analysis")
115
+ results = classifier("Transformers library is very helpful.")
116
+ '''
117
+ st.code(code, language='python')
118
+ if st.button("Run Test ", type="primary"):
119
+ with st.spinner('Wait for it...'):
120
+ time.sleep(5)
121
+
122
+ classifier = pipeline("sentiment-analysis")
123
+
124
+
125
+ # function for Sentiment Analysis or Text classification model
126
+ def object_detection():
127
+ code = '''
128
+ from transformers import pipeline
129
+
130
+ classifier = pipeline("sentiment-analysis")
131
+ results = classifier("Transformers library is very helpful.")
132
+ '''
133
+ st.code(code, language='python')
134
+ if st.button("Run Test ", type="primary"):
135
+ with st.spinner('Wait for it...'):
136
+ time.sleep(5)
137
+
138
+ classifier = pipeline("sentiment-analysis")
139
+
140
+
141
+ # function for Audio Classification model
142
+ def audio_classification():
143
+ code = '''
144
+ from transformers import pipeline
145
+
146
+ classifier = pipeline("sentiment-analysis")
147
+ results = classifier("Transformers library is very helpful.")
148
+ '''
149
+ st.code(code, language='python')
150
+ if st.button("Run Test ", type="primary"):
151
+ with st.spinner('Wait for it...'):
152
+ time.sleep(5)
153
+
154
+ classifier = pipeline("sentiment-analysis")
155
+
156
+
157
+ # function forAutomatic Speech Recognition model
158
+ def automatic_speech_recognition():
159
+ minds = load_dataset("PolyAI/minds14", name="en-US", split="train")
160
+ minds = minds.train_test_split(test_size=0.2)
161
+ st.write(minds)
162
+ minds = minds.remove_columns(["path", "transcription", "english_transcription", "lang_id"])
163
+ st.write("minds[train][0] " , minds["train"][0])
164
+ labels = minds["train"].features["intent_class"].names
165
+ st.write("labels " ,labels)
166
+ label2id, id2label = dict(), dict()
167
+ for i, label in enumerate(labels):
168
+ label2id[label] = str(i)
169
+ id2label[str(i)] = label
170
+ st.write("label2id - id2label" , label2id , id2label)
171
+
172
+ code = '''
173
+ from transformers import pipeline
174
+
175
+ classifier = pipeline("automatic-speech-recognition")
176
+ results = transcriber("./data/mlk.flac")
177
+ '''
178
+ st.code(code, language='python')
179
+ if st.button("Run Test ", type="primary"):
180
+ with st.spinner('Wait for it...'):
181
+ time.sleep(5)
182
+ transcriber = pipeline(task="automatic-speech-recognition")
183
+ results = transcriber("./data/audio.m4a")
184
+ st.write("Output:")
185
+ st.success(results)
186
+
187
+
188
+ # function for Image Captioningn model
189
+ def image_captioning():
190
+ code = '''
191
+ from transformers import pipeline
192
+
193
+ classifier = pipeline("sentiment-analysis")
194
+ results = classifier("Transformers library is very helpful.")
195
+ '''
196
+ st.code(code, language='python')
197
+ if st.button("Run Test ", type="primary"):
198
+ with st.spinner('Wait for it...'):
199
+ time.sleep(5)
200
+
201
+ classifier = pipeline("sentiment-analysis")
202
+
203
+
204
+ # function for Mask Filling model
205
+ def mask_filling():
206
+ code = '''
207
+ from transformers import pipeline
208
+
209
+ classifier = pipeline("sentiment-analysis")
210
+ results = classifier("Transformers library is very helpful.")
211
+ '''
212
+ st.code(code, language='python')
213
+ if st.button("Run Test ", type="primary"):
214
+ with st.spinner('Wait for it...'):
215
+ time.sleep(5)
216
+
217
+ classifier = pipeline("sentiment-analysis")
218
+
219
+
220
+
221
+
222
+ # function for Document Question Answering model
223
+ def document_question_answering():
224
+ code = '''
225
+ from transformers import pipeline
226
+
227
+ classifier = pipeline("sentiment-analysis")
228
+ results = classifier("Transformers library is very helpful.")
229
+ '''
230
+ st.code(code, language='python')
231
+ with st.spinner('Wait for it...'):
232
+ time.sleep(5)
233
+
234
+
235
+
236
+
237
+
238
+ # function for Named Entity Recognition model
239
+ def named_entity_recognition():
240
+ code = '''
241
+ from transformers import pipeline
242
+
243
+ classifier = pipeline("sentiment-analysis")
244
+ results = classifier("Transformers library is very helpful.")
245
+ '''
246
+ st.code(code, language='python')
247
+ if st.button("Run Test ", type="primary"):
248
+ with st.spinner('Wait for it...'):
249
+ time.sleep(5)
250
+
251
+ classifier = pipeline("sentiment-analysis")
252
+
253
+
254
+ # function for translation model
255
+ def translation():
256
+ code = '''
257
+ from transformers import pipeline
258
+
259
+ classifier = pipeline("sentiment-analysis")
260
+ results = classifier("Transformers library is very helpful.")
261
+ '''
262
+ st.code(code, language='python')
263
+ if st.button("Run Test ", type="primary"):
264
+ with st.spinner('Wait for it...'):
265
+ time.sleep(5)
266
+ classifier = pipeline("sentiment-analysis")
267
+
268
+
269
+
270
+
271
+ col1, col2 = st.columns(2)
272
+ '''
273
+ - `"audio-classification"`: will return a [`AudioClassificationPipeline`].
274
+ - `"automatic-speech-recognition"`: will return a [`AutomaticSpeechRecognitionPipeline`].
275
+ - `"conversational"`: will return a [`ConversationalPipeline`].
276
+ - `"depth-estimation"`: will return a [`DepthEstimationPipeline`].
277
+ - `"document-question-answering"`: will return a [`DocumentQuestionAnsweringPipeline`].
278
+ - `"feature-extraction"`: will return a [`FeatureExtractionPipeline`].
279
+ - `"fill-mask"`: will return a [`FillMaskPipeline`]:.
280
+ - `"image-classification"`: will return a [`ImageClassificationPipeline`].
281
+ - `"image-feature-extraction"`: will return an [`ImageFeatureExtractionPipeline`].
282
+ - `"image-segmentation"`: will return a [`ImageSegmentationPipeline`].
283
+ - `"image-to-image"`: will return a [`ImageToImagePipeline`].
284
+ - `"image-to-text"`: will return a [`ImageToTextPipeline`].
285
+ - `"mask-generation"`: will return a [`MaskGenerationPipeline`].
286
+ - `"object-detection"`: will return a [`ObjectDetectionPipeline`].
287
+ - `"question-answering"`: will return a [`QuestionAnsweringPipeline`].
288
+ - `"summarization"`: will return a [`SummarizationPipeline`].
289
+ - `"table-question-answering"`: will return a [`TableQuestionAnsweringPipeline`].
290
+ - `"text2text-generation"`: will return a [`Text2TextGenerationPipeline`].
291
+ - `"text-classification"` (alias `"sentiment-analysis"` available): will return a
292
+ [`TextClassificationPipeline`].
293
+ - `"text-generation"`: will return a [`TextGenerationPipeline`]:.
294
+ - `"text-to-audio"` (alias `"text-to-speech"` available): will return a [`TextToAudioPipeline`]:.
295
+ - `"token-classification"` (alias `"ner"` available): will return a [`TokenClassificationPipeline`].
296
+ - `"translation"`: will return a [`TranslationPipeline`].
297
+ - `"translation_xx_to_yy"`: will return a [`TranslationPipeline`].
298
+ - `"video-classification"`: will return a [`VideoClassificationPipeline`].
299
+ - `"visual-question-answering"`: will return a [`VisualQuestionAnsweringPipeline`].
300
+ - `"zero-shot-classification"`: will return a [`ZeroShotClassificationPipeline`].
301
+ - `"zero-shot-image-classification"`: will return a [`ZeroShotImageClassificationPipeline`].
302
+ - `"zero-shot-audio-classification"`: will return a [`ZeroShotAudioClassificationPipeline`].
303
+ - `"zero-shot-object-detection"`: will return a [`ZeroShotObjectDetectionPipeline`].
304
+
305
+ '''
306
+
307
+
308
+ with col1:
309
+ taskType = st.radio(
310
+ "Select a type of task to perform",
311
+ [
312
+ "Sentiment Analysis or Text classification",
313
+ "Text Generation",
314
+ "Summarization",
315
+ "Image Classification",
316
+ "Image Segmentation",
317
+ "Object Detection",
318
+ "Audio Classification",
319
+ "Automatic Speech Recognition",
320
+ "Visual Question Answering",
321
+ "Document Question Answering",
322
+ "Image Captioning",
323
+
324
+ "Mask Filling",
325
+ "Named Entity Recognition",
326
+ "Translation"
327
+ ],
328
+ captions = [
329
+ "**pipeline(task=“sentiment-analysis”)**",
330
+ "pipeline(task=“text-generation”)",
331
+ "pipeline(task=“summarization”)",
332
+ "pipeline(task=“image-classification”)",
333
+ "pipeline(task=“image-segmentation”)",
334
+ "pipeline(task=“object-detection”)",
335
+ "pipeline(task=“audio-classification”)",
336
+ "pipeline(task=“automatic-speech-recognition”)",
337
+ "pipeline(task=“vqa”)",
338
+ "pipeline(task=“document-question-answering”)",
339
+ "pipeline(task=“image-to-text”)"
340
+
341
+ "Mask Filling",
342
+ "Named Entity Recognition",
343
+ "Translation"
344
+ ], index=0)
345
+
346
+
347
+ with col2:
348
+
349
+ st.subheader(f"Example: {taskType}")
350
+ if taskType == "Sentiment Analysis or Text classification":
351
+ sentiment_analysis()
352
+
353
+ if taskType == "Text Generation":
354
+ text_generation()
355
+
356
+ if taskType == "Summarization":
357
+ summarization()
358
+
359
+ if taskType == "Image Classification":
360
+ image_classification()
361
+
362
+ if taskType == "Image Segmentation":
363
+ image_segmentation()
364
+
365
+ if taskType == "Object Detection":
366
+ object_detection()
367
+
368
+ if taskType == "Audio Classification":
369
+ audio_classification()
370
+
371
+ if taskType == "Automatic Speech Recognition":
372
+ automatic_speech_recognition()
373
+
374
+ if taskType == "Document Question Answering":
375
+ document_question_answering()
376
+
377
+ if taskType == "Image Captioning":
378
+ image_captioning()
379
+
380
+ if taskType == "Mask Filling":
381
+ mask_filling()
382
+
383
+ if taskType == "Named Entity Recognition":
384
+ named_entity_recognition()
385
+
386
+ if taskType == "Translation":
387
+ translation()
388
+
389
+