Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
|
|
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()
|