robertou2 commited on
Commit
e4c1222
1 Parent(s): 7c18751

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -35
app.py CHANGED
@@ -34,41 +34,36 @@ def principal(tweets):
34
  tweet_list = [i.text for i in tweets]
35
  text= pd.DataFrame(tweet_list)
36
  text1=text[0].values
37
- indices1=tokenizer.batch_encode_plus(text1.tolist(),
38
- max_length=128,
39
- add_special_tokens=True,
40
- return_attention_mask=True,
41
- pad_to_max_length=True,
42
- truncation=True)
43
- input_ids1=indices1["input_ids"]
44
- attention_masks1=indices1["attention_mask"]
45
- prediction_inputs1= torch.tensor(input_ids1)
46
- prediction_masks1 = torch.tensor(attention_masks1)
47
- # Set the batch size.
48
- batch_size = 25
49
- # Create the DataLoader.
50
- prediction_data1 = TensorDataset(prediction_inputs1, prediction_masks1)
51
- prediction_sampler1 = SequentialSampler(prediction_data1)
52
- prediction_dataloader1 = DataLoader(prediction_data1, sampler=prediction_sampler1, batch_size=batch_size)
53
- print('Predicting labels for {:,} test sentences...'.format(len(prediction_inputs1)))
54
- # Put model in evaluation mode
55
- model.eval()
56
- # Tracking variables
57
- predictions = []
58
- # Predict
59
- for batch in prediction_dataloader1:
60
- batch = tuple(t.to(device) for t in batch)
61
- # Unpack the inputs from our dataloader
62
- b_input_ids1, b_input_mask1 = batch
63
- # Telling the model not to compute or store gradients, saving memory and # speeding up prediction
64
- with torch.no_grad():
65
- # Forward pass, calculate logit predictions
66
- outputs1 = model(b_input_ids1, token_type_ids=None,attention_mask=b_input_mask1)
67
- logits1 = outputs1[0]
68
- # Move logits and labels to CPU
69
- logits1 = logits1.detach().cpu().numpy()
70
- # Store predictions and true labels
71
- predictions.append(logits1)
72
  flat_predictions = [item for sublist in predictions for item in sublist]
73
  flat_predictions = np.argmax(flat_predictions, axis=1).flatten()#p = [i for i in classifier(tweet_list)]
74
  df = pd.DataFrame(list(zip(tweet_list, flat_predictions)),columns =['Latest'+str(number_of_tweets)+'Tweets'+' on '+search_words, 'Sexista'])
 
34
  tweet_list = [i.text for i in tweets]
35
  text= pd.DataFrame(tweet_list)
36
  text1=text[0].values
37
+ indices1=tokenizer.batch_encode_plus(text1.tolist(), max_length=128,add_special_tokens=True,return_attention_mask=True,pad_to_max_length=True,truncation=True)
38
+ input_ids1=indices1["input_ids"]
39
+ attention_masks1=indices1["attention_mask"]
40
+ prediction_inputs1= torch.tensor(input_ids1)
41
+ prediction_masks1 = torch.tensor(attention_masks1)
42
+ # Set the batch size.
43
+ batch_size = 25
44
+ # Create the DataLoader.
45
+ prediction_data1 = TensorDataset(prediction_inputs1, prediction_masks1)
46
+ prediction_sampler1 = SequentialSampler(prediction_data1)
47
+ prediction_dataloader1 = DataLoader(prediction_data1, sampler=prediction_sampler1, batch_size=batch_size)
48
+ print('Predicting labels for {:,} test sentences...'.format(len(prediction_inputs1)))
49
+ # Put model in evaluation mode
50
+ model.eval()
51
+ # Tracking variables
52
+ predictions = []
53
+ # Predict
54
+ for batch in prediction_dataloader1:
55
+ batch = tuple(t.to(device) for t in batch)
56
+ # Unpack the inputs from our dataloader
57
+ b_input_ids1, b_input_mask1 = batch
58
+ # Telling the model not to compute or store gradients, saving memory and # speeding up prediction
59
+ with torch.no_grad():
60
+ # Forward pass, calculate logit predictions
61
+ outputs1 = model(b_input_ids1, token_type_ids=None,attention_mask=b_input_mask1)
62
+ logits1 = outputs1[0]
63
+ # Move logits and labels to CPU
64
+ logits1 = logits1.detach().cpu().numpy()
65
+ # Store predictions and true labels
66
+ predictions.append(logits1)
 
 
 
 
 
67
  flat_predictions = [item for sublist in predictions for item in sublist]
68
  flat_predictions = np.argmax(flat_predictions, axis=1).flatten()#p = [i for i in classifier(tweet_list)]
69
  df = pd.DataFrame(list(zip(tweet_list, flat_predictions)),columns =['Latest'+str(number_of_tweets)+'Tweets'+' on '+search_words, 'Sexista'])