Files changed (1) hide show
  1. app.py +0 -59
app.py DELETED
@@ -1,59 +0,0 @@
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
- # Set page title and favicon
28
- st.set_page_config(page_title='Toxicity Classification App', page_icon=':guardsman:')
29
-
30
- # Set app header
31
- st.write('# Toxicity Classification App')
32
- st.write('Enter some text and the app will classify its toxicity.')
33
-
34
- # Create a form for users to enter their text
35
- with st.form(key='text_form'):
36
- text_input = st.text_input(label='Enter your text:')
37
- submit_button = st.form_submit_button(label='Classify')
38
-
39
- # Classify the text and display the results
40
- if submit_button and text_input != '':
41
- label, score = classify_toxicity(text_input)
42
- st.write('## Classification Result')
43
- st.write(f'**Text:** {text_input}')
44
- st.write(f'**Toxicity:** {label}')
45
- st.write(f'**Score:** {score:.2f}')
46
-
47
- # Add the classification result to the persistent DataFrame
48
- st.session_state.results = st.session_state.results.append({'text': text_input, 'toxicity': label, 'score': score}, ignore_index=True)
49
-
50
- # Display the persistent DataFrame
51
- st.write('## Classification Results')
52
- st.write(st.session_state.results)
53
-
54
- # Display a chart of the classification results
55
- chart_data = st.session_state.results.groupby('toxicity').size().reset_index(name='count')
56
- chart = st.bar_chart(chart_data.set_index('toxicity'))
57
-
58
- if __name__ == '__main__':
59
- app()