# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] id="4fc265b0"
# # NLP_TP Transformers 
#

# + [markdown] id="145be2de"
# ## 01- Sentiment analysis

# + colab={"base_uri": "https://localhost:8080/"} id="583ef981" outputId="9d9f6111-4c40-43c4-faa3-fae61c43bb0d"
# !pip install transformers 

# + colab={"base_uri": "https://localhost:8080/"} id="10e24977" outputId="24112ed9-83d9-41cd-a742-e1a0b7332d43"
from transformers import pipeline

nlp = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")

result = nlp("bien dit")[0]
print(result)
print(f"label: {result['label']}, with score: {round(result['score']*100, 2)}%")

result = nlp("mauvais travail")[0]
print(f"label: {result['label']}, with score: {round(result['score']*100, 2)}%")

# + [markdown] id="80f031f4"
# ## 02- Text generation

# + id="4668bfb2"
from transformers import pipeline

# + colab={"base_uri": "https://localhost:8080/"} id="63bf964e" outputId="7d975737-cdc1-4560-a6ad-55aa7224ba4a"
# Frensh
text_generator_fr = pipeline('text-generation', model='dbddv01/gpt2-french-small')

print(text_generator_fr("je lis un", max_length=50, do_sample=False))

# + colab={"base_uri": "https://localhost:8080/"} id="bc8556ac" outputId="67a9e4d0-e6d2-441b-9b41-bd0a0e530d82"
# Arabic
text_generator_Ar = pipeline('text-generation', model='akhooli/gpt2-small-arabic')
print(text_generator_Ar("في المغرب السياحة الجبلية ", max_length=50, do_sample=False))


# + [markdown] id="1af36c17"
# ## 03- Name entity recognition (NER)

# + colab={"base_uri": "https://localhost:8080/"} id="3a2364c9" outputId="4fef81d1-b475-412d-c977-8eb4595ce87d"

from transformers import AutoTokenizer, AutoModelForTokenClassification
from transformers import pipeline

ner_english_recognition = pipeline("ner", model="dslim/bert-base-NER", tokenizer="dslim/bert-base-NER")
ner_arabic_recognition = pipeline("ner", model="hatmimoha/arabic-ner", tokenizer="hatmimoha/arabic-ner")
ner_french_recognition = pipeline("ner", model="gilf/french-postag-model", tokenizer="gilf/french-postag-model")

print(ner_arabic_recognition("في المغرب السياحة الجبلية"))
 
print(ner_english_recognition("good time"))

print(ner_french_recognition("je lis un"))

# + [markdown] id="dff416e2"
# ## 04- Question answering

# + colab={"base_uri": "https://localhost:8080/"} id="511e712a" outputId="7c74ef42-77d4-493a-d166-98b633d117d3"
from transformers import pipeline
question_answering = pipeline("question-answering")
context = """
Le ville de Salé se trouve dans le plateau côtier large de 10 à 50 km, formé de plaines douces inclinées vers l’Océan Atlantique qui s'étend de Rabat-Salé à Skhirate-Témara, et du littoral atlantique au barrage Sidi Mohammed ben Abdellah9. L'altitude de la ville de Salé et du plateau côtier tout entier ne dépasse pas les 100 m10. Le fleuve Bouregreg qui sépare Rabat et Salé, donne une vallée plus ou moins large selon les endroits, pénétrant d’une quinzaine de kilomètres en amont de l’embouchure, surplombée par les plateaux de Bettana, Sala Al Jadida et de la commune rurale de Shoul du côté de Salé, et par ceux des quartiers de Hassan, El Youssoufia, Nahda et Akkrach du côté de Rabat. L'« arrière-pays » de Rabat-Salé est plutôt vert loin de l'urbanisation de masse, notamment grâce à la présence des forêts de la Mamora et de Témara, à proximité.
"""
question = "Quelle est Sala Al Jadida?"
result = question_answering(question=question, context=context)
print("Reponse:", result['answer'])




# + [markdown] id="c9a9c291"
# ## 05- Filling masked text

# + colab={"base_uri": "https://localhost:8080/"} id="3c913305" outputId="0b4dafe1-c4ab-4fee-fc0a-6bef5384d0b8"
from transformers import pipeline

nlp = pipeline("fill-mask")

from pprint import pprint
pprint(nlp(f"Les coronavirus sont des {nlp.tokenizer.mask_token} de la famille des Coronaviridae."))



