"
],
"text/html": [
"\n",
" \n",
" \n",
"
\n",
" [96/96 07:48, Epoch 3/3]\n",
"
\n",
" \n",
" \n",
" \n",
" Epoch | \n",
" Training Loss | \n",
" Validation Loss | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" No log | \n",
" 7.252932 | \n",
"
\n",
" \n",
" 2 | \n",
" No log | \n",
" 4.746749 | \n",
"
\n",
" \n",
" 3 | \n",
" No log | \n",
" 3.628509 | \n",
"
\n",
" \n",
"
"
]
},
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"TrainOutput(global_step=96, training_loss=0.9668324788411459, metrics={'train_runtime': 483.0275, 'train_samples_per_second': 3.112, 'train_steps_per_second': 0.199, 'total_flos': 1199342615003136.0, 'train_loss': 0.9668324788411459, 'epoch': 3.0})"
]
},
"metadata": {},
"execution_count": 25
}
]
},
{
"cell_type": "code",
"source": [
"trainer.evaluate()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 124
},
"id": "PQRs93-IrT7V",
"outputId": "de69e916-9ac0-48e0-a12b-75a881abc419"
},
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
""
],
"text/html": [
"\n",
" \n",
" \n",
"
\n",
" [32/32 00:42]\n",
"
\n",
" "
]
},
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'eval_loss': 3.4542908668518066,\n",
" 'eval_runtime': 42.941,\n",
" 'eval_samples_per_second': 11.667,\n",
" 'eval_steps_per_second': 0.745,\n",
" 'epoch': 3.0}"
]
},
"metadata": {},
"execution_count": 26
}
]
},
{
"cell_type": "code",
"source": [
"model.save_pretrained(\"telugu_model\")\n",
"tokenizer.save_pretrained(\"telugu_model\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "R5bLFkoCriaW",
"outputId": "af41a671-df45-4b7e-c46f-b8dc460927cc"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('telugu_model/tokenizer_config.json',\n",
" 'telugu_model/special_tokens_map.json',\n",
" 'telugu_model/tokenizer.model',\n",
" 'telugu_model/added_tokens.json',\n",
" 'telugu_model/tokenizer.json')"
]
},
"metadata": {},
"execution_count": 27
}
]
},
{
"cell_type": "code",
"source": [
"from transformers import TrainingArguments\n",
"\n",
"training_args = TrainingArguments(\n",
" output_dir=\"./results\",\n",
" eval_strategy=\"epoch\", # Change to `eval_strategy`\n",
" learning_rate=2e-5,\n",
" per_device_train_batch_size=16,\n",
" per_device_eval_batch_size=16,\n",
" num_train_epochs=3,\n",
" weight_decay=0.01,\n",
" save_total_limit=2,\n",
" load_best_model_at_end=True,\n",
" save_strategy=\"epoch\" # Ensure it matches the eval_strategy\n",
")"
],
"metadata": {
"id": "8--s0fYErlek"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from transformers import DataCollatorForLanguageModeling\n",
"\n",
"data_collator = DataCollatorForLanguageModeling(\n",
" tokenizer=tokenizer,\n",
" mlm=False,\n",
")"
],
"metadata": {
"id": "emQx828JruP0"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from transformers import Trainer\n",
"\n",
"trainer = Trainer(\n",
" model=model,\n",
" args=training_args,\n",
" train_dataset=tokenized_datasets,\n",
" eval_dataset=tokenized_datasets,\n",
" data_collator=data_collator,\n",
" tokenizer=tokenizer,\n",
")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "GjGw3SlSrzvd",
"outputId": "320d1af2-9d78-4f9f-f674-cdbcd230c120"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
":3: FutureWarning: `tokenizer` is deprecated and will be removed in version 5.0.0 for `Trainer.__init__`. Use `processing_class` instead.\n",
" trainer = Trainer(\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"trainer.train()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 204
},
"id": "c9L9c-Cwr5xl",
"outputId": "ab870cd8-a116-47a8-b8ef-a9ca16a03364"
},
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
""
],
"text/html": [
"\n",
" \n",
" \n",
"
\n",
" [96/96 08:01, Epoch 3/3]\n",
"
\n",
" \n",
" \n",
" \n",
" Epoch | \n",
" Training Loss | \n",
" Validation Loss | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" No log | \n",
" 0.376746 | \n",
"
\n",
" \n",
" 2 | \n",
" No log | \n",
" 0.405187 | \n",
"
\n",
" \n",
" 3 | \n",
" No log | \n",
" 0.381811 | \n",
"
\n",
" \n",
"
"
]
},
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"TrainOutput(global_step=96, training_loss=0.48191014925638836, metrics={'train_runtime': 485.1496, 'train_samples_per_second': 3.098, 'train_steps_per_second': 0.198, 'total_flos': 1199342615003136.0, 'train_loss': 0.48191014925638836, 'epoch': 3.0})"
]
},
"metadata": {},
"execution_count": 31
}
]
},
{
"cell_type": "code",
"source": [
"trainer.evaluate()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 124
},
"id": "gk6PuBU0t6cv",
"outputId": "0c24dea7-1765-49e1-b9e9-1c6c0d2228ef"
},
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
""
],
"text/html": [
"\n",
" \n",
" \n",
"
\n",
" [32/32 00:41]\n",
"
\n",
" "
]
},
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'eval_loss': 0.3767460286617279,\n",
" 'eval_runtime': 42.3501,\n",
" 'eval_samples_per_second': 11.83,\n",
" 'eval_steps_per_second': 0.756,\n",
" 'epoch': 3.0}"
]
},
"metadata": {},
"execution_count": 32
}
]
},
{
"cell_type": "code",
"source": [
"model.save_pretrained(\"telugu_model\")\n",
"tokenizer.save_pretrained(\"telugu_model\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "mLqp-UnruPye",
"outputId": "94bc9301-ada9-4cb8-a3d1-bbd51966bdb2"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('telugu_model/tokenizer_config.json',\n",
" 'telugu_model/special_tokens_map.json',\n",
" 'telugu_model/tokenizer.model',\n",
" 'telugu_model/added_tokens.json',\n",
" 'telugu_model/tokenizer.json')"
]
},
"metadata": {},
"execution_count": 33
}
]
},
{
"cell_type": "code",
"source": [
"from transformers import TrainingArguments\n",
"\n",
"training_args = TrainingArguments(\n",
" output_dir=\"./results\",\n",
" eval_strategy=\"epoch\", # Changed to `eval_strategy`\n",
" learning_rate=2e-5,\n",
" per_device_train_batch_size=16,\n",
" per_device_eval_batch_size=16,\n",
" num_train_epochs=3,\n",
" weight_decay=0.01,\n",
" save_total_limit=2,\n",
" load_best_model_at_end=True,\n",
" save_strategy=\"epoch\" # Ensure it matches the eval_strategy\n",
")"
],
"metadata": {
"id": "gr6M3STRudSO"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from transformers import DataCollatorForLanguageModeling\n",
"\n",
"data_collator = DataCollatorForLanguageModeling(\n",
" tokenizer=tokenizer,\n",
" mlm=False,\n",
")"
],
"metadata": {
"id": "oIyveqPqul7e"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from transformers import Trainer\n",
"\n",
"trainer = Trainer(\n",
" model=model,\n",
" args=training_args,\n",
" train_dataset=tokenized_datasets,\n",
" eval_dataset=tokenized_datasets,\n",
" data_collator=data_collator,\n",
" tokenizer=tokenizer,\n",
")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6V2xWdHSuutl",
"outputId": "e10842b6-d227-47bc-dc7e-6577ed4b76fb"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
":3: FutureWarning: `tokenizer` is deprecated and will be removed in version 5.0.0 for `Trainer.__init__`. Use `processing_class` instead.\n",
" trainer = Trainer(\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"trainer.train()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 204
},
"id": "vZ_a9aHru1kA",
"outputId": "0a8c3134-fa11-4362-80ac-853a3b2d334b"
},
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
""
],
"text/html": [
"\n",
" \n",
" \n",
"
\n",
" [96/96 08:01, Epoch 3/3]\n",
"
\n",
" \n",
" \n",
" \n",
" Epoch | \n",
" Training Loss | \n",
" Validation Loss | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" No log | \n",
" 0.391797 | \n",
"
\n",
" \n",
" 2 | \n",
" No log | \n",
" 0.325606 | \n",
"
\n",
" \n",
" 3 | \n",
" No log | \n",
" 0.338692 | \n",
"
\n",
" \n",
"
"
]
},
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"TrainOutput(global_step=96, training_loss=0.3950839837392171, metrics={'train_runtime': 485.6068, 'train_samples_per_second': 3.095, 'train_steps_per_second': 0.198, 'total_flos': 1199342615003136.0, 'train_loss': 0.3950839837392171, 'epoch': 3.0})"
]
},
"metadata": {},
"execution_count": 37
}
]
},
{
"cell_type": "code",
"source": [
"trainer.evaluate()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 124
},
"id": "Xi-PVesVw41z",
"outputId": "bf5c8835-12f8-4d4b-9597-5a545d2ae7ce"
},
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
""
],
"text/html": [
"\n",
" \n",
" \n",
"
\n",
" [32/32 00:41]\n",
"
\n",
" "
]
},
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'eval_loss': 0.32560598850250244,\n",
" 'eval_runtime': 42.5156,\n",
" 'eval_samples_per_second': 11.784,\n",
" 'eval_steps_per_second': 0.753,\n",
" 'epoch': 3.0}"
]
},
"metadata": {},
"execution_count": 38
}
]
},
{
"cell_type": "code",
"source": [
"model.save_pretrained(\"telugu_model\")\n",
"tokenizer.save_pretrained(\"telugu_model\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "yl9S43TSxKgs",
"outputId": "91f5575e-0b82-4a93-89de-d3fec114bf9e"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('telugu_model/tokenizer_config.json',\n",
" 'telugu_model/special_tokens_map.json',\n",
" 'telugu_model/tokenizer.model',\n",
" 'telugu_model/added_tokens.json',\n",
" 'telugu_model/tokenizer.json')"
]
},
"metadata": {},
"execution_count": 39
}
]
},
{
"cell_type": "code",
"source": [
"from transformers import AutoModelForCausalLM, AutoTokenizer\n",
"\n",
"# Load the tokenizer and model\n",
"tokenizer = AutoTokenizer.from_pretrained(\"telugu_model\")\n",
"model = AutoModelForCausalLM.from_pretrained(\"telugu_model\")"
],
"metadata": {
"id": "JTZuiMD-xXhi"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Define the input text\n",
"input_text = \"మీరు ఏమిటి చేస్తున్నారు?\"\n",
"\n",
"# Tokenize the input text\n",
"inputs = tokenizer(input_text, return_tensors=\"pt\")\n",
"\n",
"# Generate the output with `max_new_tokens` parameter\n",
"outputs = model.generate(**inputs, max_new_tokens=50, num_return_sequences=1)\n",
"\n",
"# Decode the output\n",
"generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
"\n",
"print(generated_text)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "5pzGkXacxbAa",
"outputId": "77bbb841-b62e-426d-e5b7-142fc9ade4ba"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"మీరు ఏమిటి చేస్తున్నారు? మీరు కొత్త ప్రాజెక్��\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"from flask import Flask, request, jsonify\n",
"from transformers import AutoModelForCausalLM, AutoTokenizer\n",
"\n",
"app = Flask(__name__)\n",
"\n",
"# Load the tokenizer and model\n",
"tokenizer = AutoTokenizer.from_pretrained(\"telugu_model\")\n",
"model = AutoModelForCausalLM.from_pretrained(\"telugu_model\")\n",
"\n",
"@app.route('/generate', methods=['POST'])\n",
"def generate_text():\n",
" data = request.json\n",
" input_text = data.get('input_text', '')\n",
" inputs = tokenizer(input_text, return_tensors=\"pt\")\n",
" outputs = model.generate(**inputs, max_new_tokens=50, num_return_sequences=1)\n",
" generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
" return jsonify({'generated_text': generated_text})\n",
"\n",
"if __name__ == '__main__':\n",
" app.run(debug=True)\n"
],
"metadata": {
"id": "zf0MFU0FBXFx"
},
"execution_count": null,
"outputs": []
}
]
}