I want to use tapas for returning answers on questions from a pdf. This is the table:
It’s not showing any errors but returning answer not found. Can someone please help me fix this? Thanks a lot!
This is my code:
!apt-get install -y poppler-utils tesseract-ocr
import os
os.environ[“PATH”] += “:/usr/bin/poppler-utils:/usr/bin/tesseract”
from pdf2image import convert_from_path
from transformers import TapasTokenizer, TapasForQuestionAnswering
import pytesseract
import torch
import pandas as pd
pdf_path = ‘/content/PSP RfS.pdf’
images = convert_from_path(pdf_path)
table_texts =
for image in images:
table_text = pytesseract.image_to_string(image)
table_texts.append(table_text)
questions = [“What is the amount of Excess input energy charges payable by PSP Developer”]
tokenizer = TapasTokenizer.from_pretrained(“google/tapas-large-finetuned-wtq”)
model = TapasForQuestionAnswering.from_pretrained(“google/tapas-large-finetuned-wtq”)
for table_text in table_texts:
# Process table_text to convert it into a list of rows (list of lists)
rows = [row.split(‘|’) for row in table_text.strip().split(‘\n’)]
columns = rows[0]
data = [row for row in rows[1:] if len(row) == len(columns)] # Filter incomplete rows
# Create a DataFrame from the data
table_data = pd.DataFrame(data, columns=columns)
# Prepare the inputs for the TAPAS model
queries = questions * len(table_data)
inputs = tokenizer(table=table_data, queries=queries, return_tensors="pt", padding="max_length", truncation=True)
with torch.no_grad():
outputs = model(**inputs)
# Convert the logits to predicted answer coordinates
#predicted_answer_coordinates = torch.argmax(outputs.logits, dim=1).tolist()
predicted_column_indices = torch.argmax(outputs.logits, dim=1).tolist()
for idx, answer_idx in enumerate(predicted_answer_coordinates):
answer = str(table_data.iloc[idx, answer_idx].strip()) if 0 <= answer_idx < len(table_data.columns) else "Answer not found"
print("Answer:", answer)
print("Answer:", answer)