Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -129,7 +129,7 @@ global_oauth1_user_handler = None
|
|
129 |
|
130 |
oauth1_user_handler = tweepy.OAuth1UserHandler(
|
131 |
consumer_token, consumer_secret,
|
132 |
-
callback="
|
133 |
)
|
134 |
target_website = oauth1_user_handler.get_authorization_url(signin_with_twitter=True)
|
135 |
|
@@ -137,6 +137,7 @@ block = gr.Blocks(css=".container { max-width: 800px; margin: auto; }")
|
|
137 |
|
138 |
chat_history = []
|
139 |
|
|
|
140 |
def get_client_from_tokens(oauth_verifier, oauth_token):
|
141 |
new_oauth1_user_handler = tweepy.OAuth1UserHandler(
|
142 |
consumer_token, consumer_secret,
|
@@ -159,51 +160,10 @@ def get_client_from_tokens(oauth_verifier, oauth_token):
|
|
159 |
access_token_secret=access_token_secret
|
160 |
)
|
161 |
|
162 |
-
|
|
|
163 |
|
164 |
-
|
165 |
-
oauth_verifier = None
|
166 |
-
oauth_token = None
|
167 |
-
did_find = False
|
168 |
-
if hasattr(block, "server"):
|
169 |
-
for connection in block.server.server_state.connections:
|
170 |
-
# connection_app_id = connection.app.app.blocks.app_id
|
171 |
-
# if active_app_id == connection_app_id:
|
172 |
-
# print("Its a match")
|
173 |
-
if connection.headers != None:
|
174 |
-
for header in connection.headers:
|
175 |
-
header = header[1].decode()
|
176 |
-
if "oauth_verifier" in header:
|
177 |
-
oauth_verifier = re.search(r"oauth_verifier=(.+)", header).group(1)
|
178 |
-
oauth_token = re.search(r"oauth_token=(.+)&", header).group(1)
|
179 |
-
if oauth_token and oauth_verifier:
|
180 |
-
did_find = True
|
181 |
-
break
|
182 |
-
if did_find:
|
183 |
-
break
|
184 |
-
return oauth_verifier, oauth_token
|
185 |
-
|
186 |
-
def get_oath_headers():
|
187 |
-
oauth_verifier = None
|
188 |
-
oauth_token = None
|
189 |
-
did_find = False
|
190 |
-
if hasattr(block, "server"):
|
191 |
-
for connection in block.server.server_state.connections:
|
192 |
-
# connection_app_id = connection.app.app.blocks.app_id
|
193 |
-
# if active_app_id == connection_app_id:
|
194 |
-
# print("Its a match")
|
195 |
-
if connection.headers != None:
|
196 |
-
for header in connection.headers:
|
197 |
-
header = header[1].decode()
|
198 |
-
if "oauth_verifier" in header:
|
199 |
-
oauth_verifier = re.search(r"oauth_verifier=(.+)", header).group(1)
|
200 |
-
oauth_token = re.search(r"oauth_token=(.+)&", header).group(1)
|
201 |
-
if oauth_token and oauth_verifier:
|
202 |
-
did_find = True
|
203 |
-
break
|
204 |
-
if did_find:
|
205 |
-
break
|
206 |
-
return oauth_verifier, oauth_token
|
207 |
|
208 |
def block_users(client, threshold, dataset):
|
209 |
num_users_blocked = 0
|
@@ -215,7 +175,7 @@ def block_users(client, threshold, dataset):
|
|
215 |
users = json.load(user_file)
|
216 |
|
217 |
for user in users:
|
218 |
-
if
|
219 |
|
220 |
user_id = str(user["username"])
|
221 |
|
@@ -234,40 +194,52 @@ def block_users(client, threshold, dataset):
|
|
234 |
|
235 |
return num_users_blocked
|
236 |
|
237 |
-
|
238 |
-
headers = get_oath_headers()
|
239 |
-
if headers[0] == None:
|
240 |
-
return False
|
241 |
-
else:
|
242 |
-
return True
|
243 |
|
244 |
username_populated = False
|
245 |
-
def chat(radio_score = None, selected_option = None):
|
246 |
-
global client
|
247 |
-
history = []
|
248 |
|
249 |
-
# app id
|
250 |
-
chat_history.append(["headers",get_oath_headers()])
|
251 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
252 |
if radio_score != None and selected_option != None:
|
253 |
-
|
254 |
if client != None:
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
|
|
|
|
259 |
elif radio_score != None or selected_option != None:
|
260 |
-
chat_history.append(["Initialisation error!","Please tune the model by using the above options"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
261 |
|
262 |
-
return chat_history
|
263 |
|
264 |
def infer(prompt):
|
265 |
pass
|
266 |
|
|
|
267 |
have_initialised = False
|
268 |
client = None
|
269 |
name = None
|
270 |
|
|
|
|
|
|
|
|
|
|
|
271 |
def changed_tab():
|
272 |
global have_initialised
|
273 |
global chatbot
|
@@ -277,33 +249,25 @@ def changed_tab():
|
|
277 |
|
278 |
name = "no username"
|
279 |
|
280 |
-
chat_history = [
|
281 |
-
|
282 |
-
if client != None and name != "no username":
|
283 |
-
chat_history = [["Welcome {}".format(name), "Initialising WatchTower"]]
|
284 |
-
|
285 |
-
print("changed tabs - {}".format(name))
|
286 |
-
chatbot.value = chat_history
|
287 |
-
chatbot.update(value=chat_history)
|
288 |
-
elif has_oath_header() and client==None:
|
289 |
|
290 |
-
tokens = get_oath_headers()
|
291 |
-
if tokens[0] and client==None:
|
292 |
-
client = get_client_from_tokens(tokens[0],tokens[1])
|
293 |
-
name = client.get_me().data.name
|
294 |
-
have_initialised = True
|
295 |
-
chat_history = [["Welcome {}".format(name), "Initialising WatchTower"]]
|
296 |
|
297 |
-
|
298 |
-
|
299 |
|
300 |
-
|
301 |
-
|
302 |
-
|
|
|
|
|
|
|
|
|
|
|
303 |
|
304 |
with block:
|
305 |
gr.HTML('''
|
306 |
-
|
307 |
<meta name="viewport" content="width=device-width, initial-scale=1">
|
308 |
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
|
309 |
<!-- Navbar (sit on top) -->
|
@@ -315,12 +279,11 @@ with block:
|
|
315 |
''')
|
316 |
gr.HTML("<center><p><br></p></center>")
|
317 |
|
318 |
-
|
319 |
-
#todo check if user signed in
|
320 |
|
321 |
user_message = "Log in via Twitter and configure your blocking options above."
|
322 |
|
323 |
-
chat_history.append(["Welcome to Watchtower.",user_message])
|
324 |
tabs = gr.Tabs()
|
325 |
with tabs:
|
326 |
intro_tab = gr.TabItem("Introduction")
|
@@ -339,6 +302,9 @@ with block:
|
|
339 |
''')
|
340 |
with gr.Group():
|
341 |
with gr.Box():
|
|
|
|
|
|
|
342 |
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
343 |
gr.HTML(
|
344 |
value='<a href={}><img src="https://cdn.cms-twdigitalassets.com/content/dam/developer-twitter/auth-docs/sign-in-with-twitter-gray.png.twimg.1920.png" alt="Log In With Twitter"></a><br>'.format(
|
@@ -347,20 +313,18 @@ with block:
|
|
347 |
radio = gr.CheckboxGroup(value="Violent", choices=["Violent", "Hate Speech", "Misinformation"],
|
348 |
interactive=False, label="Behaviour To Block")
|
349 |
|
350 |
-
slider = gr.Slider(value=80, label="Threshold Certainty Tolerance")
|
351 |
|
352 |
chatbot = gr.Chatbot(value=chat_history, label="Watchtower Output").style()
|
353 |
btn = gr.Button("Run WatchTower").style(full_width=True)
|
354 |
-
|
355 |
-
|
356 |
-
#text.submit(fn=chat, inputs=[text,text], outputs=chatbot)
|
357 |
-
btn.click(fn=chat, inputs=[slider,radio], outputs=chatbot)
|
358 |
tabs.change(fn=changed_tab, inputs=None, outputs=None)
|
359 |
|
360 |
gr.Markdown(
|
361 |
"""___
|
362 |
<p style='text-align: center'>
|
363 |
-
Created by <a href="https://twitter.com/
|
364 |
<br/>
|
365 |
<a href="https://github.com/CartographerLabs/Pinpoint" target="_blank">GitHub</a>
|
366 |
</p>"""
|
|
|
129 |
|
130 |
oauth1_user_handler = tweepy.OAuth1UserHandler(
|
131 |
consumer_token, consumer_secret,
|
132 |
+
callback="http://127.0.0.1:7860/"
|
133 |
)
|
134 |
target_website = oauth1_user_handler.get_authorization_url(signin_with_twitter=True)
|
135 |
|
|
|
137 |
|
138 |
chat_history = []
|
139 |
|
140 |
+
|
141 |
def get_client_from_tokens(oauth_verifier, oauth_token):
|
142 |
new_oauth1_user_handler = tweepy.OAuth1UserHandler(
|
143 |
consumer_token, consumer_secret,
|
|
|
160 |
access_token_secret=access_token_secret
|
161 |
)
|
162 |
|
163 |
+
global client
|
164 |
+
client = their_client
|
165 |
|
166 |
+
return their_client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
def block_users(client, threshold, dataset):
|
169 |
num_users_blocked = 0
|
|
|
175 |
users = json.load(user_file)
|
176 |
|
177 |
for user in users:
|
178 |
+
if user["threshold"] >= threshold:
|
179 |
|
180 |
user_id = str(user["username"])
|
181 |
|
|
|
194 |
|
195 |
return num_users_blocked
|
196 |
|
197 |
+
|
|
|
|
|
|
|
|
|
|
|
198 |
|
199 |
username_populated = False
|
|
|
|
|
|
|
200 |
|
|
|
|
|
201 |
|
202 |
+
def chat(selected_option=None,radio_score=None, url_params = None):
|
203 |
+
global client
|
204 |
+
global chat_history
|
205 |
+
history = []
|
206 |
+
|
207 |
+
# app id
|
208 |
+
if "oauth_verifier" in url_params and "oauth_token" in url_params and client is None:
|
209 |
+
client = get_client_from_tokens(url_params["oauth_verifier"], url_params["oauth_token"])
|
210 |
if radio_score != None and selected_option != None:
|
211 |
+
|
212 |
if client != None:
|
213 |
+
history.append(
|
214 |
+
["Model tuned to a '{}%' threshold and is using the '{}' dataset.".format(radio_score, selected_option),
|
215 |
+
"{} Account blocking initialised".format(str(selected_option).capitalize())])
|
216 |
+
num_users_blocked = block_users(client, radio_score, selected_option)
|
217 |
+
history.append(
|
218 |
+
["Blocked {} user account(s).".format(num_users_blocked), "Thank you for using Watchtower."])
|
219 |
elif radio_score != None or selected_option != None:
|
220 |
+
chat_history.append(["Initialisation error!", "Please tune the model by using the above options"])
|
221 |
+
|
222 |
+
history = chat_history + history
|
223 |
+
chatbot.value = history
|
224 |
+
chatbot.update(value=history)
|
225 |
+
|
226 |
+
return history
|
227 |
+
|
228 |
|
|
|
229 |
|
230 |
def infer(prompt):
|
231 |
pass
|
232 |
|
233 |
+
|
234 |
have_initialised = False
|
235 |
client = None
|
236 |
name = None
|
237 |
|
238 |
+
def predict(text, url_params):
|
239 |
+
print(url_params)
|
240 |
+
return [None,chat(radio.value, slider.value, url_params)]
|
241 |
+
|
242 |
+
|
243 |
def changed_tab():
|
244 |
global have_initialised
|
245 |
global chatbot
|
|
|
249 |
|
250 |
name = "no username"
|
251 |
|
252 |
+
chat_history = [
|
253 |
+
["Welcome to Watchtower.".format(name), "Log in via Twitter and configure your blocking options above."]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
|
256 |
+
chatbot.value = chat_history
|
257 |
+
chatbot.update(value=chat_history)
|
258 |
|
259 |
+
get_window_url_params = """
|
260 |
+
function(text_input, url_params) {
|
261 |
+
console.log(text_input, url_params);
|
262 |
+
const params = new URLSearchParams(window.location.search);
|
263 |
+
url_params = Object.fromEntries(params);
|
264 |
+
return [text_input, url_params];
|
265 |
+
}
|
266 |
+
"""
|
267 |
|
268 |
with block:
|
269 |
gr.HTML('''
|
270 |
+
|
271 |
<meta name="viewport" content="width=device-width, initial-scale=1">
|
272 |
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
|
273 |
<!-- Navbar (sit on top) -->
|
|
|
279 |
''')
|
280 |
gr.HTML("<center><p><br></p></center>")
|
281 |
|
282 |
+
# todo check if user signed in
|
|
|
283 |
|
284 |
user_message = "Log in via Twitter and configure your blocking options above."
|
285 |
|
286 |
+
chat_history.append(["Welcome to Watchtower.", user_message])
|
287 |
tabs = gr.Tabs()
|
288 |
with tabs:
|
289 |
intro_tab = gr.TabItem("Introduction")
|
|
|
302 |
''')
|
303 |
with gr.Group():
|
304 |
with gr.Box():
|
305 |
+
url_params = gr.JSON({}, visible=False, label="URL Params")
|
306 |
+
text_input = gr.Text(label="Input", visible=False)
|
307 |
+
text_output = gr.Text(label="Output", visible=False)
|
308 |
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
309 |
gr.HTML(
|
310 |
value='<a href={}><img src="https://cdn.cms-twdigitalassets.com/content/dam/developer-twitter/auth-docs/sign-in-with-twitter-gray.png.twimg.1920.png" alt="Log In With Twitter"></a><br>'.format(
|
|
|
313 |
radio = gr.CheckboxGroup(value="Violent", choices=["Violent", "Hate Speech", "Misinformation"],
|
314 |
interactive=False, label="Behaviour To Block")
|
315 |
|
316 |
+
slider = gr.Slider(value=80,interactive=True, label="Threshold Certainty Tolerance")
|
317 |
|
318 |
chatbot = gr.Chatbot(value=chat_history, label="Watchtower Output").style()
|
319 |
btn = gr.Button("Run WatchTower").style(full_width=True)
|
320 |
+
btn.click(fn=predict, inputs=[text_input, url_params],
|
321 |
+
outputs=[text_output, chatbot], _js=get_window_url_params)
|
|
|
|
|
322 |
tabs.change(fn=changed_tab, inputs=None, outputs=None)
|
323 |
|
324 |
gr.Markdown(
|
325 |
"""___
|
326 |
<p style='text-align: center'>
|
327 |
+
Created by <a href="https://twitter.com/_JamesStevenson" target="_blank"James Stevenson</a> et al. 2021-2022
|
328 |
<br/>
|
329 |
<a href="https://github.com/CartographerLabs/Pinpoint" target="_blank">GitHub</a>
|
330 |
</p>"""
|