





'''simple docstring'''


import os

import jsonlines
import numpy as np
from tqdm import tqdm


_SCREAMING_SNAKE_CASE	 =							20_48
_SCREAMING_SNAKE_CASE	 =							40_96
_SCREAMING_SNAKE_CASE	 =							42
_SCREAMING_SNAKE_CASE	 =							os.environ.pop("PROCESS_TRAIN", "false")
_SCREAMING_SNAKE_CASE	 =							{"null": 0, "short": 1, "long": 2, "yes": 3, "no": 4}


def 	__a(SCREAMING_SNAKE_CASE_  :					Union[str, Any]    ):



    '''simple docstring'''





    def choose_first(SCREAMING_SNAKE_CASE_  :					str		,       SCREAMING_SNAKE_CASE_  :					List[Any]=False    ):
        assert isinstance(SCREAMING_SNAKE_CASE_		,       SCREAMING_SNAKE_CASE_    )
        if len(SCREAMING_SNAKE_CASE_    ) == 1:
            _lowerCAmelCase				=      answer[0]
            return {k: [answer[k]] for k in answer} if is_long_answer else answer
        for a in answer:
            if is_long_answer:
                _lowerCAmelCase				=      {k: [a[k]] for k in a}
            if len(a["start_token"]    ) > 0:
                break
        return a

    _lowerCAmelCase				=      {"id": example["id"]}
    _lowerCAmelCase				=      example["annotations"]
    _lowerCAmelCase				=      annotation["yes_no_answer"]
    if 0 in yes_no_answer or 1 in yes_no_answer:
        _lowerCAmelCase				=      ["yes"] if 1 in yes_no_answer else ["no"]
        _lowerCAmelCase				=      _lowerCAmelCase				=      []
        _lowerCAmelCase				=      _lowerCAmelCase				=      []
        _lowerCAmelCase				=      ["<cls>"]
    else:
        _lowerCAmelCase				=      ["short"]
        _lowerCAmelCase				=      choose_first(annotation["short_answers"]    )
        if len(out["start_token"]    ) == 0:
            # answer will be long if short is not available
            _lowerCAmelCase				=      ["long"]
            _lowerCAmelCase				=      choose_first(annotation["long_answer"]		,       is_long_answer=SCREAMING_SNAKE_CASE_    )
            _lowerCAmelCase				=      []
        answer.update(SCREAMING_SNAKE_CASE_    )

    # disregard some samples
    if len(answer["start_token"]    ) > 1 or answer["start_token"] == answer["end_token"]:
        _lowerCAmelCase				=      True
    else:
        _lowerCAmelCase				=      False

    _lowerCAmelCase				=      ["start_token", "end_token", "start_byte", "end_byte", "text"]
    if not all(isinstance(answer[k]		,       SCREAMING_SNAKE_CASE_    ) for k in cols    ):
        raise ValueError("Issue in ID"		,       example["id"]    )

    return answer


def 	__a(SCREAMING_SNAKE_CASE_  :					Optional[int]		,       SCREAMING_SNAKE_CASE_  :					List[Any]=False    ):



    '''simple docstring'''





    _lowerCAmelCase				=      _get_single_answer(SCREAMING_SNAKE_CASE_    )
    # bytes are of no use
    del answer["start_byte"]
    del answer["end_byte"]

    # handle yes_no answers explicitly
    if answer["category"][0] in ["yes", "no"]:  # category is list with one element
        _lowerCAmelCase				=      example["document"]["tokens"]
        _lowerCAmelCase				=      []
        for i in range(len(doc["token"]    )    ):
            if not doc["is_html"][i]:
                context.append(doc["token"][i]    )
        return {
            "context": " ".join(SCREAMING_SNAKE_CASE_    ),
            "answer": {
                "start_token": -100,  # ignore index in cross-entropy
                "end_token": -100,  # ignore index in cross-entropy
                "category": answer["category"],
                "span": answer["category"],  # extra
            },
        }

    # later, help in removing all no answers
    if answer["start_token"] == [-1]:
        return {
            "context": "None",
            "answer": {
                "start_token": -1,
                "end_token": -1,
                "category": "null",
                "span": "None",  # extra
            },
        }

    # handling normal samples

    _lowerCAmelCase				=      ["start_token", "end_token"]
    answer.update({k: answer[k][0] if len(answer[k]    ) > 0 else answer[k] for k in cols}    )  # e.g. [10] == 10

    _lowerCAmelCase				=      example["document"]["tokens"]
    _lowerCAmelCase				=      answer["start_token"]
    _lowerCAmelCase				=      answer["end_token"]

    _lowerCAmelCase				=      []
    for i in range(len(doc["token"]    )    ):
        if not doc["is_html"][i]:
            context.append(doc["token"][i]    )
        else:
            if answer["start_token"] > i:
                start_token -= 1
            if answer["end_token"] > i:
                end_token -= 1
    _lowerCAmelCase				=      " ".join(context[start_token:end_token]    )

    # checking above code
    if assertion:
        _lowerCAmelCase				=      doc["is_html"][answer["start_token"] : answer["end_token"]]
        _lowerCAmelCase				=      doc["token"][answer["start_token"] : answer["end_token"]]
        _lowerCAmelCase				=      " ".join([old[i] for i in range(len(SCREAMING_SNAKE_CASE_    )    ) if not is_html[i]]    )
        if new != old:
            print("ID:"		,       example["id"]    )
            print("New:"		,       SCREAMING_SNAKE_CASE_		,       end="\n"    )
            print("Old:"		,       SCREAMING_SNAKE_CASE_		,       end="\n\n"    )

    return {
        "context": " ".join(SCREAMING_SNAKE_CASE_    ),
        "answer": {
            "start_token": start_token,
            "end_token": end_token - 1,  # this makes it inclusive
            "category": answer["category"],  # either long or short
            "span": new,  # extra
        },
    }


