Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files
.gitattributes
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
test.csv filter=lfs diff=lfs merge=lfs -text
|
2 |
+
train.csv filter=lfs diff=lfs merge=lfs -text
|
test.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c2513ce4abb98c4d1d216e3ca0d4377d57589a0989aa8c06a840509a16c786e8
|
3 |
+
size 60354593
|
train.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bd4084611bd27c939ba98e5e63bc3e5a2c1a4e99477dcba46c829e4c986c429d
|
3 |
+
size 68802655
|
train.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# pip install transformers
|
2 |
+
from transformers import pipeline
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
+
|
5 |
+
import torch
|
6 |
+
import torch.nn.functional as F
|
7 |
+
|
8 |
+
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
|
9 |
+
|
10 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
+
|
13 |
+
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
14 |
+
res = classifier(["We are very happy to show you the 🤗 Transformers Library", "We hope you don't hate it."])
|
15 |
+
|
16 |
+
#for result in res:
|
17 |
+
# print(res)
|
18 |
+
|
19 |
+
tokens = tokenizer.tokenize("We are very happy to show you the 🤗 Transformers Library")
|
20 |
+
token_ids = tokenizer.convert_tokens_to_ids(tokens)
|
21 |
+
input_ids = tokenizer("We are very happy to show you the 🤗 Transformers Library");
|
22 |
+
|
23 |
+
#print(f' Tokens: {tokens}')
|
24 |
+
#print(f'Token IDs: {token_ids}')
|
25 |
+
#print(f'Input IDs: {input_ids}')
|
26 |
+
|
27 |
+
x_train = ["We are very happy to show you the 🤗 Transformers Library",
|
28 |
+
"We hope you don't hate it."]
|
29 |
+
|
30 |
+
batch = tokenizer(x_train, padding=True, truncation=True, max_length=512, return_tensors="pt")
|
31 |
+
|
32 |
+
with torch.no_grad():
|
33 |
+
outputs = model(**batch, labels=torch.tensor([1,0]))
|
34 |
+
print(outputs)
|
35 |
+
predictions = F.softmax(outputs.logits, dim=1)
|
36 |
+
print(predictions)
|
37 |
+
labels = torch.argmax(predictions, dim=1)
|
38 |
+
print(labels)
|
39 |
+
labels = [model.config.id2label[label_id] for label_id in labels.tolist()]
|
40 |
+
print(labels)
|
41 |
+
|
42 |
+
save_directory = "saved"
|
43 |
+
tokenizer.save_pretrained(save_directory)
|
44 |
+
model.save_pretrained(save_directory)
|
45 |
+
|
46 |
+
tokenizer = AutoTokenizer.from_pretrained(save_directory)
|
47 |
+
model = AutoModelForSequenceClassification.from_pretrained(save_directory)
|