Update app.py
Browse files
app.py
CHANGED
@@ -9,41 +9,30 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(
|
|
9 |
qa_pipeline = pipeline("table-question-answering", model="google/tapas-large-finetuned-wtq")
|
10 |
|
11 |
def ask_table(data, question):
|
12 |
-
logging.info(f"Received Data: {data}")
|
13 |
-
logging.info(f"Received Question: {question}")
|
14 |
-
|
15 |
try:
|
16 |
-
|
17 |
-
return {"answer": "Please provide both a table and a question."}
|
18 |
|
19 |
-
|
20 |
-
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
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 |
-
#
|
33 |
-
processed_table = [dict(zip(headers, row)) for row in rows]
|
34 |
|
35 |
if not processed_table:
|
36 |
-
return
|
37 |
|
38 |
-
#
|
39 |
answers = qa_pipeline(table=processed_table, query=question)
|
40 |
-
|
41 |
|
42 |
-
return
|
43 |
-
|
44 |
except Exception as e:
|
45 |
-
logging.error(f"Error
|
46 |
-
return
|
47 |
|
48 |
# Define Gradio interface
|
49 |
iface = gr.Interface(
|
@@ -54,7 +43,7 @@ iface = gr.Interface(
|
|
54 |
row_count=(2, "dynamic"),
|
55 |
col_count=(19, "dynamic"),
|
56 |
type="array",
|
57 |
-
label="Input Table
|
58 |
),
|
59 |
gr.Textbox(
|
60 |
lines=2,
|
|
|
9 |
qa_pipeline = pipeline("table-question-answering", model="google/tapas-large-finetuned-wtq")
|
10 |
|
11 |
def ask_table(data, question):
|
|
|
|
|
|
|
12 |
try:
|
13 |
+
logging.info(f"Received table: {data}, question: {question}")
|
|
|
14 |
|
15 |
+
if not data or not question:
|
16 |
+
return "Please provide both a table and a question."
|
17 |
|
18 |
+
# Convert the table to a format compatible with TAPAS
|
19 |
+
headers = data[0] # Assume the first row contains headers
|
20 |
+
rows = data[1:] # Remaining rows are table data
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
# Process the table into dictionary format
|
23 |
+
processed_table = [dict(zip(headers, row)) for row in rows if any(row)]
|
24 |
|
25 |
if not processed_table:
|
26 |
+
return "The table is empty or invalid."
|
27 |
|
28 |
+
# Query the TAPAS model
|
29 |
answers = qa_pipeline(table=processed_table, query=question)
|
30 |
+
logging.info(f"Answer: {answers}")
|
31 |
|
32 |
+
return answers.get("answer", "No answer found.")
|
|
|
33 |
except Exception as e:
|
34 |
+
logging.error(f"Error: {str(e)}")
|
35 |
+
return f"Error processing your request: {str(e)}"
|
36 |
|
37 |
# Define Gradio interface
|
38 |
iface = gr.Interface(
|
|
|
43 |
row_count=(2, "dynamic"),
|
44 |
col_count=(19, "dynamic"),
|
45 |
type="array",
|
46 |
+
label="Input Table"
|
47 |
),
|
48 |
gr.Textbox(
|
49 |
lines=2,
|