This script performs Table Question Answering (Table QA) locally using the google/tapas-base-finetuned-wtq model via the Hugging Face transformers library.
You provide a table (as a Pandas DataFrame within the script) and a question about the data in the table, and the model will attempt to find the answer within that table structure.
- Performs Table QA locally on your machine.
- Uses the
google/tapas-base-finetuned-wtqmodel (TAPAS architecture). - Answers natural language questions based on structured table data.
- Requires table data defined as a Pandas DataFrame in the script.
- Leverages the Hugging Face
transformersandpandaslibraries. - Optionally utilizes GPU for faster processing.
- Table QA Model:
google/tapas-base-finetuned-wtq(fine-tuned on WikiTableQuestions)
Before running the script, ensure you have the following installed:
- Python: Python 3.8 or later recommended.
- System Dependencies: None specific beyond standard build tools.
- Python Libraries: You need
pandasfor table handling and potentiallytorch-scatterwhich is often required by TAPAS implementations withintransformers. Use pip in a virtual environment:pip install transformers torch pandas torch-scatter
transformers: The core Hugging Face library.torch: The deep learning framework backend (PyTorch).pandas: Required for creating and handling the table data structure (DataFrame).torch-scatter: Often a required dependency for TAPAS model computations. Install it explicitly just in case.
- Clone or Download: Get the
run_table_qa.pyscript onto your local machine. - Create Virtual Environment (Recommended):
(Use
python3 -m venv .venv source .venv/bin/activate.\.venv\Scripts\activateon Windows) - Install Python Libraries: Run the pip command from the Prerequisites section within your activated virtual environment.
-
Configure Inputs (Table & Question):
- Open the
run_table_qa.pyscript in a text editor. - Table Data:
- Locate the section marked
# 1. Define the table data.... - Modify the
datadictionary and the resultingpd.DataFrame(data)to represent the table you want to query. Ensure column names are strings.
- Locate the section marked
- Question:
- Locate the line:
question = "What time is the concert at RAC Arena?" - Change the question text to be relevant to the table data you provided.
- Locate the line:
- Open the
-
Run the Script:
- Open your terminal or command prompt.
- Make sure your virtual environment is activated.
- Navigate to the directory containing the script.
- Execute the script using Python:
python run_table_qa.py
The script will print the table data and the question being asked. The final output will be the model's answer derived from analyzing the table: