patrawtf commited on
Commit
4c83d7c
1 Parent(s): 7a1d852

Create tapas.py

Browse files
Files changed (1) hide show
  1. app/tapas.py +33 -0
app/tapas.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import TapasTokenizer, TFTapasForQuestionAnswering, TapexTokenizer, BartForConditionalGeneration
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 = "microsoft/tapex-large-finetuned-wtq"
14
+ model = BartForConditionalGeneration.from_pretrained(model_name)
15
+ tokenizer = TapexTokenizer.from_pretrained(model_name)
16
+
17
+ queries = [query]
18
+
19
+ encoding = tokenizer(table=table, queries=queries, padding=True, return_tensors="tf",truncated=True)
20
+ outputs = model.generate(**encoding)
21
+ ans = tokenizer.batch_decode(outputs, skip_special_tokens=True)
22
+
23
+
24
+
25
+ query_result = {
26
+ "query": query,
27
+ "answer": ans[0]
28
+ }
29
+
30
+ b = datetime.datetime.now()
31
+ print(b - a)
32
+
33
+ return query_result, table