Documentation error in question-answering pipeline
Author: nishchay47b
Pull request: no
Labels: name: wontfix

Hi,
The [QuestionAnsweringPipeline](https://huggingface.co/transformers/main_classes/pipelines.html?highlight=pipelines#transformers.QuestionAnsweringPipeline.__call__) is returning the start and end position of the context string and not according to "the tokenized version of the input." as mentioned in the doc.

```python
from transformers import AutoTokenizer, AutoModelForQuestionAnswering, pipeline
tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
qa_pipeline = pipeline("question-answering")
text = r"""
🤗 Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides general-purpose
architectures (BERT, GPT-2, RoBERTa, XLM, DistilBert, XLNet…) for Natural Language Understanding (NLU) and Natural
Language Generation (NLG) with over 32+ pretrained models in 100+ languages and deep interoperability between
TensorFlow 2.0 and PyTorch.
"""

questions = [
    "How many pretrained models?"
]

result = qa_pipeline(question=questions, context=text)
print(result)

#this is the correct answer
print(text[int(result["start"]):int(result["end"])])

#this is not correct
print(tokenizer.tokenize(text[int(result["start"]):int(result["end"])))
```

Sorry if I misunderstood the doc

Comments:
You are correct! Do you want to open a PR fixing the docs?

Sure, I'll do that. Closing this issue for now, I'll refer to this in the PR.

There is one more thing, sorry I didn't realize this earlier, and please let me know if I am wrong, The [span_to_answer](https://huggingface.co/transformers/main_classes/pipelines.html#transformers.QuestionAnsweringPipeline.span_to_answer) method of this pipeline object doesn't add much value, because the object itself when called with `question` and `context` will return the start and end indexes of the answer in the context. Moreover, it will give the wrong results because `span_to_answer` is expecting token index and not string indexes.
In continuation to above code;
```python
print(len(tokenizer.tokenize(text)))
# output: 96

 print(qa_pipeline.span_to_answer(text=text,start=int(result[0]["start"]), end=int(result[0]["end"])))
# this will print {'answer': '', 'start': 0, 'end': 0} because the start index of the answer is 256 and end index is 264 while the 
# tokenized length is 96, this line in the function will stop the loop
# if token_idx > end:
#                break

```
This part in `__call__` [method](https://huggingface.co/transformers/main_classes/pipelines.html#transformers.QuestionAnsweringPipeline.__call__) is already taking care of remapping to string indexes:
```python
             # Convert the answer (tokens) back to the original text
                answers += [
                    {
                        "score": score.item(),
                        "start": np.where(char_to_word == feature.token_to_orig_map[s])[0][0].item(),
                        "end": np.where(char_to_word == feature.token_to_orig_map[e])[0][-1].item(),
                        "answer": " ".join(
                            example.doc_tokens[feature.token_to_orig_map[s] : feature.token_to_orig_map[e] + 1]
                        ),
                    }
```
Unless we have some method to get the token index, this method I think will not work.


This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.