File size: 2,014 Bytes
efc35b3
349d391
c677693
 
 
 
efc35b3
349d391
 
d3ef69c
349d391
c677693
ae36587
44189f0
ae36587
 
44189f0
ae36587
 
 
b1b7def
ae36587
 
44189f0
 
ae36587
5690b37
ae36587
c677693
ae36587
b1b7def
ae36587
c677693
ae36587
 
349d391
 
 
 
 
c677693
44189f0
c677693
 
 
ae36587
c677693
 
 
 
 
 
349d391
 
c677693
44189f0
349d391
 
 
c677693
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import gradio as gr
from transformers import pipeline
import logging

# Set up logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

# Load the Tapas model
qa_pipeline = pipeline("table-question-answering", model="google/tapas-large-finetuned-wtq")

def ask_table(data, question):
    try:
        logging.info(f"Received table: {data}, question: {question}")
        
        if not data or not question:
            return "Please provide both a table and a question."
        
        # Convert the table to a format compatible with TAPAS
        headers = data[0]  # Assume the first row contains headers
        rows = data[1:]    # Remaining rows are table data
        
        # Process the table into dictionary format
        processed_table = [dict(zip(headers, row)) for row in rows if any(row)]
        
        if not processed_table:
            return "The table is empty or invalid."
        
        # Query the TAPAS model
        answers = qa_pipeline(table=processed_table, query=question)
        logging.info(f"Answer: {answers}")
        
        return answers.get("answer", "No answer found.")
    except Exception as e:
        logging.error(f"Error: {str(e)}")
        return f"Error processing your request: {str(e)}"

# Define Gradio interface
iface = gr.Interface(
    fn=ask_table,
    inputs=[
        gr.Dataframe(
            headers=None,  # Expect the user to provide headers in the first row
            row_count=(2, "dynamic"),
            col_count=(19, "dynamic"),
            type="array",
            label="Input Table"
        ),
        gr.Textbox(
            lines=2,
            placeholder="Enter your question about the table here...",
            label="Ask a Question"
        )
    ],
    outputs="text",
    title="Table Question Answering",
    description="Provide a table with headers in the first row and ask questions. Supports up to 50 rows and 20 columns."
)

# Launch Gradio app
iface.launch(share=True)