karan99300 commited on
Commit
0b631d9
·
verified ·
1 Parent(s): a521b82

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +39 -0
  2. train.csv +0 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import BertTokenizer, BertForSequenceClassification
3
+ import gradio as gr
4
+
5
+ label_dict={'neutral': 0,'negative': 1, 'positive': 2}
6
+
7
+ model = BertForSequenceClassification.from_pretrained("bert-base-uncased",
8
+ num_labels=len(label_dict),
9
+ output_attentions=False,
10
+ output_hidden_states=False)
11
+ tokenizer = BertTokenizer.from_pretrained('bert-base-uncased',
12
+ do_lower_case=True)
13
+ model.load_state_dict(torch.load('finetuned_BERT_epoch_2.model',map_location='cpu'))
14
+ model.eval()
15
+
16
+ def get_key_by_value(dictionary, target_value):
17
+ for key, value in dictionary.items():
18
+ if value == target_value:
19
+ return key
20
+
21
+ def predict_sentiment(text):
22
+ inputs = tokenizer(text, return_tensors="pt")
23
+ inputs.to('cpu')
24
+ with torch.no_grad():
25
+ outputs = model(**inputs)
26
+ logits = outputs.logits
27
+ probabilities = torch.nn.functional.softmax(logits, dim=1)
28
+ predicted_class = torch.argmax(probabilities, dim=1).item()
29
+ return get_key_by_value(label_dict,predicted_class)
30
+
31
+ iface = gr.Interface(
32
+ fn=predict_sentiment,
33
+ inputs=gr.Textbox(),
34
+ outputs=gr.Textbox(),
35
+ live=True,
36
+ title="BERT Sentiment Analysis (CPU)",
37
+ description="Enter a text and get sentiment prediction. Model runs on CPU.",
38
+ )
39
+ iface.launch()
train.csv ADDED
The diff for this file is too large to render. See raw diff