Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app (1).py +37 -0
- requirements (1).txt +3 -0
app (1).py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
4 |
+
import torch
|
5 |
+
|
6 |
+
@st.cache_resource
|
7 |
+
def get_model():
|
8 |
+
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
9 |
+
model = BertForSequenceClassification.from_pretrained('tournas/FineTuneBert')
|
10 |
+
return tokenizer, model
|
11 |
+
|
12 |
+
tokenizer, model = get_model()
|
13 |
+
|
14 |
+
user_input = st.text_area('Enter Text to Analyze')
|
15 |
+
button = st.button('Analyze')
|
16 |
+
|
17 |
+
d = {
|
18 |
+
|
19 |
+
1: 'Toxic',
|
20 |
+
0: 'Non Toxic'
|
21 |
+
}
|
22 |
+
|
23 |
+
if user_input and button:
|
24 |
+
st.write('Analyzing...please wait.')
|
25 |
+
|
26 |
+
# Tokenization with padding and truncation
|
27 |
+
test_sample = tokenizer([user_input], padding=True, truncation=True, max_length=512, return_tensors='pt')
|
28 |
+
|
29 |
+
# Get output from the model
|
30 |
+
output = model(**test_sample)
|
31 |
+
|
32 |
+
# Softmax for probabilities
|
33 |
+
probs = torch.softmax(output.logits, dim=-1).detach().numpy()
|
34 |
+
|
35 |
+
# Prediction: Find the class with the highest probability
|
36 |
+
y_pred = np.argmax(probs, axis=1)
|
37 |
+
st.write('prediction: ',d[y_pred[0]])
|
requirements (1).txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
torch
|
3 |
+
transformers
|