Jainesh212 commited on
Commit
624c70b
·
1 Parent(s): 20fdf2e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -20
app.py CHANGED
@@ -1,29 +1,19 @@
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
 
21
  # Define the Streamlit app
22
  def app():
23
- # Create a persistent DataFrame
24
  if 'results' not in st.session_state:
25
  st.session_state.results = pd.DataFrame(columns=['text', 'toxicity', 'score'])
26
-
27
  # Create a form for users to enter their text
28
  with st.form(key='text_form'):
29
  text_input = st.text_input(label='Enter your text:')
@@ -31,18 +21,25 @@ def app():
31
 
32
  # Classify the text and display the results
33
  if submit_button and text_input != '':
34
- label, score = classify_toxicity(text_input)
 
 
 
 
 
35
  st.write('Classification Result:')
36
  st.write(f'Text: {text_input}')
37
  st.write(f'Toxicity: {label}')
38
- st.write(f'Score: {score}')
39
-
40
  # Add the classification result to the persistent DataFrame
41
- st.session_state.results = st.session_state.results.append({'text': text_input, 'toxicity': label, 'score': score}, ignore_index=True)
 
 
42
 
43
  # Display the persistent DataFrame
44
  st.write('Classification Results:')
45
  st.write(st.session_state.results)
46
 
47
  if __name__ == '__main__':
48
- app()
 
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:')
 
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()