初始化项目,由ModelHub XC社区提供模型
Model: Omarrran/koshur-kouter-ks-en_v1 Source: Original Platform
This commit is contained in:
129
notebooks/colab_load_stage1_model.ipynb
Normal file
129
notebooks/colab_load_stage1_model.ipynb
Normal file
@@ -0,0 +1,129 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Koshur Kouter v1 — Colab loader\n",
|
||||
"\n",
|
||||
"This notebook loads the **full merged Stage 1** model from Hugging Face and runs a few example translations.\n",
|
||||
"\n",
|
||||
"- Repo: `Omarrran/koshur-kouter-ks-en_v1`\n",
|
||||
"- Base model family: `sarvamai/sarvam-translate` / Gemma 3 text stack\n",
|
||||
"- Recommended for full `bfloat16` loading on Colab: **L4 / A100 / better**\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!pip -q install -U transformers accelerate sentencepiece safetensors\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import torch\n",
|
||||
"from transformers import AutoTokenizer, AutoModelForCausalLM\n",
|
||||
"\n",
|
||||
"REPO_ID = \"Omarrran/koshur-kouter-ks-en_v1\"\n",
|
||||
"\n",
|
||||
"tokenizer = AutoTokenizer.from_pretrained(REPO_ID)\n",
|
||||
"model = AutoModelForCausalLM.from_pretrained(\n",
|
||||
" REPO_ID,\n",
|
||||
" torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,\n",
|
||||
" device_map=\"auto\",\n",
|
||||
")\n",
|
||||
"model.eval()\n",
|
||||
"print(model.__class__.__name__)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def first_nonempty_line(text: str) -> str:\n",
|
||||
" for line in text.splitlines():\n",
|
||||
" line = line.strip()\n",
|
||||
" if line:\n",
|
||||
" return line\n",
|
||||
" return text.strip()\n",
|
||||
"\n",
|
||||
"def build_prompt(source_text: str, direction: str):\n",
|
||||
" if direction == \"ks2en\":\n",
|
||||
" system = \"Translate the text below to English. Return only the translation.\"\n",
|
||||
" elif direction == \"en2ks\":\n",
|
||||
" system = \"Translate the text below to Kashmiri. Return only the translation.\"\n",
|
||||
" else:\n",
|
||||
" system = \"Translate the text below. Return only the translation.\"\n",
|
||||
" return [\n",
|
||||
" {\"role\": \"system\", \"content\": system},\n",
|
||||
" {\"role\": \"user\", \"content\": source_text},\n",
|
||||
" ]\n",
|
||||
"\n",
|
||||
"def translate(source_text: str, direction: str, max_new_tokens: int = 48):\n",
|
||||
" messages = build_prompt(source_text, direction)\n",
|
||||
" prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
|
||||
" inputs = tokenizer(prompt, return_tensors=\"pt\", truncation=True, max_length=1024).to(model.device)\n",
|
||||
" with torch.no_grad():\n",
|
||||
" outputs = model.generate(\n",
|
||||
" **inputs,\n",
|
||||
" max_new_tokens=max_new_tokens,\n",
|
||||
" do_sample=False,\n",
|
||||
" repetition_penalty=1.15,\n",
|
||||
" no_repeat_ngram_size=3,\n",
|
||||
" pad_token_id=tokenizer.eos_token_id,\n",
|
||||
" )\n",
|
||||
" decoded = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)\n",
|
||||
" return first_nonempty_line(decoded)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"examples = [\n",
|
||||
" (\"Please sit down and have some tea.\", \"en2ks\"),\n",
|
||||
" (\"The children are playing in the garden.\", \"en2ks\"),\n",
|
||||
" (\"کٔشیر چھُ اکھ خوبصورت جٲی\", \"ks2en\"),\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"for text, direction in examples:\n",
|
||||
" print(\"SOURCE:\", text)\n",
|
||||
" print(\"DIRECTION:\", direction)\n",
|
||||
" print(\"PRED:\", translate(text, direction))\n",
|
||||
" print(\"-\" * 80)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Optional lower-memory path\n",
|
||||
"\n",
|
||||
"If your Colab GPU is tight on VRAM, you can try a quantized load with `bitsandbytes`. The uploaded repository is still a **full merged model**; quantized loading is only a runtime option.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
Reference in New Issue
Block a user