{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":121150,"databundleVersionId":15833391,"isSourceIdPinned":false},{"sourceType":"datasetVersion","sourceId":14972964,"datasetId":9584325,"databundleVersionId":15845324}],"dockerImageVersionId":31286,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from transformers import AutoModelForSeq2SeqLM, AutoTokenizer\nimport torch\nimport pandas as pd\nimport os\n\n# Поиск модели\npossible_model_paths = [\n    \"./mt5-akkadian-final\",\n    \"/kaggle/working/mt5-akkadian-final\",\n    \"/kaggle/input/datasets/demiruzun/base-my\"\n]\n\nmodel_path = None\nfor path in possible_model_paths:\n    if os.path.exists(path):\n        model_path = path\n        print(f\"✅ Модель найдена: {path}\")\n        break\n\nif model_path is None:\n    raise FileNotFoundError(\"Модель не найдена!\")\n\n# Загружаем модель\nprint(\"📥 Загружаем модель...\")\nmodel = AutoModelForSeq2SeqLM.from_pretrained(model_path)\ntokenizer = AutoTokenizer.from_pretrained(model_path)\ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nmodel.to(device)\n\n# Поиск тестового файла\npossible_test_paths = [\n    '/kaggle/input/deep-past-initiative-machine-translation/test.csv',\n    '/kaggle/input/deep-past-challenge/test.csv',\n    '../input/deep-past-initiative-machine-translation/test.csv',\n    'test.csv'\n]\n\ntest_df = None\nfor path in possible_test_paths:\n    if os.path.exists(path):\n        print(f\"✅ Найден тестовый файл: {path}\")\n        test_df = pd.read_csv(path)\n        break\n\nif test_df is None:\n    raise FileNotFoundError(\"Тестовый файл не найден!\")\n\nprint(f\"📊 Тестовых примеров: {len(test_df)}\")\n\n# Определяем колонку с текстом\ntext_column = None\nfor col in ['transliteration', 'akkadian', 'text']:\n    if col in test_df.columns:\n        text_column = col\n        print(f\"🔤 Используем колонку: {col}\")\n        break\n\nif text_column is None:\n    text_column = test_df.columns[1]\n    print(f\"🔤 Используем колонку: {text_column}\")\n\nprefix = \"translate Akkadian to English: \"\n\ndef translate(texts):\n    if not texts:\n        return []\n    \n    # Очищаем от NaN\n    texts = [str(t) if pd.notna(t) else \"\" for t in texts]\n    \n    inputs = tokenizer(\n        [prefix + t for t in texts],\n        return_tensors=\"pt\",\n        padding=True,\n        truncation=True,\n        max_length=256\n    ).to(device)\n    \n    with torch.no_grad():\n        outputs = model.generate(\n            **inputs,\n            max_length=128,\n            num_beams=4,\n            length_penalty=1.05,\n            no_repeat_ngram_size=3,\n            early_stopping=True\n        )\n    \n    return tokenizer.batch_decode(outputs, skip_special_tokens=True)\n\n# Переводим\ntranslations = []\nbatch_size = 8\n\nfor i in range(0, len(test_df), batch_size):\n    batch_texts = test_df[text_column].iloc[i:i+batch_size].tolist()\n    batch_preds = translate(batch_texts)\n    translations.extend(batch_preds)\n    print(f\"  Прогресс: {min(i+batch_size, len(test_df))}/{len(test_df)}\")\n\n# Сохраняем\nsubmission = pd.DataFrame({\n    'id': test_df['id'] if 'id' in test_df.columns else range(len(test_df)),\n    'translation': translations\n})\n\nsubmission.to_csv('submission.csv', index=False)\n# print(\"\\n✅ submission.csv готов к отправке!\")\n# print(submission)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-26T18:55:21.811484Z","iopub.execute_input":"2026-02-26T18:55:21.811895Z","iopub.status.idle":"2026-02-26T18:56:08.995363Z","shell.execute_reply.started":"2026-02-26T18:55:21.811848Z","shell.execute_reply":"2026-02-26T18:56:08.993631Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}