{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "4cdfd669", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "W0411 22:03:00.156000 23924 Lib\\site-packages\\torch\\distributed\\elastic\\multiprocessing\\redirects.py:29] NOTE: Redirects are currently not supported in Windows or MacOs.\n" ] } ], "source": [ "import torch\n", "from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline\n", "import gradio as gr\n", "from datasets import load_dataset\n", "import sacrebleu\n", "import speech_recognition as sr\n", "from typing import List, Tuple, Optional\n", "import yaml\n", "import warnings\n", "warnings.filterwarnings('ignore')" ] }, { "cell_type": "code", "execution_count": 2, "id": "589fa540", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using device: cuda\n" ] } ], "source": [ "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", "print(f\"Using device: {device}\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "baaff196", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "GPU name: NVIDIA GeForce RTX 4050 Laptop GPU\n" ] } ], "source": [ "print(f\"GPU name: {torch.cuda.get_device_name(0)}\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "128248cb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "GPU properties: 6.44 GB\n" ] } ], "source": [ "print(f\"GPU properties: {torch.cuda.get_device_properties(0).total_memory/1e9:.2f} GB\")" ] }, { "cell_type": "code", "execution_count": null, "id": "7bb8cde9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loading model and tokenizer...\n", "Please wait while the model is being loaded. This may take a few moments.\n" ] }, { "ename": "", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31mnotebook controller is DISPOSED. \n", "\u001b[1;31mView Jupyter log for further details." ] } ], "source": [ "with open(\"../config.yml\", \"r\") as f:\n", " config = yaml.safe_load(f)\n", "\n", "# Use config values\n", "print(config[\"messages\"][\"loading\"])\n", "print(config[\"messages\"][\"waiting\"])" ] }, { "cell_type": "code", "execution_count": null, "id": "ad5120bd", "metadata": {}, "outputs": [ { "ename": "", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31mnotebook controller is DISPOSED. \n", "\u001b[1;31mView Jupyter log for further details." ] } ], "source": [ "tokenizer = AutoTokenizer.from_pretrained(\n", " config[\"model\"][\"name\"],\n", " src_lang=config[\"model\"][\"src_lang\"],\n", " use_fast=config[\"model\"][\"use_fast_tokenizer\"]\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "1ae2a9aa", "metadata": {}, "outputs": [ { "ename": "", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31mnotebook controller is DISPOSED. \n", "\u001b[1;31mView Jupyter log for further details." ] } ], "source": [ "MODEL_NAME = config[\"model\"][\"name\"]" ] }, { "cell_type": "code", "execution_count": null, "id": "06e06c36", "metadata": {}, "outputs": [ { "ename": "", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31mnotebook controller is DISPOSED. \n", "\u001b[1;31mView Jupyter log for further details." ] } ], "source": [ "model = AutoModelForSeq2SeqLM.from_pretrained(\n", " MODEL_NAME,\n", " torch_dtype=torch.float16 if device == \"cuda\" else torch.float32\n", ").to(device)" ] }, { "cell_type": "code", "execution_count": null, "id": "93900624", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model loaded successfully!\n", "Model parameters: 615.1M\n", "Model dtype: torch.float16\n" ] }, { "ename": "", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31mnotebook controller is DISPOSED. \n", "\u001b[1;31mView Jupyter log for further details." ] } ], "source": [ "print(\"Model loaded successfully!\")\n", "print(f\"Model parameters: {sum(p.numel() for p in model.parameters()) / 1e6:.1f}M\")\n", "print(f\"Model dtype: {model.dtype}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "3dc505d0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Configured 50 languages\n", "\n", "Sample languages: English, French, Arabic, Spanish, German, Chinese (Simplified), Chinese (Traditional), Japanese, Korean, Russian...\n" ] }, { "ename": "", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31mnotebook controller is DISPOSED. \n", "\u001b[1;31mView Jupyter log for further details." ] } ], "source": [ "# Language code mapping (FLORES-200 format)\n", "LANGUAGE_CODES = {\n", " \"English\": \"eng_Latn\",\n", " \"French\": \"fra_Latn\",\n", " \"Arabic\": \"arb_Arab\",\n", " \"Spanish\": \"spa_Latn\",\n", " \"German\": \"deu_Latn\",\n", " \"Chinese (Simplified)\": \"zho_Hans\",\n", " \"Chinese (Traditional)\": \"zho_Hant\",\n", " \"Japanese\": \"jpn_Jpan\",\n", " \"Korean\": \"kor_Hang\",\n", " \"Russian\": \"rus_Cyrl\",\n", " \"Portuguese\": \"por_Latn\",\n", " \"Italian\": \"ita_Latn\",\n", " \"Dutch\": \"nld_Latn\",\n", " \"Turkish\": \"tur_Latn\",\n", " \"Polish\": \"pol_Latn\",\n", " \"Hindi\": \"hin_Deva\",\n", " \"Bengali\": \"ben_Beng\",\n", " \"Urdu\": \"urd_Arab\",\n", " \"Vietnamese\": \"vie_Latn\",\n", " \"Thai\": \"tha_Thai\",\n", " \"Indonesian\": \"ind_Latn\",\n", " \"Malay\": \"zsm_Latn\",\n", " \"Swahili\": \"swh_Latn\",\n", " \"Greek\": \"ell_Grek\",\n", " \"Hebrew\": \"heb_Hebr\",\n", " \"Persian\": \"pes_Arab\",\n", " \"Ukrainian\": \"ukr_Cyrl\",\n", " \"Czech\": \"ces_Latn\",\n", " \"Swedish\": \"swe_Latn\",\n", " \"Danish\": \"dan_Latn\",\n", " \"Finnish\": \"fin_Latn\",\n", " \"Norwegian\": \"nob_Latn\",\n", " \"Hungarian\": \"hun_Latn\",\n", " \"Romanian\": \"ron_Latn\",\n", " \"Bulgarian\": \"bul_Cyrl\",\n", " \"Croatian\": \"hrv_Latn\",\n", " \"Serbian\": \"srp_Cyrl\",\n", " \"Slovak\": \"slk_Latn\",\n", " \"Lithuanian\": \"lit_Latn\",\n", " \"Latvian\": \"lvs_Latn\",\n", " \"Estonian\": \"est_Latn\",\n", " \"Slovenian\": \"slv_Latn\",\n", " \"Catalan\": \"cat_Latn\",\n", " \"Tagalog\": \"tgl_Latn\",\n", " \"Tamil\": \"tam_Taml\",\n", " \"Telugu\": \"tel_Telu\",\n", " \"Kannada\": \"kan_Knda\",\n", " \"Malayalam\": \"mal_Mlym\",\n", " \"Marathi\": \"mar_Deva\",\n", " \"Gujarati\": \"guj_Gujr\"\n", "}\n", "\n", "# Get list of supported languages\n", "SUPPORTED_LANGUAGES = list(LANGUAGE_CODES.keys())\n", "\n", "print(f\"Configured {len(SUPPORTED_LANGUAGES)} languages\")\n", "print(f\"\\nSample languages: {', '.join(SUPPORTED_LANGUAGES[:10])}...\")" ] }, { "cell_type": "code", "execution_count": null, "id": "c7aacaa2", "metadata": {}, "outputs": [ { "ename": "", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31mnotebook controller is DISPOSED. \n", "\u001b[1;31mView Jupyter log for further details." ] } ], "source": [] } ], "metadata": { "kernelspec": { "display_name": "pytorch_env", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.9" } }, "nbformat": 4, "nbformat_minor": 5 }