File size: 2,010 Bytes
9e2cb0a
7d33fb7
709816c
8a75de9
3a65c6e
8a75de9
709816c
7d33fb7
9c89421
7d33fb7
 
 
 
 
9429d1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fd5709
9429d1c
6fd5709
 
9429d1c
 
 
 
 
 
 
7d33fb7
 
 
 
 
db6e115
a93e5c6
db6e115
7d33fb7
9e2cb0a
 
 
 
 
 
 
 
 
 
 
dce65c7
9e2cb0a
 
ba70565
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import gradio as gr
import torch
from datasets import load_dataset
from console_logging.console import Console
import numpy as np
console = Console()

dataset = load_dataset("zeroshot/twitter-financial-news-sentiment", )

from transformers import AutoModelForSequenceClassification, AutoTokenizer

model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")

labels = [label for label in dataset['train'].features.keys() if label not in ['text']]

def preprocess_data(examples):
  # take a batch of texts
  text = examples["text"]
  # encode them
  encoding = tokenizer(text, padding="max_length", truncation=True, max_length=128)
  # add labels
  labels_batch = {k: examples[k] for k in examples.keys() if k in labels}
  # create numpy array of shape (batch_size, num_labels)
  labels_matrix = np.zeros((len(text), len(labels)))
  # fill numpy array
  for idx, label in enumerate(labels):
    labels_matrix[:, idx] = labels_batch[label]

  encoding["labels"] = labels_matrix.tolist()
  
  return encoding

encoded_dataset = dataset.map(preprocess_data, batched=True, remove_columns=dataset['train'].column_names)

example = encoded_dataset['train'][0]
console.log(example.keys())







def sentiment_score(review):
  tokens = tokenizer.encode(review, return_tensors='pt')
  result = model(tokens)
  return int(torch.argmax(result.logits))

dataset['sentiment'] = dataset['train']['text'].apply(lambda x: sentiment_score(x[:512]))

"""

categories = ('Car in good condition','Damaged Car')

def is_car(x) : return x[0].isupper()

def image_classifier(img):
    pred,index,probs = learn.predict(img) 
    return dict(zip(categories, map(float,probs)))

# image = gr.inputs.Image(shape=(192,192))
image = gr.components.Image(shape=(192,192))
label = gr.components.Label()
examples = ['./car.jpg','./crash.jpg','./carf.jpg']

intf = gr.Interface(fn= image_classifier,inputs=image,outputs=label,examples=examples)
intf.launch()"""