{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "gpuType": "T4" }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" }, "accelerator": "GPU", "widgets": { "application/vnd.jupyter.widget-state+json": { "4ec604360f5f489ebc058331b7836e20": { "model_module": "@jupyter-widgets/output", "model_name": "OutputModel", "model_module_version": "1.0.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/output", "_model_module_version": "1.0.0", "_model_name": "OutputModel", "_view_count": null, "_view_module": "@jupyter-widgets/output", "_view_module_version": "1.0.0", "_view_name": "OutputView", "layout": "IPY_MODEL_6e115a3de875472e8dcd1537fdc745bc", "msg_id": "", "outputs": [ { "output_type": "display_data", "data": { "text/plain": "\u001b[32m⠏\u001b[0m Waiting for authorization\n", "text/html": "
 Waiting for authorization\n
\n" }, "metadata": {} } ] } }, "6e115a3de875472e8dcd1537fdc745bc": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } } } } }, "cells": [ { "cell_type": "code", "source": [ "from nltk.corpus import stopwords\n", "from nltk.tokenize import word_tokenize\n", "import nltk\n", "nltk.download('punkt_tab')\n", "nltk.download('stopwords')\n", "def clean_text(text: str, lang: str = 'english') -> str:\n", " # lowercase\n", " text = text.lower()\n", "\n", " # tokenize\n", " tokens = word_tokenize(text)\n", "\n", " # get stopword list for language\n", " stop_words = set(stopwords.words(lang))\n", "\n", " # filter stopwords\n", " tokens = [t for t in tokens if t not in stop_words]\n", "\n", " # join back to string\n", " return \" \".join(tokens)\n" ], "metadata": { "id": "_mVEG6R3rSD8", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "9ac3c2ca-43f5-40be-fdbe-09aa7095db56" }, "execution_count": 1, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "[nltk_data] Downloading package punkt_tab to /root/nltk_data...\n", "[nltk_data] Package punkt_tab is already up-to-date!\n", "[nltk_data] Downloading package stopwords to /root/nltk_data...\n", "[nltk_data] Package stopwords is already up-to-date!\n" ] } ] }, { "cell_type": "code", "source": [ "import torch\n", "device='cuda' if torch.cuda.is_available() else 'cpu'\n", "device" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "B3e_bL5XoO6k", "outputId": "ce96b0ad-34e9-40d8-fd00-7ff3d6147584" }, "execution_count": 2, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'cuda'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 2 } ] }, { "cell_type": "code", "source": [ "import pandas as pd\n", "\n", "splits = {'train': 'data/train-00000-of-00001.parquet', 'validation': 'data/validation-00000-of-00001.parquet', 'test': 'data/test-00000-of-00001.parquet'}\n", "df = pd.read_parquet(\"hf://datasets/cfilt/iitb-english-hindi/\" + splits[\"train\"])" ], "metadata": { "id": "zQ5vddsq6wSc", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "c4f9f023-5b33-4e16-86f1-156ccf598670" }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "/usr/local/lib/python3.12/dist-packages/huggingface_hub/utils/_auth.py:94: UserWarning: \n", "The secret `HF_TOKEN` does not exist in your Colab secrets.\n", "To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n", "You will be able to reuse this secret in all of your notebooks.\n", "Please note that authentication is recommended but still optional to access public models or datasets.\n", " warnings.warn(\n" ] } ] }, { "cell_type": "code", "source": [ "len(df)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Vm5bkx9aSdYp", "outputId": "8c5b1eb4-736d-46d5-d0ab-321d1d6e95af" }, "execution_count": 4, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1659083" ] }, "metadata": {}, "execution_count": 4 } ] }, { "cell_type": "code", "source": [ "# df=df[:1000]" ], "metadata": { "id": "1nZqbdb7CYIj" }, "execution_count": 5, "outputs": [] }, { "cell_type": "code", "source": [ "df" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 423 }, "id": "SG0S31uYAPdm", "outputId": "7c207893-bbae-4c6b-b4b8-1130699b648e" }, "execution_count": 6, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " translation\n", "0 {'en': 'Give your application an accessibility...\n", "1 {'en': 'Accerciser Accessibility Explorer', 'h...\n", "2 {'en': 'The default plugin layout for the bott...\n", "3 {'en': 'The default plugin layout for the top ...\n", "4 {'en': 'A list of plugins that are disabled by...\n", "... ...\n", "1659078 {'en': 'The Prime Minister, Shri Narendra Modi...\n", "1659079 {'en': 'In a tweet, the Prime Minister said, c...\n", "1659080 {'en': 'I also congratulate all those who took...\n", "1659081 {'en': 'The NDA family will work together for ...\n", "1659082 {'en': 'I assure all possible support from the...\n", "\n", "[1659083 rows x 1 columns]" ], "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
translation
0{'en': 'Give your application an accessibility...
1{'en': 'Accerciser Accessibility Explorer', 'h...
2{'en': 'The default plugin layout for the bott...
3{'en': 'The default plugin layout for the top ...
4{'en': 'A list of plugins that are disabled by...
......
1659078{'en': 'The Prime Minister, Shri Narendra Modi...
1659079{'en': 'In a tweet, the Prime Minister said, c...
1659080{'en': 'I also congratulate all those who took...
1659081{'en': 'The NDA family will work together for ...
1659082{'en': 'I assure all possible support from the...
\n", "

1659083 rows × 1 columns

\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", "
\n" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "dataframe", "variable_name": "df" } }, "metadata": {}, "execution_count": 6 } ] }, { "cell_type": "code", "source": [ "df['English']=df['translation'].apply(lambda x:x['en'])\n", "df['Hindi']=df['translation'].apply(lambda x:x['hi'])\n", "df.drop(columns=['translation'],inplace=True)\n", "df\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 597 }, "id": "DSTtOmChBUDB", "outputId": "b736d7a4-99e5-418e-b649-3550563c7d3e" }, "execution_count": 7, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " English \\\n", "0 Give your application an accessibility workout \n", "1 Accerciser Accessibility Explorer \n", "2 The default plugin layout for the bottom panel \n", "3 The default plugin layout for the top panel \n", "4 A list of plugins that are disabled by default \n", "... ... \n", "1659078 The Prime Minister, Shri Narendra Modi has con... \n", "1659079 In a tweet, the Prime Minister said, congratul... \n", "1659080 I also congratulate all those who took oath as... \n", "1659081 The NDA family will work together for the prog... \n", "1659082 I assure all possible support from the Centre ... \n", "\n", " Hindi \n", "0 अपने अनुप्रयोग को पहुंचनीयता व्यायाम का लाभ दें \n", "1 एक्सेर्साइसर पहुंचनीयता अन्वेषक \n", "2 निचले पटल के लिए डिफोल्ट प्लग-इन खाका \n", "3 ऊपरी पटल के लिए डिफोल्ट प्लग-इन खाका \n", "4 उन प्लग-इनों की सूची जिन्हें डिफोल्ट रूप से नि... \n", "... ... \n", "1659078 प्रधानमंत्री श्री नरेन्द्र मोदी ने बिहार के मु... \n", "1659079 एक ट्वीट में प्रधानमंत्री ने कहा, बिहार के मुख... \n", "1659080 मैं उन सभी को भी बधाई देता हूं, जिन्होंने बिहा... \n", "1659081 एनडीए परिवार बिहार की प्रगति के लिए साथ मिलकर ... \n", "1659082 बिहार के कल्याण के लिए केंद्र की ओर से हरसंभव ... \n", "\n", "[1659083 rows x 2 columns]" ], "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
EnglishHindi
0Give your application an accessibility workoutअपने अनुप्रयोग को पहुंचनीयता व्यायाम का लाभ दें
1Accerciser Accessibility Explorerएक्सेर्साइसर पहुंचनीयता अन्वेषक
2The default plugin layout for the bottom panelनिचले पटल के लिए डिफोल्ट प्लग-इन खाका
3The default plugin layout for the top panelऊपरी पटल के लिए डिफोल्ट प्लग-इन खाका
4A list of plugins that are disabled by defaultउन प्लग-इनों की सूची जिन्हें डिफोल्ट रूप से नि...
.........
1659078The Prime Minister, Shri Narendra Modi has con...प्रधानमंत्री श्री नरेन्द्र मोदी ने बिहार के मु...
1659079In a tweet, the Prime Minister said, congratul...एक ट्वीट में प्रधानमंत्री ने कहा, बिहार के मुख...
1659080I also congratulate all those who took oath as...मैं उन सभी को भी बधाई देता हूं, जिन्होंने बिहा...
1659081The NDA family will work together for the prog...एनडीए परिवार बिहार की प्रगति के लिए साथ मिलकर ...
1659082I assure all possible support from the Centre ...बिहार के कल्याण के लिए केंद्र की ओर से हरसंभव ...
\n", "

1659083 rows × 2 columns

\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", "
\n" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "dataframe", "variable_name": "df" } }, "metadata": {}, "execution_count": 7 } ] }, { "cell_type": "code", "source": [ "import os\n", "if not os.path.exists(\"data.csv\"):\n", " df['English']=df['English'].apply(lambda x:clean_text(x))\n", " df['Hindi']=df['Hindi'].apply(lambda x:clean_text(x))\n", " df\n", "else:\n", " df=pd.read_csv(\"data.csv\")" ], "metadata": { "id": "ILHw4cf4BknZ" }, "execution_count": 8, "outputs": [] }, { "cell_type": "code", "source": [ "df.to_csv('data.csv',index=False)" ], "metadata": { "id": "Ptb--m0YVgH8" }, "execution_count": 9, "outputs": [] }, { "cell_type": "code", "source": [ "from typing import List\n", "from collections import Counter\n", "def build_vocabs(sentences:List[str]):\n", " vocab=Counter(' '.join(sentences).split())\n", " vocab={k:i+3 for i,(k,v) in enumerate(vocab.items())}\n", " vocab['']=0\n", " vocab['']=1\n", " vocab['']=2\n", " return vocab" ], "metadata": { "id": "ZwkZ_kEtByrJ" }, "execution_count": 10, "outputs": [] }, { "cell_type": "code", "source": [ "en_vocab=build_vocabs(df['English'].astype(str).fillna(''))\n", "hi_vocab=build_vocabs(df['Hindi'].astype(str).fillna(''))" ], "metadata": { "id": "z9bqTV9dDgxP" }, "execution_count": 11, "outputs": [] }, { "cell_type": "code", "source": [ "def sent_tokens(sentence:str,vocab):\n", " tokens=[vocab['']]\n", " tokens+=[vocab[w] for w in sentence.split()]\n", " tokens+=[vocab['']]\n", " return tokens" ], "metadata": { "id": "f5F5pzldDjTI" }, "execution_count": 12, "outputs": [] }, { "cell_type": "code", "source": [ "sent_tokens(\"give application accessibility workout\",en_vocab)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "0CG_cFyVEA5O", "outputId": "3084db32-988e-485a-a5e3-f649b4648ed1" }, "execution_count": 13, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[1, 3, 4, 5, 6, 2]" ] }, "metadata": {}, "execution_count": 13 } ] }, { "cell_type": "code", "source": [ "import torch\n", "from torch.utils.data import Dataset,DataLoader\n", "import torch.nn.utils.rnn as rnn_utils\n" ], "metadata": { "id": "Qi4AHOr8EF3L" }, "execution_count": 14, "outputs": [] }, { "cell_type": "code", "source": [ "import numpy as np\n", "\n", "def create_memmap(df, en_vocab, hi_vocab):\n", "\n", " n = len(df)\n", "\n", " max_en = max(len(sent_tokens(s, en_vocab)) for s in df['English'])\n", " max_hi = max(len(sent_tokens(s, hi_vocab)) for s in df['Hindi'])\n", "\n", " en_mem = np.memmap(\"en.dat\", dtype='int32', mode='w+', shape=(n, max_en))\n", " hi_mem = np.memmap(\"hi.dat\", dtype='int32', mode='w+', shape=(n, max_hi))\n", "\n", " en_mem[:] = en_vocab['']\n", " hi_mem[:] = hi_vocab['']\n", "\n", " for i in range(n):\n", "\n", " en_tokens = sent_tokens(df['English'].iloc[i], en_vocab)\n", " hi_tokens = sent_tokens(df['Hindi'].iloc[i], hi_vocab)\n", "\n", " en_mem[i, :len(en_tokens)] = en_tokens\n", " hi_mem[i, :len(hi_tokens)] = hi_tokens\n", "\n", " en_mem.flush()\n", " hi_mem.flush()\n", "\n", " return (n, max_en), (n, max_hi)" ], "metadata": { "id": "CoUCPWIiYdeF" }, "execution_count": 15, "outputs": [] }, { "cell_type": "code", "source": [ "if not os.path.exists(\"en.dat\") or not os.path.exists(\"hi.dat\"):\n", " print(\"Creating memmaps...\")\n", " cleaned_df = df.copy()\n", " cleaned_df['English'] = cleaned_df['English'].astype(str).fillna('')\n", " cleaned_df['Hindi'] = cleaned_df['Hindi'].astype(str).fillna('')\n", "\n", " en_shape, hi_shape = create_memmap(cleaned_df, en_vocab, hi_vocab)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "y3JRKeA1bCtJ", "outputId": "60d88897-6c30-469a-b5a4-98dc6c93ad44" }, "execution_count": 16, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Creating memmaps...\n" ] } ] }, { "cell_type": "code", "source": [ "import torch\n", "from torch.utils.data import Dataset\n", "\n", "class Sent_data_loader(Dataset):\n", " def __init__(self, en_shape, hi_shape):\n", " self.en = np.memmap(\"en.dat\", dtype=\"int32\", mode=\"r\", shape=en_shape)\n", " self.hi = np.memmap(\"hi.dat\", dtype=\"int32\", mode=\"r\", shape=hi_shape)\n", "\n", " def __len__(self):\n", " return self.en.shape[0]\n", "\n", " def __getitem__(self, idx):\n", " return torch.from_numpy(self.en[idx]).long(), \\\n", " torch.from_numpy(self.hi[idx]).long()" ], "metadata": { "id": "VrYB70LDEvVZ" }, "execution_count": 17, "outputs": [] }, { "cell_type": "code", "source": [ "test=Sent_data_loader(en_shape,hi_shape)\n", "\n", "test[0]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "gFdi379YFYSY", "outputId": "21418d5f-9b6d-42c3-9d3b-a74e357afef0" }, "execution_count": 18, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "/tmp/ipython-input-253654522.py:13: UserWarning: The given NumPy array is not writable, and PyTorch does not support non-writable tensors. This means writing to this tensor will result in undefined behavior. You may want to copy the array to protect its data or make it writable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at /pytorch/torch/csrc/utils/tensor_numpy.cpp:213.)\n", " return torch.from_numpy(self.en[idx]).long(), \\\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "(tensor([1, 3, 4, 5, 6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),\n", " tensor([ 1, 3, 4, 5, 6, 7, 8, 9, 10, 2, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))" ] }, "metadata": {}, "execution_count": 18 } ] }, { "cell_type": "code", "source": [ "dataset=Sent_data_loader(en_shape,hi_shape)\n", "data_loader=DataLoader(dataset=dataset,batch_size=8,shuffle=False)" ], "metadata": { "id": "LOLHv4D5FckB" }, "execution_count": 19, "outputs": [] }, { "cell_type": "code", "source": [ "for _ in data_loader:\n", " print(_)\n", " break" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "1AKU4yzFFqWD", "outputId": "156bae1d-a9e5-4383-e743-cfae71c968d3" }, "execution_count": 20, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[tensor([[ 1, 3, 4, 5, 6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0],\n", " [ 1, 7, 5, 8, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0],\n", " [ 1, 9, 10, 11, 12, 13, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0],\n", " [ 1, 9, 10, 11, 14, 13, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0],\n", " [ 1, 15, 16, 17, 9, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0],\n", " [ 1, 18, 19, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0],\n", " [ 1, 19, 18, 20, 21, 22, 23, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0],\n", " [ 1, 18, 24, 25, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0]]), tensor([[ 1, 3, 4, 5, 6, 7, 8, 9, 10, 2, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [ 1, 11, 6, 12, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [ 1, 13, 14, 15, 16, 17, 18, 19, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [ 1, 20, 14, 15, 16, 17, 18, 19, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [ 1, 21, 22, 23, 24, 25, 17, 26, 27, 28, 29, 30, 31, 2, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [ 1, 32, 5, 33, 34, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [ 1, 35, 36, 37, 38, 39, 5, 40, 41, 33, 42, 23, 32, 2, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [ 1, 43, 37, 44, 39, 15, 45, 5, 33, 46, 2, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])]\n" ] } ] }, { "cell_type": "code", "source": [ "import torch.nn as nn" ], "metadata": { "id": "GGn2EizzFrL6" }, "execution_count": 21, "outputs": [] }, { "cell_type": "code", "source": [ "class Encoder(nn.Module):\n", " def __init__(self,input_size,embed_size,hidden_size):\n", " super().__init__()\n", " self.embedding=nn.Embedding(input_size,embed_size)\n", " self.rnn=nn.GRU(embed_size,hidden_size,batch_first=True)\n", " def forward(self,x):\n", " embedding=self.embedding(x)\n", " outputs, hidden = self.rnn(embedding)\n", " return hidden\n", "\n", "\n", "class Decoder(nn.Module):\n", " def __init__(self,output_size,embed_size,hidden_size):\n", " super().__init__()\n", " self.embedding=nn.Embedding(output_size,embed_size)\n", " self.rnn=nn.GRU(embed_size,hidden_size,batch_first=True)\n", " self.ff=nn.Linear(hidden_size,output_size)\n", " def forward(self,x,hidden):\n", "\n", " embedded = self.embedding(x).unsqueeze(1)\n", " output, hidden = self.rnn(embedded, hidden)\n", "\n", " prediction = self.ff(output.squeeze(1))\n", " return prediction, hidden\n", "\n", "\n", "class seq2seq(nn.Module):\n", " def __init__(self,encoder,decoder):\n", " super().__init__()\n", " self.encoder=encoder\n", " self.decoder=decoder\n", "\n", " def forward(self, src, trg, teacher_forcing_ratio=0.5):\n", "\n", " batch_size = trg.shape[0]\n", " trg_len = trg.shape[1]\n", " trg_vocab_size = self.decoder.ff.out_features\n", " outputs = torch.zeros(batch_size, trg_len, trg_vocab_size).to(trg.device)\n", "\n", " encoder_hidden = self.encoder(src)\n", "\n", " input = trg[:, 0]\n", "\n", " for t in range(1, trg_len):\n", "\n", " output, encoder_hidden = self.decoder(input, encoder_hidden)\n", "\n", " outputs[:, t, :] = output\n", "\n", " teacher_force = torch.rand(1).item() < teacher_forcing_ratio\n", " top1 = output.argmax(1)\n", "\n", " input = trg[:, t] if teacher_force else top1\n", "\n", "\n", " return outputs[:, 1:, :]" ], "metadata": { "id": "INHPXnSxH1eQ" }, "execution_count": 22, "outputs": [] }, { "cell_type": "code", "source": [ "input_size_en=len(en_vocab)\n", "output_size_hi=len(hi_vocab)" ], "metadata": { "id": "YcVolo9-Jwp4" }, "execution_count": 23, "outputs": [] }, { "cell_type": "code", "source": [ "encoder = Encoder(input_size=input_size_en, embed_size=256, hidden_size=512)\n", "decoder = Decoder(output_size=output_size_hi, embed_size=256, hidden_size=512)\n", "\n", "model = seq2seq(encoder, decoder)\n", "\n", "# Move model to device BEFORE initializing the optimizer\n", "model.to(device)\n", "\n", "criterion = nn.CrossEntropyLoss(ignore_index=en_vocab[\"\"])\n", "optimizer = torch.optim.Adam(model.parameters())" ], "metadata": { "id": "iDPxAfCzJljp" }, "execution_count": 24, "outputs": [] }, { "cell_type": "code", "source": [ "device='cuda' if torch.cuda.is_available() else 'cpu'\n", "device" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "3FP8Nt_FShev", "outputId": "5166e4d5-b938-4199-a211-dcab6db2b812" }, "execution_count": 25, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'cuda'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 25 } ] }, { "cell_type": "code", "source": [ "import tqdm\n", "for epoch in range(100):\n", " model.train()\n", " total_loss=0\n", " for i,batch in tqdm.tqdm(enumerate(data_loader)):\n", " src_tensor=batch[0] # Shape: (batch_size, src_seq_len)\n", " trg_tensor=batch[1] # Shape: (batch_size, trg_seq_len)\n", " src_tensor=src_tensor.to(device)\n", " trg_tensor=trg_tensor.to(device)\n", " optimizer.zero_grad()\n", "\n", " output = model(src_tensor, trg_tensor)\n", "\n", " trg_target_for_loss = trg_tensor[:, 1:]\n", "\n", " output_dim = output.shape[-1]\n", " reshaped_output = output.reshape(-1, output_dim)\n", "\n", " reshaped_trg_target = trg_target_for_loss.reshape(-1)\n", "\n", " loss = criterion(reshaped_output, reshaped_trg_target)\n", "\n", " loss.backward()\n", " optimizer.step()\n", "\n", " total_loss+=loss.item()\n", " avg_loss=total_loss/len(data_loader)\n", " print(f\"Epoch: {epoch+1},Loss: {avg_loss:.4f}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "jnAuhYi9J7KW", "outputId": "7f1bf773-172a-4047-af84-4f3d3d286eda" }, "execution_count": 26, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "125it [00:06, 20.48it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 1,Loss: 4.4307\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.16it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 2,Loss: 2.5995\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:08, 15.54it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 3,Loss: 0.9380\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.37it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 4,Loss: 0.3596\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 21.53it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 5,Loss: 0.2339\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 23.99it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 6,Loss: 0.1630\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.16it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 7,Loss: 0.1223\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 23.38it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 8,Loss: 0.1161\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.54it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 9,Loss: 0.0978\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 21.88it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 10,Loss: 0.1059\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.80it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 11,Loss: 0.0978\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.15it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 12,Loss: 0.1136\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.33it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 13,Loss: 0.1035\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:07, 17.70it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 14,Loss: 0.1158\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.77it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 15,Loss: 0.0896\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 21.75it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 16,Loss: 0.0952\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.23it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 17,Loss: 0.0856\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 23.11it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 18,Loss: 0.0808\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 23.02it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 19,Loss: 0.0753\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.10it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 20,Loss: 0.0834\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.08it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 21,Loss: 0.0855\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.01it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 22,Loss: 0.0836\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 21.36it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 23,Loss: 0.0811\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.08it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 24,Loss: 0.0886\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.24it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 25,Loss: 0.0718\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.97it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 26,Loss: 0.0817\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 23.88it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 27,Loss: 0.0640\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 23.36it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 28,Loss: 0.0878\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.96it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 29,Loss: 0.0902\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.07it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 30,Loss: 0.0961\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.42it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 31,Loss: 0.0846\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.05it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 32,Loss: 0.0765\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.19it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 33,Loss: 0.0799\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.24it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 34,Loss: 0.0898\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.47it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 35,Loss: 0.0782\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.00it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 36,Loss: 0.0805\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.05it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 37,Loss: 0.0762\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.07it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 38,Loss: 0.0887\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:07, 17.25it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 39,Loss: 0.0835\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.43it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 40,Loss: 0.0757\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 21.74it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 41,Loss: 0.0873\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.27it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 42,Loss: 0.1228\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.49it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 43,Loss: 0.1669\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.96it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 44,Loss: 0.3121\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.28it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 45,Loss: 0.1480\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.57it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 46,Loss: 0.0841\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.15it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 47,Loss: 0.0814\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.15it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 48,Loss: 0.0813\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.81it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 49,Loss: 0.0694\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:06, 20.52it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 50,Loss: 0.0729\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.94it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 51,Loss: 0.0751\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.23it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 52,Loss: 0.0815\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.27it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 53,Loss: 0.0672\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.55it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 54,Loss: 0.0737\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.56it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 55,Loss: 0.0649\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.13it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 56,Loss: 0.0666\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.07it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 57,Loss: 0.0643\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.22it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 58,Loss: 0.0771\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.01it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 59,Loss: 0.0759\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.34it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 60,Loss: 0.0894\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.53it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 61,Loss: 0.0747\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 23.28it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 62,Loss: 0.0626\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.03it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 63,Loss: 0.0698\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 21.80it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 64,Loss: 0.0839\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.15it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 65,Loss: 0.0723\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.32it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 66,Loss: 0.0815\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.35it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 67,Loss: 0.0800\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.23it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 68,Loss: 0.0769\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.41it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 69,Loss: 0.0811\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.14it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 70,Loss: 0.0831\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.83it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 71,Loss: 0.0730\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.92it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 72,Loss: 0.0652\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 21.67it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 73,Loss: 0.0718\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.40it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 74,Loss: 0.0790\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.08it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 75,Loss: 0.0740\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.33it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 76,Loss: 0.0789\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.19it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 77,Loss: 0.0634\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.83it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 78,Loss: 0.0810\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.24it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 79,Loss: 0.0841\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.18it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 80,Loss: 0.0660\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.90it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 81,Loss: 0.0786\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.10it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 82,Loss: 0.0681\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.01it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 83,Loss: 0.0752\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 21.70it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 84,Loss: 0.0678\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.74it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 85,Loss: 0.0643\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.41it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 86,Loss: 0.0604\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.03it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 87,Loss: 0.0654\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.89it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 88,Loss: 0.0726\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 21.96it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 89,Loss: 0.0785\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.89it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 90,Loss: 0.0782\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 21.61it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 91,Loss: 0.0734\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.02it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 92,Loss: 0.0756\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 21.72it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 93,Loss: 0.0721\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.91it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 94,Loss: 0.0811\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.74it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 95,Loss: 0.0659\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 23.77it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 96,Loss: 0.0626\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 24.68it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 97,Loss: 0.0661\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 22.24it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 98,Loss: 0.0703\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:04, 25.09it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 99,Loss: 0.0644\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "125it [00:05, 21.45it/s]" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Epoch: 100,Loss: 0.0649\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "\n" ] } ] }, { "cell_type": "code", "source": [ "import torch\n", "import numpy as np\n", "\n", "def predict(sentence, model, en_vocab, hi_vocab, max_len=50, device='cpu'):\n", " model.eval()\n", "\n", " hi_idx_to_word = {v: k for k, v in hi_vocab.items()}\n", "\n", " tokenized_en_sent = sent_tokens(sentence, en_vocab)\n", " src_tensor = torch.tensor(tokenized_en_sent, dtype=torch.long).unsqueeze(0).to(device)\n", "\n", " with torch.no_grad():\n", " encoder_hidden = model.encoder(src_tensor)\n", "\n", " decoder_input = torch.tensor([hi_vocab['']], dtype=torch.long).to(device)\n", " predicted_hi_tokens = []\n", "\n", " for _ in range(max_len):\n", " output, encoder_hidden = model.decoder(decoder_input, encoder_hidden)\n", "\n", " predicted_token_id = output.argmax(1).item()\n", " predicted_hi_tokens.append(predicted_token_id)\n", "\n", " if predicted_token_id == hi_vocab['']:\n", " break\n", "\n", " decoder_input = torch.tensor([predicted_token_id], dtype=torch.long).to(device)\n", "\n", "\n", " if predicted_hi_tokens and predicted_hi_tokens[0] == hi_vocab['']:\n", " predicted_hi_tokens = predicted_hi_tokens[1:]\n", " if predicted_hi_tokens and predicted_hi_tokens[-1] == hi_vocab['']:\n", " predicted_hi_tokens = predicted_hi_tokens[:-1]\n", "\n", " predicted_sentence = ' '.join([hi_idx_to_word[token_id] for token_id in predicted_hi_tokens if token_id in hi_idx_to_word])\n", " return predicted_sentence" ], "metadata": { "id": "UCmWyhm7LMII" }, "execution_count": 27, "outputs": [] }, { "cell_type": "code", "source": [ "predict(\"give application accessibility workout\",model=model,en_vocab=en_vocab,hi_vocab=hi_vocab,device=device)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "7syDgMOfNjjY", "outputId": "f63380e1-2224-4b22-930e-64a1de187f8c" }, "execution_count": 28, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'अपने अनुप्रयोग को पहुंचनीयता व्यायाम का लाभ दें'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 28 } ] }, { "cell_type": "code", "source": [ "torch.save(model.state_dict(), 'model.pth')" ], "metadata": { "id": "Pqg-XIDQNtsq" }, "execution_count": 29, "outputs": [] }, { "cell_type": "code", "source": [ "from google.colab import files\n", "files.download(\"/content/model.pth\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 17 }, "id": "mCRWX7IVcvSD", "outputId": "4dde786a-0a50-48a6-e44c-5649c7858b16" }, "execution_count": 30, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "" ], "application/javascript": [ "\n", " async function download(id, filename, size) {\n", " if (!google.colab.kernel.accessAllowed) {\n", " return;\n", " }\n", " const div = document.createElement('div');\n", " const label = document.createElement('label');\n", " label.textContent = `Downloading \"${filename}\": `;\n", " div.appendChild(label);\n", " const progress = document.createElement('progress');\n", " progress.max = size;\n", " div.appendChild(progress);\n", " document.body.appendChild(div);\n", "\n", " const buffers = [];\n", " let downloaded = 0;\n", "\n", " const channel = await google.colab.kernel.comms.open(id);\n", " // Send a message to notify the kernel that we're ready.\n", " channel.send({})\n", "\n", " for await (const message of channel.messages) {\n", " // Send a message to notify the kernel that we're ready.\n", " channel.send({})\n", " if (message.buffers) {\n", " for (const buffer of message.buffers) {\n", " buffers.push(buffer);\n", " downloaded += buffer.byteLength;\n", " progress.value = downloaded;\n", " }\n", " }\n", " }\n", " const blob = new Blob(buffers, {type: 'application/binary'});\n", " const a = document.createElement('a');\n", " a.href = window.URL.createObjectURL(blob);\n", " a.download = filename;\n", " div.appendChild(a);\n", " a.click();\n", " div.remove();\n", " }\n", " " ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "" ], "application/javascript": [ "download(\"download_1d4c196e-9f44-4a89-b085-3e8aff06b51d\", \"model.pth\", 11032573)" ] }, "metadata": {} } ] }, { "cell_type": "code", "source": [ "from google.colab import files\n", "files.download(\"/content/en.dat\")\n", "\n", "from google.colab import files\n", "files.download(\"/content/hi.dat\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 17 }, "id": "qjaRST7lssQd", "outputId": "464c6679-0037-41fc-88cb-b2dceb3f7bd8" }, "execution_count": 31, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "" ], "application/javascript": [ "\n", " async function download(id, filename, size) {\n", " if (!google.colab.kernel.accessAllowed) {\n", " return;\n", " }\n", " const div = document.createElement('div');\n", " const label = document.createElement('label');\n", " label.textContent = `Downloading \"${filename}\": `;\n", " div.appendChild(label);\n", " const progress = document.createElement('progress');\n", " progress.max = size;\n", " div.appendChild(progress);\n", " document.body.appendChild(div);\n", "\n", " const buffers = [];\n", " let downloaded = 0;\n", "\n", " const channel = await google.colab.kernel.comms.open(id);\n", " // Send a message to notify the kernel that we're ready.\n", " channel.send({})\n", "\n", " for await (const message of channel.messages) {\n", " // Send a message to notify the kernel that we're ready.\n", " channel.send({})\n", " if (message.buffers) {\n", " for (const buffer of message.buffers) {\n", " buffers.push(buffer);\n", " downloaded += buffer.byteLength;\n", " progress.value = downloaded;\n", " }\n", " }\n", " }\n", " const blob = new Blob(buffers, {type: 'application/binary'});\n", " const a = document.createElement('a');\n", " a.href = window.URL.createObjectURL(blob);\n", " a.download = filename;\n", " div.appendChild(a);\n", " a.click();\n", " div.remove();\n", " }\n", " " ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "" ], "application/javascript": [ "download(\"download_f21668fb-dcff-4df6-81a9-c6abd670a5bd\", \"en.dat\", 80000)" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "" ], "application/javascript": [ "\n", " async function download(id, filename, size) {\n", " if (!google.colab.kernel.accessAllowed) {\n", " return;\n", " }\n", " const div = document.createElement('div');\n", " const label = document.createElement('label');\n", " label.textContent = `Downloading \"${filename}\": `;\n", " div.appendChild(label);\n", " const progress = document.createElement('progress');\n", " progress.max = size;\n", " div.appendChild(progress);\n", " document.body.appendChild(div);\n", "\n", " const buffers = [];\n", " let downloaded = 0;\n", "\n", " const channel = await google.colab.kernel.comms.open(id);\n", " // Send a message to notify the kernel that we're ready.\n", " channel.send({})\n", "\n", " for await (const message of channel.messages) {\n", " // Send a message to notify the kernel that we're ready.\n", " channel.send({})\n", " if (message.buffers) {\n", " for (const buffer of message.buffers) {\n", " buffers.push(buffer);\n", " downloaded += buffer.byteLength;\n", " progress.value = downloaded;\n", " }\n", " }\n", " }\n", " const blob = new Blob(buffers, {type: 'application/binary'});\n", " const a = document.createElement('a');\n", " a.href = window.URL.createObjectURL(blob);\n", " a.download = filename;\n", " div.appendChild(a);\n", " a.click();\n", " div.remove();\n", " }\n", " " ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "" ], "application/javascript": [ "download(\"download_486a30cd-2bb5-4059-ac64-638a285ca1dc\", \"hi.dat\", 128000)" ] }, "metadata": {} } ] }, { "cell_type": "code", "source": [ "torch.save(en_vocab,\"en_vocab.pth\")\n", "torch.save(hi_vocab,\"hi_vocab.pth\")" ], "metadata": { "id": "RkYFCZlm2eqV" }, "execution_count": 33, "outputs": [] }, { "cell_type": "code", "source": [ "from google.colab import files\n", "files.download(\"/content/en_vocab.pth\")\n", "files.download(\"/content/hi_vocab.pth\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 17 }, "id": "veMvyPte4pMW", "outputId": "6c73ba89-b154-4227-db70-12fd33adce54" }, "execution_count": 34, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "" ], "application/javascript": [ "\n", " async function download(id, filename, size) {\n", " if (!google.colab.kernel.accessAllowed) {\n", " return;\n", " }\n", " const div = document.createElement('div');\n", " const label = document.createElement('label');\n", " label.textContent = `Downloading \"${filename}\": `;\n", " div.appendChild(label);\n", " const progress = document.createElement('progress');\n", " progress.max = size;\n", " div.appendChild(progress);\n", " document.body.appendChild(div);\n", "\n", " const buffers = [];\n", " let downloaded = 0;\n", "\n", " const channel = await google.colab.kernel.comms.open(id);\n", " // Send a message to notify the kernel that we're ready.\n", " channel.send({})\n", "\n", " for await (const message of channel.messages) {\n", " // Send a message to notify the kernel that we're ready.\n", " channel.send({})\n", " if (message.buffers) {\n", " for (const buffer of message.buffers) {\n", " buffers.push(buffer);\n", " downloaded += buffer.byteLength;\n", " progress.value = downloaded;\n", " }\n", " }\n", " }\n", " const blob = new Blob(buffers, {type: 'application/binary'});\n", " const a = document.createElement('a');\n", " a.href = window.URL.createObjectURL(blob);\n", " a.download = filename;\n", " div.appendChild(a);\n", " a.click();\n", " div.remove();\n", " }\n", " " ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "" ], "application/javascript": [ "download(\"download_075ea84e-2ebe-499e-bcc0-db5c8747149e\", \"en_vocab.pth\", 5683)" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "" ], "application/javascript": [ "\n", " async function download(id, filename, size) {\n", " if (!google.colab.kernel.accessAllowed) {\n", " return;\n", " }\n", " const div = document.createElement('div');\n", " const label = document.createElement('label');\n", " label.textContent = `Downloading \"${filename}\": `;\n", " div.appendChild(label);\n", " const progress = document.createElement('progress');\n", " progress.max = size;\n", " div.appendChild(progress);\n", " document.body.appendChild(div);\n", "\n", " const buffers = [];\n", " let downloaded = 0;\n", "\n", " const channel = await google.colab.kernel.comms.open(id);\n", " // Send a message to notify the kernel that we're ready.\n", " channel.send({})\n", "\n", " for await (const message of channel.messages) {\n", " // Send a message to notify the kernel that we're ready.\n", " channel.send({})\n", " if (message.buffers) {\n", " for (const buffer of message.buffers) {\n", " buffers.push(buffer);\n", " downloaded += buffer.byteLength;\n", " progress.value = downloaded;\n", " }\n", " }\n", " }\n", " const blob = new Blob(buffers, {type: 'application/binary'});\n", " const a = document.createElement('a');\n", " a.href = window.URL.createObjectURL(blob);\n", " a.download = filename;\n", " div.appendChild(a);\n", " a.click();\n", " div.remove();\n", " }\n", " " ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "" ], "application/javascript": [ "download(\"download_6ab99ee7-fe73-411b-9f6e-ee637bb19d4a\", \"hi_vocab.pth\", 11763)" ] }, "metadata": {} } ] }, { "cell_type": "code", "source": [ "!pip install mlflow" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "dRUK8CPx5ZWk", "outputId": "600128da-dd3e-4eca-b816-5d5f77e29574" }, "execution_count": 35, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Collecting mlflow\n", " Downloading mlflow-3.10.0-py3-none-any.whl.metadata (31 kB)\n", "Collecting mlflow-skinny==3.10.0 (from mlflow)\n", " Downloading mlflow_skinny-3.10.0-py3-none-any.whl.metadata (32 kB)\n", "Collecting mlflow-tracing==3.10.0 (from mlflow)\n", " Downloading mlflow_tracing-3.10.0-py3-none-any.whl.metadata (19 kB)\n", "Collecting Flask-CORS<7 (from mlflow)\n", " Downloading flask_cors-6.0.2-py3-none-any.whl.metadata (5.3 kB)\n", "Requirement already satisfied: Flask<4 in /usr/local/lib/python3.12/dist-packages (from mlflow) (3.1.2)\n", "Requirement already satisfied: alembic!=1.10.0,<2 in /usr/local/lib/python3.12/dist-packages (from mlflow) (1.18.4)\n", "Requirement already satisfied: cryptography<47,>=43.0.0 in /usr/local/lib/python3.12/dist-packages (from mlflow) (43.0.3)\n", "Collecting docker<8,>=4.0.0 (from mlflow)\n", " Downloading docker-7.1.0-py3-none-any.whl.metadata (3.8 kB)\n", "Collecting graphene<4 (from mlflow)\n", " Downloading graphene-3.4.3-py2.py3-none-any.whl.metadata (6.9 kB)\n", "Collecting gunicorn<26 (from mlflow)\n", " Downloading gunicorn-25.1.0-py3-none-any.whl.metadata (5.5 kB)\n", "Collecting huey<3,>=2.5.4 (from mlflow)\n", " Downloading huey-2.6.0-py3-none-any.whl.metadata (4.3 kB)\n", "Requirement already satisfied: matplotlib<4 in /usr/local/lib/python3.12/dist-packages (from mlflow) (3.10.0)\n", "Requirement already satisfied: numpy<3 in /usr/local/lib/python3.12/dist-packages (from mlflow) (2.0.2)\n", "Requirement already satisfied: pandas<3 in /usr/local/lib/python3.12/dist-packages (from mlflow) (2.2.2)\n", "Requirement already satisfied: pyarrow<24,>=4.0.0 in /usr/local/lib/python3.12/dist-packages (from mlflow) (18.1.0)\n", "Requirement already satisfied: scikit-learn<2 in /usr/local/lib/python3.12/dist-packages (from mlflow) (1.6.1)\n", "Requirement already satisfied: scipy<2 in /usr/local/lib/python3.12/dist-packages (from mlflow) (1.16.3)\n", "Collecting skops<1 (from mlflow)\n", " Downloading skops-0.13.0-py3-none-any.whl.metadata (5.6 kB)\n", "Requirement already satisfied: sqlalchemy<3,>=1.4.0 in /usr/local/lib/python3.12/dist-packages (from mlflow) (2.0.46)\n", "Requirement already satisfied: cachetools<8,>=5.0.0 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (7.0.1)\n", "Requirement already satisfied: click<9,>=7.0 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (8.3.1)\n", "Requirement already satisfied: cloudpickle<4 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (3.1.2)\n", "Collecting databricks-sdk<1,>=0.20.0 (from mlflow-skinny==3.10.0->mlflow)\n", " Downloading databricks_sdk-0.91.0-py3-none-any.whl.metadata (40 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m40.1/40.1 kB\u001b[0m \u001b[31m4.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: fastapi<1 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (0.129.0)\n", "Requirement already satisfied: gitpython<4,>=3.1.9 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (3.1.46)\n", "Requirement already satisfied: importlib_metadata!=4.7.0,<9,>=3.7.0 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (8.7.1)\n", "Requirement already satisfied: opentelemetry-api<3,>=1.9.0 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (1.38.0)\n", "Requirement already satisfied: opentelemetry-proto<3,>=1.9.0 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (1.38.0)\n", "Requirement already satisfied: opentelemetry-sdk<3,>=1.9.0 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (1.38.0)\n", "Requirement already satisfied: packaging<27 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (26.0)\n", "Requirement already satisfied: protobuf<7,>=3.12.0 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (5.29.6)\n", "Requirement already satisfied: pydantic<3,>=2.0.0 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (2.12.3)\n", "Requirement already satisfied: python-dotenv<2,>=0.19.0 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (1.2.1)\n", "Requirement already satisfied: pyyaml<7,>=5.1 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (6.0.3)\n", "Requirement already satisfied: requests<3,>=2.17.3 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (2.32.4)\n", "Requirement already satisfied: sqlparse<1,>=0.4.0 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (0.5.5)\n", "Requirement already satisfied: typing-extensions<5,>=4.0.0 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (4.15.0)\n", "Requirement already satisfied: uvicorn<1 in /usr/local/lib/python3.12/dist-packages (from mlflow-skinny==3.10.0->mlflow) (0.41.0)\n", "Requirement already satisfied: Mako in /usr/local/lib/python3.12/dist-packages (from alembic!=1.10.0,<2->mlflow) (1.3.10)\n", "Requirement already satisfied: cffi>=1.12 in /usr/local/lib/python3.12/dist-packages (from cryptography<47,>=43.0.0->mlflow) (2.0.0)\n", "Requirement already satisfied: urllib3>=1.26.0 in /usr/local/lib/python3.12/dist-packages (from docker<8,>=4.0.0->mlflow) (2.5.0)\n", "Requirement already satisfied: blinker>=1.9.0 in /usr/local/lib/python3.12/dist-packages (from Flask<4->mlflow) (1.9.0)\n", "Requirement already satisfied: itsdangerous>=2.2.0 in /usr/local/lib/python3.12/dist-packages (from Flask<4->mlflow) (2.2.0)\n", "Requirement already satisfied: jinja2>=3.1.2 in /usr/local/lib/python3.12/dist-packages (from Flask<4->mlflow) (3.1.6)\n", "Requirement already satisfied: markupsafe>=2.1.1 in /usr/local/lib/python3.12/dist-packages (from Flask<4->mlflow) (3.0.3)\n", "Requirement already satisfied: werkzeug>=3.1.0 in /usr/local/lib/python3.12/dist-packages (from Flask<4->mlflow) (3.1.5)\n", "Collecting graphql-core<3.3,>=3.1 (from graphene<4->mlflow)\n", " Downloading graphql_core-3.2.7-py3-none-any.whl.metadata (11 kB)\n", "Collecting graphql-relay<3.3,>=3.1 (from graphene<4->mlflow)\n", " Downloading graphql_relay-3.2.0-py3-none-any.whl.metadata (12 kB)\n", "Requirement already satisfied: python-dateutil<3,>=2.7.0 in /usr/local/lib/python3.12/dist-packages (from graphene<4->mlflow) (2.9.0.post0)\n", "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib<4->mlflow) (1.3.3)\n", "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.12/dist-packages (from matplotlib<4->mlflow) (0.12.1)\n", "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.12/dist-packages (from matplotlib<4->mlflow) (4.61.1)\n", "Requirement already satisfied: kiwisolver>=1.3.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib<4->mlflow) (1.4.9)\n", "Requirement already satisfied: pillow>=8 in /usr/local/lib/python3.12/dist-packages (from matplotlib<4->mlflow) (11.3.0)\n", "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib<4->mlflow) (3.3.2)\n", "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.12/dist-packages (from pandas<3->mlflow) (2025.2)\n", "Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.12/dist-packages (from pandas<3->mlflow) (2025.3)\n", "Requirement already satisfied: joblib>=1.2.0 in /usr/local/lib/python3.12/dist-packages (from scikit-learn<2->mlflow) (1.5.3)\n", "Requirement already satisfied: threadpoolctl>=3.1.0 in /usr/local/lib/python3.12/dist-packages (from scikit-learn<2->mlflow) (3.6.0)\n", "Requirement already satisfied: prettytable>=3.9 in /usr/local/lib/python3.12/dist-packages (from skops<1->mlflow) (3.17.0)\n", "Requirement already satisfied: greenlet>=1 in /usr/local/lib/python3.12/dist-packages (from sqlalchemy<3,>=1.4.0->mlflow) (3.3.1)\n", "Requirement already satisfied: pycparser in /usr/local/lib/python3.12/dist-packages (from cffi>=1.12->cryptography<47,>=43.0.0->mlflow) (3.0)\n", "Requirement already satisfied: google-auth~=2.0 in /usr/local/lib/python3.12/dist-packages (from databricks-sdk<1,>=0.20.0->mlflow-skinny==3.10.0->mlflow) (2.47.0)\n", "Requirement already satisfied: starlette<1.0.0,>=0.40.0 in /usr/local/lib/python3.12/dist-packages (from fastapi<1->mlflow-skinny==3.10.0->mlflow) (0.52.1)\n", "Requirement already satisfied: typing-inspection>=0.4.2 in /usr/local/lib/python3.12/dist-packages (from fastapi<1->mlflow-skinny==3.10.0->mlflow) (0.4.2)\n", "Requirement already satisfied: annotated-doc>=0.0.2 in /usr/local/lib/python3.12/dist-packages (from fastapi<1->mlflow-skinny==3.10.0->mlflow) (0.0.4)\n", "Requirement already satisfied: gitdb<5,>=4.0.1 in /usr/local/lib/python3.12/dist-packages (from gitpython<4,>=3.1.9->mlflow-skinny==3.10.0->mlflow) (4.0.12)\n", "Requirement already satisfied: zipp>=3.20 in /usr/local/lib/python3.12/dist-packages (from importlib_metadata!=4.7.0,<9,>=3.7.0->mlflow-skinny==3.10.0->mlflow) (3.23.0)\n", "Requirement already satisfied: opentelemetry-semantic-conventions==0.59b0 in /usr/local/lib/python3.12/dist-packages (from opentelemetry-sdk<3,>=1.9.0->mlflow-skinny==3.10.0->mlflow) (0.59b0)\n", "Requirement already satisfied: wcwidth in /usr/local/lib/python3.12/dist-packages (from prettytable>=3.9->skops<1->mlflow) (0.6.0)\n", "Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.12/dist-packages (from pydantic<3,>=2.0.0->mlflow-skinny==3.10.0->mlflow) (0.7.0)\n", "Requirement already satisfied: pydantic-core==2.41.4 in /usr/local/lib/python3.12/dist-packages (from pydantic<3,>=2.0.0->mlflow-skinny==3.10.0->mlflow) (2.41.4)\n", "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.12/dist-packages (from python-dateutil<3,>=2.7.0->graphene<4->mlflow) (1.17.0)\n", "Requirement already satisfied: charset_normalizer<4,>=2 in /usr/local/lib/python3.12/dist-packages (from requests<3,>=2.17.3->mlflow-skinny==3.10.0->mlflow) (3.4.4)\n", "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.12/dist-packages (from requests<3,>=2.17.3->mlflow-skinny==3.10.0->mlflow) (3.11)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.12/dist-packages (from requests<3,>=2.17.3->mlflow-skinny==3.10.0->mlflow) (2026.1.4)\n", "Requirement already satisfied: h11>=0.8 in /usr/local/lib/python3.12/dist-packages (from uvicorn<1->mlflow-skinny==3.10.0->mlflow) (0.16.0)\n", "Requirement already satisfied: smmap<6,>=3.0.1 in /usr/local/lib/python3.12/dist-packages (from gitdb<5,>=4.0.1->gitpython<4,>=3.1.9->mlflow-skinny==3.10.0->mlflow) (5.0.2)\n", "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.12/dist-packages (from google-auth~=2.0->databricks-sdk<1,>=0.20.0->mlflow-skinny==3.10.0->mlflow) (0.4.2)\n", "Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.12/dist-packages (from google-auth~=2.0->databricks-sdk<1,>=0.20.0->mlflow-skinny==3.10.0->mlflow) (4.9.1)\n", "Requirement already satisfied: anyio<5,>=3.6.2 in /usr/local/lib/python3.12/dist-packages (from starlette<1.0.0,>=0.40.0->fastapi<1->mlflow-skinny==3.10.0->mlflow) (4.12.1)\n", "Requirement already satisfied: pyasn1<0.7.0,>=0.6.1 in /usr/local/lib/python3.12/dist-packages (from pyasn1-modules>=0.2.1->google-auth~=2.0->databricks-sdk<1,>=0.20.0->mlflow-skinny==3.10.0->mlflow) (0.6.2)\n", "Downloading mlflow-3.10.0-py3-none-any.whl (10.2 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m10.2/10.2 MB\u001b[0m \u001b[31m90.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hDownloading mlflow_skinny-3.10.0-py3-none-any.whl (3.0 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.0/3.0 MB\u001b[0m \u001b[31m125.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hDownloading mlflow_tracing-3.10.0-py3-none-any.whl (1.5 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.5/1.5 MB\u001b[0m \u001b[31m94.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hDownloading docker-7.1.0-py3-none-any.whl (147 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m147.8/147.8 kB\u001b[0m \u001b[31m18.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hDownloading flask_cors-6.0.2-py3-none-any.whl (13 kB)\n", "Downloading graphene-3.4.3-py2.py3-none-any.whl (114 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m114.9/114.9 kB\u001b[0m \u001b[31m14.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hDownloading gunicorn-25.1.0-py3-none-any.whl (197 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m197.1/197.1 kB\u001b[0m \u001b[31m25.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hDownloading huey-2.6.0-py3-none-any.whl (76 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.0/77.0 kB\u001b[0m \u001b[31m8.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hDownloading skops-0.13.0-py3-none-any.whl (131 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m131.2/131.2 kB\u001b[0m \u001b[31m17.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hDownloading databricks_sdk-0.91.0-py3-none-any.whl (808 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m808.4/808.4 kB\u001b[0m \u001b[31m65.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hDownloading graphql_core-3.2.7-py3-none-any.whl (207 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m207.3/207.3 kB\u001b[0m \u001b[31m25.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hDownloading graphql_relay-3.2.0-py3-none-any.whl (16 kB)\n", "Installing collected packages: huey, gunicorn, graphql-core, graphql-relay, docker, skops, graphene, Flask-CORS, databricks-sdk, mlflow-tracing, mlflow-skinny, mlflow\n", "Successfully installed Flask-CORS-6.0.2 databricks-sdk-0.91.0 docker-7.1.0 graphene-3.4.3 graphql-core-3.2.7 graphql-relay-3.2.0 gunicorn-25.1.0 huey-2.6.0 mlflow-3.10.0 mlflow-skinny-3.10.0 mlflow-tracing-3.10.0 skops-0.13.0\n" ] } ] }, { "cell_type": "code", "source": [ "!pip install dagshub\n", "import dagshub\n", "dagshub.init(repo_owner='vanshsharma7832', repo_name='Sentence-Translator', mlflow=True)\n", "\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 1000, "referenced_widgets": [ "4ec604360f5f489ebc058331b7836e20", "6e115a3de875472e8dcd1537fdc745bc" ] }, "id": "Jj0g6RYz-P6g", "outputId": "cd96e955-758b-4fac-a4b5-7cf359a284d8" }, "execution_count": 37, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Requirement already satisfied: dagshub in /usr/local/lib/python3.12/dist-packages (0.6.7)\n", "Requirement already satisfied: PyYAML>=5 in /usr/local/lib/python3.12/dist-packages (from dagshub) (6.0.3)\n", "Requirement already satisfied: appdirs>=1.4.4 in /usr/local/lib/python3.12/dist-packages (from dagshub) (1.4.4)\n", "Requirement already satisfied: click>=8.0.4 in /usr/local/lib/python3.12/dist-packages (from dagshub) (8.3.1)\n", "Requirement already satisfied: httpx>=0.23.0 in /usr/local/lib/python3.12/dist-packages (from dagshub) (0.28.1)\n", "Requirement already satisfied: GitPython>=3.1.29 in /usr/local/lib/python3.12/dist-packages (from dagshub) (3.1.46)\n", "Requirement already satisfied: rich>=13.1.0 in /usr/local/lib/python3.12/dist-packages (from dagshub) (13.9.4)\n", "Requirement already satisfied: dacite~=1.6.0 in /usr/local/lib/python3.12/dist-packages (from dagshub) (1.6.0)\n", "Requirement already satisfied: tenacity>=8.2.2 in /usr/local/lib/python3.12/dist-packages (from dagshub) (9.1.4)\n", "Requirement already satisfied: gql[requests] in /usr/local/lib/python3.12/dist-packages (from dagshub) (4.0.0)\n", "Requirement already satisfied: dataclasses-json in /usr/local/lib/python3.12/dist-packages (from dagshub) (0.6.7)\n", "Requirement already satisfied: pandas in /usr/local/lib/python3.12/dist-packages (from dagshub) (2.2.2)\n", "Requirement already satisfied: treelib>=1.6.4 in /usr/local/lib/python3.12/dist-packages (from dagshub) (1.8.0)\n", "Requirement already satisfied: pathvalidate>=3.0.0 in /usr/local/lib/python3.12/dist-packages (from dagshub) (3.3.1)\n", "Requirement already satisfied: python-dateutil in /usr/local/lib/python3.12/dist-packages (from dagshub) (2.9.0.post0)\n", "Requirement already satisfied: boto3 in /usr/local/lib/python3.12/dist-packages (from dagshub) (1.42.54)\n", "Requirement already satisfied: semver in /usr/local/lib/python3.12/dist-packages (from dagshub) (3.0.4)\n", "Requirement already satisfied: dagshub-annotation-converter>=0.1.12 in /usr/local/lib/python3.12/dist-packages (from dagshub) (0.1.16)\n", "Requirement already satisfied: lxml in /usr/local/lib/python3.12/dist-packages (from dagshub-annotation-converter>=0.1.12->dagshub) (6.0.2)\n", "Requirement already satisfied: pillow in /usr/local/lib/python3.12/dist-packages (from dagshub-annotation-converter>=0.1.12->dagshub) (11.3.0)\n", "Requirement already satisfied: pydantic>=2.0.0 in /usr/local/lib/python3.12/dist-packages (from dagshub-annotation-converter>=0.1.12->dagshub) (2.12.3)\n", "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.12/dist-packages (from dagshub-annotation-converter>=0.1.12->dagshub) (4.15.0)\n", "Requirement already satisfied: gitdb<5,>=4.0.1 in /usr/local/lib/python3.12/dist-packages (from GitPython>=3.1.29->dagshub) (4.0.12)\n", "Requirement already satisfied: anyio in /usr/local/lib/python3.12/dist-packages (from httpx>=0.23.0->dagshub) (4.12.1)\n", "Requirement already satisfied: certifi in /usr/local/lib/python3.12/dist-packages (from httpx>=0.23.0->dagshub) (2026.1.4)\n", "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.12/dist-packages (from httpx>=0.23.0->dagshub) (1.0.9)\n", "Requirement already satisfied: idna in /usr/local/lib/python3.12/dist-packages (from httpx>=0.23.0->dagshub) (3.11)\n", "Requirement already satisfied: h11>=0.16 in /usr/local/lib/python3.12/dist-packages (from httpcore==1.*->httpx>=0.23.0->dagshub) (0.16.0)\n", "Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.12/dist-packages (from rich>=13.1.0->dagshub) (4.0.0)\n", "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.12/dist-packages (from rich>=13.1.0->dagshub) (2.19.2)\n", "Requirement already satisfied: six>=1.13.0 in /usr/local/lib/python3.12/dist-packages (from treelib>=1.6.4->dagshub) (1.17.0)\n", "Requirement already satisfied: botocore<1.43.0,>=1.42.54 in /usr/local/lib/python3.12/dist-packages (from boto3->dagshub) (1.42.54)\n", "Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in /usr/local/lib/python3.12/dist-packages (from boto3->dagshub) (1.1.0)\n", "Requirement already satisfied: s3transfer<0.17.0,>=0.16.0 in /usr/local/lib/python3.12/dist-packages (from boto3->dagshub) (0.16.0)\n", "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in /usr/local/lib/python3.12/dist-packages (from dataclasses-json->dagshub) (3.26.2)\n", "Requirement already satisfied: typing-inspect<1,>=0.4.0 in /usr/local/lib/python3.12/dist-packages (from dataclasses-json->dagshub) (0.9.0)\n", "Requirement already satisfied: graphql-core<3.3,>=3.2 in /usr/local/lib/python3.12/dist-packages (from gql[requests]->dagshub) (3.2.7)\n", "Requirement already satisfied: yarl<2.0,>=1.6 in /usr/local/lib/python3.12/dist-packages (from gql[requests]->dagshub) (1.22.0)\n", "Requirement already satisfied: backoff<3.0,>=1.11.1 in /usr/local/lib/python3.12/dist-packages (from gql[requests]->dagshub) (2.2.1)\n", "Requirement already satisfied: requests<3,>=2.26 in /usr/local/lib/python3.12/dist-packages (from gql[requests]->dagshub) (2.32.4)\n", "Requirement already satisfied: requests_toolbelt<2,>=1.0.0 in /usr/local/lib/python3.12/dist-packages (from gql[requests]->dagshub) (1.0.0)\n", "Requirement already satisfied: numpy>=1.26.0 in /usr/local/lib/python3.12/dist-packages (from pandas->dagshub) (2.0.2)\n", "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.12/dist-packages (from pandas->dagshub) (2025.2)\n", "Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.12/dist-packages (from pandas->dagshub) (2025.3)\n", "Requirement already satisfied: urllib3!=2.2.0,<3,>=1.25.4 in /usr/local/lib/python3.12/dist-packages (from botocore<1.43.0,>=1.42.54->boto3->dagshub) (2.5.0)\n", "Requirement already satisfied: smmap<6,>=3.0.1 in /usr/local/lib/python3.12/dist-packages (from gitdb<5,>=4.0.1->GitPython>=3.1.29->dagshub) (5.0.2)\n", "Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.12/dist-packages (from markdown-it-py>=2.2.0->rich>=13.1.0->dagshub) (0.1.2)\n", "Requirement already satisfied: packaging>=17.0 in /usr/local/lib/python3.12/dist-packages (from marshmallow<4.0.0,>=3.18.0->dataclasses-json->dagshub) (26.0)\n", "Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.12/dist-packages (from pydantic>=2.0.0->dagshub-annotation-converter>=0.1.12->dagshub) (0.7.0)\n", "Requirement already satisfied: pydantic-core==2.41.4 in /usr/local/lib/python3.12/dist-packages (from pydantic>=2.0.0->dagshub-annotation-converter>=0.1.12->dagshub) (2.41.4)\n", "Requirement already satisfied: typing-inspection>=0.4.2 in /usr/local/lib/python3.12/dist-packages (from pydantic>=2.0.0->dagshub-annotation-converter>=0.1.12->dagshub) (0.4.2)\n", "Requirement already satisfied: charset_normalizer<4,>=2 in /usr/local/lib/python3.12/dist-packages (from requests<3,>=2.26->gql[requests]->dagshub) (3.4.4)\n", "Requirement already satisfied: mypy-extensions>=0.3.0 in /usr/local/lib/python3.12/dist-packages (from typing-inspect<1,>=0.4.0->dataclasses-json->dagshub) (1.1.0)\n", "Requirement already satisfied: multidict>=4.0 in /usr/local/lib/python3.12/dist-packages (from yarl<2.0,>=1.6->gql[requests]->dagshub) (6.7.1)\n", "Requirement already satisfied: propcache>=0.2.1 in /usr/local/lib/python3.12/dist-packages (from yarl<2.0,>=1.6->gql[requests]->dagshub) (0.4.1)\n" ] }, { "output_type": "display_data", "data": { "text/plain": [ " \u001b[1m❗❗❗ AUTHORIZATION REQUIRED ❗❗❗\u001b[0m \n" ], "text/html": [ "
                                       ❗❗❗ AUTHORIZATION REQUIRED ❗❗❗                                        \n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "Output()" ], "application/vnd.jupyter.widget-view+json": { "version_major": 2, "version_minor": 0, "model_id": "4ec604360f5f489ebc058331b7836e20" } }, "metadata": {} }, { "output_type": "stream", "name": "stdout", "text": [ "\n", "\n", "Open the following link in your browser to authorize the client:\n", "https://dagshub.com/login/oauth/authorize?state=7122499f-4a30-48f5-bb81-cebd5dbeb6eb&client_id=32b60ba385aa7cecf24046d8195a71c07dd345d9657977863b52e7748e0f0f28&middleman_request_id=d69209eee37bc01e33d0d52592ae719b884f0b18c66d6e90dc33f13164ded3ba\n", "\n", "\n" ] }, { "output_type": "display_data", "data": { "text/plain": [], "text/html": [ "
\n"
            ]
          },
          "metadata": {}
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "Accessing as vanshsharma7832\n"
            ],
            "text/html": [
              "
Accessing as vanshsharma7832\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "Initialized MLflow to track repo \u001b[32m\"vanshsharma7832/Sentence-Translator\"\u001b[0m\n" ], "text/html": [ "
Initialized MLflow to track repo \"vanshsharma7832/Sentence-Translator\"\n",
              "
\n" ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "Repository vanshsharma7832/Sentence-Translator initialized!\n" ], "text/html": [ "
Repository vanshsharma7832/Sentence-Translator initialized!\n",
              "
\n" ] }, "metadata": {} } ] }, { "cell_type": "code", "source": [ "import mlflow\n", "with mlflow.start_run():\n", " mlflow.log_metric('loss', 0.0644)\n", " # mlflow.log_artifact('model.pth',model)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "awYYBxaP-dBD", "outputId": "2e2d8477-5e80-4603-b2bc-1bf31781b681" }, "execution_count": 42, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "🏃 View run enthused-donkey-213 at: https://dagshub.com/vanshsharma7832/Sentence-Translator.mlflow/#/experiments/0/runs/6f6da30dc49d45ae97b252a9788c132c\n", "🧪 View experiment at: https://dagshub.com/vanshsharma7832/Sentence-Translator.mlflow/#/experiments/0\n" ] } ] }, { "cell_type": "code", "source": [], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "IZz_j4Zw-4Bd", "outputId": "a26b9105-c6df-45c1-edb4-cd42ff014f36" }, "execution_count": 41, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "8.11653953244604" ] }, "metadata": {}, "execution_count": 41 } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "2-cR3hdb_GbW" }, "execution_count": null, "outputs": [] } ] }