Schiro commited on
Commit
5ce7ce8
·
verified ·
1 Parent(s): fa448ff

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +3 -4
  2. model_setup.py +10 -2
app.py CHANGED
@@ -1,10 +1,9 @@
1
  import gradio as gr
2
- from model_setup import predict, config
3
 
4
  def analyze_sentiment(text):
5
- idx = predict(text)
6
- label = config.id2label[idx]
7
- return label
8
 
9
  demo = gr.Interface(
10
  fn=analyze_sentiment,
 
1
  import gradio as gr
2
+ from model_setup import predict_scores, config
3
 
4
  def analyze_sentiment(text):
5
+ scores = predict_scores(text)
6
+ return {config.id2label[i]: scores[i] for i in range(3)}
 
7
 
8
  demo = gr.Interface(
9
  fn=analyze_sentiment,
model_setup.py CHANGED
@@ -19,7 +19,7 @@ MODEL = "cardiffnlp/twitter-roberta-base-sentiment-latest"
19
 
20
  tokenizer = AutoTokenizer.from_pretrained(MODEL)
21
  config = AutoConfig.from_pretrained(MODEL)
22
- model = AutoModelForSequenceClassification.from_pretrained(MODEL)
23
 
24
 
25
  # Predict sentiment of a list of texts, in batches; returns class indices
@@ -36,4 +36,12 @@ def predict_batch(texts, batch_size=32):
36
 
37
  # Predict sentiment of a single text; returns the class index
38
  def predict(text):
39
- return predict_batch([text])[0]
 
 
 
 
 
 
 
 
 
19
 
20
  tokenizer = AutoTokenizer.from_pretrained(MODEL)
21
  config = AutoConfig.from_pretrained(MODEL)
22
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL, use_safetensors=True)
23
 
24
 
25
  # Predict sentiment of a list of texts, in batches; returns class indices
 
36
 
37
  # Predict sentiment of a single text; returns the class index
38
  def predict(text):
39
+ return predict_batch([text])[0]
40
+
41
+
42
+ # Probability over the three classes for a single text
43
+ def predict_scores(text):
44
+ encoded = tokenizer(preprocess(text), return_tensors='pt', truncation=True)
45
+ with torch.no_grad():
46
+ logits = model(**encoded).logits
47
+ return logits.softmax(dim=1)[0].tolist()