<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <a href="https
Step1:  Running a Tapas fine-tuned checkpoint
This notebook shows how to load and make predictions with TAPAS model, which was introduced in the paper
Step2:  Fetch models fom Google Storage
Next we can get pretrained checkpoint from Google Storage. For the sake of speed, this is a medium sized model trained on TABFACT. Note that best results in the paper were obtained with a large model.
Step3:  Imports
Step5:  Load checkpoint for prediction
Here's the prediction code, which will create and interaction_pb2.Interaction protobuf object, which is the datastructure we use to store examples, and then call the prediction script.
Step7:  Predict
<ASSISTANT_TASK:>
Python Code:
# Copyright 2019 The Google AI Language Team Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Explanation: <a href="https://colab.research.google.com/github/google-research/tapas/blob/master/notebooks/tabfact_predictions.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>
Copyright 2020 The Google AI Language Team Authors
Licensed under the Apache License, Version 2.0 (the "License");
End of explanation
! pip install tapas-table-parsing
Explanation: Running a Tapas fine-tuned checkpoint
This notebook shows how to load and make predictions with TAPAS model, which was introduced in the paper: TAPAS: Weakly Supervised Table Parsing via Pre-training
Clone and install the repository
First, let's install the code.
End of explanation
! gsutil cp "gs://tapas_models/2020_10_07/tapas_tabfact_inter_masklm_medium_reset.zip" "tapas_model.zip" && unzip tapas_model.zip
! mv tapas_tabfact_inter_masklm_medium_reset tapas_model
Explanation: Fetch models fom Google Storage
Next we can get pretrained checkpoint from Google Storage. For the sake of speed, this is a medium sized model trained on TABFACT. Note that best results in the paper were obtained with a large model.
End of explanation
import tensorflow.compat.v1 as tf
import os 
import shutil
import csv
import pandas as pd
import IPython
tf.get_logger().setLevel('ERROR')
from tapas.utils import tf_example_utils
from tapas.protos import interaction_pb2
from tapas.utils import number_annotation_utils
import math
Explanation: Imports
End of explanation
os.makedirs('results/tabfact/tf_examples', exist_ok=True)
os.makedirs('results/tabfact/model', exist_ok=True)
with open('results/tabfact/model/checkpoint', 'w') as f:
  f.write('model_checkpoint_path: "model.ckpt-0"')
for suffix in ['.data-00000-of-00001', '.index', '.meta']:
  shutil.copyfile(f'tapas_model/model.ckpt{suffix}', f'results/tabfact/model/model.ckpt-0{suffix}')
max_seq_length = 512
vocab_file = "tapas_model/vocab.txt"
config = tf_example_utils.ClassifierConversionConfig(
    vocab_file=vocab_file,
    max_seq_length=max_seq_length,
    max_column_id=max_seq_length,
    max_row_id=max_seq_length,
    strip_column_names=False,
    add_aggregation_candidates=False,
)
converter = tf_example_utils.ToClassifierTensorflowExample(config)
def convert_interactions_to_examples(tables_and_queries):
  Calls Tapas converter to convert interaction to example.
  for idx, (table, queries) in enumerate(tables_and_queries):
    interaction = interaction_pb2.Interaction()
    for position, query in enumerate(queries):
      question = interaction.questions.add()
      question.original_text = query
      question.id = f"{idx}-0_{position}"
    for header in table[0]:
      interaction.table.columns.add().text = header
    for line in table[1:]:
      row = interaction.table.rows.add()
      for cell in line:
        row.cells.add().text = cell
    number_annotation_utils.add_numeric_values(interaction)
    for i in range(len(interaction.questions)):
      try:
        yield converter.convert(interaction, i)
      except ValueError as e:
        print(f"Can't convert interaction: {interaction.id} error: {e}")
        
def write_tf_example(filename, examples):
  with tf.io.TFRecordWriter(filename) as writer:
    for example in examples:
      writer.write(example.SerializeToString())
def predict(table_data, queries):
  table = [list(map(lambda s: s.strip(), row.split("|"))) 
           for row in table_data.split("\n") if row.strip()]
  examples = convert_interactions_to_examples([(table, queries)])
  write_tf_example("results/tabfact/tf_examples/test.tfrecord", examples)
  write_tf_example("results/tabfact/tf_examples/dev.tfrecord", [])
  
  ! python -m tapas.run_task_main \
    --task="TABFACT" \
    --output_dir="results" \
    --noloop_predict \
    --test_batch_size={len(queries)} \
    --tapas_verbosity="ERROR" \
    --compression_type= \
    --reset_position_index_per_cell \
    --init_checkpoint="tapas_model/model.ckpt" \
    --bert_config_file="tapas_model/bert_config.json" \
    --mode="predict" 2> error
  results_path = "results/tabfact/model/test.tsv"
  all_results = []
  df = pd.DataFrame(table[1:], columns=table[0])
  display(IPython.display.HTML(df.to_html(index=False)))
  print()
  with open(results_path) as csvfile:
    reader = csv.DictReader(csvfile, delimiter='\t')
    for row in reader:
      supported = int(row["pred_cls"])
      all_results.append(supported)
      score = float(row["logits_cls"])
      position = int(row['position'])
      if supported:
        print("> SUPPORTS:", queries[position])
      else:
        print("> REFUTES:", queries[position])
  return all_results
Explanation: Load checkpoint for prediction
Here's the prediction code, which will create and interaction_pb2.Interaction protobuf object, which is the datastructure we use to store examples, and then call the prediction script.
End of explanation
# Based on TabFact table 2-1610384-4.html.csv
result = predict(
tournament            | wins | top - 10 | top - 25 | events | cuts made
masters tournament    | 0    | 0        | 1        | 3      | 2        
us open               | 0    | 0        | 0        | 4      | 3        
the open championship | 0    | 0        | 0        | 2      | 1        
pga championship      | 0    | 1        | 1        | 4      | 2        
totals                | 0    | 1        | 2        | 13     | 8        
, ["The most frequently occurring number of events is 4", "The most frequently occurring number of events is 3"])
Explanation: Predict
End of explanation
<END_TASK>