patrawtf commited on
Commit
b634378
1 Parent(s): a2c209a

create app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import TapasTokenizer, TFTapasForQuestionAnswering
2
+ import pandas as pd
3
+ import datetime
4
+
5
+
6
+ def execute_query(query, csv_file):
7
+ a = datetime.datetime.now()
8
+
9
+ table = pd.read_csv(csv_file.name, delimiter=",")
10
+ table.fillna(0, inplace=True)
11
+ table = table.astype(str)
12
+
13
+ model_name = "google/tapas-base-finetuned-wtq"
14
+ model = TFTapasForQuestionAnswering.from_pretrained(model_name)
15
+ tokenizer = TapasTokenizer.from_pretrained(model_name)
16
+
17
+ queries = [query]
18
+
19
+ inputs = tokenizer(table=table, queries=queries, padding=True, return_tensors="tf",truncated=True)
20
+ outputs = model(**inputs)
21
+
22
+ predicted_answer_coordinates, predicted_aggregation_indices = tokenizer.convert_logits_to_predictions(
23
+ inputs, outputs.logits, outputs.logits_aggregation
24
+ )
25
+
26
+ # let's print out the results:
27
+ id2aggregation = {0: "NONE", 1: "SUM", 2: "AVERAGE", 3: "COUNT"}
28
+ aggregation_predictions_string = [id2aggregation[x] for x in predicted_aggregation_indices]
29
+
30
+ answers = []
31
+ for coordinates in predicted_answer_coordinates:
32
+ if len(coordinates) == 1:
33
+ # only a single cell:
34
+ answers.append(table.iat[coordinates[0]])
35
+ else:
36
+ # multiple cells
37
+ cell_values = []
38
+ for coordinate in coordinates:
39
+ cell_values.append(table.iat[coordinate])
40
+ answers.append(cell_values)
41
+
42
+ for query, answer, predicted_agg in zip(queries, answers, aggregation_predictions_string):
43
+ if predicted_agg != "NONE":
44
+ answers.append(predicted_agg)
45
+
46
+ query_result = {
47
+ "query": query,
48
+ "result": answers
49
+ }
50
+
51
+ b = datetime.datetime.now()
52
+ print(b - a)
53
+
54
+ return query_result, table
55
+