def 	__a(SCREAMING_SNAKE_CASE_  :					Dict		,       SCREAMING_SNAKE_CASE_  :					List[Any]		,       SCREAMING_SNAKE_CASE_  :					Dict=2048		,       SCREAMING_SNAKE_CASE_  :					Union[str, Any]=4096		,       SCREAMING_SNAKE_CASE_  :					Dict=True    ):



    '''simple docstring'''





    _lowerCAmelCase				=      get_context_and_ans(SCREAMING_SNAKE_CASE_		,       assertion=SCREAMING_SNAKE_CASE_    )
    _lowerCAmelCase				=      out["answer"]

    # later, removing these samples
    if answer["start_token"] == -1:
        return {
            "example_id": example["id"],
            "input_ids": [[-1]],
            "labels": {
                "start_token": [-1],
                "end_token": [-1],
                "category": ["null"],
            },
        }

    _lowerCAmelCase				=      tokenizer(example["question"]["text"]		,       out["context"]    ).input_ids
    _lowerCAmelCase				=      input_ids.index(tokenizer.sep_token_id    ) + 1

    # return yes/no
    if answer["category"][0] in ["yes", "no"]:  # category is list with one element
        _lowerCAmelCase				=      []
        _lowerCAmelCase				=      []
        _lowerCAmelCase				=      input_ids[:q_len]
        _lowerCAmelCase				=      range(SCREAMING_SNAKE_CASE_		,       len(SCREAMING_SNAKE_CASE_    )		,       max_length - doc_stride    )
        for i in doc_start_indices:
            _lowerCAmelCase				=      i + max_length - q_len
            _lowerCAmelCase				=      input_ids[i:end_index]
            inputs.append(q_indices + slice    )
            category.append(answer["category"][0]    )
            if slice[-1] == tokenizer.sep_token_id:
                break

        return {
            "example_id": example["id"],
            "input_ids": inputs,
            "labels": {
                "start_token": [-100] * len(SCREAMING_SNAKE_CASE_    ),
                "end_token": [-100] * len(SCREAMING_SNAKE_CASE_    ),
                "category": category,
            },
        }

    _lowerCAmelCase				=      out["context"].split()
    _lowerCAmelCase				=      splitted_context[answer["end_token"]]
    _lowerCAmelCase				=      len(
        tokenizer(
            " ".join(splitted_context[: answer["start_token"]]    )		,       add_special_tokens=SCREAMING_SNAKE_CASE_		,       ).input_ids    )
    _lowerCAmelCase				=      len(
        tokenizer(" ".join(splitted_context[: answer["end_token"]]    )		,       add_special_tokens=SCREAMING_SNAKE_CASE_    ).input_ids    )

    answer["start_token"] += q_len
    answer["end_token"] += q_len

    # fixing end token
    _lowerCAmelCase				=      len(tokenizer(SCREAMING_SNAKE_CASE_		,       add_special_tokens=SCREAMING_SNAKE_CASE_    ).input_ids    )
    if num_sub_tokens > 1:
        answer["end_token"] += num_sub_tokens - 1

    _lowerCAmelCase				=      input_ids[answer["start_token"] : answer["end_token"] + 1]  # right & left are inclusive
    _lowerCAmelCase				=      answer["start_token"]
    _lowerCAmelCase				=      answer["end_token"]

    if assertion:
        _lowerCAmelCase				=      tokenizer.decode(SCREAMING_SNAKE_CASE_    )
        if answer["span"] != new:
            print("ISSUE IN TOKENIZATION"    )
            print("OLD:"		,       answer["span"]    )
            print("NEW:"		,       SCREAMING_SNAKE_CASE_		,       end="\n\n"    )

    if len(SCREAMING_SNAKE_CASE_    ) <= max_length:
        return {
            "example_id": example["id"],
            "input_ids": [input_ids],
            "labels": {
                "start_token": [answer["start_token"]],
                "end_token": [answer["end_token"]],
                "category": answer["category"],
            },
        }

    _lowerCAmelCase				=      input_ids[:q_len]
    _lowerCAmelCase				=      range(SCREAMING_SNAKE_CASE_		,       len(SCREAMING_SNAKE_CASE_    )		,       max_length - doc_stride    )

    _lowerCAmelCase				=      []
    _lowerCAmelCase				=      []
    _lowerCAmelCase				=      []
    _lowerCAmelCase				=      []  # null, yes, no, long, short
    for i in doc_start_indices:
        _lowerCAmelCase				=      i + max_length - q_len
        _lowerCAmelCase				=      input_ids[i:end_index]
        inputs.append(q_indices + slice    )
        assert len(inputs[-1]    ) <= max_length, "Issue in truncating length"

        if start_token >= i and end_token <= end_index - 1:
            _lowerCAmelCase				=      start_token - i + q_len
            _lowerCAmelCase				=      end_token - i + q_len
            answers_category.append(answer["category"][0]    )  # ["short"] -> "short"
        else:
            _lowerCAmelCase				=      -100
            _lowerCAmelCase				=      -100
            answers_category.append("null"    )
        _lowerCAmelCase				=      inputs[-1][start_token : end_token + 1]

        answers_start_token.append(SCREAMING_SNAKE_CASE_    )
        answers_end_token.append(SCREAMING_SNAKE_CASE_    )
        if assertion:
            if new != old and new != [tokenizer.cls_token_id]:
                print("ISSUE in strided for ID:"		,       example["id"]    )
                print("New:"		,       tokenizer.decode(SCREAMING_SNAKE_CASE_    )    )
                print("Old:"		,       tokenizer.decode(SCREAMING_SNAKE_CASE_    )		,       end="\n\n"    )
        if slice[-1] == tokenizer.sep_token_id:
            break

    return {
        "example_id": example["id"],
        "input_ids": inputs,
        "labels": {
            "start_token": answers_start_token,
            "end_token": answers_end_token,
            "category": answers_category,
        },
    }


