robertou2 commited on
Commit
54e1678
·
1 Parent(s): 2f4222d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -51
app.py CHANGED
@@ -65,62 +65,67 @@ def run():
65
  termino=st.checkbox('Término')
66
  usuario=st.checkbox('Usuario')
67
  submit_button = st.form_submit_button(label='Analizar')
 
68
  if submit_button:
69
  date_since = "2020-09-14"
70
- if (termino):
 
 
 
71
  new_search = search_words + " -filter:retweets"
72
  tweets =tw.Cursor(api.search_tweets,q=new_search,lang="es",since=date_since).items(number_of_tweets)
73
  elif (usuario):
74
  tweets = api.user_timeline(screen_name = search_words,count=number_of_tweets)
75
-
76
- #new_search = search_words + " -filter:retweets"
77
- #tweets = tweepy.Cursor(api.search,q=new_search,lang="es",since=date_since).items(number_of_tweets)
78
- #tweets =tw.Cursor(api.search_tweets,q=search_words).items(number_of_tweets)
79
- #tweets =tw.Cursor(api.search_tweets,q=new_search,lang="es",since=date_since).items(number_of_tweets)
80
- tweet_list = [i.text for i in tweets]
81
- #tweet_list = [strip_undesired_chars(i.text) for i in tweets]
82
- text= pd.DataFrame(tweet_list)
83
- text[0] = text[0].apply(preprocess)
84
- text1=text[0].values
85
- indices1=tokenizer.batch_encode_plus(text1.tolist(),
86
- max_length=128,
87
- add_special_tokens=True,
88
- return_attention_mask=True,
89
- pad_to_max_length=True,
90
- truncation=True)
91
- input_ids1=indices1["input_ids"]
92
- attention_masks1=indices1["attention_mask"]
93
- prediction_inputs1= torch.tensor(input_ids1)
94
- prediction_masks1 = torch.tensor(attention_masks1)
95
- # Set the batch size.
96
- batch_size = 25
97
- # Create the DataLoader.
98
- prediction_data1 = TensorDataset(prediction_inputs1, prediction_masks1)
99
- prediction_sampler1 = SequentialSampler(prediction_data1)
100
- prediction_dataloader1 = DataLoader(prediction_data1, sampler=prediction_sampler1, batch_size=batch_size)
101
- print('Predicting labels for {:,} test sentences...'.format(len(prediction_inputs1)))
102
- # Put model in evaluation mode
103
- model.eval()
104
- # Tracking variables
105
- predictions = []
106
- # Predict
107
- for batch in prediction_dataloader1:
108
- batch = tuple(t.to(device) for t in batch)
109
- # Unpack the inputs from our dataloader
110
- b_input_ids1, b_input_mask1 = batch
111
- # Telling the model not to compute or store gradients, saving memory and # speeding up prediction
112
- with torch.no_grad():
113
- # Forward pass, calculate logit predictions
114
- outputs1 = model(b_input_ids1, token_type_ids=None,attention_mask=b_input_mask1)
115
- logits1 = outputs1[0]
116
- # Move logits and labels to CPU
117
- logits1 = logits1.detach().cpu().numpy()
118
- # Store predictions and true labels
119
- predictions.append(logits1)
120
- flat_predictions = [item for sublist in predictions for item in sublist]
121
- flat_predictions = np.argmax(flat_predictions, axis=1).flatten()#p = [i for i in classifier(tweet_list)]
122
- df = pd.DataFrame(list(zip(tweet_list, flat_predictions)),columns =['Latest'+str(number_of_tweets)+'Tweets'+' on '+search_words, 'Sexista'])
123
- df['Sexista']= np.where(df['Sexista']== 0, 'No Sexista', 'Sexista')
124
- st.table(df)
 
125
  #st.write(df)
126
  run()
 
65
  termino=st.checkbox('Término')
66
  usuario=st.checkbox('Usuario')
67
  submit_button = st.form_submit_button(label='Analizar')
68
+ error=False
69
  if submit_button:
70
  date_since = "2020-09-14"
71
+ if ( termino == False and usuario == False):
72
+ st.text('Error no se ha seleccionado ningun check')
73
+ error=True
74
+ elif (termino):
75
  new_search = search_words + " -filter:retweets"
76
  tweets =tw.Cursor(api.search_tweets,q=new_search,lang="es",since=date_since).items(number_of_tweets)
77
  elif (usuario):
78
  tweets = api.user_timeline(screen_name = search_words,count=number_of_tweets)
79
+ elif ( termino == True and usuario == True):
80
+ st.text('Error se han seleccionado los dos check')
81
+ error=True
82
+
83
+
84
+ if (error == False):
85
+ tweet_list = [i.text for i in tweets]
86
+ #tweet_list = [strip_undesired_chars(i.text) for i in tweets]
87
+ text= pd.DataFrame(tweet_list)
88
+ text[0] = text[0].apply(preprocess)
89
+ text1=text[0].values
90
+ indices1=tokenizer.batch_encode_plus(text1.tolist(),
91
+ max_length=128,
92
+ add_special_tokens=True,
93
+ return_attention_mask=True,
94
+ pad_to_max_length=True,
95
+ truncation=True)
96
+ input_ids1=indices1["input_ids"]
97
+ attention_masks1=indices1["attention_mask"]
98
+ prediction_inputs1= torch.tensor(input_ids1)
99
+ prediction_masks1 = torch.tensor(attention_masks1)
100
+ # Set the batch size.
101
+ batch_size = 25
102
+ # Create the DataLoader.
103
+ prediction_data1 = TensorDataset(prediction_inputs1, prediction_masks1)
104
+ prediction_sampler1 = SequentialSampler(prediction_data1)
105
+ prediction_dataloader1 = DataLoader(prediction_data1, sampler=prediction_sampler1, batch_size=batch_size)
106
+ print('Predicting labels for {:,} test sentences...'.format(len(prediction_inputs1)))
107
+ # Put model in evaluation mode
108
+ model.eval()
109
+ # Tracking variables
110
+ predictions = []
111
+ # Predict
112
+ for batch in prediction_dataloader1:
113
+ batch = tuple(t.to(device) for t in batch)
114
+ # Unpack the inputs from our dataloader
115
+ b_input_ids1, b_input_mask1 = batch
116
+ # Telling the model not to compute or store gradients, saving memory and # speeding up prediction
117
+ with torch.no_grad():
118
+ # Forward pass, calculate logit predictions
119
+ outputs1 = model(b_input_ids1, token_type_ids=None,attention_mask=b_input_mask1)
120
+ logits1 = outputs1[0]
121
+ # Move logits and labels to CPU
122
+ logits1 = logits1.detach().cpu().numpy()
123
+ # Store predictions and true labels
124
+ predictions.append(logits1)
125
+ flat_predictions = [item for sublist in predictions for item in sublist]
126
+ flat_predictions = np.argmax(flat_predictions, axis=1).flatten()#p = [i for i in classifier(tweet_list)]
127
+ df = pd.DataFrame(list(zip(tweet_list, flat_predictions)),columns =['Latest'+str(number_of_tweets)+'Tweets'+' on '+search_words, 'Sexista'])
128
+ df['Sexista']= np.where(df['Sexista']== 0, 'No Sexista', 'Sexista')
129
+ st.table(df)
130
  #st.write(df)
131
  run()