transformers safetensors mt5 text2text-generation text-generation question-answering turkish squad tr license:mit autotrain_compatible endpoints_compatible region:us
---
language:
- tr
license: mit
pipeline_tag: question-answering
tags:
- text-generation
- question-answering
- turkish
- squad
- mt5
---

# mT5-small based Turkish Question Answering System

[Google's Multilingual T5-small](https://huggingface.co/google/mt5-small) is fine-tuned on [Turkish SQuAD](https://github.com/boun-tabi/SQuAD-TR) for **Q&A** downstream task by using Pytorch Lightning.

The notebook that includes all fine tuning process will be shared on my Github page later [github](https://github.com/google-research/multilingual-t5). mT5 small model has 300 million parameters and model size is about 1.2GB. Therefore, it takes significant amount of time to fine tune it.

## Usage 🚀

```python
from transformers import T5TokenizerFast, AutoModelForSeq2SeqLM

tokenizer = T5TokenizerFast.from_pretrained('google/mt5-small')
model = AutoModelForSeq2SeqLM.from_pretrained("anilguven/mt5-small_squad_tr")

def get_answer(question,context):
    input_str = context + " " + question
    source_encoding=tokenizer(
    input_str,
    max_length=512,
    padding="max_length",
    truncation="only_second",
    return_attention_mask=True,
    add_special_tokens=True,
    return_tensors="pt")
    
    model.to("cpu")
    
    generated_ids=model.generate(
      input_ids=source_encoding["input_ids"],
      attention_mask=source_encoding["attention_mask"],
      num_beams=10, 
      num_return_sequences=1,
      max_length=128)

    preds=[tokenizer.decode(gen_id, skip_special_tokens=True, clean_up_tokenization_spaces=True) for gen_id in generated_ids]

    return preds #"".join(preds)
```

### Example 1
```python
question={
    "context":"Ingilizce adi 'Normans' Fransizca kelime Normans/Normanz, Normant çogul, modern Fransiz normand, hangi kendisi \
    Eski Düsük Frankonian Nortmann 'Northman' ödünç veya dogrudan Eski Norse Norðmaðr, Nortmannus, veya Nordmannus (Ortaçag Latince \
    kaydedildi, 9. yüzyil) 'Norseman, Viking' anlamina gelir.",
    "question":"Norman kelimesinin Latince versiyonu ilk ne zaman kaydedildi?"
    }
    
get_answer(question["question"],question["context"])
```
>  9. yüzyil


### Example 2
```python
question={
    "context":"Karar sorununa bir örnek asagidaki gibidir. Girdi keyfi bir grafiktir. Sorun, verilen grafigin bagli olup olmadigina \
     karar vermekten olusur. Bu karar problemi ile iliskili biçimsel dil daha sonra bagli tüm grafiklerin kümesidir - tabii ki, bu \
     dilin kesin bir tanimini elde etmek için, grafiklerin ikili dize olarak nasil kodlandigina karar vermelidir.",
    "question":"Karar probleminde kullanilan çiktiya bir örnek ne tür bir grafik?"
    }
    
get_answer(question["question"],question["context"])
```
>  No Answer