def 	__a(SCREAMING_SNAKE_CASE_  :					Tuple		,       SCREAMING_SNAKE_CASE_  :					str		,       SCREAMING_SNAKE_CASE_  :					List[str]=2048		,       SCREAMING_SNAKE_CASE_  :					Dict=4096		,       SCREAMING_SNAKE_CASE_  :					Optional[int]=False    ):



    '''simple docstring'''





    _lowerCAmelCase				=      get_strided_contexts_and_ans(
        SCREAMING_SNAKE_CASE_		,       SCREAMING_SNAKE_CASE_		,       doc_stride=SCREAMING_SNAKE_CASE_		,       max_length=SCREAMING_SNAKE_CASE_		,       assertion=SCREAMING_SNAKE_CASE_		,       )

    return example


def 	__a(SCREAMING_SNAKE_CASE_  :					Optional[int]		,       SCREAMING_SNAKE_CASE_  :					List[Any]    ):



    '''simple docstring'''





    with jsonlines.open(SCREAMING_SNAKE_CASE_		,       "a"    ) as writer:
        for example in tqdm(SCREAMING_SNAKE_CASE_		,       total=len(SCREAMING_SNAKE_CASE_    )		,       desc="Saving samples ... "    ):
            _lowerCAmelCase				=      example["labels"]
            for ids, start, end, cat in zip(
                example["input_ids"]		,       labels["start_token"]		,       labels["end_token"]		,       labels["category"]		,       ):
                if start == -1 and end == -1:
                    continue  # leave waste samples with no answer
                if cat == "null" and np.random.rand() < 0.6:
                    continue  # removing 50 % samples
                writer.write(
                    {
                        "input_ids": ids,
                        "start_token": start,
                        "end_token": end,
                        "category": CATEGORY_MAPPING[cat],
                    }    )


