kelvinleong commited on
Commit
a2de75a
·
1 Parent(s): dba9431

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import torch
4
+ from transformers import BertTokenizer, BertForSequenceClassification
5
+
6
+ # Load the BERT tokenizer and model
7
+ tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
8
+ model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
9
+
10
+ # Define the prediction function
11
+ def predict_sentiment(text):
12
+ # Tokenize the text
13
+ encoded_text = tokenizer.encode_plus(
14
+ text,
15
+ max_length=128,
16
+ padding='max_length',
17
+ truncation=True,
18
+ return_attention_mask=True,
19
+ return_tensors='pt'
20
+ )
21
+
22
+ # Make the prediction
23
+ output = model(encoded_text['input_ids'], attention_mask=encoded_text['attention_mask'])
24
+ prediction = torch.argmax(output.logits, dim=1).item()
25
+
26
+ # Return the predicted sentiment
27
+ if prediction == 1:
28
+ return "Positive"
29
+ else:
30
+ return "Negative"
31
+
32
+ # Define the Streamlit app
33
+ def app():
34
+ st.title("BERT Sentiment Analysis")
35
+ st.write("Upload a CSV file with a 'text' column and I'll predict the sentiment for each row.")
36
+
37
+ # Get user input
38
+ file = st.file_uploader("Upload CSV file", type=["csv"])
39
+ if file is not None:
40
+ df = pd.read_csv(file)
41
+
42
+ # Make the predictions and add them to the dataframe
43
+ df['sentiment'] = df['text'].apply(predict_sentiment)
44
+
45
+ # Display the results
46
+ st.write(df)
47
+
48
+ if __name__ == '__main__':
49
+ app()