Jainesh212 commited on
Commit
567e725
·
1 Parent(s): 624c70b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -19
app.py CHANGED
@@ -1,45 +1,62 @@
1
  import streamlit as st
 
2
  import pandas as pd
3
- from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
4
 
5
- # Load the pre-trained BERT model and pipeline for text classification
 
 
6
  model_name = 'nlptown/bert-base-multilingual-uncased-sentiment'
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
- classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
 
 
 
 
 
 
 
10
 
11
  # Define the Streamlit app
12
  def app():
13
- # Create a persistent DataFrame to store the classification results
14
  if 'results' not in st.session_state:
15
  st.session_state.results = pd.DataFrame(columns=['text', 'toxicity', 'score'])
16
 
 
 
 
 
 
 
 
17
  # Create a form for users to enter their text
18
  with st.form(key='text_form'):
19
- text_input = st.text_input(label='Enter your text:')
20
  submit_button = st.form_submit_button(label='Classify')
21
 
22
  # Classify the text and display the results
23
  if submit_button and text_input != '':
24
- # Classify the text using the pre-trained BERT model
25
- result = classifier(text_input)[0]
26
- label = result['label']
27
- score = result['score']
28
 
29
- # Display the classification results
30
- st.write('Classification Result:')
31
- st.write(f'Text: {text_input}')
32
- st.write(f'Toxicity: {label}')
33
- st.write(f'Score: {score:.3f}')
34
 
35
  # Add the classification result to the persistent DataFrame
36
- st.session_state.results = st.session_state.results.append(
37
- {'text': text_input, 'toxicity': label, 'score': score}, ignore_index=True
38
- )
39
 
40
  # Display the persistent DataFrame
41
- st.write('Classification Results:')
42
- st.write(st.session_state.results)
 
 
 
 
 
 
43
 
44
  if __name__ == '__main__':
45
  app()
 
1
  import streamlit as st
2
+ import transformers
3
  import pandas as pd
 
4
 
5
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
6
+
7
+ # Load the pre-trained BERT model
8
  model_name = 'nlptown/bert-base-multilingual-uncased-sentiment'
9
  tokenizer = AutoTokenizer.from_pretrained(model_name)
10
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
11
+ pipeline = TextClassificationPipeline(model=model, tokenizer=tokenizer, framework='pt', task='text-classification')
12
+
13
+ # Define the toxicity classification function
14
+ def classify_toxicity(text):
15
+ result = pipeline(text)[0]
16
+ label = result['label']
17
+ score = result['score']
18
+ return label, score
19
 
20
  # Define the Streamlit app
21
  def app():
22
+ # Create a persistent DataFrame
23
  if 'results' not in st.session_state:
24
  st.session_state.results = pd.DataFrame(columns=['text', 'toxicity', 'score'])
25
 
26
+ # Set page title and favicon
27
+ st.set_page_config(page_title='Toxicity Classifier', page_icon=':microscope:')
28
+
29
+ # Set app title and description
30
+ st.title('Toxicity Classifier')
31
+ st.markdown('This app uses a pre-trained BERT model to classify text into three toxicity levels: **positive**, **neutral**, and **negative**.')
32
+
33
  # Create a form for users to enter their text
34
  with st.form(key='text_form'):
35
+ text_input = st.text_input(label='Enter your text:', help='Enter some text to classify.')
36
  submit_button = st.form_submit_button(label='Classify')
37
 
38
  # Classify the text and display the results
39
  if submit_button and text_input != '':
40
+ label, score = classify_toxicity(text_input)
 
 
 
41
 
42
+ # Create a collapsible section for the classification result
43
+ with st.beta_expander('Classification Result', expanded=True):
44
+ st.write(f'Text: {text_input}')
45
+ st.write(f'Toxicity: {label}')
46
+ st.write(f'Score: {score:.4f}')
47
 
48
  # Add the classification result to the persistent DataFrame
49
+ st.session_state.results = st.session_state.results.append({'text': text_input, 'toxicity': label, 'score': score}, ignore_index=True)
 
 
50
 
51
  # Display the persistent DataFrame
52
+ if not st.session_state.results.empty:
53
+ st.markdown('## Classification Results')
54
+ st.dataframe(st.session_state.results.style.highlight_max(subset=['score'], color='lightgreen'))
55
+
56
+ # Add a footer with some info and links
57
+ st.markdown('---')
58
+ st.markdown('Made with :heart: by ChatGPT')
59
+ st.markdown('[GitHub](https://github.com/chatgpt/toxicity-classifier) - [OpenAI Blog](https://openai.com/blog/) - [Streamlit Docs](https://docs.streamlit.io/)')
60
 
61
  if __name__ == '__main__':
62
  app()