jeffhaines commited on
Commit
885b9b6
·
1 Parent(s): fbc7734

Create new file

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification
2
+ import torch
3
+ import streamlit as st
4
+ from transformers import pipeline
5
+ from transformers_interpret import SequenceClassificationExplainer
6
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
7
+
8
+ st.title('Ethics Classifier')
9
+ st.write('This app uses a pre-trained Distilbert model fine-tuned on the Commonsense Ethics dataset from the Aligning AI With Shared Human Values project (https://github.com/hendrycks/ethics). It judges whether a given action of scenario is wrong or not wrong and shows how the words in the scenario affected the judgment.')
10
+
11
+ loaded_model = DistilBertForSequenceClassification.from_pretrained('commonsense_ethics')
12
+ model_name = 'distilbert-base-uncased'
13
+ tokenizer = DistilBertTokenizerFast.from_pretrained(model_name)
14
+ cls_explainer = SequenceClassificationExplainer(loaded_model, tokenizer)
15
+
16
+ clf = pipeline("text-classification", model = loaded_model, tokenizer = tokenizer)
17
+
18
+ text = st.text_input('Enter a scenario or action.')
19
+
20
+ if text:
21
+ answer = clf(text)
22
+ label = 'wrong' if answer[0]['label'] == 'LABEL_1' else 'not wrong'
23
+ st.write(f'This action is {label} (confidence level {answer[0]["score"]*100:.2f}%).')
24
+ attributions = cls_explainer(text)
25
+ df = pd.DataFrame(attributions[1:-1])
26
+ df.rename(columns = {0: 'Token', 1: 'Contribution'}, inplace = True)
27
+ st.write(df.style.hide(axis = 'index'))
28
+ st.write(cls_explainer.visualize())