Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,13 @@
|
|
| 2 |
# coding: utf-8
|
| 3 |
import json
|
| 4 |
import os
|
|
|
|
| 5 |
import time
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
import
|
| 9 |
|
| 10 |
test_html = '''
|
| 11 |
<!-- Header -->
|
|
@@ -79,7 +82,7 @@ test_html = '''
|
|
| 79 |
<p id="caption" class="w3-opacity w3-large"></p>
|
| 80 |
</div>
|
| 81 |
</div>
|
| 82 |
-
|
| 83 |
<script>
|
| 84 |
// Modal Image Gallery
|
| 85 |
function onClick(element) {
|
|
@@ -112,52 +115,95 @@ function toggleFunction() {
|
|
| 112 |
</html>
|
| 113 |
'''
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
consumer_token = os.getenv('CONSUMER_TOKEN')
|
| 116 |
consumer_secret = os.getenv('CONSUMER_SECRET')
|
| 117 |
my_access_token = os.getenv('ACCESS_TOKEN')
|
| 118 |
my_access_secret = os.getenv('ACCESS_SECRET')
|
| 119 |
bearer = os.getenv('BEARER')
|
| 120 |
-
client_id = os.getenv('CLIENT_ID')
|
| 121 |
-
client_secret = os.getenv('CLIENT_SECRET')
|
| 122 |
-
|
| 123 |
-
oauth1_user_handler = tweepy.OAuth2UserHandler(client_id=client_id,
|
| 124 |
-
client_secret=client_secret,
|
| 125 |
-
redirect_uri="https://hf.space/embed/User1342/WatchTower/",
|
| 126 |
-
scope=["block.write"]
|
| 127 |
|
| 128 |
-
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
block = gr.Blocks(css=".container { max-width: 800px; margin: auto; }")
|
| 134 |
|
| 135 |
chat_history = []
|
| 136 |
|
| 137 |
-
def
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
scope=["block.write"])
|
| 147 |
-
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
if url != None:
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
return their_client
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
def block_users(client, threshold, dataset):
|
| 163 |
num_users_blocked = 0
|
|
@@ -188,39 +234,40 @@ def block_users(client, threshold, dataset):
|
|
| 188 |
|
| 189 |
return num_users_blocked
|
| 190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
username_populated = False
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
client = get_client_from_tokens(url_params)
|
| 198 |
-
history = []
|
| 199 |
|
| 200 |
if radio_score != None and selected_option != None:
|
| 201 |
response = "no blocking"
|
| 202 |
if client != None:
|
| 203 |
-
chat_history.append(
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
chat_history.append(
|
| 208 |
-
["Blocked {} user account(s).".format(num_users_blocked), "Thank you for using Watchtower."])
|
| 209 |
elif radio_score != None or selected_option != None:
|
| 210 |
-
chat_history.append(["Initialisation error!",
|
| 211 |
|
| 212 |
return chat_history
|
| 213 |
|
| 214 |
-
|
| 215 |
def infer(prompt):
|
| 216 |
pass
|
| 217 |
|
| 218 |
-
|
| 219 |
have_initialised = False
|
| 220 |
client = None
|
| 221 |
name = None
|
| 222 |
|
| 223 |
-
|
| 224 |
def changed_tab():
|
| 225 |
global have_initialised
|
| 226 |
global chatbot
|
|
@@ -230,8 +277,7 @@ def changed_tab():
|
|
| 230 |
|
| 231 |
name = "no username"
|
| 232 |
|
| 233 |
-
chat_history = [
|
| 234 |
-
["Welcome to Watchtower.".format(name), "Log in via Twitter and configure your blocking options above."]]
|
| 235 |
|
| 236 |
if client != None and name != "no username":
|
| 237 |
chat_history = [["Welcome {}".format(name), "Initialising WatchTower"]]
|
|
@@ -239,25 +285,25 @@ def changed_tab():
|
|
| 239 |
print("changed tabs - {}".format(name))
|
| 240 |
chatbot.value = chat_history
|
| 241 |
chatbot.update(value=chat_history)
|
|
|
|
| 242 |
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
| 247 |
|
| 248 |
chatbot.value = chat_history
|
| 249 |
chatbot.update(value=chat_history)
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
url_params = Object.fromEntries(params);
|
| 256 |
-
return [text_input, url_params];
|
| 257 |
-
}
|
| 258 |
-
"""
|
| 259 |
with block:
|
| 260 |
gr.HTML('''
|
|
|
|
| 261 |
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 262 |
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
|
| 263 |
<!-- Navbar (sit on top) -->
|
|
@@ -269,11 +315,12 @@ with block:
|
|
| 269 |
''')
|
| 270 |
gr.HTML("<center><p><br></p></center>")
|
| 271 |
|
| 272 |
-
|
|
|
|
| 273 |
|
| 274 |
user_message = "Log in via Twitter and configure your blocking options above."
|
| 275 |
|
| 276 |
-
chat_history.append(["Welcome to Watchtower.",
|
| 277 |
tabs = gr.Tabs()
|
| 278 |
with tabs:
|
| 279 |
intro_tab = gr.TabItem("Introduction")
|
|
@@ -281,9 +328,6 @@ with block:
|
|
| 281 |
gr.HTML(test_html)
|
| 282 |
|
| 283 |
prediction_tab = gr.TabItem("Getting Started")
|
| 284 |
-
text_input = gr.Text(label="Input")
|
| 285 |
-
text_output = gr.Text(label="Output")
|
| 286 |
-
|
| 287 |
with prediction_tab:
|
| 288 |
gr.HTML('''
|
| 289 |
<header class="w3-display-container w3-content w3-wide" style="max-height:250px;" id="home">
|
|
@@ -307,9 +351,10 @@ with block:
|
|
| 307 |
|
| 308 |
chatbot = gr.Chatbot(value=chat_history, label="Watchtower Output").style()
|
| 309 |
btn = gr.Button("Run WatchTower").style(full_width=True)
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
|
|
|
| 313 |
tabs.change(fn=changed_tab, inputs=None, outputs=None)
|
| 314 |
|
| 315 |
gr.Markdown(
|
|
@@ -321,5 +366,4 @@ with block:
|
|
| 321 |
</p>"""
|
| 322 |
)
|
| 323 |
|
| 324 |
-
|
| 325 |
block.launch(enable_queue=False)
|
|
|
|
| 2 |
# coding: utf-8
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
+
import re
|
| 6 |
import time
|
| 7 |
+
from random import random
|
| 8 |
+
import socket
|
| 9 |
|
| 10 |
+
from threading import Thread
|
| 11 |
+
from time import sleep
|
| 12 |
|
| 13 |
test_html = '''
|
| 14 |
<!-- Header -->
|
|
|
|
| 82 |
<p id="caption" class="w3-opacity w3-large"></p>
|
| 83 |
</div>
|
| 84 |
</div>
|
| 85 |
+
|
| 86 |
<script>
|
| 87 |
// Modal Image Gallery
|
| 88 |
function onClick(element) {
|
|
|
|
| 115 |
</html>
|
| 116 |
'''
|
| 117 |
|
| 118 |
+
import gradio as gr
|
| 119 |
+
import tweepy
|
| 120 |
+
from fastapi import FastAPI, Request
|
| 121 |
+
|
| 122 |
consumer_token = os.getenv('CONSUMER_TOKEN')
|
| 123 |
consumer_secret = os.getenv('CONSUMER_SECRET')
|
| 124 |
my_access_token = os.getenv('ACCESS_TOKEN')
|
| 125 |
my_access_secret = os.getenv('ACCESS_SECRET')
|
| 126 |
bearer = os.getenv('BEARER')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
+
global_oauth1_user_handler = None
|
| 129 |
|
| 130 |
+
oauth1_user_handler = tweepy.OAuth1UserHandler(
|
| 131 |
+
consumer_token, consumer_secret,
|
| 132 |
+
callback="https://huggingface.co/spaces/User1342/WatchTower"
|
| 133 |
+
)
|
| 134 |
+
target_website = oauth1_user_handler.get_authorization_url(signin_with_twitter=True)
|
| 135 |
|
| 136 |
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,
|
| 143 |
+
callback="http://127.0.0.1:7860/"
|
| 144 |
+
)
|
| 145 |
+
new_oauth1_user_handler.request_token = {
|
| 146 |
+
"oauth_token": oauth_token,
|
| 147 |
+
"oauth_token_secret": consumer_secret
|
| 148 |
+
}
|
|
|
|
|
|
|
| 149 |
|
| 150 |
+
access_token, access_token_secret = new_oauth1_user_handler.get_access_token(
|
| 151 |
+
oauth_verifier
|
| 152 |
+
)
|
|
|
|
| 153 |
|
| 154 |
+
their_client = tweepy.Client(
|
| 155 |
+
bearer_token=bearer,
|
| 156 |
+
consumer_key=consumer_token,
|
| 157 |
+
consumer_secret=consumer_secret,
|
| 158 |
+
access_token=access_token,
|
| 159 |
+
access_token_secret=access_token_secret
|
| 160 |
+
)
|
| 161 |
|
| 162 |
return their_client
|
| 163 |
|
| 164 |
+
def get_oath_headers():
|
| 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
|
|
|
|
| 234 |
|
| 235 |
return num_users_blocked
|
| 236 |
|
| 237 |
+
def has_oath_header():
|
| 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 |
response = "no blocking"
|
| 254 |
if client != None:
|
| 255 |
+
chat_history.append(["Model tuned to a '{}%' threshold and is using the '{}' dataset.".format(radio_score, selected_option),
|
| 256 |
+
"{} Account blocking initialised".format(selected_option.capitalize())])
|
| 257 |
+
num_users_blocked = block_users(client,radio_score,selected_option)
|
| 258 |
+
chat_history.append(["Blocked {} user account(s).".format(num_users_blocked), "Thank you for using Watchtower."])
|
|
|
|
|
|
|
| 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 |
|
| 278 |
name = "no username"
|
| 279 |
|
| 280 |
+
chat_history = [["Welcome to Watchtower.".format(name), "Log in via Twitter and configure your blocking options above."]]
|
|
|
|
| 281 |
|
| 282 |
if client != None and name != "no username":
|
| 283 |
chat_history = [["Welcome {}".format(name), "Initialising WatchTower"]]
|
|
|
|
| 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 |
chatbot.value = chat_history
|
| 298 |
chatbot.update(value=chat_history)
|
| 299 |
|
| 300 |
+
elif not has_oath_header() and not have_initialised:
|
| 301 |
+
chatbot.value = chat_history
|
| 302 |
+
chatbot.update(value=chat_history)
|
| 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 |
''')
|
| 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")
|
|
|
|
| 328 |
gr.HTML(test_html)
|
| 329 |
|
| 330 |
prediction_tab = gr.TabItem("Getting Started")
|
|
|
|
|
|
|
|
|
|
| 331 |
with prediction_tab:
|
| 332 |
gr.HTML('''
|
| 333 |
<header class="w3-display-container w3-content w3-wide" style="max-height:250px;" id="home">
|
|
|
|
| 351 |
|
| 352 |
chatbot = gr.Chatbot(value=chat_history, label="Watchtower Output").style()
|
| 353 |
btn = gr.Button("Run WatchTower").style(full_width=True)
|
| 354 |
+
#radio.change(fn=chat, inputs=[radio], outputs=chatbot)
|
| 355 |
+
#slider.change(fn=chat, inputs=[slider], outputs=chatbot)
|
| 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(
|
|
|
|
| 366 |
</p>"""
|
| 367 |
)
|
| 368 |
|
|
|
|
| 369 |
block.launch(enable_queue=False)
|