# + colab={"base_uri": "https://localhost:8080/"} id="cc8ba494" outputId="77ed09b5-e13d-4096-80ac-99140e943bc8"
#Arabic
arabic_fill_mask = pipeline('fill-mask', model='CAMeL-Lab/bert-base-camelbert-ca')
pprint(arabic_fill_mask("جمعيات تدق ناقوس الخطر بشأن استنزاف الموارد[MASK]‬ بالجنوب الشرقي ."))

# + [markdown] id="6f56d9cc"
# ## 06- Summarization

# + colab={"base_uri": "https://localhost:8080/"} id="6186b099" outputId="4ad638b3-9a4c-429e-de28-565f368c9993"
from transformers import pipeline

summarizer = pipeline("summarization")

ARTICLE = """ 
Le Maroc était connu sous le nom de royaume de Marrakech, sous les trois dynasties qui avaient cette ville comme capitale. Puis, sous le nom de royaume de Fès, sous les dynasties qui résidaient à Fès. Au xixe siècle, les cartographes européens mentionnaient toujours un « royaume de Maroc », en indiquant l'ancienne capitale « Maroc » (pour Marrakech). Sous la dynastie des Alaouites, toujours au pouvoir, le pays est passé de l'appellation d'« Empire chérifien » à celle de « royaume du Maroc » en 195725, le sultan <NAME> ben Youssef en devenant le roi, en tant que <NAME>. Il peut être aussi surnommé « Royaume chérifien », en référence au souverain alaouite, descendant du prophète de l'islam Mahomet, qualifié de « chérif ».
"""
print(summarizer(ARTICLE, max_length=130, min_length=30, do_sample=False))

# + [markdown] id="d3e5558b"
# ## 07- Translation

# + colab={"base_uri": "https://localhost:8080/"} id="5269606b" outputId="8cea9d0f-b82d-49c1-a27f-371f6af5d79c"
from transformers import pipeline
# English to french
translator = pipeline("translation_en_to_fr")
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

pprint(translator("This allows people to understand complex terms or phrases.", max_length=40))


# + colab={"base_uri": "https://localhost:8080/"} id="a7451bc5" outputId="c4901140-8429-444c-873f-7fd97b12285d"
# english to Arabic

from transformers import MarianTokenizer, MarianMTModel

tokenizer = MarianTokenizer.from_pretrained("marefa-nlp/marefa-mt-en-ar")
model = MarianMTModel.from_pretrained("marefa-nlp/marefa-mt-en-ar")

text = "Mountain tourism in Morocco"

translated_tokens = model.generate(**tokenizer.prepare_seq2seq_batch(text, return_tensors="pt"))
Output_text = [tokenizer.decode(t, skip_special_tokens=True) for t in translated_tokens]

print(Output_text)

# + colab={"base_uri": "https://localhost:8080/", "height": 181, "referenced_widgets": ["<KEY>", "<KEY>", "8ffc79499f63482b82122e66a9811d7a", "14fff933a0b24375804d082133a4dd59", "0beead98f5b1460d976ed506a1123129", "<KEY>", "98267d9b90f441508572dac91e2755e8", "<KEY>", "<KEY>", "c561e407f6754b68b093e96e0a93486c", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "24338d0496b645a1a025f19037f18aa5", "09fabcb54c0e42568124f91f56a22288", "<KEY>", "2e36e37457d4439a91bf9ac538356ee5", "23ec9403ed1e4ca1848ffa302bffc0d9", "<KEY>", "<KEY>", "3fae572982f14e7094d947507f8de719", "<KEY>"]} id="e032ce8d" outputId="5076fd6e-f965-436f-c65d-58ace5647822"
# Arabic to English
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast

text_ar = "السياحة الجبلية في المغرب"

model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50-many-to-many-mmt")
tokenizer = MBart50TokenizerFast.from_pretrained("facebook/mbart-large-50-many-to-many-mmt")
tokenizer.src_lang = "ar_AR"
encoded_ar = tokenizer(text_ar, return_tensors="pt")
generated_tokens = model.generate(**encoded_ar, forced_bos_token_id=tokenizer.lang_code_to_id["en_XX"])
pprint(tokenizer.batch_decode(generated_tokens, skip_special_tokens=True))


# + [markdown] id="7531300a"
# ## 08- Feature extraction

# + colab={"base_uri": "https://localhost:8080/"} id="f397f772" outputId="65ce663d-7f02-4a76-cbd3-2deac6026905"
from sklearn.feature_extraction.text import CountVectorizer

# sentences.
sentences = [
    "This is a sample sentence",
    "I am interested in politics",
    "You are a very good software engineer, engineer.",]

vectorizer = CountVectorizer(stop_words='english')

vectorizer.fit(sentences)

vectorizer.get_feature_names()

StarcoderdataJupyterScriptsDedupFiltered