Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,39 @@
|
|
| 1 |
-
from transformers import AutoModelForSequenceClassification
|
| 2 |
-
from transformers import TFAutoModelForSequenceClassification
|
| 3 |
-
from transformers import AutoTokenizer
|
| 4 |
import numpy as np
|
| 5 |
from scipy.special import softmax
|
| 6 |
-
import csv
|
| 7 |
import urllib.request
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
new_text = []
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
for t in text.split(" "):
|
| 15 |
-
t = '@user' if t.startswith('@') and len(t) > 1 else t
|
| 16 |
-
t = 'http' if t.startswith('http') else t
|
| 17 |
-
new_text.append(t)
|
| 18 |
-
return " ".join(new_text)
|
| 19 |
-
|
| 20 |
-
# Tasks:
|
| 21 |
-
# emoji, emotion, hate, irony, offensive, sentiment
|
| 22 |
-
# stance/abortion, stance/atheism, stance/climate, stance/feminist, stance/hillary
|
| 23 |
-
|
| 24 |
-
task='sentiment'
|
| 25 |
MODEL = f"cardiffnlp/twitter-roberta-base-{task}"
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
labels=[]
|
| 31 |
mapping_link = f"https://raw.githubusercontent.com/cardiffnlp/tweeteval/main/datasets/{task}/mapping.txt"
|
| 32 |
with urllib.request.urlopen(mapping_link) as f:
|
| 33 |
-
|
| 34 |
-
csvreader = csv.reader(html, delimiter='\t')
|
| 35 |
-
labels = [row[1] for row in csvreader if len(row) > 1]
|
| 36 |
-
|
| 37 |
-
# PT
|
| 38 |
-
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
|
| 39 |
-
model.save_pretrained(MODEL)
|
| 40 |
|
|
|
|
| 41 |
text = "Good night 😊"
|
| 42 |
-
|
| 43 |
-
encoded_input = tokenizer(text, return_tensors='pt')
|
| 44 |
output = model(**encoded_input)
|
| 45 |
-
scores = output
|
| 46 |
-
scores = softmax(scores)
|
| 47 |
-
|
| 48 |
-
# # TF
|
| 49 |
-
# model = TFAutoModelForSequenceClassification.from_pretrained(MODEL)
|
| 50 |
-
# model.save_pretrained(MODEL)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
# output = model(encoded_input)
|
| 55 |
-
# scores = output[0][0].numpy()
|
| 56 |
-
# scores = softmax(scores)
|
| 57 |
-
|
| 58 |
-
ranking = np.argsort(scores)
|
| 59 |
-
ranking = ranking[::-1]
|
| 60 |
for i in range(scores.shape[0]):
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
print(f"{i+1}) {l} {np.round(float(s), 4)}")
|
|
|
|
| 1 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from scipy.special import softmax
|
|
|
|
| 4 |
import urllib.request
|
| 5 |
+
import csv
|
| 6 |
+
from huggingface_hub import snapshot_download
|
| 7 |
|
| 8 |
+
# Define model
|
| 9 |
+
task = 'sentiment'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
MODEL = f"cardiffnlp/twitter-roberta-base-{task}"
|
| 11 |
|
| 12 |
+
# Download model
|
| 13 |
+
snapshot_download(repo_id=MODEL)
|
| 14 |
+
|
| 15 |
+
# Load tokenizer and model
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL, local_files_only=True)
|
| 17 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL, local_files_only=True)
|
| 18 |
+
|
| 19 |
+
# Preprocessing function
|
| 20 |
+
def preprocess(text):
|
| 21 |
+
return " ".join(['@user' if t.startswith('@') else 'http' if t.startswith('http') else t for t in text.split()])
|
| 22 |
|
| 23 |
+
# Load labels
|
| 24 |
+
labels = []
|
| 25 |
mapping_link = f"https://raw.githubusercontent.com/cardiffnlp/tweeteval/main/datasets/{task}/mapping.txt"
|
| 26 |
with urllib.request.urlopen(mapping_link) as f:
|
| 27 |
+
labels = [row[1] for row in csv.reader(f.read().decode('utf-8').split("\n"), delimiter='\t') if len(row) > 1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Sentiment analysis
|
| 30 |
text = "Good night 😊"
|
| 31 |
+
encoded_input = tokenizer(preprocess(text), return_tensors='pt')
|
|
|
|
| 32 |
output = model(**encoded_input)
|
| 33 |
+
scores = softmax(output.logits.detach().numpy()[0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# Print results
|
| 36 |
+
ranking = np.argsort(scores)[::-1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
for i in range(scores.shape[0]):
|
| 38 |
+
print(f"{i+1}) {labels[ranking[i]]} {np.round(float(scores[ranking[i]]), 4)}")
|
| 39 |
+
|
|
|