Coder Social home page Coder Social logo

ahmedshahriar / twittercelebritymatcher Goto Github PK

View Code? Open in Web Editor NEW
13.0 2.0 2.0 5.88 MB

Match celebrity users with their respective tweets by making use of Semantic Textual Similarity on over 900+ celebrity users' 2.5 million+ scraped tweets utilizing SBERT, streamlit, tweepy and FastAPI

Home Page: https://share.streamlit.io/ahmedshahriar/twittercelebritymatcher/main/main.py

License: Apache License 2.0

Python 99.09% Dockerfile 0.91%
python39 tweepy sentence-transformers multilingual-bert sbert pytorch-gpu pytorch web-scraping twitter-scraping streamlit

twittercelebritymatcher's Introduction

Twitter Celebrity Matcher

made-with-python License Binder Live in Streamlit

The objective of this project is to match celebrity users with their respective tweets by making use of Semantic Textual Similarity.

This app finds similar Twitter users based on their tweets. It works in two ways -

  1. Get a list of the most similar celebrity Twitter accounts based on a predefined Twitter celebrity list (915
    Twitter celebrity accounts).
  2. Find similarities between two Twitter users based on their tweets.

View this project on kaggle - kaggle.com/code/ahmedshahriarsakib/twitter-celebrity-matcher-sbert-tweepy


๐Ÿ’ป Streamlit App โ˜‘๏ธ How it works? ๐Ÿ› ๏ธ Project Structure ๐Ÿ’พ Dataset ๐Ÿ“ท Screenshots ๐Ÿ”Ž Findings โฌ‡๏ธ Built With

Setup ๐Ÿ”ง

Add the following to your .env file to work with tweepy:

ACCESS_KEY=YOUR_ACCESS_KEY
ACCESS_SECRET=YOUR_ACCESS_SECRET
CONSUMER_KEY=YOUR_CONSUMER_KEY
CONSUMER_SECRET=YOUR_CONSUMER_SECRET

Virtual Environment Setup

Set up a virtual environment (preferably python 3.8+, features such as walrus operator was used) and run:

$ pip install -r requirements.txt

Check main.py file to run the streamlit app or API.

Docker Setup

Build -

docker build -t twitter-celebrity-matcher .

Run -

docker run -p 8501:8501 twitter-celebrity-matcher

If you have CUDA enabled GPU, you can set up pytorch (for SBERT) with pip -

$ pip install --no-cache-dir --force-reinstall torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio===0.11.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html

Static type testing -

$ mypy main.py --ignore-missing-imports

Streamlit App๐Ÿ’ป

The app file is located at app/app.py.

To run the app, run the following command after installing necessary dependencies from requirements.txt -

$ streamlit run main.py

will start the app at default port 8501.

App Walkthrough

Project Structure

TwitterCelebrityMatcher
โ”œโ”€โ”€ api
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ api.py
โ”œโ”€โ”€ app
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ app.py
|   โ”œโ”€โ”€ appdata.py
โ”‚   โ”œโ”€โ”€ apphome.py
โ”‚   โ”œโ”€โ”€ appinit.py
โ”‚   โ”œโ”€โ”€ utils.py
โ”œโ”€โ”€ celebrity-listing
โ”‚   โ”œโ”€โ”€ Top-1000-Celebrity-Twitter-Accounts.csv
โ”œโ”€โ”€ core
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ dataprep.py
โ”‚   โ”œโ”€โ”€ matcher.py
โ”‚   โ”œโ”€โ”€ scraper.py
โ”‚   โ”œโ”€โ”€ utils.py
โ”œโ”€โ”€ twitter-celebrity-embed-data
โ”‚   โ”œโ”€โ”€ twitter-celebrity-embed-data.csv
โ””โ”€โ”€ .env
    |
    config.py
    |
    main.py    
  • api - fastApi script
  • app - streamlit app scripts
    • app.py - main app script
    • appdata.py - dataclass contains configuration variables
    • apphome.py - core functionalities
    • appinit.py - helper script to initialize the core objects (scraper and matcher)
    • utils.py - utility functions (e.g. social sharing html script)
  • core - contains 3 helper scripts
    • dataprep.py - scripts for all preprocessing tasks ( tweet cleaning, embedding generation etc.)
    • matcher.py - core functionalities to match twitter users
    • matcher.py - twitter scraper helper script
    • utils.py - utility functions
  • config.py - all configuration variables
  • main.py - run 3 apps - console scripts, streamlit app or API

utitlities directory contains a standalone tweepy scraper scripts - tweepy_scraper.py and emoticon proprocessing scripts (original from ekphrasis module)

How it works?

