saideep-arikontham commited on
Commit
b48f1b7
1 Parent(s): 3564b10

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
+ import torch
4
+ from peft import PeftModel, PeftConfig
5
+
6
+ base_model = "cardiffnlp/twitter-roberta-base-sentiment-latest"
7
+ adapter_model = 'saideep-arikontham/twitter-roberta-base-sentiment-latest-biden-stance'
8
+
9
+ # define label maps
10
+ id2label = {0: "Anti-Biden", 1 : "Pro-Biden"}
11
+ label2id = {"Anti-Biden" : 0, "Pro-Biden" : 1}
12
+
13
+ # generate classification model from model_checkpoint
14
+ model = AutoModelForSequenceClassification.from_pretrained(base_model, num_labels=2, id2label = id2label, label2id = label2id, ignore_mismatched_sizes=True)
15
+
16
+ model = PeftModel.from_pretrained(model, adapter_model)
17
+ tokenizer = AutoTokenizer.from_pretrained(adapter_model)
18
+
19
+ def greet(text):
20
+
21
+ model.to('cpu')
22
+ inputs = tokenizer.encode(text, return_tensors="pt").to("cpu")
23
+ # compute logits
24
+ logits = model(inputs).logits
25
+ # convert logits to label
26
+ predictions = torch.argmax(logits)
27
+
28
+ return "This text is " + id2label[predictions.tolist()] + "!!"