Skip to content

About

Website to classify whether tweets intend to be humorous

Topics

Resources

Stars

10 stars

Watchers

8 watching

Forks

Repository files navigation

ACL Anthology Actions Status Coverage

pgHumor-clasificahumor

Website to crowd-annotate tweets for Humor Research. Originally created for pgHumor, and also used in the HAHA competitions. If you want to learn general information about the data and its format, see the HUMOR website.

Software Stack

  • Backend & API: FastAPI running on Uvicorn with uvloop and httptools for async request handling; Pydantic for data validation; Jinja2 for server-side HTML rendering.
  • Database Layer: MySQL 26.x with SQLAlchemy 2.0 (using async drivers: asyncmy in production and aiosqlite for in-memory unit tests).
  • Frontend: Vanilla modern JavaScript (ES6+), HTML5, and CSS3 without jQuery or Bootstrap JS dependencies.
  • Tooling & Environment: Python >=3.15 managed via uv; containerized with Docker Compose; tested and linted with ruff, ty, and pytest.

Setup

After cloning the repo, you can run this code in two ways: with Docker or via uv. The first one is the recommended way to get started (or to just use for the database), and the second one is for the extraction and analysis part, and for advanced usage (such as debugging with an IDE).

Docker

You need Docker and Docker Compose. To run the web server in debug mode, auto-detecting changes:

docker compose up --build

If you're on a VPN, first exit the VPN, create the containers for the first time (so that the network is created correctly), then you can enter the VPN again.

uv

  1. Clone the repo and cd into web/:

    git clone https://github.com/pln-fing-udelar/pghumor-clasificahumor
    cd web/
  2. Install Python, MySQL, and SQLite3 library headers. In Ubuntu, it'd be:

    sudo apt install libmysqlclient-dev libsqlite3-dev python3-dev
  3. Install the dependencies using uv:

    uv sync
  4. Create a .env file with the database configuration (setting environment variable values):

    DB_HOST=SET_VALUE
    DB_USER=SET_VALUE
    DB_PASS=SET_VALUE
    DB_NAME=SET_VALUE
  5. Set up a MySQL 26.10 instance. It could be the instance generated with the Docker setup (docker compose up database).

  6. Run:

    uv run --env-file ../.env uvicorn clasificahumor.__main__:app --host :: --port 5000 --reload

Code Quality & Testing

cd web/
uv run ruff check --fix
uv run ruff format
uv run ty check
uv run pytest --cov-fail-under=90

Tweet data

You need data to mess with. There's a dump with the downloaded tweets in the HUMOR repo.

First, create a database with db/schema.sql:

mysql -u $USER -p < db/schema.sql

The Docker image's default user is root. The default password for the dev environment in Docker is specified in the docker-compose.override.yml file.

To load a database dump, run in another shell:

mysql -u $USER -p pghumor < dump.sql

You can prefix it with docker compose exec database to run it in the database Docker container. Or you can use a local mysql:

# First check the IP address of the container.
# Note the actual Docker container name depends on the local directory name.
DB_HOST=$(docker container inspect clasificahumor-database-1 | jq -r '.[0].NetworkSettings.Networks."clasificahumor_net".IPAddress')
mycli -h "DB_HOST" -u root -p
# You can also set the password in the command like: -p $PASSWORD

You can append a database name at the end of the command (e.g., pghumor) to select it when starting the session.

mycli sometimes gives Unicode issues, so if that happens, use mysql instead.

Useful SQL commands

List the databases:

SHOW DATABASES;

List pghumor database tables:

USE pghumor;
SHOW tables;

Describe a particular table (e.g., tweets):

DESCRIBE tweets;

Show some data from a table:

SELECT * FROM tweets LIMIT 10;

Testing

To run it using the production web server, do:

docker compose -f docker-compose.yml -f docker-compose.testing.yml up -d --build

Then you can do some testing, such as running a load test:

./web/load_test.sh

Manipulating production data

To back up the data in production:

docker exec clasificahumor-database-1 mysqldump -u root -p pghumor > dump.sql

To run a SQL script in production (e.g., to restore some data):

docker exec -i clasificahumor-database-1 mysql -u root -p pghumor < dump.sql

To open a MySQL interactive session in production:

docker exec -i clasificahumor-database-1 mysql -u root -p pghumor

For these commands, using Docker Compose directly (docker compose exec database) is also supported instead of the Docker CLI directly (docker exec clasificahumor-database-1). However, the extra flags needed for each of them change as the Docker Compose exec subcommand uses a pseudo TTY, and it's interactive by default, while the Docker CLI exec subcommand doesn't.

Production setup

The repo was first cloned in production in /opt/clasificahumor. The following command was run:

git config receive.denyCurrentBranch updateInstead

The file /opt/clasificahumor/.git/hooks/post-update in production has been set with the following content to deploy on git push:

#!/usr/bin/env bash

pushd .. > /dev/null  # So it loads the .env file in the working directory.
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d --build
popd > /dev/null

Deploy to production

Add a git remote to push to production:

git remote add production $YOUR_USERNAME@clasificahumor.com:/opt/clasificahumor

Then just push to production:

git push production

After many Docker builds in production, if there's not enough storage available, you can run:

docker system prune

Tweet extraction

Follow these steps to download new tweets and add them to the database.

Download new tweets

Add the following to the .env file with the content (replace with the Twitter API credentials values):

CONSUMER_TOKEN=...
CONSUMER_SECRET=...
ACCESS_TOKEN=...
ACCESS_TOKEN_SECRET=...

Note that normally we wouldn't need the access token and access token secret as we're not authenticating other users to this "Twitter app." However, the app access token can be used to act in the name of the Twitter app user owner (user-based authentication), and thus gain greater Twitter API rate limits than in an app-based authentication context.

Download tweets from the hose

cd web/
./extraction/download_hose.py > tweets1.jsonl

Download tweets from humorous accounts

./extraction/download_from_accounts.py > tweets2.jsonl

Persist the downloaded tweets into the database

./extraction/persist.py < tweets.jsonl

See the options available in the command with ./extraction/persist.py --help.

Analysis

To compute the agreement (for example, with this annotations_by_tweet.csv file):

cd web/
./analysis/agreement.py FILE

Troubleshooting

If you have an SSL connection error when trying to access the database, see MySQL ERROR 2026 - SSL connection error - Ubuntu 20.04.

About

Website to classify whether tweets intend to be humorous

Topics

Resources

Stars

10 stars

Watchers

8 watching

Forks

Releases

Packages

Used by

Contributors

Languages