The celebrity tweets were collected using tweepy. After preprocessing the tweets, the tweets were embedded using sentence-transformers pretrained multilingual model - paraphrase-multilingual-MiniLM-L12-v2. The similarity score between tweets embeddings is calculated using cosine similarity.

This app -

  1. Takes a Twitter username
  2. Scrapes the tweets if it's unavailable
  3. Generate embeddings of the given Twitter account's tweets
  4. Calculate the mean embedding of the tweets (limitation ~3200 tweets)
  5. Finds the cosine similarity between
    • Given user's tweets embeddings and celebrity tweets embeddings
    • Given two user's tweets embeddings
  6. Returns the most similar celebrity Twitter accounts based on similarity score or just score between two users

Dataset

List of Celebrity Twitter Accounts

Open Source GitHub Gist - Top-1000-Celebrity-Twitter-Accounts.csv

NB:

I uploaded the relevant datasets on kaggle. Feel free to use the datasets -

  1. Top 1000 Twitter Celebrity Accounts - Kaggle
  2. Top 1000 Twitter Celebrity Tweets And Embeddings - Kaggle
  3. Emoticon Dictionary - Kaggle

Generated Embeddings of Celebrity Tweets

The final embeddings of all celebrity tweets -

Data Preprocessing

The tweets were collected and saved as bytestring to preserve the text encoding (some tweets may contain complicated symbol/emojis etc.). So first the tweets are decoded as string and then some preprocessing techniques are applied.

Current pipeline :

  • Removed hashtags, urls, and mentions
  • Replaced emoticons with their textual representation (ekphrasis)
  • Replaced emoji with their textual representation (demoji package)

Model

Citation
@inproceedings{reimers-2019-sentence-bert,
    title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
    author = "Reimers, Nils and Gurevych, Iryna",
    booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
    month = "11",
    year = "2019",
    publisher = "Association for Computational Linguistics",
    url = "http://arxiv.org/abs/1908.10084",
}

Word embedding dimension : 384

Download SBERT pretrained models directly from here

Screenshots

Match Top Celebrities

Match 1v1 Celebrities

User Interface

Result Section

Findings!๐Ÿ”Ž

  • Most of the celebrities are from music, film or sports industry. The similarity results in these categories are very impressive.

Music

Click to view similar celebrities from music industry - individual (query - Taylor Swift)

Click to view similar celebrities from music industry - band (query - Coldplay)

Sports

Click to view similar celebrities from music industry - footballer (query - Cristiano Ronaldo)

Regional Film Industry

  • The multilingual model does a great job finding region/culture specific attributes. Below is an example of bollywood celebrities
Click to view similar celebrities from film industry - actor (query - Shah Rukh Khan)

Author

  • The number of authors in this celebrity list is comparatively lower than other dominating professions (actors, singers, footballers etc.). So in this example the first few results (first two from the results -> both are above 0.94 score ) were good but then the score drops. The drop rate is higher that the results from the dominating celebrity professions where the top score is around 0.96+ and first 10 or 20 from the list have very close score. This will vary but with only 5 users the performance of the pretrained model is great in this case
Click to view similar celebrity authors - (query - John Green)

Multiple Roles - Business Magnet/ Philanthropist

  • From the screenshot below, it's very clear that there are few people on the list who are very active in multiple categories.
    • The closest of them all (for Bill Gates) is Richard Branson although the score is comparatively lower (0.82) than what we see in other categories. Considering how close their roles are (both are billionaires, investor, tech) this is a very positive result.
    • Leonardo DiCaprio is the second placeholder because he falls into Philanthropist category, same goes for Tony Robbins
    • Other famous billionaires such as - anand mahindra and Ratan N. Tata makes it to the top 10
    • Most of the top people are either founders of large organizations, activists, philanthropists or authors. (Bill Gates published few books, so this could be another factor)
Click to view similar celebrity billionaires/investors/businessmen/investor - (query - Bill Gates)

NB: Due to space limitation, the screenshots above displays only top 10 results, try the app to view more top results which are very similar to the query

FYI

Test Workstation (RAM 16gb 2400Hz, AMD 2600x, 1050ti (768 CUDA cores))

  • With CUDA enabled GPU the app runs ~5x faster than CPU.
  • With the current scraping scripts, it takes ~14-16 seconds to download tweets
  • Takes ~2h 5m in total to preprocess and generate embedding for all users' (915 total) tweets

Built With

demoji==1.1.0
ekphrasis==0.5.1
jupyter==1.0.0
python-dotenv==0.19.2
schedule==1.1.0
sentence-transformers==2.2.0
torch==1.11.0+cu113
torchaudio==0.11.0+cu113
torchvision==0.12.0+cu113
streamlit==1.7.0
tweepy==4.7.0

Reference

twittercelebritymatcher's People

Contributors

ahmedshahriar avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.