Keshavp08 commited on
Commit
8ca4db4
·
1 Parent(s): e90812e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import torch
5
+ import os
6
+ from transformers import BertTokenizer, BertForSequenceClassification
7
+
8
+ # Load pre-trained BERT model and tokenizer
9
+ tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
10
+ model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2)
11
+ model.eval()
12
+
13
+ # Define function to classify text
14
+ def classify_text(text):
15
+ inputs = tokenizer(text, padding=True, truncation=True, return_tensors='pt')
16
+ outputs = model(**inputs)
17
+ probs = torch.softmax(outputs.logits, dim=1).detach().numpy()[0]
18
+ return probs[1]
19
+
20
+ # Define function to save classification results to a persistent DataFrame
21
+ def save_to_df(text, label):
22
+ data = {'text': [text], 'toxicity': [label]}
23
+ df = pd.DataFrame(data)
24
+ df.to_csv('results.csv', mode='a', header=not os.path.exists('results.csv'), index=False)
25
+
26
+ # Load existing results from persistent DataFrame
27
+ if os.path.exists('results.csv'):
28
+ results_df = pd.read_csv('results.csv')
29
+ else:
30
+ results_df = pd.DataFrame(columns=['text', 'toxicity'])
31
+
32
+ # Define Streamlit app
33
+ def app():
34
+ st.title('Toxicity Classification App')
35
+
36
+ # Allow user to input text
37
+ text = st.text_input('Enter text to classify:')
38
+ if text:
39
+ # Classify text and display results
40
+ label = classify_text(text)
41
+ st.write(f'Toxic probability: {label:.2f}')
42
+ st.write('Classification: ', 'Toxic' if label >= 0.5 else 'Non-toxic')
43
+
44
+ # Allow user to save results
45
+ if st.button('Save results'):
46
+ save_to_df(text, label)
47
+ st.success('Results saved to DataFrame')
48
+
49
+ # Display existing results
50
+ st.subheader('Existing Results')
51
+ st.write(results_df)