Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,7 @@ qa_pipeline = pipeline("table-question-answering", model="google/tapas-large-fin
|
|
11 |
def ask_table(data, question):
|
12 |
logging.info(f"Received Data: {data}")
|
13 |
logging.info(f"Received Question: {question}")
|
|
|
14 |
try:
|
15 |
if not data or not question:
|
16 |
return {"answer": "Please provide both a table and a question."}
|
@@ -18,15 +19,30 @@ def ask_table(data, question):
|
|
18 |
headers = data[0] # First row is headers
|
19 |
rows = data[1:] # Remaining rows are data
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
# Convert to TAPAS-compatible format
|
22 |
processed_table = [dict(zip(headers, row)) for row in rows]
|
23 |
|
24 |
if not processed_table:
|
25 |
return {"answer": "Table data is empty or improperly formatted."}
|
26 |
|
|
|
27 |
answers = qa_pipeline(table=processed_table, query=question)
|
28 |
-
|
|
|
|
|
|
|
29 |
except Exception as e:
|
|
|
30 |
return {"answer": f"Error: {str(e)}"}
|
31 |
|
32 |
# Define Gradio interface
|
@@ -38,7 +54,7 @@ iface = gr.Interface(
|
|
38 |
row_count=(2, "dynamic"),
|
39 |
col_count=(19, "dynamic"),
|
40 |
type="array",
|
41 |
-
label="Input Table"
|
42 |
),
|
43 |
gr.Textbox(
|
44 |
lines=2,
|
|
|
11 |
def ask_table(data, question):
|
12 |
logging.info(f"Received Data: {data}")
|
13 |
logging.info(f"Received Question: {question}")
|
14 |
+
|
15 |
try:
|
16 |
if not data or not question:
|
17 |
return {"answer": "Please provide both a table and a question."}
|
|
|
19 |
headers = data[0] # First row is headers
|
20 |
rows = data[1:] # Remaining rows are data
|
21 |
|
22 |
+
# Check if headers and rows exist
|
23 |
+
if not headers or not rows:
|
24 |
+
return {"answer": "Table data is empty or improperly formatted."}
|
25 |
+
|
26 |
+
# Check table size (up to 50 rows, 20 columns)
|
27 |
+
if len(rows) > 50:
|
28 |
+
return {"answer": "The table has too many rows. Limit: 50 rows."}
|
29 |
+
if len(headers) > 20:
|
30 |
+
return {"answer": "The table has too many columns. Limit: 20 columns."}
|
31 |
+
|
32 |
# Convert to TAPAS-compatible format
|
33 |
processed_table = [dict(zip(headers, row)) for row in rows]
|
34 |
|
35 |
if not processed_table:
|
36 |
return {"answer": "Table data is empty or improperly formatted."}
|
37 |
|
38 |
+
# Call the model to get the answer
|
39 |
answers = qa_pipeline(table=processed_table, query=question)
|
40 |
+
answer_text = answers.get("answer", "No answer found.")
|
41 |
+
|
42 |
+
return {"answer": answer_text}
|
43 |
+
|
44 |
except Exception as e:
|
45 |
+
logging.error(f"Error processing table: {str(e)}")
|
46 |
return {"answer": f"Error: {str(e)}"}
|
47 |
|
48 |
# Define Gradio interface
|
|
|
54 |
row_count=(2, "dynamic"),
|
55 |
col_count=(19, "dynamic"),
|
56 |
type="array",
|
57 |
+
label="Input Table (First row = Headers)"
|
58 |
),
|
59 |
gr.Textbox(
|
60 |
lines=2,
|