rajistics commited on
Commit
75ebf28
1 Parent(s): d399bd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -0
app.py CHANGED
@@ -20,7 +20,121 @@ st.write(pydicom)
20
 
21
  #def print_memory_usage():
22
  # logging.info(f"RAM memory % used: {psutil.virtual_memory()[2]}")
 
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
 
26
 
 
20
 
21
  #def print_memory_usage():
22
  # logging.info(f"RAM memory % used: {psutil.virtual_memory()[2]}")
23
+ @st.cache(allow_output_mutation=True, suppress_st_warning=True, max_entries=1)
24
 
25
+ def load_model(model_name):
26
+ return (
27
+ AutoModelForSequenceClassification.from_pretrained(model_name),
28
+ AutoTokenizer.from_pretrained(model_name),
29
+ )
30
+
31
+ print ("before main")
32
+
33
+
34
+ st.title("Transformers Interpet Demo App")
35
+
36
+ print ("before main")
37
+
38
+ #image = Image.open("./images/tight@1920x_transparent.png")
39
+ #st.sidebar.image(image, use_column_width=True)
40
+ st.sidebar.markdown(
41
+ "Check out the package on [Github](https://github.com/cdpierse/transformers-interpret)"
42
+ )
43
+ st.info(
44
+ "Due to limited resources only low memory models are available. Run this [app locally](https://github.com/cdpierse/transformers-interpret-streamlit) to run the full selection of available models. "
45
+ )
46
+
47
+ # uncomment the options below to test out the app with a variety of classification models.
48
+ models = {
49
+ # "textattack/distilbert-base-uncased-rotten-tomatoes": "",
50
+ # "textattack/bert-base-uncased-rotten-tomatoes": "",
51
+ # "textattack/roberta-base-rotten-tomatoes": "",
52
+ # "mrm8488/bert-mini-finetuned-age_news-classification": "BERT-Mini finetuned on AG News dataset. Predicts news class (sports/tech/business/world) of text.",
53
+ # "nateraw/bert-base-uncased-ag-news": "BERT finetuned on AG News dataset. Predicts news class (sports/tech/business/world) of text.",
54
+ "distilbert-base-uncased-finetuned-sst-2-english": "DistilBERT model finetuned on SST-2 sentiment analysis task. Predicts positive/negative sentiment.",
55
+ # "ProsusAI/finbert": "BERT model finetuned to predict sentiment of financial text. Finetuned on Financial PhraseBank data. Predicts positive/negative/neutral.",
56
+ "sampathkethineedi/industry-classification": "DistilBERT Model to classify a business description into one of 62 industry tags.",
57
+ "MoritzLaurer/policy-distilbert-7d": "DistilBERT model finetuned to classify text into one of seven political categories.",
58
+ # # "MoritzLaurer/covid-policy-roberta-21": "(Under active development ) RoBERTA model finetuned to identify COVID policy measure classes ",
59
+ # "mrm8488/bert-tiny-finetuned-sms-spam-detection": "Tiny bert model finetuned for spam detection. 0 == not spam, 1 == spam",
60
+ }
61
+ model_name = st.sidebar.selectbox(
62
+ "Choose a classification model", list(models.keys())
63
+ )
64
+ model, tokenizer = load_model(model_name)
65
+
66
+
67
+ print ("Model loaded")
68
+ if model_name.startswith("textattack/"):
69
+ model.config.id2label = {0: "NEGATIVE (0) ", 1: "POSITIVE (1)"}
70
+ model.eval()
71
+ print ("Model Evaluated")
72
+ cls_explainer = SequenceClassificationExplainer(model=model, tokenizer=tokenizer)
73
+ print ("Model Explained")
74
+ if cls_explainer.accepts_position_ids:
75
+ emb_type_name = st.sidebar.selectbox(
76
+ "Choose embedding type for attribution.", ["word", "position"]
77
+ )
78
+ if emb_type_name == "word":
79
+ emb_type_num = 0
80
+ if emb_type_name == "position":
81
+ emb_type_num = 1
82
+ else:
83
+ emb_type_num = 0
84
+
85
+ explanation_classes = ["predicted"] + list(model.config.label2id.keys())
86
+ explanation_class_choice = st.sidebar.selectbox(
87
+ "Explanation class: The class you would like to explain output with respect to.",
88
+ explanation_classes,
89
+ )
90
+ my_expander = st.beta_expander(
91
+ "Click here for a description of models and their tasks"
92
+ )
93
+ with my_expander:
94
+ st.json(models)
95
+
96
+ # st.info("Max char limit of 350 (memory management)")
97
+ text = st.text_area(
98
+ "Enter text to be interpreted",
99
+ "I like you, I love you",
100
+ height=400,
101
+ max_chars=850,
102
+ )
103
+ print ("Before button")
104
+ if st.button('Say hello'):
105
+ st.write('Why hello there')
106
+ else:
107
+ st.write('Goodbye')
108
+ print ("After test button")
109
+
110
+ if st.button("Interpret Text"):
111
+ #print_memory_usage()
112
+ st.text("Output")
113
+ with st.spinner("Interpreting your text (This may take some time)"):
114
+ print ("Interpreting text")
115
+ if explanation_class_choice != "predicted":
116
+ word_attributions = cls_explainer(
117
+ text,
118
+ class_name=explanation_class_choice,
119
+ embedding_type=emb_type_num,
120
+ internal_batch_size=2,
121
+ )
122
+ else:
123
+ word_attributions = cls_explainer(
124
+ text, embedding_type=emb_type_num, internal_batch_size=2
125
+ )
126
+
127
+ if word_attributions:
128
+ print ("Word Attributions")
129
+ word_attributions_expander = st.beta_expander(
130
+ "Click here for raw word attributions"
131
+ )
132
+ with word_attributions_expander:
133
+ st.json(word_attributions)
134
+ components.v1.html(
135
+ cls_explainer.visualize()._repr_html_(), scrolling=True, height=350
136
+ )
137
+ print ("end of stuff")
138
 
139
 
140