SunilMahi commited on
Commit
ae36587
·
verified ·
1 Parent(s): b1b7def

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -26
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
- if not data or not question:
17
- return {"answer": "Please provide both a table and a question."}
18
 
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
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 (First row = Headers)"
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,