File size: 3,158 Bytes
31debee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "!pip install transformers accelerate torch huggingface_hub\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from huggingface_hub import HfApi\n",
        "from transformers import AutoModelForCausalLM, AutoTokenizer\n",
        "import torch\n",
        "\n",
        "ИДЕНТИФИКАТОР_РЕПО = \"darkps/darkit-v1.5-transformers\"\n",
        "\n",
        "АПИ = HfApi()\n",
        "\n",
        "СПИСОК_ФАЙЛОВ = АПИ.list_repo_files(ИДЕНТИФИКАТОР_РЕПО)\n",
        "\n",
        "print(\"Файлы репозитория:\")\n",
        "for файл in СПИСОК_ФАЙЛОВ:\n",
        "    print(файл)\n",
        "\n",
        "ТОКЕНИЗАТОР = AutoTokenizer.from_pretrained(\n",
        "    ИДЕНТИФИКАТОР_РЕПО,\n",
        "    trust_remote_code=True\n",
        ")\n",
        "\n",
        "МОДЕЛЬ = AutoModelForCausalLM.from_pretrained(\n",
        "    ИДЕНТИФИКАТОР_РЕПО,\n",
        "    torch_dtype=torch.float16,\n",
        "    device_map=\"auto\",\n",
        "    trust_remote_code=True\n",
        ")\n",
        "\n",
        "МОДЕЛЬ.eval()\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "ЗАПРОС = \"Привет, как у тебя дела?\"\n",
        "\n",
        "СООБЩЕНИЯ = [\n",
        "    {\n",
        "        \"role\": \"user\",\n",
        "        \"content\": ЗАПРОС\n",
        "    }\n",
        "]\n",
        "\n",
        "ТЕКСТ = ТОКЕНИЗАТОР.apply_chat_template(\n",
        "    СООБЩЕНИЯ,\n",
        "    tokenize=False,\n",
        "    add_generation_prompt=True\n",
        ")\n",
        "\n",
        "ВХОДЫ = ТОКЕНИЗАТОР(\n",
        "    ТЕКСТ,\n",
        "    return_tensors=\"pt\"\n",
        ").to(МОДЕЛЬ.device)\n",
        "\n",
        "with torch.no_grad():\n",
        "    ОТВЕТЫ = МОДЕЛЬ.generate(\n",
        "        **ВХОДЫ,\n",
        "        max_new_tokens=128,\n",
        "        temperature=0.7,\n",
        "        top_p=0.8,\n",
        "        top_k=20,\n",
        "        do_sample=True,\n",
        "        eos_token_id=ТОКЕНИЗАТОР.eos_token_id\n",
        "    )\n",
        "\n",
        "ОТВЕТ = ТОКЕНИЗАТОР.decode(\n",
        "    ОТВЕТЫ[0][ВХОДЫ.input_ids.shape[-1]:],\n",
        "    skip_special_tokens=True\n",
        ")\n",
        "\n",
        "print(ОТВЕТ)\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}