Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,15 @@
|
|
| 1 |
#!/usr/bin/env python
|
| 2 |
# coding: utf-8
|
|
|
|
|
|
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
-
import re
|
| 6 |
import time
|
| 7 |
-
|
| 8 |
-
import
|
| 9 |
-
|
| 10 |
-
from threading import Thread
|
| 11 |
-
from time import sleep
|
| 12 |
|
| 13 |
-
|
|
|
|
| 14 |
<!-- Header -->
|
| 15 |
<header class="w3-display-container w3-content w3-wide" style="max-width:1500px;" id="home">
|
| 16 |
<img class="w3-image" src="https://cdn.pixabay.com/photo/2018/12/10/16/22/city-3867295_960_720.png" alt="Architecture" width="1500" height="800">
|
|
@@ -68,7 +67,7 @@ test_html = '''
|
|
| 68 |
<p id="caption" class="w3-opacity w3-large"></p>
|
| 69 |
</div>
|
| 70 |
</div>
|
| 71 |
-
|
| 72 |
<script>
|
| 73 |
// Modal Image Gallery
|
| 74 |
function onClick(element) {
|
|
@@ -101,24 +100,26 @@ function toggleFunction() {
|
|
| 101 |
</html>
|
| 102 |
'''
|
| 103 |
|
| 104 |
-
|
| 105 |
-
import tweepy
|
| 106 |
-
from fastapi import FastAPI, Request
|
| 107 |
-
|
| 108 |
consumer_token = os.getenv('CONSUMER_TOKEN')
|
| 109 |
consumer_secret = os.getenv('CONSUMER_SECRET')
|
| 110 |
my_access_token = os.getenv('ACCESS_TOKEN')
|
| 111 |
my_access_secret = os.getenv('ACCESS_SECRET')
|
| 112 |
bearer = os.getenv('BEARER')
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
block = gr.Blocks(css=".container { max-width: 800px; margin: auto; }", title="WatchTower")
|
| 117 |
|
|
|
|
| 118 |
chat_history = []
|
| 119 |
|
| 120 |
-
|
| 121 |
def get_client_from_tokens(oauth_verifier, oauth_token):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
new_oauth1_user_handler = tweepy.OAuth1UserHandler(
|
| 123 |
consumer_token, consumer_secret,
|
| 124 |
callback="https://hf.space/embed/User1342/WatchTower/"
|
|
@@ -140,12 +141,22 @@ def get_client_from_tokens(oauth_verifier, oauth_token):
|
|
| 140 |
access_token_secret=access_token_secret
|
| 141 |
)
|
| 142 |
|
|
|
|
| 143 |
global client
|
| 144 |
client = their_client
|
| 145 |
|
| 146 |
return their_client
|
| 147 |
|
|
|
|
| 148 |
def block_users(client, threshold, dataset):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
num_users_blocked = 0
|
| 150 |
|
| 151 |
for filename in os.listdir("users"):
|
|
@@ -176,11 +187,15 @@ def block_users(client, threshold, dataset):
|
|
| 176 |
return num_users_blocked
|
| 177 |
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
-
username_populated = False
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
def chat(selected_option=None,radio_score=None, url_params = None):
|
| 184 |
global client
|
| 185 |
global chat_history
|
| 186 |
history = []
|
|
@@ -207,7 +222,6 @@ def chat(selected_option=None,radio_score=None, url_params = None):
|
|
| 207 |
return history
|
| 208 |
|
| 209 |
|
| 210 |
-
|
| 211 |
def infer(prompt):
|
| 212 |
pass
|
| 213 |
|
|
@@ -216,12 +230,20 @@ have_initialised = False
|
|
| 216 |
client = None
|
| 217 |
name = None
|
| 218 |
|
| 219 |
-
def predict(slider_value, url_params):
|
| 220 |
-
print(url_params)
|
| 221 |
-
return [None,chat(radio.value, slider_value, url_params)]
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
target_website = None
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
global have_initialised
|
| 226 |
global chatbot
|
| 227 |
global chat_history
|
|
@@ -233,18 +255,20 @@ def changed_tab():
|
|
| 233 |
chat_history = [
|
| 234 |
["Welcome to Watchtower.".format(name), "Log in via Twitter and configure your blocking options above."]]
|
| 235 |
|
| 236 |
-
|
| 237 |
chatbot.value = chat_history
|
| 238 |
chatbot.update(value=chat_history)
|
| 239 |
|
| 240 |
twitter_auth_button.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(
|
| 241 |
-
|
| 242 |
-
twitter_auth_button.update(
|
| 243 |
-
|
| 244 |
-
|
|
|
|
| 245 |
return '<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(
|
| 246 |
-
|
| 247 |
|
|
|
|
|
|
|
| 248 |
get_window_url_params = """
|
| 249 |
function(text_input, url_params) {
|
| 250 |
console.log(text_input, url_params);
|
|
@@ -255,6 +279,10 @@ get_window_url_params = """
|
|
| 255 |
"""
|
| 256 |
|
| 257 |
def get_target_website():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
oauth1_user_handler = tweepy.OAuth1UserHandler(
|
| 259 |
consumer_token, consumer_secret,
|
| 260 |
callback="https://hf.space/embed/User1342/WatchTower/"
|
|
@@ -263,14 +291,14 @@ def get_target_website():
|
|
| 263 |
|
| 264 |
return target_website
|
| 265 |
|
| 266 |
-
|
| 267 |
twitter_auth_button = gr.HTML(
|
| 268 |
-
|
| 269 |
-
|
| 270 |
|
|
|
|
| 271 |
with block:
|
| 272 |
gr.HTML('''
|
| 273 |
-
|
| 274 |
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 275 |
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
|
| 276 |
<!-- Navbar (sit on top) -->
|
|
@@ -291,7 +319,7 @@ with block:
|
|
| 291 |
with tabs:
|
| 292 |
intro_tab = gr.TabItem("Introduction")
|
| 293 |
with intro_tab:
|
| 294 |
-
gr.HTML(
|
| 295 |
|
| 296 |
prediction_tab = gr.TabItem("Getting Started")
|
| 297 |
with prediction_tab:
|
|
@@ -314,13 +342,13 @@ with block:
|
|
| 314 |
radio = gr.CheckboxGroup(value="Violent", choices=["Violent", "Hate Speech", "Misinformation"],
|
| 315 |
interactive=False, label="Behaviour To Block")
|
| 316 |
|
| 317 |
-
slider = gr.Slider(value=20,interactive=True, label="Threshold Certainty Tolerance")
|
| 318 |
|
| 319 |
chatbot = gr.Chatbot(value=chat_history, label="Watchtower Output").style()
|
| 320 |
btn = gr.Button("Run WatchTower").style(full_width=True)
|
| 321 |
-
btn.click(fn=
|
| 322 |
outputs=[text_output, chatbot], _js=get_window_url_params)
|
| 323 |
-
tabs.change(fn=
|
| 324 |
gr.Markdown(
|
| 325 |
"""___
|
| 326 |
<p style='text-align: center'>
|
|
@@ -332,7 +360,7 @@ with block:
|
|
| 332 |
|
| 333 |
block.__enter__()
|
| 334 |
block.set_event_trigger(
|
| 335 |
-
|
| 336 |
-
|
| 337 |
|
| 338 |
block.launch(enable_queue=False)
|
|
|
|
| 1 |
#!/usr/bin/env python
|
| 2 |
# coding: utf-8
|
| 3 |
+
|
| 4 |
+
# Imports
|
| 5 |
import json
|
| 6 |
import os
|
|
|
|
| 7 |
import time
|
| 8 |
+
import gradio as gr
|
| 9 |
+
import tweepy
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# The HTML body used for the WatchTower page
|
| 12 |
+
html_body = '''
|
| 13 |
<!-- Header -->
|
| 14 |
<header class="w3-display-container w3-content w3-wide" style="max-width:1500px;" id="home">
|
| 15 |
<img class="w3-image" src="https://cdn.pixabay.com/photo/2018/12/10/16/22/city-3867295_960_720.png" alt="Architecture" width="1500" height="800">
|
|
|
|
| 67 |
<p id="caption" class="w3-opacity w3-large"></p>
|
| 68 |
</div>
|
| 69 |
</div>
|
| 70 |
+
|
| 71 |
<script>
|
| 72 |
// Modal Image Gallery
|
| 73 |
function onClick(element) {
|
|
|
|
| 100 |
</html>
|
| 101 |
'''
|
| 102 |
|
| 103 |
+
# Twitter keys
|
|
|
|
|
|
|
|
|
|
| 104 |
consumer_token = os.getenv('CONSUMER_TOKEN')
|
| 105 |
consumer_secret = os.getenv('CONSUMER_SECRET')
|
| 106 |
my_access_token = os.getenv('ACCESS_TOKEN')
|
| 107 |
my_access_secret = os.getenv('ACCESS_SECRET')
|
| 108 |
bearer = os.getenv('BEARER')
|
| 109 |
|
| 110 |
+
# Setup the gradio block and add some generic CSS
|
|
|
|
| 111 |
block = gr.Blocks(css=".container { max-width: 800px; margin: auto; }", title="WatchTower")
|
| 112 |
|
| 113 |
+
# Chat history variable used for the chatbot prompt on the 'getting started' page.
|
| 114 |
chat_history = []
|
| 115 |
|
|
|
|
| 116 |
def get_client_from_tokens(oauth_verifier, oauth_token):
|
| 117 |
+
'''
|
| 118 |
+
This function is used for generating a Tweepy client object based on Oauth verifier and token paramiters
|
| 119 |
+
:param oauth_verifier:
|
| 120 |
+
:param oauth_token:
|
| 121 |
+
:return: A Tweepy client object
|
| 122 |
+
'''
|
| 123 |
new_oauth1_user_handler = tweepy.OAuth1UserHandler(
|
| 124 |
consumer_token, consumer_secret,
|
| 125 |
callback="https://hf.space/embed/User1342/WatchTower/"
|
|
|
|
| 141 |
access_token_secret=access_token_secret
|
| 142 |
)
|
| 143 |
|
| 144 |
+
# TODO: The below is not necessary and can be removed.
|
| 145 |
global client
|
| 146 |
client = their_client
|
| 147 |
|
| 148 |
return their_client
|
| 149 |
|
| 150 |
+
|
| 151 |
def block_users(client, threshold, dataset):
|
| 152 |
+
'''
|
| 153 |
+
Used for blocking a series of users based on the threshold and datasets provided. Here the users folder is used.
|
| 154 |
+
TODO: Datasets not implemented.
|
| 155 |
+
:param client:
|
| 156 |
+
:param threshold:
|
| 157 |
+
:param dataset:
|
| 158 |
+
:return: The number of blocked users.
|
| 159 |
+
'''
|
| 160 |
num_users_blocked = 0
|
| 161 |
|
| 162 |
for filename in os.listdir("users"):
|
|
|
|
| 187 |
return num_users_blocked
|
| 188 |
|
| 189 |
|
| 190 |
+
def chat(selected_option=None, radio_score=None, url_params=None):
|
| 191 |
+
'''
|
| 192 |
+
This function is used to initialise blocking users once the user has authenticated with Twitter.
|
| 193 |
+
:param selected_option:
|
| 194 |
+
:param radio_score:
|
| 195 |
+
:param url_params:
|
| 196 |
+
:return: the chatbot history is returned (including information on blocked accounts).
|
| 197 |
+
'''
|
| 198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
global client
|
| 200 |
global chat_history
|
| 201 |
history = []
|
|
|
|
| 222 |
return history
|
| 223 |
|
| 224 |
|
|
|
|
| 225 |
def infer(prompt):
|
| 226 |
pass
|
| 227 |
|
|
|
|
| 230 |
client = None
|
| 231 |
name = None
|
| 232 |
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
+
def button_pressed(slider_value, url_params):
|
| 235 |
+
#print(url_params)
|
| 236 |
+
return [None, chat(radio.value, slider_value, url_params)]
|
| 237 |
+
|
| 238 |
+
# The website that the user will visit to authenticate WatchTower.
|
| 239 |
target_website = None
|
| 240 |
+
|
| 241 |
+
def update_target_website():
|
| 242 |
+
'''
|
| 243 |
+
Updates the URL used to authenticate WatchTower with Twitter.
|
| 244 |
+
#TODO this function is full of old code and can be optimised.
|
| 245 |
+
:return:
|
| 246 |
+
'''
|
| 247 |
global have_initialised
|
| 248 |
global chatbot
|
| 249 |
global chat_history
|
|
|
|
| 255 |
chat_history = [
|
| 256 |
["Welcome to Watchtower.".format(name), "Log in via Twitter and configure your blocking options above."]]
|
| 257 |
|
|
|
|
| 258 |
chatbot.value = chat_history
|
| 259 |
chatbot.update(value=chat_history)
|
| 260 |
|
| 261 |
twitter_auth_button.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(
|
| 262 |
+
get_target_website())
|
| 263 |
+
twitter_auth_button.update(
|
| 264 |
+
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(
|
| 265 |
+
get_target_website()))
|
| 266 |
+
|
| 267 |
return '<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(
|
| 268 |
+
get_target_website())
|
| 269 |
|
| 270 |
+
# The below is a JS blob used to retrieve the URL params.
|
| 271 |
+
# Thanks to here: https://discuss.huggingface.co/t/hugging-face-and-gradio-url-paramiters/21110/2
|
| 272 |
get_window_url_params = """
|
| 273 |
function(text_input, url_params) {
|
| 274 |
console.log(text_input, url_params);
|
|
|
|
| 279 |
"""
|
| 280 |
|
| 281 |
def get_target_website():
|
| 282 |
+
'''
|
| 283 |
+
A wrapper function used for retrieving the URL a user will use to authenticate WatchTower with Twitter.
|
| 284 |
+
:return:
|
| 285 |
+
'''
|
| 286 |
oauth1_user_handler = tweepy.OAuth1UserHandler(
|
| 287 |
consumer_token, consumer_secret,
|
| 288 |
callback="https://hf.space/embed/User1342/WatchTower/"
|
|
|
|
| 291 |
|
| 292 |
return target_website
|
| 293 |
|
| 294 |
+
# The Gradio HTML component used for the 'sign in with Twitter' button
|
| 295 |
twitter_auth_button = gr.HTML(
|
| 296 |
+
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(
|
| 297 |
+
get_target_website()))
|
| 298 |
|
| 299 |
+
# The main chunk of code that uses Gradio blocks to create the UI
|
| 300 |
with block:
|
| 301 |
gr.HTML('''
|
|
|
|
| 302 |
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 303 |
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
|
| 304 |
<!-- Navbar (sit on top) -->
|
|
|
|
| 319 |
with tabs:
|
| 320 |
intro_tab = gr.TabItem("Introduction")
|
| 321 |
with intro_tab:
|
| 322 |
+
gr.HTML(html_body)
|
| 323 |
|
| 324 |
prediction_tab = gr.TabItem("Getting Started")
|
| 325 |
with prediction_tab:
|
|
|
|
| 342 |
radio = gr.CheckboxGroup(value="Violent", choices=["Violent", "Hate Speech", "Misinformation"],
|
| 343 |
interactive=False, label="Behaviour To Block")
|
| 344 |
|
| 345 |
+
slider = gr.Slider(value=20, interactive=True, label="Threshold Certainty Tolerance")
|
| 346 |
|
| 347 |
chatbot = gr.Chatbot(value=chat_history, label="Watchtower Output").style()
|
| 348 |
btn = gr.Button("Run WatchTower").style(full_width=True)
|
| 349 |
+
btn.click(fn=button_pressed, inputs=[slider, url_params],
|
| 350 |
outputs=[text_output, chatbot], _js=get_window_url_params)
|
| 351 |
+
tabs.change(fn=update_target_website, inputs=None, outputs=None)
|
| 352 |
gr.Markdown(
|
| 353 |
"""___
|
| 354 |
<p style='text-align: center'>
|
|
|
|
| 360 |
|
| 361 |
block.__enter__()
|
| 362 |
block.set_event_trigger(
|
| 363 |
+
event_name="load", fn=update_target_website, inputs=None, outputs=[html_button], no_target=True
|
| 364 |
+
)
|
| 365 |
|
| 366 |
block.launch(enable_queue=False)
|