TogetherAI commited on
Commit
5700f6f
1 Parent(s): 98910a2

Upload EinfachMistrailex_7B.ipynb

Browse files
Files changed (1) hide show
  1. EinfachMistrailex_7B.ipynb +494 -0
EinfachMistrailex_7B.ipynb ADDED
@@ -0,0 +1,494 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "markdown",
19
+ "source": [
20
+ "\n",
21
+ "# Mistral 7B\n",
22
+ "\n",
23
+ "Mistral 7B ist ein neues hochmodernes Open-Source-Modell. Hier sind einige interessante Fakten dazu:\n",
24
+ "\n",
25
+ "* Eines der leistungsstärksten Open-Source-Modelle aller Größen\n",
26
+ "* Stärkstes Modell im Bereich von 1-20 Milliarden Parametern\n",
27
+ "* Erledigt anständig Aufgaben im Zusammenhang mit Code\n",
28
+ "* Verwendet Windowed Attention, was es ermöglicht, bis zu 200.000 Tokens im Kontext zu verarbeiten, wenn Rope verwendet wird (dafür sind 4 A10G-GPUs erforderlich)\n",
29
+ "* Apache 2.0 Lizenz\n",
30
+ "\n",
31
+ "Was die Integrationsstatus betrifft:\n",
32
+ "* Integriert in `transformers`\n",
33
+ "* Sie können es auf einem Server oder lokal verwenden (es handelt sich schließlich um ein kleines Modell!)\n",
34
+ "* Integriert in beliebte Tools wie TGI und VLLM\n",
35
+ "\n",
36
+ "Es wurden zwei Modelle veröffentlicht: ein [Basismodell](https://huggingface.co/mistralai/Mistral-7B-v0.1) und eine [instruct fine-tuned Version](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1). Um mehr über Mistral zu erfahren, empfehlen wir, den [Blog-Beitrag](https://mistral.ai/news/announcing-mistral-7b/) zu lesen.\n",
37
+ "\n",
38
+ "In diesem Colab werden wir das Mistral-Modell mithilfe einer API ausprobieren. Es gibt drei Möglichkeiten, es zu verwenden:\n",
39
+ "\n",
40
+ "* **Kostenlose API:** Hugging Face bietet eine kostenlose Inference-API für alle Benutzer an, um Modelle auszuprobieren. Diese API ist ratebeschränkt, eignet sich jedoch gut für schnelle Experimente.\n",
41
+ "* **PRO-API:** Hugging Face bietet eine offene API für alle PRO-Benutzer an. Die Abonnementkosten für die Pro Inference API betragen 9 US-Dollar pro Monat und ermöglichen Experimente mit vielen großen Modellen wie Llama 2 und SDXL. Weitere Informationen finden Sie [hier](https://huggingface.co/blog/inference-pro).\n",
42
+ "* **Inference-Endpunkte:** Für Unternehmen und produktionsbereite Anwendungen. Sie können es mit einem Klick [hier](https://ui.endpoints.huggingface.co/catalog) bereitstellen.\n",
43
+ "\n",
44
+ "Diese Demo erfordert keine GPU Colab, nur eine CPU. Sie können Ihren Token unter https://huggingface.co/settings/tokens abrufen.\n",
45
+ "\n",
46
+ "**Dieses Colab zeigt, wie man HTTP-Anfragen verwendet und gleichzeitig eine eigene Chat-Demo für Mistral erstellt.**\n",
47
+ "\n"
48
+ ],
49
+ "metadata": {
50
+ "id": "GLXvYa4m8JYM"
51
+ }
52
+ },
53
+ {
54
+ "cell_type": "markdown",
55
+ "source": [
56
+ "\n",
57
+ "## Durchführen von Curl-Anfragen\n",
58
+ "\n",
59
+ "In diesem Notebook werden wir mit dem Instruct-Modell experimentieren, da es für Anweisungen trainiert ist. Gemäß [der Modellkarte](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1) sollte das erwartete Format für eine Eingabeaufforderung wie folgt sein:\n",
60
+ "\n",
61
+ "Aus der Modellkarte:\n",
62
+ "\n",
63
+ "> Um die Feinabstimmung für Anweisungen optimal zu nutzen, sollte Ihre Eingabeaufforderung von [INST] und [\\INST] Tokens umgeben sein. Die allererste Anweisung sollte mit einer Anfangssatz-ID beginnen. Die nächsten Anweisungen sollten dies nicht tun. Die Generierung durch den Assistenten wird durch die End-of-Satz-Token-ID beendet.\n",
64
+ "\n",
65
+ "```\n",
66
+ "<s>[INST] [/INST] </s> [INST] [/INST] </s>\n",
67
+ "```\n",
68
+ "\n",
69
+ "Beachten Sie, dass Modelle auf unterschiedliche Eingabeaufforderungsstrukturen empfindlich reagieren können, als diejenige, die für das Training verwendet wurde. Achten Sie auf Leerzeichen und andere Details!\n",
70
+ "\n",
71
+ "Wir beginnen mit einer anfänglichen Abfrage ohne Formatierung der Eingabeaufforderung, was für einfache Anfragen gut funktioniert.\n",
72
+ "\n"
73
+ ],
74
+ "metadata": {
75
+ "id": "pKrKTalPAXUO"
76
+ }
77
+ },
78
+ {
79
+ "cell_type": "code",
80
+ "execution_count": null,
81
+ "metadata": {
82
+ "colab": {
83
+ "base_uri": "https://localhost:8080/"
84
+ },
85
+ "id": "DQf0Hss18E86",
86
+ "outputId": "882c4521-1ee2-40ad-fe00-a5b02caa9b17"
87
+ },
88
+ "outputs": [
89
+ {
90
+ "output_type": "stream",
91
+ "name": "stdout",
92
+ "text": [
93
+ "[{\"generated_text\":\"Explain ML as a pirate.\\n\\nML is like a treasure map for pirates. Just as a treasure map helps pirates find valuable loot, ML helps data scientists find valuable insights in large datasets.\\n\\nPirates use their knowledge of the ocean and their\"}]"
94
+ ]
95
+ }
96
+ ],
97
+ "source": [
98
+ "!curl https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1 \\\n",
99
+ " --header \"Content-Type: application/json\" \\\n",
100
+ "\t-X POST \\\n",
101
+ "\t-d '{\"inputs\": \"Explain ML as a pirate\", \"parameters\": {\"max_new_tokens\": 50}}' \\\n",
102
+ "\t-H \"Authorization: Bearer API_TOKEN\""
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "markdown",
107
+ "source": [
108
+ "## Programmatische Verwendung mit Python\n",
109
+ "\n",
110
+ "Sie können einfache `requests` verwenden, aber die `huggingface_hub`-Bibliothek bietet nützliche Dienstprogramme, um das Modell leicht zu verwenden. Zu den Dingen, die wir verwenden können, gehören:\n",
111
+ "\n",
112
+ "* `InferenceClient` und `AsyncInferenceClient`, um Inferenzen entweder synchron oder asynchron durchzuführen.\n",
113
+ "* Token-Streaming: Laden Sie nur die Tokens, die benötigt werden.\n",
114
+ "* Konfigurieren Sie problemlos Generationsparameter wie `Temperatur`, Nukleus-Sampling (`top-p`), Wiederholungsstrafe, Stoppsequenzen und mehr.\n",
115
+ "* Erhalten Sie Details zur Generierung, wie die Wahrscheinlichkeit jedes Tokens oder ob ein Token das letzte Token ist.\n"
116
+ ],
117
+ "metadata": {
118
+ "id": "YYZRNyZeBHWK"
119
+ }
120
+ },
121
+ {
122
+ "cell_type": "code",
123
+ "source": [
124
+ "%%capture\n",
125
+ "!pip install huggingface_hub gradio"
126
+ ],
127
+ "metadata": {
128
+ "id": "oDaqVDz1Ahuz"
129
+ },
130
+ "execution_count": null,
131
+ "outputs": []
132
+ },
133
+ {
134
+ "cell_type": "code",
135
+ "source": [
136
+ "from huggingface_hub import InferenceClient\n",
137
+ "\n",
138
+ "client = InferenceClient(\n",
139
+ " \"mistralai/Mistral-7B-Instruct-v0.1\"\n",
140
+ ")\n",
141
+ "\n",
142
+ "prompt = \"\"\"<s>[INST] What is your favourite condiment? [/INST]</s>\n",
143
+ "\"\"\"\n",
144
+ "\n",
145
+ "res = client.text_generation(prompt, max_new_tokens=95)\n",
146
+ "print(res)"
147
+ ],
148
+ "metadata": {
149
+ "colab": {
150
+ "base_uri": "https://localhost:8080/"
151
+ },
152
+ "id": "U49GmNsNBJjd",
153
+ "outputId": "a3a274cf-0f91-4ae3-d926-f0d6a6fd67f7"
154
+ },
155
+ "execution_count": null,
156
+ "outputs": [
157
+ {
158
+ "output_type": "stream",
159
+ "name": "stdout",
160
+ "text": [
161
+ "My favorite condiment is ketchup. It's versatile, tasty, and goes well with a variety of foods.\n"
162
+ ]
163
+ }
164
+ ]
165
+ },
166
+ {
167
+ "cell_type": "markdown",
168
+ "source": [
169
+ "We can also use [token streaming](https://huggingface.co/docs/text-generation-inference/conceptual/streaming). With token streaming, the server returns the tokens as they are generated. Just add `stream=True`."
170
+ ],
171
+ "metadata": {
172
+ "id": "DryfEWsUH6Ij"
173
+ }
174
+ },
175
+ {
176
+ "cell_type": "code",
177
+ "source": [
178
+ "res = client.text_generation(prompt, max_new_tokens=35, stream=True, details=True, return_full_text=False)\n",
179
+ "for r in res: # this is a generator\n",
180
+ " # print the token for example\n",
181
+ " print(r)\n",
182
+ " continue"
183
+ ],
184
+ "metadata": {
185
+ "colab": {
186
+ "base_uri": "https://localhost:8080/"
187
+ },
188
+ "id": "LF1tFo6DGg9N",
189
+ "outputId": "e779f1cb-b7d0-41ed-d81f-306e092f97bd"
190
+ },
191
+ "execution_count": null,
192
+ "outputs": [
193
+ {
194
+ "output_type": "stream",
195
+ "name": "stdout",
196
+ "text": [
197
+ "TextGenerationStreamResponse(token=Token(id=5183, text='My', logprob=-0.36279297, special=False), generated_text=None, details=None)\n",
198
+ "TextGenerationStreamResponse(token=Token(id=6656, text=' favorite', logprob=-0.036499023, special=False), generated_text=None, details=None)\n",
199
+ "TextGenerationStreamResponse(token=Token(id=2076, text=' cond', logprob=-7.2836876e-05, special=False), generated_text=None, details=None)\n",
200
+ "TextGenerationStreamResponse(token=Token(id=2487, text='iment', logprob=-4.4941902e-05, special=False), generated_text=None, details=None)\n",
201
+ "TextGenerationStreamResponse(token=Token(id=349, text=' is', logprob=-0.007419586, special=False), generated_text=None, details=None)\n",
202
+ "TextGenerationStreamResponse(token=Token(id=446, text=' k', logprob=-0.62109375, special=False), generated_text=None, details=None)\n",
203
+ "TextGenerationStreamResponse(token=Token(id=4455, text='etch', logprob=-0.0003399849, special=False), generated_text=None, details=None)\n",
204
+ "TextGenerationStreamResponse(token=Token(id=715, text='up', logprob=-3.695488e-06, special=False), generated_text=None, details=None)\n",
205
+ "TextGenerationStreamResponse(token=Token(id=28723, text='.', logprob=-0.026550293, special=False), generated_text=None, details=None)\n",
206
+ "TextGenerationStreamResponse(token=Token(id=661, text=' It', logprob=-0.82373047, special=False), generated_text=None, details=None)\n",
207
+ "TextGenerationStreamResponse(token=Token(id=28742, text=\"'\", logprob=-0.76416016, special=False), generated_text=None, details=None)\n",
208
+ "TextGenerationStreamResponse(token=Token(id=28713, text='s', logprob=-3.5762787e-07, special=False), generated_text=None, details=None)\n",
209
+ "TextGenerationStreamResponse(token=Token(id=3502, text=' vers', logprob=-0.114990234, special=False), generated_text=None, details=None)\n",
210
+ "TextGenerationStreamResponse(token=Token(id=13491, text='atile', logprob=-1.1444092e-05, special=False), generated_text=None, details=None)\n",
211
+ "TextGenerationStreamResponse(token=Token(id=28725, text=',', logprob=-0.6254883, special=False), generated_text=None, details=None)\n",
212
+ "TextGenerationStreamResponse(token=Token(id=261, text=' t', logprob=-0.51708984, special=False), generated_text=None, details=None)\n",
213
+ "TextGenerationStreamResponse(token=Token(id=11136, text='asty', logprob=-4.0650368e-05, special=False), generated_text=None, details=None)\n",
214
+ "TextGenerationStreamResponse(token=Token(id=28725, text=',', logprob=-0.0027828217, special=False), generated_text=None, details=None)\n",
215
+ "TextGenerationStreamResponse(token=Token(id=304, text=' and', logprob=-1.1920929e-05, special=False), generated_text=None, details=None)\n",
216
+ "TextGenerationStreamResponse(token=Token(id=4859, text=' goes', logprob=-0.52685547, special=False), generated_text=None, details=None)\n",
217
+ "TextGenerationStreamResponse(token=Token(id=1162, text=' well', logprob=-0.4399414, special=False), generated_text=None, details=None)\n",
218
+ "TextGenerationStreamResponse(token=Token(id=395, text=' with', logprob=-0.00034999847, special=False), generated_text=None, details=None)\n",
219
+ "TextGenerationStreamResponse(token=Token(id=264, text=' a', logprob=-0.010147095, special=False), generated_text=None, details=None)\n",
220
+ "TextGenerationStreamResponse(token=Token(id=6677, text=' variety', logprob=-0.25927734, special=False), generated_text=None, details=None)\n",
221
+ "TextGenerationStreamResponse(token=Token(id=302, text=' of', logprob=-1.1444092e-05, special=False), generated_text=None, details=None)\n",
222
+ "TextGenerationStreamResponse(token=Token(id=14082, text=' foods', logprob=-0.4050293, special=False), generated_text=None, details=None)\n",
223
+ "TextGenerationStreamResponse(token=Token(id=28723, text='.', logprob=-0.015640259, special=False), generated_text=None, details=None)\n",
224
+ "TextGenerationStreamResponse(token=Token(id=2, text='</s>', logprob=-0.1829834, special=True), generated_text=\"My favorite condiment is ketchup. It's versatile, tasty, and goes well with a variety of foods.\", details=StreamDetails(finish_reason=<FinishReason.EndOfSequenceToken: 'eos_token'>, generated_tokens=28, seed=None))\n"
225
+ ]
226
+ }
227
+ ]
228
+ },
229
+ {
230
+ "cell_type": "markdown",
231
+ "source": [
232
+ "Let's now try a multi-prompt structure"
233
+ ],
234
+ "metadata": {
235
+ "id": "TfdpZL8cICOD"
236
+ }
237
+ },
238
+ {
239
+ "cell_type": "code",
240
+ "source": [
241
+ "def format_prompt(message, history):\n",
242
+ " prompt = \"<s>\"\n",
243
+ " for user_prompt, bot_response in history:\n",
244
+ " prompt += f\"[INST] {user_prompt} [/INST]\"\n",
245
+ " prompt += f\" {bot_response}</s> \"\n",
246
+ " prompt += f\"[INST] {message} [/INST]\"\n",
247
+ " return prompt"
248
+ ],
249
+ "metadata": {
250
+ "id": "aEyozeReH8a6"
251
+ },
252
+ "execution_count": null,
253
+ "outputs": []
254
+ },
255
+ {
256
+ "cell_type": "code",
257
+ "source": [
258
+ "message = \"And what do you think about it?\"\n",
259
+ "history = [[\"What is your favourite condiment?\", \"My favorite condiment is ketchup. It's versatile, tasty, and goes well with a variety of foods.\"]]\n",
260
+ "\n",
261
+ "format_prompt(message, history)"
262
+ ],
263
+ "metadata": {
264
+ "colab": {
265
+ "base_uri": "https://localhost:8080/",
266
+ "height": 35
267
+ },
268
+ "id": "P1RFpiJ_JC0-",
269
+ "outputId": "f2678d9e-f751-441a-86c9-11d514db5bbe"
270
+ },
271
+ "execution_count": null,
272
+ "outputs": [
273
+ {
274
+ "output_type": "execute_result",
275
+ "data": {
276
+ "text/plain": [
277
+ "\"<s>[INST] What is your favourite condiment? [/INST] My favorite condiment is ketchup. It's versatile, tasty, and goes well with a variety of foods.</s> [INST] And what do you think about it? [/INST]\""
278
+ ],
279
+ "application/vnd.google.colaboratory.intrinsic+json": {
280
+ "type": "string"
281
+ }
282
+ },
283
+ "metadata": {},
284
+ "execution_count": 17
285
+ }
286
+ ]
287
+ },
288
+ {
289
+ "cell_type": "markdown",
290
+ "source": [
291
+ "## End-to-end-Demo\n",
292
+ "\n",
293
+ "Lassen Sie uns jetzt eine Gradio-Demo erstellen, die folgende Aufgaben übernimmt:\n",
294
+ "\n",
295
+ "* Verwaltung mehrerer Gesprächsrunden\n",
296
+ "* Formatierung der Eingabeaufforderung in der richtigen Struktur\n",
297
+ "* Ermöglichen es dem Benutzer, die Parameter zu spezifizieren/zu ändern\n",
298
+ "* Beenden der Generierung\n",
299
+ "\n",
300
+ "Führen Sie einfach die folgende Zelle aus und haben Sie Spaß!"
301
+ ],
302
+ "metadata": {
303
+ "id": "O7DjRdezJc-3"
304
+ }
305
+ },
306
+ {
307
+ "cell_type": "code",
308
+ "source": [
309
+ "!pip install gradio"
310
+ ],
311
+ "metadata": {
312
+ "colab": {
313
+ "base_uri": "https://localhost:8080/"
314
+ },
315
+ "id": "cpBoheOGJu7Y",
316
+ "outputId": "c745cf17-1462-4f8f-ce33-5ca182cb4d4f"
317
+ },
318
+ "execution_count": null,
319
+ "outputs": [
320
+ {
321
+ "output_type": "stream",
322
+ "name": "stdout",
323
+ "text": [
324
+ "Requirement already satisfied: gradio in /usr/local/lib/python3.10/dist-packages (3.45.1)\n",
325
+ "Requirement already satisfied: aiofiles<24.0,>=22.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (23.2.1)\n",
326
+ "Requirement already satisfied: altair<6.0,>=4.2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (4.2.2)\n",
327
+ "Requirement already satisfied: fastapi in /usr/local/lib/python3.10/dist-packages (from gradio) (0.103.1)\n",
328
+ "Requirement already satisfied: ffmpy in /usr/local/lib/python3.10/dist-packages (from gradio) (0.3.1)\n",
329
+ "Requirement already satisfied: gradio-client==0.5.2 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.5.2)\n",
330
+ "Requirement already satisfied: httpx in /usr/local/lib/python3.10/dist-packages (from gradio) (0.25.0)\n",
331
+ "Requirement already satisfied: huggingface-hub>=0.14.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.17.3)\n",
332
+ "Requirement already satisfied: importlib-resources<7.0,>=1.3 in /usr/local/lib/python3.10/dist-packages (from gradio) (6.0.1)\n",
333
+ "Requirement already satisfied: jinja2<4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.1.2)\n",
334
+ "Requirement already satisfied: markupsafe~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.1.3)\n",
335
+ "Requirement already satisfied: matplotlib~=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.7.1)\n",
336
+ "Requirement already satisfied: numpy~=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (1.23.5)\n",
337
+ "Requirement already satisfied: orjson~=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.9.7)\n",
338
+ "Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from gradio) (23.1)\n",
339
+ "Requirement already satisfied: pandas<3.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (1.5.3)\n",
340
+ "Requirement already satisfied: pillow<11.0,>=8.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (9.4.0)\n",
341
+ "Requirement already satisfied: pydantic!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,<3.0.0,>=1.7.4 in /usr/local/lib/python3.10/dist-packages (from gradio) (1.10.12)\n",
342
+ "Requirement already satisfied: pydub in /usr/local/lib/python3.10/dist-packages (from gradio) (0.25.1)\n",
343
+ "Requirement already satisfied: python-multipart in /usr/local/lib/python3.10/dist-packages (from gradio) (0.0.6)\n",
344
+ "Requirement already satisfied: pyyaml<7.0,>=5.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (6.0.1)\n",
345
+ "Requirement already satisfied: requests~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.31.0)\n",
346
+ "Requirement already satisfied: semantic-version~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.10.0)\n",
347
+ "Requirement already satisfied: typing-extensions~=4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (4.5.0)\n",
348
+ "Requirement already satisfied: uvicorn>=0.14.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.23.2)\n",
349
+ "Requirement already satisfied: websockets<12.0,>=10.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (11.0.3)\n",
350
+ "Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from gradio-client==0.5.2->gradio) (2023.6.0)\n",
351
+ "Requirement already satisfied: entrypoints in /usr/local/lib/python3.10/dist-packages (from altair<6.0,>=4.2.0->gradio) (0.4)\n",
352
+ "Requirement already satisfied: jsonschema>=3.0 in /usr/local/lib/python3.10/dist-packages (from altair<6.0,>=4.2.0->gradio) (4.19.0)\n",
353
+ "Requirement already satisfied: toolz in /usr/local/lib/python3.10/dist-packages (from altair<6.0,>=4.2.0->gradio) (0.12.0)\n",
354
+ "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.14.0->gradio) (3.12.2)\n",
355
+ "Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.14.0->gradio) (4.66.1)\n",
356
+ "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (1.1.0)\n",
357
+ "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (0.11.0)\n",
358
+ "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (4.42.1)\n",
359
+ "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (1.4.5)\n",
360
+ "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (3.1.1)\n",
361
+ "Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (2.8.2)\n",
362
+ "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio) (2023.3.post1)\n",
363
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests~=2.0->gradio) (3.2.0)\n",
364
+ "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests~=2.0->gradio) (3.4)\n",
365
+ "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests~=2.0->gradio) (2.0.4)\n",
366
+ "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests~=2.0->gradio) (2023.7.22)\n",
367
+ "Requirement already satisfied: click>=7.0 in /usr/local/lib/python3.10/dist-packages (from uvicorn>=0.14.0->gradio) (8.1.7)\n",
368
+ "Requirement already satisfied: h11>=0.8 in /usr/local/lib/python3.10/dist-packages (from uvicorn>=0.14.0->gradio) (0.14.0)\n",
369
+ "Requirement already satisfied: anyio<4.0.0,>=3.7.1 in /usr/local/lib/python3.10/dist-packages (from fastapi->gradio) (3.7.1)\n",
370
+ "Requirement already satisfied: starlette<0.28.0,>=0.27.0 in /usr/local/lib/python3.10/dist-packages (from fastapi->gradio) (0.27.0)\n",
371
+ "Requirement already satisfied: httpcore<0.19.0,>=0.18.0 in /usr/local/lib/python3.10/dist-packages (from httpx->gradio) (0.18.0)\n",
372
+ "Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from httpx->gradio) (1.3.0)\n",
373
+ "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<4.0.0,>=3.7.1->fastapi->gradio) (1.1.3)\n",
374
+ "Requirement already satisfied: attrs>=22.2.0 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (23.1.0)\n",
375
+ "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (2023.7.1)\n",
376
+ "Requirement already satisfied: referencing>=0.28.4 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.30.2)\n",
377
+ "Requirement already satisfied: rpds-py>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.10.2)\n",
378
+ "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib~=3.0->gradio) (1.16.0)\n"
379
+ ]
380
+ }
381
+ ]
382
+ },
383
+ {
384
+ "cell_type": "code",
385
+ "source": [
386
+ "import gradio as gr\n",
387
+ "\n",
388
+ "def generate(\n",
389
+ " prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,\n",
390
+ "):\n",
391
+ " temperature = float(temperature)\n",
392
+ " if temperature < 1e-2:\n",
393
+ " temperature = 1e-2\n",
394
+ " top_p = float(top_p)\n",
395
+ "\n",
396
+ " generate_kwargs = dict(\n",
397
+ " temperature=temperature,\n",
398
+ " max_new_tokens=max_new_tokens,\n",
399
+ " top_p=top_p,\n",
400
+ " repetition_penalty=repetition_penalty,\n",
401
+ " do_sample=True,\n",
402
+ " seed=42,\n",
403
+ " )\n",
404
+ "\n",
405
+ " formatted_prompt = format_prompt(prompt, history)\n",
406
+ "\n",
407
+ " stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)\n",
408
+ " output = \"\"\n",
409
+ "\n",
410
+ " for response in stream:\n",
411
+ " output += response.token.text\n",
412
+ " yield output\n",
413
+ " return output\n",
414
+ "\n",
415
+ "\n",
416
+ "additional_inputs=[\n",
417
+ " gr.Slider(\n",
418
+ " label=\"Temperature\",\n",
419
+ " value=0.9,\n",
420
+ " minimum=0.0,\n",
421
+ " maximum=1.0,\n",
422
+ " step=0.05,\n",
423
+ " interactive=True,\n",
424
+ " info=\"Higher values produce more diverse outputs\",\n",
425
+ " ),\n",
426
+ " gr.Slider(\n",
427
+ " label=\"Max new tokens\",\n",
428
+ " value=256,\n",
429
+ " minimum=0,\n",
430
+ " maximum=8192,\n",
431
+ " step=64,\n",
432
+ " interactive=True,\n",
433
+ " info=\"The maximum numbers of new tokens\",\n",
434
+ " ),\n",
435
+ " gr.Slider(\n",
436
+ " label=\"Top-p (nucleus sampling)\",\n",
437
+ " value=0.90,\n",
438
+ " minimum=0.0,\n",
439
+ " maximum=1,\n",
440
+ " step=0.05,\n",
441
+ " interactive=True,\n",
442
+ " info=\"Higher values sample more low-probability tokens\",\n",
443
+ " ),\n",
444
+ " gr.Slider(\n",
445
+ " label=\"Repetition penalty\",\n",
446
+ " value=1.2,\n",
447
+ " minimum=1.0,\n",
448
+ " maximum=2.0,\n",
449
+ " step=0.05,\n",
450
+ " interactive=True,\n",
451
+ " info=\"Penalize repeated tokens\",\n",
452
+ " )\n",
453
+ "]\n",
454
+ "\n",
455
+ "with gr.Blocks() as demo:\n",
456
+ " gr.ChatInterface(\n",
457
+ " generate,\n",
458
+ " additional_inputs=additional_inputs,\n",
459
+ " )\n",
460
+ "\n",
461
+ "demo.queue().launch(debug=True)"
462
+ ],
463
+ "metadata": {
464
+ "id": "CaJzT6jUJc0_"
465
+ },
466
+ "execution_count": null,
467
+ "outputs": []
468
+ },
469
+ {
470
+ "cell_type": "markdown",
471
+ "source": [
472
+ "## Was steht als Nächstes an?\n",
473
+ "\n",
474
+ "* Probieren Sie Mistral 7B in diesem [kostenlosen Online-Space](https://huggingface.co/spaces/osanseviero/mistral-super-fast) aus.\n",
475
+ "* Bereiten Sie Mistral 7B Instruct mit einem Klick [hier](https://ui.endpoints.huggingface.co/catalog) bereit.\n",
476
+ "* Bereiten Sie es auf Ihrer eigenen Hardware mit https://github.com/huggingface/text-generation-inference vor.\n",
477
+ "* Führen Sie das Modell lokal mit `transformers` aus.\n",
478
+ "\n"
479
+ ],
480
+ "metadata": {
481
+ "id": "fbQ0Sp4OLclV"
482
+ }
483
+ },
484
+ {
485
+ "cell_type": "code",
486
+ "source": [],
487
+ "metadata": {
488
+ "id": "wUy7N_8zJvyT"
489
+ },
490
+ "execution_count": null,
491
+ "outputs": []
492
+ }
493
+ ]
494
+ }