kaushikbar commited on
Commit
f657d03
·
1 Parent(s): b6907c2

Add application file

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ gr.Interface.load("huggingface/NDugar/debertav3-mnli-snli-anli").launch()
3
+
4
+ '''
5
+ import gradio as gr
6
+ import datetime
7
+ from transformers import pipeline
8
+
9
+ models = {'norsk': 'NbAiLab/nb-bert-base-mnli', #'english': 'DeepPavlov/xlm-roberta-large-en-ru-mnli',
10
+ English: Narsil/deberta-large-mnli-zero-cls
11
+ German: Sahajtomar/German_Zeroshot
12
+ Spanish: Recognai/zeroshot_selectra_medium
13
+ Italian: joeddav/xlm-roberta-large-xnl}
14
+ classifier = pipeline("zero-shot-classification", model="NbAiLab/nb-bert-base-mnli")
15
+
16
+
17
+ def sequence_to_classify(sequence, labels):
18
+ hypothesis_template = 'Dette eksempelet er {}.'
19
+ label_clean = str(labels).split(",")
20
+ response = classifier(sequence, label_clean, hypothesis_template=hypothesis_template, multi_class=True)
21
+ predicted_labels = response['labels']
22
+ predicted_scores = response['scores']
23
+ clean_output = {idx: float(predicted_scores.pop(0)) for idx in predicted_labels}
24
+ print("Date:{} , Sequece:{}, Labels: {}".format(
25
+ str(datetime.datetime.now()),
26
+ sequence,
27
+ predicted_labels)
28
+ )
29
+ return clean_output
30
+
31
+ example_text1="Folkehelseinstituttets mest optimistiske anslag er at alle voksne er ferdigvaksinert innen midten av september."
32
+ example_labels1="politikk,helse,sport,religion"
33
+ example_text2="Kutt smør i terninger, og la det temperere seg litt mens deigen elter. Ha hvetemel, sukker, gjær, salt og kardemomme i en bakebolle til kjøkkenmaskin. Bruker du fersk gjær kan du smuldre gjæren i bollen, eller røre den ut i melken. Alt vil ettehvert blande seg godt, så begge deler er like bra."
34
+ example_labels2="helse,sport,religion, mat"
35
+
36
+ iface = gr.Interface(
37
+ title = "Zero-shot Classification of Norwegian Text",
38
+ description = "Demo of zero-shot classification using NB-Bert base model (Norwegian).",
39
+ fn=sequence_to_classify,
40
+ inputs=[gr.inputs.Textbox(lines=2,
41
+ label="Write a norwegian text you would like to classify...",
42
+ placeholder="Text here..."),
43
+ gr.inputs.Textbox(lines=10,
44
+ label="Possible candidate labels",
45
+ placeholder="labels here...")],
46
+ outputs=gr.outputs.Label(num_top_classes=3),
47
+ capture_session=True,
48
+ interpretation="default"
49
+ ,examples=[
50
+ [example_text1, example_labels1],
51
+ [example_text2, example_labels2]
52
+ ])
53
+ iface.launch()
54
+ '''