diff --git a/Your-first-Transformers.ipynb b/Your-first-Transformers.ipynb index 7b585e4..17d2d6e 100644 --- a/Your-first-Transformers.ipynb +++ b/Your-first-Transformers.ipynb @@ -2,6 +2,7 @@ "cells": [ { "cell_type": "markdown", + "id": "befb1f02", "metadata": {}, "source": [ "## Transformers Kütüphanesine Giriş (Colab Önerilir) 🤗\n", @@ -40,6 +41,7 @@ }, { "cell_type": "markdown", + "id": "0e626fbf", "metadata": {}, "source": [ "__Colab'da çalışıyorsanız__, Colab ortamınıza uygun kütüphaneleri yüklemeniz gerekir (kurulum talimatlarını izlediyseniz, bunlar yerel olarak mevcut olacaktır)." @@ -47,9 +49,51 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 1, + "id": "f78385a2", + "metadata": { + "execution": { + "iopub.execute_input": "2026-04-02T15:44:13.425655Z", + "iopub.status.busy": "2026-04-02T15:44:13.425217Z", + "iopub.status.idle": "2026-04-02T15:44:16.409304Z", + "shell.execute_reply": "2026-04-02T15:44:16.406900Z", + "shell.execute_reply.started": "2026-04-02T15:44:13.425626Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: transformers in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (4.49.0)\n", + "Requirement already satisfied: torch in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (2.6.0+cpu.cxx11.abi)\n", + "Requirement already satisfied: pytesseract in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (0.3.13)\n", + "Requirement already satisfied: filelock in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from transformers) (3.17.0)\n", + "Requirement already satisfied: huggingface-hub<1.0,>=0.26.0 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from transformers) (0.29.1)\n", + "Requirement already satisfied: numpy>=1.17 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from transformers) (1.26.4)\n", + "Requirement already satisfied: packaging>=20.0 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from transformers) (24.2)\n", + "Requirement already satisfied: pyyaml>=5.1 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from transformers) (6.0.2)\n", + "Requirement already satisfied: regex!=2019.12.17 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from transformers) (2024.11.6)\n", + "Requirement already satisfied: requests in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from transformers) (2.32.3)\n", + "Requirement already satisfied: tokenizers<0.22,>=0.21 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from transformers) (0.21.0)\n", + "Requirement already satisfied: safetensors>=0.4.1 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from transformers) (0.5.2)\n", + "Requirement already satisfied: tqdm>=4.27 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from transformers) (4.67.1)\n", + "Requirement already satisfied: fsspec>=2023.5.0 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from huggingface-hub<1.0,>=0.26.0->transformers) (2025.2.0)\n", + "Requirement already satisfied: typing-extensions>=3.7.4.3 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from huggingface-hub<1.0,>=0.26.0->transformers) (4.12.2)\n", + "Requirement already satisfied: networkx in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from torch) (3.4.2)\n", + "Requirement already satisfied: jinja2 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from torch) (3.1.5)\n", + "Requirement already satisfied: setuptools in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from torch) (75.8.0)\n", + "Requirement already satisfied: sympy==1.13.1 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from torch) (1.13.1)\n", + "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from sympy==1.13.1->torch) (1.3.0)\n", + "Requirement already satisfied: Pillow>=8.0.0 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from pytesseract) (11.1.0)\n", + "Requirement already satisfied: MarkupSafe>=2.0 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from jinja2->torch) (3.0.2)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from requests->transformers) (3.4.1)\n", + "Requirement already satisfied: idna<4,>=2.5 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from requests->transformers) (2.10)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from requests->transformers) (2.3.0)\n", + "Requirement already satisfied: certifi>=2017.4.17 in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from requests->transformers) (2025.1.31)\n" + ] + } + ], "source": [ "# HuggingFace'den transformers kütüphanesini yükleyin\n", "!pip install transformers torch pytesseract" @@ -57,9 +101,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 2, + "id": "211835f0", + "metadata": { + "execution": { + "iopub.execute_input": "2026-04-02T15:44:18.161168Z", + "iopub.status.busy": "2026-04-02T15:44:18.160778Z", + "iopub.status.idle": "2026-04-02T15:44:19.238704Z", + "shell.execute_reply": "2026-04-02T15:44:19.236866Z", + "shell.execute_reply.started": "2026-04-02T15:44:18.161134Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: sentencepiece in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (0.2.0)\n", + "Requirement already satisfied: sacremoses in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (0.1.1)\n", + "Requirement already satisfied: regex in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from sacremoses) (2024.11.6)\n", + "Requirement already satisfied: click in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from sacremoses) (8.1.8)\n", + "Requirement already satisfied: joblib in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from sacremoses) (1.4.2)\n", + "Requirement already satisfied: tqdm in /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/lib/python3.12/site-packages (from sacremoses) (4.67.1)\n" + ] + } + ], "source": [ "# Ayrıca, bu modellerin bazılarının altında kullanılan bazı ekstra toollara da ihtiyacınız olacak.\n", "! pip install sentencepiece sacremoses" @@ -67,6 +133,7 @@ }, { "cell_type": "markdown", + "id": "746ecfd3", "metadata": {}, "source": [ "Bu not defterinde, Pipelines'ı kullanarak bazı çok güçlü modelleri indirip kolayca kullanacaksınız. Bu modellerin bazılarının oldukça büyük olduğunu unutmayın (500 MB'a kadar, bu nedenle makinenizde yeterli disk alanı olduğundan emin olun veya bu not defterini daha hızlı indirme hızlarına sahip bir Colab'da çalıştırın! \n", @@ -78,6 +145,7 @@ }, { "cell_type": "markdown", + "id": "33954eb4", "metadata": {}, "source": [ "### Basic Sentiment : 😀 / 😕 / 😠 / 😟" @@ -85,6 +153,7 @@ }, { "cell_type": "markdown", + "id": "f62e6a50", "metadata": {}, "source": [ "Bunu göz önünde bulundurarak, bir model belirtmeden duygu analizi için bir pipeline oluşturun ve \"Transformers are awesome!\" cümlesiyle bu modeli test etmeye çalışın. Diğer cümleleri de denemekten çekinmeyin." @@ -92,15 +161,134 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 3, + "id": "69fba3f3", + "metadata": { + "execution": { + "iopub.execute_input": "2026-04-02T15:45:42.821138Z", + "iopub.status.busy": "2026-04-02T15:45:42.820575Z", + "iopub.status.idle": "2026-04-02T15:48:11.310566Z", + "shell.execute_reply": "2026-04-02T15:48:11.309478Z", + "shell.execute_reply.started": "2026-04-02T15:45:42.821097Z" + } + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2026-04-02 18:45:47.158887: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.\n", + "2026-04-02 18:45:47.185561: I external/local_tsl/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.\n", + "2026-04-02 18:45:47.362406: I external/local_tsl/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.\n", + "2026-04-02 18:45:47.578176: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:479] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n", + "2026-04-02 18:45:47.771829: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:10575] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n", + "2026-04-02 18:45:47.773688: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1442] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n", + "2026-04-02 18:45:48.142098: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", + "To enable the following instructions: AVX2 AVX_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", + "2026-04-02 18:45:50.708229: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n", + "No model was supplied, defaulted to distilbert/distilbert-base-uncased-finetuned-sst-2-english and revision 714eb0f (https://huggingface.co/distilbert/distilbert-base-uncased-finetuned-sst-2-english).\n", + "Using a pipeline without specifying a model name and revision in production is not recommended.\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "df1abb9d7ee64e84bb893f8b0a874012", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "config.json: 0%| | 0.00/629 [00:00 Sonuç: [{'label': 'POSITIVE', 'score': 0.9998854398727417}]\n", + "Cümle: The weather is a bit gloomy today. -> Sonuç: [{'label': 'NEGATIVE', 'score': 0.9906337261199951}]\n", + "Cümle: I'm not sure how I feel about this new update. -> Sonuç: [{'label': 'NEGATIVE', 'score': 0.9991415739059448}]\n" + ] + } + ], "source": [ - "pass # SENİN KODUN BURAYA" + "from transformers import pipeline\n", + "\n", + "# Herhangi bir model belirtmeden duygu analizi pipeline'ı oluşturuyoruz\n", + "basic_classifier = pipeline(\"sentiment-analysis\")\n", + "\n", + "# \"Transformers are awesome!\" cümlesini test edelim\n", + "result = basic_classifier(\"Transformers are awesome!\")\n", + "print(f\"Test 1: {result}\")\n", + "\n", + "# Birkaç farklı duygu içeren cümle daha deneyelim\n", + "sentences = [\n", + " \"I am having a wonderful day!\",\n", + " \"The weather is a bit gloomy today.\",\n", + " \"I'm not sure how I feel about this new update.\"\n", + "]\n", + "\n", + "for sentence in sentences:\n", + " res = basic_classifier(sentence)\n", + " print(f\"Cümle: {sentence} -> Sonuç: {res}\")" ] }, { "cell_type": "markdown", + "id": "be58dd94", "metadata": {}, "source": [ "### Nüanslı Duygu 🤔" @@ -108,6 +296,7 @@ }, { "cell_type": "markdown", + "id": "397cbeda", "metadata": {}, "source": [ "HuggingFace, bir model belirtmezsek varsayılan olarak `distilbert-base-uncased-finetuned-sst-2-english` modelini kullanır. Bu model birçok temel kullanım durumunda iyi sonuç verir, ancak oldukça sınırlı bir metin külliyatı üzerinde eğitildiği için: \n", @@ -119,15 +308,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 4, + "id": "9c415d77", + "metadata": { + "execution": { + "iopub.execute_input": "2026-04-02T15:49:54.687502Z", + "iopub.status.busy": "2026-04-02T15:49:54.686578Z", + "iopub.status.idle": "2026-04-02T15:49:54.727985Z", + "shell.execute_reply": "2026-04-02T15:49:54.726523Z", + "shell.execute_reply.started": "2026-04-02T15:49:54.687438Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'label': 'NEGATIVE', 'score': 0.9997040629386902}]\n" + ] + } + ], "source": [ - "pass # SENİN KODUN BURAYA" + "# Mevcut classifier (DistilBERT) ile argo/modern dili test edelim\n", + "print(basic_classifier(\"These beats are sick!\"))" ] }, { "cell_type": "markdown", + "id": "ccf1261f", "metadata": {}, "source": [ "HuggingFace modellerinin listesine gidin ve Twitter duygularını uzmanlaşmış bir model bulup bulamayacağınızı kontrol edin (`“twitter-roberta-base-sentiment-latest”` aramak iyi bir başlangıç olabilir) - umarım bu yeni terimler ile biraz daha güncel olur! Şimdi ikinci bir boru hattı oluşturun, bu sefer kullanmak istediğimiz modeli __belirtin__ (`model=` kullanın) ve ince ayarlı bir model kullandığımız için performansımızın anında nasıl iyileştiğini görün." @@ -135,15 +343,141 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 5, + "id": "6f22aa93", + "metadata": { + "execution": { + "iopub.execute_input": "2026-04-02T15:50:53.945033Z", + "iopub.status.busy": "2026-04-02T15:50:53.943659Z", + "iopub.status.idle": "2026-04-02T15:54:36.083613Z", + "shell.execute_reply": "2026-04-02T15:54:36.082600Z", + "shell.execute_reply.started": "2026-04-02T15:50:53.944974Z" + } + }, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "83b46484dcb4402a90eac306d29e4e83", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "config.json: 0%| | 0.00/929 [00:00-\"` tiki dil arasında çeviri yapıp yapamayacağınızı görmek için (örneğin İngilizce'den İspanyolca'ya)." @@ -227,15 +702,53 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 9, + "id": "db90fc6e-23f2-4825-9438-c4b98d83a27e", + "metadata": { + "execution": { + "iopub.execute_input": "2026-04-02T16:08:48.418999Z", + "iopub.status.busy": "2026-04-02T16:08:48.418658Z", + "iopub.status.idle": "2026-04-02T16:08:50.024481Z", + "shell.execute_reply": "2026-04-02T16:08:50.022938Z", + "shell.execute_reply.started": "2026-04-02T16:08:48.418979Z" + } + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Device set to use cpu\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Orijinal: HuggingFace makes Natural Language Processing accessible for everyone.\n", + "İspanyolca Çeviri: HuggingFace hace que el procesamiento de lenguaje natural sea accesible para todos.\n" + ] + } + ], "source": [ - "pass # SENİN KODUN BURAYA" + "from transformers import pipeline\n", + "\n", + "# İngilizce'den (en) İspanyolca'ya (es) bir çeviri pipeline'ı oluşturuyoruz\n", + "translator = pipeline(\"translation\", model=\"Helsinki-NLP/opus-mt-en-es\")\n", + "\n", + "# Çevirmek istediğimiz cümle\n", + "text_to_translate = \"HuggingFace makes Natural Language Processing accessible for everyone.\"\n", + "\n", + "# Çeviriyi gerçekleştir\n", + "translation_result = translator(text_to_translate)\n", + "\n", + "print(f\"Orijinal: {text_to_translate}\")\n", + "print(f\"İspanyolca Çeviri: {translation_result[0]['translation_text']}\")" ] }, { "cell_type": "markdown", + "id": "5b6c98a8", "metadata": {}, "source": [ "### Özetleme" @@ -243,6 +756,7 @@ }, { "cell_type": "markdown", + "id": "0af5a436", "metadata": {}, "source": [ "Bir başka gerçekten yararlı NLP görevi, büyük miktarda bilgiyi çok az sayıda kelimeyle özetlemektir. BART, özetleme gibi görevlerde iyi performans gösteren bir modeldir; derslerde kısaca gördüğünüz iki modelin birleşiminden oluşur: BERT modeli ve otogresif stil GPT modeli. Bu model hakkında daha fazla bilgi için [bu bağlantıya](https://www.projectpro.io/article/transformers-bart-model-explained/553) göz atın. \n", @@ -254,26 +768,159 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 10, + "id": "2d76fcf7", + "metadata": { + "execution": { + "iopub.execute_input": "2026-04-02T16:08:50.026766Z", + "iopub.status.busy": "2026-04-02T16:08:50.026510Z", + "iopub.status.idle": "2026-04-02T16:14:30.739737Z", + "shell.execute_reply": "2026-04-02T16:14:30.738345Z", + "shell.execute_reply.started": "2026-04-02T16:08:50.026748Z" + } + }, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "e541bac85f0246eb9f6ae12f27125707", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "config.json: 0.00B [00:00, ?B/s]" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "6ac828c3e0834ecda694356d06709bce", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "pytorch_model.bin: 0%| | 0.00/611M [00:00