Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -14,6 +14,8 @@ consumer_secret = os.getenv('CONSUMER_SECRET')
|
|
14 |
my_access_token = os.getenv('ACCESS_TOKEN')
|
15 |
my_access_secret = os.getenv('ACCESS_SECRET')
|
16 |
bearer = os.getenv('BEARER')
|
|
|
|
|
17 |
html_data = '''
|
18 |
<!DOCTYPE html>
|
19 |
<html>
|
@@ -133,7 +135,7 @@ def get_client_from_tokens(oauth_verifier, oauth_token):
|
|
133 |
'''
|
134 |
new_oauth1_user_handler = tweepy.OAuth1UserHandler(
|
135 |
consumer_token, consumer_secret,
|
136 |
-
callback="https://hf.space/embed/User1342/WatchTower/"
|
137 |
)
|
138 |
new_oauth1_user_handler.request_token = {
|
139 |
"oauth_token": oauth_token,
|
@@ -158,11 +160,36 @@ def get_client_from_tokens(oauth_verifier, oauth_token):
|
|
158 |
|
159 |
return their_client
|
160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
def block_users(client, threshold, dataset):
|
163 |
'''
|
164 |
Used for blocking a series of users based on the threshold and datasets provided. Here the users folder is used.
|
165 |
-
TODO: Datasets not implemented.
|
166 |
:param client:
|
167 |
:param threshold:
|
168 |
:param dataset:
|
@@ -178,36 +205,30 @@ def block_users(client, threshold, dataset):
|
|
178 |
|
179 |
for user in users:
|
180 |
print("Reviewing user {}".format(user))
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
continue
|
198 |
-
except tweepy.errors.BadRequest as e:
|
199 |
-
print("bad request error")
|
200 |
-
print(e)
|
201 |
-
finished = True
|
202 |
continue
|
203 |
-
|
204 |
-
|
|
|
|
|
|
|
205 |
continue
|
206 |
-
|
207 |
-
finished = True
|
208 |
-
me = client.get_me()
|
209 |
-
print("{} blocked {}".format(me.data["username"], user))
|
210 |
-
num_users_blocked = num_users_blocked + 1
|
211 |
|
212 |
return num_users_blocked
|
213 |
|
@@ -231,9 +252,20 @@ def chat(selected_option=None, radio_score=None, url_params=None):
|
|
231 |
if radio_score != None and selected_option != None:
|
232 |
|
233 |
if client != None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
234 |
history.append(
|
235 |
-
["Model tuned to a '{}%' threshold and is using the
|
236 |
-
"{} Account blocking initialised".format(
|
237 |
num_users_blocked = block_users(client, radio_score, selected_option)
|
238 |
history.append(
|
239 |
["Blocked {} user account(s).".format(num_users_blocked), "Thank you for using Watchtower."])
|
@@ -315,7 +347,7 @@ def get_target_website():
|
|
315 |
'''
|
316 |
oauth1_user_handler = tweepy.OAuth1UserHandler(
|
317 |
consumer_token, consumer_secret,
|
318 |
-
callback="https://hf.space/embed/User1342/WatchTower/"
|
319 |
)
|
320 |
target_website = oauth1_user_handler.get_authorization_url(signin_with_twitter=True)
|
321 |
|
@@ -359,12 +391,10 @@ with block:
|
|
359 |
get_target_website())).style(
|
360 |
)
|
361 |
with gr.Row().style(mobile_collapse=True, equal_height=True):
|
362 |
-
radio = gr.CheckboxGroup(value="Violent", choices=["Violent", "Hate Speech", "Misinformation"],
|
363 |
-
interactive=False, label="Behaviour To Block").style(
|
364 |
-
rounded=(False, True, True, False),
|
365 |
-
)
|
366 |
|
367 |
-
slider = gr.Slider(value=
|
368 |
|
369 |
chatbot = gr.Chatbot(value=chat_history, label="Watchtower Output").style(
|
370 |
rounded=(False, True, True, False),
|
|
|
14 |
my_access_token = os.getenv('ACCESS_TOKEN')
|
15 |
my_access_secret = os.getenv('ACCESS_SECRET')
|
16 |
bearer = os.getenv('BEARER')
|
17 |
+
|
18 |
+
|
19 |
html_data = '''
|
20 |
<!DOCTYPE html>
|
21 |
<html>
|
|
|
135 |
'''
|
136 |
new_oauth1_user_handler = tweepy.OAuth1UserHandler(
|
137 |
consumer_token, consumer_secret,
|
138 |
+
callback="http://127.0.0.1:7860/'#https://hf.space/embed/User1342/WatchTower/"
|
139 |
)
|
140 |
new_oauth1_user_handler.request_token = {
|
141 |
"oauth_token": oauth_token,
|
|
|
160 |
|
161 |
return their_client
|
162 |
|
163 |
+
def block_user(user_id, user, reason):
|
164 |
+
finished = False
|
165 |
+
while not finished:
|
166 |
+
|
167 |
+
try:
|
168 |
+
print("preparing to block {}".format(user_id))
|
169 |
+
client.block(target_user_id=user_id)
|
170 |
+
print("User blocked")
|
171 |
+
except tweepy.errors.TooManyRequests as e:
|
172 |
+
print("time out error")
|
173 |
+
print(e)
|
174 |
+
finished = True
|
175 |
+
# time.sleep(240)
|
176 |
+
continue
|
177 |
+
except tweepy.errors.BadRequest as e:
|
178 |
+
print("bad request error")
|
179 |
+
print(e)
|
180 |
+
finished = True
|
181 |
+
continue
|
182 |
+
except:
|
183 |
+
time.sleep(240)
|
184 |
+
continue
|
185 |
+
time.sleep(1)
|
186 |
+
finished = True
|
187 |
+
me = client.get_me()
|
188 |
+
print("{} blocked {}, for {}".format(me.data["username"], user, reason))
|
189 |
|
190 |
def block_users(client, threshold, dataset):
|
191 |
'''
|
192 |
Used for blocking a series of users based on the threshold and datasets provided. Here the users folder is used.
|
|
|
193 |
:param client:
|
194 |
:param threshold:
|
195 |
:param dataset:
|
|
|
205 |
|
206 |
for user in users:
|
207 |
print("Reviewing user {}".format(user))
|
208 |
+
|
209 |
+
if "threshold" in user:
|
210 |
+
# old type of dataset being used, only 'violent' data available
|
211 |
+
if "Violent" in dataset:
|
212 |
+
if user["threshold"] >= threshold:
|
213 |
+
|
214 |
+
user_id = str(user["username"])
|
215 |
+
block_user(user_id, user, "Violent - old dataset")
|
216 |
+
num_users_blocked = num_users_blocked + 1
|
217 |
+
else:
|
218 |
+
# modern dataset being used
|
219 |
+
if "Violent" in dataset:
|
220 |
+
if user["violence-threshold"] >= threshold:
|
221 |
+
user_id = str(user["username"])
|
222 |
+
block_user(user_id, user, "Violent")
|
223 |
+
num_users_blocked = num_users_blocked + 1
|
|
|
|
|
|
|
|
|
|
|
224 |
continue
|
225 |
+
if "Hate Speech" in dataset:
|
226 |
+
if user["toxicity-threshold"] >= threshold:
|
227 |
+
user_id = str(user["username"])
|
228 |
+
block_user(user_id, user, "Hate Speech")
|
229 |
+
num_users_blocked = num_users_blocked + 1
|
230 |
continue
|
231 |
+
|
|
|
|
|
|
|
|
|
232 |
|
233 |
return num_users_blocked
|
234 |
|
|
|
252 |
if radio_score != None and selected_option != None:
|
253 |
|
254 |
if client != None:
|
255 |
+
|
256 |
+
# Extract the list to a string representation
|
257 |
+
if type(selected_option) is list:
|
258 |
+
block_type = ""
|
259 |
+
for b_type in selected_option:
|
260 |
+
block_type = block_type + " + " + b_type.capitalize()
|
261 |
+
block_type = "'" + block_type[3:] + "'"
|
262 |
+
else:
|
263 |
+
block_type = selected_option
|
264 |
+
|
265 |
+
# Display to user, set options
|
266 |
history.append(
|
267 |
+
["Model tuned to a '{}%' threshold and is using the {} dataset.".format(radio_score, block_type.capitalize()),
|
268 |
+
"{} Account blocking initialised".format(block_type.capitalize())])
|
269 |
num_users_blocked = block_users(client, radio_score, selected_option)
|
270 |
history.append(
|
271 |
["Blocked {} user account(s).".format(num_users_blocked), "Thank you for using Watchtower."])
|
|
|
347 |
'''
|
348 |
oauth1_user_handler = tweepy.OAuth1UserHandler(
|
349 |
consumer_token, consumer_secret,
|
350 |
+
callback="http://127.0.0.1:7860/"#"https://hf.space/embed/User1342/WatchTower/"
|
351 |
)
|
352 |
target_website = oauth1_user_handler.get_authorization_url(signin_with_twitter=True)
|
353 |
|
|
|
391 |
get_target_website())).style(
|
392 |
)
|
393 |
with gr.Row().style(mobile_collapse=True, equal_height=True):
|
394 |
+
radio = gr.CheckboxGroup(value=["Violent", "Hate Speech"], choices=["Violent", "Hate Speech", "Misinformation"],
|
395 |
+
interactive=False, label="Behaviour To Block").style(rounded=(False, True, True, False))
|
|
|
|
|
396 |
|
397 |
+
slider = gr.Slider(value=10, interactive=True, label="Threshold Confidence Tolerance")
|
398 |
|
399 |
chatbot = gr.Chatbot(value=chat_history, label="Watchtower Output").style(
|
400 |
rounded=(False, True, True, False),
|