Spaces:
Runtime error
Runtime error
Upload existing code from zero-shot-example
Browse files
app.py
CHANGED
@@ -1,4 +1,61 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
|
3 |
x = st.slider('Select a value')
|
4 |
-
st.write(x, 'squared is', x * x)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
|
4 |
|
5 |
x = st.slider('Select a value')
|
6 |
+
st.write(x, 'squared is', x * x)
|
7 |
+
|
8 |
+
model_ids = {
|
9 |
+
'Bart MNLI': 'facebook/bart-large-mnli',
|
10 |
+
'Bart MNLI + Yahoo Answers': 'joeddav/bart-large-mnli-yahoo-answers',
|
11 |
+
'XLM Roberta XNLI (cross-lingual)': 'joeddav/xlm-roberta-large-xnli'
|
12 |
+
}
|
13 |
+
|
14 |
+
MODEL_DESC = {
|
15 |
+
'Bart MNLI': """Bart with a classification head trained on MNLI.\n\nSequences are posed as NLI premises and topic labels are turned into premises, i.e. `business` -> `This text is about business.`""",
|
16 |
+
'Bart MNLI + Yahoo Answers': """Bart with a classification head trained on MNLI and then further fine-tuned on Yahoo Answers topic classification.\n\nSequences are posed as NLI premises and topic labels are turned into premises, i.e. `business` -> `This text is about business.`""",
|
17 |
+
'XLM Roberta XNLI (cross-lingual)': """XLM Roberta, a cross-lingual model, with a classification head trained on XNLI. Supported languages include: _English, French, Spanish, German, Greek, Bulgarian, Russian, Turkish, Arabic, Vietnamese, Thai, Chinese, Hindi, Swahili, and Urdu_.
|
18 |
+
Note that this model seems to be less reliable than the English-only models when classifying longer sequences.
|
19 |
+
Examples were automatically translated and may contain grammatical mistakes.
|
20 |
+
Sequences are posed as NLI premises and topic labels are turned into premises, i.e. `business` -> `This text is about business.`""",
|
21 |
+
}
|
22 |
+
|
23 |
+
device = 0 if torch.cuda.is_available() else -1
|
24 |
+
|
25 |
+
@st.cache_resource
|
26 |
+
def load_models():
|
27 |
+
return {id: AutoModelForSequenceClassification.from_pretrained(id) for id in model_ids.values()}
|
28 |
+
|
29 |
+
models = load_models()
|
30 |
+
|
31 |
+
@st.cache_resource
|
32 |
+
def load_tokenizer(tok_id):
|
33 |
+
return AutoTokenizer.from_pretrained(tok_id)
|
34 |
+
|
35 |
+
def get_most_likely(nli_model_id, sequence, labels, hypothesis_template, multi_class):
|
36 |
+
classifier = pipeline(
|
37 |
+
'zero-shot-classification',
|
38 |
+
model=models[nli_model_id],
|
39 |
+
tokenizer=load_tokenizer(nli_model_id),
|
40 |
+
device=device
|
41 |
+
)
|
42 |
+
outputs = classifier(
|
43 |
+
sequence,
|
44 |
+
candidate_labels=labels,
|
45 |
+
hypothesis_template=hypothesis_template,
|
46 |
+
multi_label=multi_class
|
47 |
+
)
|
48 |
+
return outputs['labels'], outputs['scores']
|
49 |
+
|
50 |
+
def main():
|
51 |
+
|
52 |
+
hypothesis_template = "This text is about {}."
|
53 |
+
|
54 |
+
model_desc = st.sidebar.selectbox('Model', list(MODEL_DESC.keys()), 0)
|
55 |
+
st.sidebar.markdown('#### Model Description')
|
56 |
+
st.sidebar.markdown(MODEL_DESC[model_desc])
|
57 |
+
|
58 |
+
model_id = model_ids[model_desc]
|
59 |
+
|
60 |
+
if __name__ == '__main__':
|
61 |
+
main()
|