#!/usr/bin/env python # coding: utf-8 import json import os import re import time from random import random import socket from threading import Thread from time import sleep test_html = '''
Architecture

WATCH Tower

Block Violent Content Before It Reaches Your Feed

WatchTower identifies, blocks, and filters out violent and radical content before it reaches your Twitter feed.


WatchTower works to protect you from violent, misinformation, hate speech and other malicious communication by using a suite of machine learning models to identify user accounts that post content that commonly falls into these categories. WatchTower is broken down into two components, the first utilises the Twitter streaming API and applies a suite of machine learning models to identify users that commonly post malicious information, while the second element provides a web UI where users can authenticaate with Twitter and tailor the types and thresholds for the accounts they block.


WatchTower was developed solely by James Stevenson and primarily uses Pinpoint, a machine learning model also developed by James. The future roadmap sees WatchTower incoperate other models for identifying contrent such as misinformation and hate speech. More on Pinpoint and the model WatchTower uses to identify violent extremism can be seen below.

Model Accuracy:

Machine learning models can be validated based on several statistics. These statistics for Pinpoint the main ML model used by WatchTower can be seen below.


Accuracy

73%

Recall

62%

Precision

78%

F-Measure

69%

Chirp Development Challenge 2022

WatchTower was developed for the Chirp 2022 Twitter API Developer Challenge

Watchtower was developed solely by James Stevenson for the Chirp 2022 Twitter API Developer Challenge. More infomration of this can be found below.


Architecture

''' import gradio as gr import tweepy from fastapi import FastAPI, Request consumer_token = os.getenv('CONSUMER_TOKEN') consumer_secret = os.getenv('CONSUMER_SECRET') my_access_token = os.getenv('ACCESS_TOKEN') my_access_secret = os.getenv('ACCESS_SECRET') bearer = os.getenv('BEARER') global_oauth1_user_handler = None oauth1_user_handler = tweepy.OAuth1UserHandler( consumer_token, consumer_secret, callback="https://hf.space/embed/User1342/WatchTower/" ) target_website = oauth1_user_handler.get_authorization_url(signin_with_twitter=True) print("target website is {}".format(target_website)) block = gr.Blocks(css=".container { max-width: 800px; margin: auto; }") chat_history = [] def get_client_from_tokens(oauth_verifier, oauth_token): new_oauth1_user_handler = tweepy.OAuth1UserHandler( consumer_token, consumer_secret, callback="https://hf.space/embed/User1342/WatchTower/" ) new_oauth1_user_handler.request_token = { "oauth_token": oauth_token, "oauth_token_secret": consumer_secret } access_token, access_token_secret = new_oauth1_user_handler.get_access_token( oauth_verifier ) their_client = tweepy.Client( bearer_token=bearer, consumer_key=consumer_token, consumer_secret=consumer_secret, access_token=access_token, access_token_secret=access_token_secret ) global client client = their_client return their_client def block_users(client, threshold, dataset): num_users_blocked = 0 for filename in os.listdir("users"): filename = os.path.join("users", filename) user_file = open(filename, "r") users = json.load(user_file) for user in users: if user["threshold"] >= threshold: user_id = str(user["username"]) finished = False while not finished: try: client.block(target_user_id=user_id) except tweepy.errors.TooManyRequests as e: print(e) time.sleep(240) continue finished = True me = client.get_me() print("{} blocked {}".format(me.data["username"], user)) num_users_blocked = num_users_blocked + 1 return num_users_blocked username_populated = False def chat(selected_option=None,radio_score=None, url_params = None): global client global chat_history history = [] # app id if "oauth_verifier" in url_params and "oauth_token" in url_params and client is None: client = get_client_from_tokens(url_params["oauth_verifier"], url_params["oauth_token"]) if radio_score != None and selected_option != None: if client != None and url_params != None: history.append( ["Model tuned to a '{}%' threshold and is using the '{}' dataset.".format(radio_score, selected_option), "{} Account blocking initialised".format(str(selected_option).capitalize())]) num_users_blocked = block_users(client, radio_score, selected_option) history.append( ["Blocked {} user account(s).".format(num_users_blocked), "Thank you for using Watchtower."]) elif radio_score != None or selected_option != None: chat_history.append(["Initialisation error!", "Please tune the model by using the above options"]) history = chat_history + history chatbot.value = history chatbot.update(value=history) return history def infer(prompt): pass have_initialised = False client = None name = None def predict(slider_value, url_params): print(url_params) return [None,chat(radio.value, slider_value, url_params)] def changed_tab(): global have_initialised global chatbot global chat_history global client global name name = "no username" chat_history = [ ["Welcome to Watchtower.".format(name), "Log in via Twitter and configure your blocking options above."]] chatbot.value = chat_history chatbot.update(value=chat_history) get_window_url_params = """ function(text_input, url_params) { console.log(text_input, url_params); const params = new URLSearchParams(window.location.search); url_params = Object.fromEntries(params); return [text_input, url_params]; } """ with block: gr.HTML('''

WATCH Tower

''') gr.HTML("


") # todo check if user signed in user_message = "Log in via Twitter and configure your blocking options above." chat_history.append(["Welcome to Watchtower.", user_message]) tabs = gr.Tabs() with tabs: intro_tab = gr.TabItem("Introduction") with intro_tab: gr.HTML(test_html) prediction_tab = gr.TabItem("Getting Started") with prediction_tab: gr.HTML('''
Architecture

WATCH Tower

''') with gr.Group(): with gr.Box(): url_params = gr.JSON({}, visible=False, label="URL Params") text_input = gr.Text(label="Input", visible=False) text_output = gr.Text(label="Output", visible=False) with gr.Row().style(mobile_collapse=False, equal_height=True): gr.HTML( value='Log In With Twitter
'.format( target_website)) with gr.Row().style(mobile_collapse=False, equal_height=True): radio = gr.CheckboxGroup(value="Violent", choices=["Violent", "Hate Speech", "Misinformation"], interactive=False, label="Behaviour To Block") slider = gr.Slider(value=80,interactive=True, label="Threshold Certainty Tolerance") chatbot = gr.Chatbot(value=chat_history, label="Watchtower Output").style() btn = gr.Button("Run WatchTower").style(full_width=True) btn.click(fn=predict, inputs=[slider, url_params], outputs=[text_output, chatbot], _js=get_window_url_params) tabs.change(fn=changed_tab, inputs=None, outputs=None) block.launch(enable_queue=False)