if __name__ == "__main__":
       from datasets import load_dataset

       from transformers import BigBirdTokenizer

       _SCREAMING_SNAKE_CASE	 =							load_dataset("natural_questions")
       _SCREAMING_SNAKE_CASE	 =							BigBirdTokenizer.from_pretrained("google/bigbird-roberta-base")

       _SCREAMING_SNAKE_CASE	 =							data["train" if PROCESS_TRAIN == "true" else "validation"]

       _SCREAMING_SNAKE_CASE	 =							{
           "tokenizer": tokenizer,
           "doc_stride": DOC_STRIDE,
           "max_length": MAX_LENGTH,
           "assertion": False,
       }
       _SCREAMING_SNAKE_CASE	 =							data.map(prepare_inputs, fn_kwargs=fn_kwargs)
       _SCREAMING_SNAKE_CASE	 =							data.remove_columns(["annotations", "document", "id", "question"])
       print(data)

       np.random.seed(SEED)
       _SCREAMING_SNAKE_CASE	 =							"nq-training.jsonl" if PROCESS_TRAIN == "true" else "nq-validation.jsonl"
       save_to_disk(data, file_name=cache_file_name)









'''simple docstring'''


import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
       from transformers import TvltImageProcessor

if is_speech_available():
       from transformers import TvltFeatureExtractor

from transformers import TvltProcessor

@require_torch
class    lowerCAmelCase_		(		unittest.TestCase		):




 def 							_snake_case   (	self		)    ->							Dict:
     _lowerCAmelCase				=      "ZinengTang/tvlt-base"
     _lowerCAmelCase				=      tempfile.mkdtemp()




 def 							_snake_case   (	self			,     **_lowerCAmelCase		)    ->							Any:
     return TvltImageProcessor.from_pretrained(self.checkpoint			,     **_lowerCAmelCase		)




 def 							_snake_case   (	self			,     **_lowerCAmelCase		)    ->							str:
     return TvltFeatureExtractor.from_pretrained(self.checkpoint			,     **_lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							Optional[Any]:
     shutil.rmtree(self.tmpdirname		)




 def 							_snake_case   (	self		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)
     processor.save_pretrained(self.tmpdirname		)
     _lowerCAmelCase				=      TvltProcessor.from_pretrained(self.tmpdirname		)

     self.assertIsInstance(processor.feature_extractor			,     _lowerCAmelCase		)
     self.assertIsInstance(processor.image_processor			,     _lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							Any:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     _lowerCAmelCase				=      np.ones([12000]		)

     _lowerCAmelCase				=      feature_extractor(_lowerCAmelCase			,     return_tensors="np"		)
     _lowerCAmelCase				=      processor(audio=_lowerCAmelCase			,     return_tensors="np"		)

     for key in audio_dict.keys():
         self.assertAlmostEqual(audio_dict[key].sum()			,     input_processor[key].sum()			,     delta=1E-2		)




 def 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     _lowerCAmelCase				=      np.ones([3, 224, 224]		)

     _lowerCAmelCase				=      image_processor(_lowerCAmelCase			,     return_tensors="np"		)
     _lowerCAmelCase				=      processor(images=_lowerCAmelCase			,     return_tensors="np"		)

     for key in image_dict.keys():
         self.assertAlmostEqual(image_dict[key].sum()			,     input_processor[key].sum()			,     delta=1E-2		)




 def 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     _lowerCAmelCase				=      np.ones([12000]		)
     _lowerCAmelCase				=      np.ones([3, 224, 224]		)

     _lowerCAmelCase				=      processor(audio=_lowerCAmelCase			,     images=_lowerCAmelCase		)

     self.assertListEqual(list(inputs.keys()		)			,     ["audio_values", "audio_mask", "pixel_values", "pixel_mask"]		)

     # test if it raises when no input is passed
     with pytest.raises(_lowerCAmelCase		):
         processor()





 def 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     self.assertListEqual(
         processor.model_input_names			,     image_processor.model_input_names + feature_extractor.model_input_names			,     msg="`processor` and `image_processor`+`feature_extractor` model input names do not match"			,     )


