{"metadata":{"kernelspec":{"display_name":"Python 3","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.10.14"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":84795,"databundleVersionId":10462807,"sourceType":"competition"}],"dockerImageVersionId":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false},"papermill":{"default_parameters":{},"duration":22.341371,"end_time":"2024-12-11T03:22:13.479076","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2024-12-11T03:21:51.137705","version":"2.6.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install git+https://github.com/SmartManoj/jupyter-notify.git --user -q\n%load_ext jupyternotify","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T10:40:57.829196Z","iopub.execute_input":"2025-01-16T10:40:57.829963Z","iopub.status.idle":"2025-01-16T10:41:10.631192Z","shell.execute_reply.started":"2025-01-16T10:40:57.829929Z","shell.execute_reply":"2025-01-16T10:41:10.630459Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%notify\nfrom huggingface_hub import snapshot_download\n\nsnapshot_download(repo_id=\"reach-vb/phi-4-Q4_K_M-GGUF\", local_dir= '.')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T09:53:46.839944Z","iopub.execute_input":"2025-01-16T09:53:46.840282Z","iopub.status.idle":"2025-01-16T09:57:22.847248Z","shell.execute_reply.started":"2025-01-16T09:53:46.840251Z","shell.execute_reply":"2025-01-16T09:57:22.846613Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T10:41:15.828308Z","iopub.execute_input":"2025-01-16T10:41:15.828641Z","iopub.status.idle":"2025-01-16T10:41:16.775221Z","shell.execute_reply.started":"2025-01-16T10:41:15.828612Z","shell.execute_reply":"2025-01-16T10:41:16.77428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install gguf -q","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T09:57:41.280233Z","iopub.execute_input":"2025-01-16T09:57:41.280645Z","iopub.status.idle":"2025-01-16T09:57:49.523963Z","shell.execute_reply.started":"2025-01-16T09:57:41.28061Z","shell.execute_reply":"2025-01-16T09:57:49.523109Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from transformers import AutoModelForCausalLM, AutoTokenizer\nmodel_id = \".\"\nfilename = \"phi-4-q4_k_m.gguf\"\n\ntokenizer = AutoTokenizer.from_pretrained(model_id, gguf_file=filename,torch_dtype=\"auto\",\n    device_map=\"auto\", legacy=False)\nmodel = AutoModelForCausalLM.from_pretrained(model_id, gguf_file=filename)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import re\n\ndef clean_artifacts(text):\n    text = text.replace(\"Ġ\", \" \")  # Replace tokenization artifacts\n    text = re.sub(r\"<\\|.*?\\|>\", \"\", text)  # Remove special tokens like <|im_end|>\n    return text.strip()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T10:05:00.906318Z","iopub.execute_input":"2025-01-16T10:05:00.907083Z","iopub.status.idle":"2025-01-16T10:05:00.910589Z","shell.execute_reply.started":"2025-01-16T10:05:00.90705Z","shell.execute_reply":"2025-01-16T10:05:00.909981Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\nprompt = \"2+2?\"\nmessages = [\n    {\"role\": \"system\", \"content\": \"You are Qwen, created by Alibaba Cloud. You are a helpful assistant.\"},\n    {\"role\": \"user\", \"content\": prompt}\n]\ntext = tokenizer.apply_chat_template(\n    messages,\n    tokenize=False,\n    add_generation_prompt=True\n)\nmodel_inputs = tokenizer([text], return_tensors=\"pt\").to(model.device)\n\ngenerated_ids = model.generate(\n    **model_inputs,\n    max_new_tokens=256\n)\ngenerated_ids = [\n    output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)\n]\n\nresponse = tokenizer.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)[0]\ndisplay(clean_artifacts(response))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T10:05:51.858449Z","iopub.execute_input":"2025-01-16T10:05:51.858754Z","iopub.status.idle":"2025-01-16T10:06:08.394535Z","shell.execute_reply.started":"2025-01-16T10:05:51.858726Z","shell.execute_reply":"2025-01-16T10:06:08.393787Z"}},"outputs":[],"execution_count":null}]}