darkps commited on
Commit
dbd79e6
·
verified ·
1 Parent(s): 215e2ca

Upload notebook.ipynb

Browse files
Files changed (1) hide show
  1. notebook.ipynb +99 -90
notebook.ipynb CHANGED
@@ -1,93 +1,102 @@
1
  {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": null,
6
- "metadata": {},
7
- "outputs": [],
8
- "source": [
9
- "!pip install llama-cpp-python huggingface_hub --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cu124\n"
10
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  },
12
- {
13
- "cell_type": "code",
14
- "execution_count": null,
15
- "metadata": {},
16
- "outputs": [],
17
- "source": [
18
- "from huggingface_hub import HfApi\n",
19
- "from llama_cpp import Llama\n",
20
- "import os\n",
21
- "\n",
22
- "REPO_ID = \"darkps/darkit-v2.5-transformers\"\n",
23
- "api = HfApi()\n",
24
- "\n",
25
- "files = api.list_repo_files(REPO_ID)\n",
26
- "gguf_files = [f for f in files if f.endswith(\".gguf\")]\n",
27
- "\n",
28
- "for i, f in enumerate(gguf_files):\n",
29
- " print(f\"[{i}] {f}\")\n",
30
- "\n",
31
- "choice = int(input(\"Select model number: \"))\n",
32
- "filename = gguf_files[choice]\n",
33
- "\n",
34
- "llm = Llama.from_pretrained(\n",
35
- " repo_id=REPO_ID,\n",
36
- " filename=filename,\n",
37
- " n_ctx=2048,\n",
38
- " n_batch=128,\n",
39
- " n_ubatch=128,\n",
40
- " n_threads=os.cpu_count() or 4,\n",
41
- " n_threads_batch=os.cpu_count() or 4,\n",
42
- " n_gpu_layers=-1,\n",
43
- " verbose=False,\n",
44
- " no_perf=True,\n",
45
- ")\n"
46
- ]
47
- },
48
- {
49
- "cell_type": "code",
50
- "execution_count": null,
51
- "metadata": {},
52
- "outputs": [],
53
- "source": [
54
- "llm.set_cache(None)\n",
55
- "\n",
56
- "PROMPT = \"Hi how are you?\"\n",
57
- "\n",
58
- "stream = llm(\n",
59
- " f\"<|im_start|>user\\n{PROMPT}<|im_end|>\\n<|im_start|>assistant\\n\",\n",
60
- " max_tokens=128,\n",
61
- " temperature=0.7,\n",
62
- " top_p=0.8,\n",
63
- " top_k=20,\n",
64
- " stream=True,\n",
65
- " stop=[\n",
66
- " \"<|im_end|>\",\n",
67
- " \"<|im_start|>\",\n",
68
- " \"\\n\\nUser:\",\n",
69
- " \"\\n\\nAssistant:\"\n",
70
- " ],\n",
71
- " echo=False\n",
72
- ")\n",
73
- "\n",
74
- "for chunk in stream:\n",
75
- " text = chunk[\"choices\"][0][\"text\"]\n",
76
- "\n",
77
- " if text:\n",
78
- " print(text, end=\"\", flush=True)\n",
79
- "\n",
80
- "print()\n"
81
- ]
82
- }
83
- ],
84
- "metadata": {
85
- "kernelspec": {
86
- "display_name": "Python 3",
87
- "language": "python",
88
- "name": "python3"
89
- }
90
- },
91
- "nbformat": 4,
92
- "nbformat_minor": 0
93
  }
 
1
  {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": None,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "!pip install -U transformers accelerate huggingface_hub sentencepiece safetensors\n"
10
+ ],
11
+ },
12
+ {
13
+ "cell_type": "code",
14
+ "execution_count": None,
15
+ "metadata": {},
16
+ "outputs": [],
17
+ "source": [
18
+ "from huggingface_hub import HfApi\n",
19
+ "from transformers import AutoTokenizer, AutoModelForCausalLM\n",
20
+ "import torch\n",
21
+ "import os\n",
22
+ "\n",
23
+ "REPO_ID = \"darkps/darkit-v2.5-transformers\"\n",
24
+ "api = HfApi()\n",
25
+ "\n",
26
+ "files = api.list_repo_files(REPO_ID)\n",
27
+ "model_files = [f for f in files if f.endswith(\".safetensors\") or f.endswith(\".bin\")]\n",
28
+ "\n",
29
+ "for i, f in enumerate(model_files):\n",
30
+ " print(f\"[{i}] {f}\")\n",
31
+ "\n",
32
+ "tokenizer = AutoTokenizer.from_pretrained(REPO_ID, trust_remote_code=True)\n",
33
+ "\n",
34
+ "model = AutoModelForCausalLM.from_pretrained(\n",
35
+ " REPO_ID,\n",
36
+ " trust_remote_code=True,\n",
37
+ " torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,\n",
38
+ " device_map=\"auto\",\n",
39
+ " low_cpu_mem_usage=True,\n",
40
+ ")\n",
41
+ ],
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "execution_count": None,
46
+ "metadata": {},
47
+ "outputs": [],
48
+ "source": [
49
+ "from transformers import TextIteratorStreamer\n",
50
+ "from threading import Thread\n",
51
+ "\n",
52
+ "PROMPT = \"Hi how are you?\"\n",
53
+ "\n",
54
+ "messages = [\n",
55
+ " {\"role\": \"user\", \"content\": PROMPT}\n",
56
+ "]\n",
57
+ "\n",
58
+ "inputs = tokenizer.apply_chat_template(\n",
59
+ " messages,\n",
60
+ " tokenize=True,\n",
61
+ " add_generation_prompt=True,\n",
62
+ " return_tensors=\"pt\"\n",
63
+ ")\n",
64
+ "\n",
65
+ "inputs = inputs.to(model.device)\n",
66
+ "\n",
67
+ "streamer = TextIteratorStreamer(\n",
68
+ " tokenizer,\n",
69
+ " skip_prompt=True,\n",
70
+ " skip_special_tokens=True\n",
71
+ ")\n",
72
+ "\n",
73
+ "gen_kwargs = dict(\n",
74
+ " inputs,\n",
75
+ " max_new_tokens=128,\n",
76
+ " temperature=0.7,\n",
77
+ " top_p=0.8,\n",
78
+ " top_k=20,\n",
79
+ " do_sample=True,\n",
80
+ " streamer=streamer,\n",
81
+ ")\n",
82
+ "\n",
83
+ "thread = Thread(target=model.generate, kwargs=gen_kwargs)\n",
84
+ "thread.start()\n",
85
+ "\n",
86
+ "for text in streamer:\n",
87
+ " print(text, end=\"\", flush=True)\n",
88
+ "\n",
89
+ "print()\n",
90
+ ],
91
+ },
92
+ ],
93
+ "metadata": {
94
+ "kernelspec": {
95
+ "display_name": "Python 3",
96
+ "language": "python",
97
+ "name": "python3",
98
+ }
99
  },
100
+ "nbformat": 4,
101
+ "nbformat_minor": 0,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  }