chore: import upstream snapshot with attribution
Code Quality / Python Lint & Format (push) Has been cancelled
Code Quality / Python Tests (push) Has been cancelled
Code Quality / JavaScript/TypeScript Lint (advisory) (push) Has been cancelled
Security Scan / CodeQL Analysis (python) (push) Has been cancelled
Security Scan / Dependency Review (push) Has been cancelled
Security Scan / CodeQL Analysis (javascript-typescript) (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 13:43:57 +08:00
commit 3fbbd7970c
12165 changed files with 1331979 additions and 0 deletions
@@ -0,0 +1,397 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"import requests, base64"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"invoke_url = \"https://ai.api.nvidia.com/v1/vlm/microsoft/phi-3-vision-128k-instruct\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"with open(\"./img/demo.png\", \"rb\") as f:\n",
" image_b64 = base64.b64encode(f.read()).decode()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"len(image_b64)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"assert len(image_b64) < 180_000, \\\n",
" \"To upload larger images, use the assets API (see docs)\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"stream = False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"headers = {\n",
" \"Authorization\": \"Bearer Your Nvidia NIM API Key\",\n",
" \"Accept\": \"text/event-stream\" if stream else \"application/json\"\n",
"}\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"# payload = {\n",
"# \"messages\": [\n",
"# {\n",
"# \"role\": \"user\",\n",
"# \"content\": f'Tell me what is in the picture <img src=\"data:image/png;base64,{image_b64}\" />'\n",
"# }\n",
"# ],\n",
"# \"max_tokens\": 1024,\n",
"# \"temperature\": 0.6,\n",
"# \"top_p\": 1.0,\n",
"# \"stream\": stream\n",
"# }\n",
"\n",
"payload = {\n",
" \"messages\": [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": f'Please create Python code for image, and use plt to save the new picture under imgs/ and name it phi-3-vision.jpg. <img src=\"data:image/png;base64,{image_b64}\" />'\n",
" }\n",
" ],\n",
" \"max_tokens\": 1024,\n",
" \"temperature\": 0.6,\n",
" \"top_p\": 1.0,\n",
" \"stream\": stream\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"response = requests.post(invoke_url, headers=headers, json=payload)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"if stream:\n",
" for line in response.iter_lines():\n",
" if line:\n",
" print(line.decode(\"utf-8\"))\n",
"else:\n",
" print(response.json())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"code = response.json()[\"choices\"][0][\"message\"][\"content\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"# Find the starting index of the Python code block within the generated code\n",
"# The index function locates the substring '```python' and adds 9 to skip past it\n",
"begin = code.index('```python') + 9\n",
"\n",
"# Slice the code string from the calculated starting index to the end\n",
"# This removes the initial part of the string up to and including '```python'\n",
"code = code[begin:]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"end = code.index('```')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"code = code[:end]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"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.11.8"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
+269
View File
@@ -0,0 +1,269 @@
{
"cells": [
{
"cell_type": "code",
"source": [
"import torch \n",
"from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline "
],
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": "2024-08-20 03:12:24.098444: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory\n2024-08-20 03:12:24.098474: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.\n"
}
],
"execution_count": 1,
"metadata": {
"gather": {
"logged": 1724123553820
}
}
},
{
"cell_type": "code",
"source": [
"torch.random.manual_seed(0) \n",
"model = AutoModelForCausalLM.from_pretrained( \n",
" \"../phi-3-instruct\", \n",
" device_map=\"cuda\", \n",
" torch_dtype=\"auto\", \n",
" trust_remote_code=True, \n",
") "
],
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]",
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "c29888138a4f4714b7f65112733da2d2"
}
},
"metadata": {}
}
],
"execution_count": 2,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724123630099
}
}
},
{
"cell_type": "code",
"source": [
"tokenizer = AutoTokenizer.from_pretrained(\"../phi-3-instruct\") "
],
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": "You are using the default legacy behaviour of the <class 'transformers.models.llama.tokenization_llama_fast.LlamaTokenizerFast'>. This is expected, and simply means that the `legacy` (previous) behavior will be used so nothing changes for you. If you want to use the new behaviour, set `legacy=False`. This should only be set if you understand what it means, and thoroughly read the reason why this was added as explained in https://github.com/huggingface/transformers/pull/24565 - if you loaded a llama tokenizer from a GGUF file you can ignore this message.\n"
}
],
"execution_count": 3,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724123630234
}
}
},
{
"cell_type": "code",
"source": [
"# messages = [ \n",
"# {\"role\": \"system\", \"content\": \"你是我的人工智能助手,协助我用中文解答问题\"}, \n",
"# {\"role\": \"user\", \"content\": \"你能否介绍一下如何提升学习效率吗?\"}, \n",
"# ] \n",
"\n",
"# <|system|>\n",
"# You are a helpful assistant.<|end|>\n",
"# <|user|>\n",
"# Question?<|end|>\n",
"# <|assistant|>\n",
"\n",
"messages = \"<|system|>\\n 你是我的人工智能助手,协助我用中文解答问题.\\n<|end|><|user|>\\n 你知道长沙吗?? \\n<|end|><|assistant|>\"\n"
],
"outputs": [],
"execution_count": 9,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724123692174
}
}
},
{
"cell_type": "code",
"source": [
"pipe = pipeline( \n",
" \"text-generation\", \n",
" model=model, \n",
" tokenizer=tokenizer, \n",
") "
],
"outputs": [],
"execution_count": 10,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724123693760
}
}
},
{
"cell_type": "code",
"source": [
"generation_args = { \n",
" \"max_new_tokens\": 1024, \n",
" \"return_full_text\": False, \n",
" \"temperature\": 0.3, \n",
" \"do_sample\": False, \n",
"} "
],
"outputs": [],
"execution_count": 11,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724123695747
}
}
},
{
"cell_type": "code",
"source": [
"output = pipe(messages, **generation_args) "
],
"outputs": [],
"execution_count": 12,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724123704229
}
}
},
{
"cell_type": "code",
"source": [
"output[0]['generated_text']"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "' 是的,我知道长沙。长沙是中国南部的一座大城市,位于湖南省中部,是中国的国际性大都市。它拥有丰富的历史和文化,曾是明朝的都城。长沙以其繁华的商业、独特的自然风光和丰富的历史遗迹而闻名,如橘子洲、岳麓山和长沙博物院。此外,长沙也是中国重要的科技和教育中心,包括中国工程院和中国科学院长沙分院。'"
},
"metadata": {}
}
],
"execution_count": 8,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724123639263
}
}
}
],
"metadata": {
"kernelspec": {
"name": "python38-azureml",
"language": "python",
"display_name": "Python 3.8 - AzureML"
},
"language_info": {
"name": "python",
"version": "3.9.19",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"microsoft": {
"ms_spell_check": {
"ms_spell_check_language": "en"
},
"host": {
"AzureML": {
"notebookHasBeenCompleted": true
}
}
},
"kernel_info": {
"name": "python38-azureml"
},
"nteract": {
"version": "nteract-front-end@1.0.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
+621
View File
@@ -0,0 +1,621 @@
{
"cells": [
{
"cell_type": "code",
"source": [
"# pip install opencv-python"
],
"outputs": [],
"execution_count": 1,
"metadata": {
"gather": {
"logged": 1724068852431
}
}
},
{
"cell_type": "code",
"source": [
"# import cv2\n",
"# import numpy as np"
],
"outputs": [],
"execution_count": 2,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724068852519
}
}
},
{
"cell_type": "code",
"source": [
"# def save_keyframes(video_path, output_folder):\n",
"# videoCapture = cv2.VideoCapture(video_path)\n",
"# success, frame = videoCapture.read()\n",
"# i = 0\n",
"# while success:\n",
"# gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n",
" \n",
"# hist = cv2.calcHist([gray_frame], [0], None, [256], [0, 256])\n",
" \n",
"# success, next_frame = videoCapture.read()\n",
"# if not success:\n",
"# break\n",
" \n",
"# next_gray_frame = cv2.cvtColor(next_frame, cv2.COLOR_BGR2GRAY)\n",
" \n",
"# next_hist = cv2.calcHist([next_gray_frame], [0], None, [256], [0, 256])\n",
" \n",
"# similarity = cv2.compareHist(hist, next_hist, cv2.HISTCMP_CORREL)\n",
" \n",
"# if similarity < 0.9:\n",
"# i += 1\n",
"# cv2.imwrite(f\"{output_folder}/keyframe_{i}.jpg\", frame)\n",
"# print(f\"Saved keyframe {i}\")\n",
" \n",
"# frame = next_frame\n",
"\n",
"# videoCapture.release()"
],
"outputs": [],
"execution_count": 3,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724068852581
}
}
},
{
"cell_type": "code",
"source": [
"# save_keyframes('../video/copilot.mp4', '../output')"
],
"outputs": [],
"execution_count": 4,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724068852723
}
}
},
{
"cell_type": "code",
"source": [
"from PIL import Image\n",
"import requests, base64"
],
"outputs": [],
"execution_count": 5,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724068852785
}
}
},
{
"cell_type": "code",
"source": [
"images = [] \n",
"placeholder = \"\" \n",
"for i in range(1,22): \n",
" with open(\"../output/keyframe_\"+str(i)+\".jpg\", \"rb\") as f:\n",
"\n",
" images.append(Image.open(\"../output/keyframe_\"+str(i)+\".jpg\"))\n",
" placeholder += f\"<|image_{i}|>\\n\"\n",
" # print(i)"
],
"outputs": [],
"execution_count": 18,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724069071375
}
}
},
{
"cell_type": "code",
"source": [
"images"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 19,
"data": {
"text/plain": "[<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>,\n <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720>]"
},
"metadata": {}
}
],
"execution_count": 19,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724069078111
}
}
},
{
"cell_type": "code",
"source": [
"from transformers import AutoModelForCausalLM \n",
"from transformers import AutoProcessor"
],
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": "2024-08-19 12:00:55.508879: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory\n2024-08-19 12:00:55.508910: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.\n"
}
],
"execution_count": 8,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724068856588
}
}
},
{
"cell_type": "code",
"source": [
"model_id = \"../Phi3Vision\""
],
"outputs": [],
"execution_count": 9,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724068856676
}
}
},
{
"cell_type": "code",
"source": [
"model = AutoModelForCausalLM.from_pretrained(model_id, device_map=\"cuda\", trust_remote_code=True, torch_dtype=\"auto\", _attn_implementation='flash_attention_2')"
],
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]",
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "4e678ae3115b4d89bff5caf06c97933d"
}
},
"metadata": {}
}
],
"execution_count": 10,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724068938629
}
}
},
{
"cell_type": "code",
"source": [
"messages = [\n",
" {\"role\": \"user\", \"content\": placeholder+\"Summarize the video.\"}, \n",
"]"
],
"outputs": [],
"execution_count": 11,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724068938692
}
}
},
{
"cell_type": "code",
"source": [
"pip install transformers -U"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "Requirement already satisfied: transformers in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (4.44.0)\nRequirement already satisfied: safetensors>=0.4.1 in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (from transformers) (0.4.3)\nRequirement already satisfied: requests in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (from transformers) (2.31.0)\nRequirement already satisfied: pyyaml>=5.1 in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (from transformers) (6.0.1)\nRequirement already satisfied: huggingface-hub<1.0,>=0.23.2 in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (from transformers) (0.24.5)\nRequirement already satisfied: filelock in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (from transformers) (3.14.0)\nRequirement already satisfied: tqdm>=4.27 in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (from transformers) (4.66.2)\nRequirement already satisfied: regex!=2019.12.17 in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (from transformers) (2024.4.28)\nRequirement already satisfied: packaging>=20.0 in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (from transformers) (24.0)\nRequirement already satisfied: numpy>=1.17 in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (from transformers) (1.23.5)\nRequirement already satisfied: tokenizers<0.20,>=0.19 in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (from transformers) (0.19.1)\nRequirement already satisfied: typing-extensions>=3.7.4.3 in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (from huggingface-hub<1.0,>=0.23.2->transformers) (4.12.2)\nRequirement already satisfied: fsspec>=2023.5.0 in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (from huggingface-hub<1.0,>=0.23.2->transformers) (2023.10.0)\nRequirement already satisfied: certifi>=2017.4.17 in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (from requests->transformers) (2022.9.24)\nRequirement already satisfied: charset-normalizer<4,>=2 in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (from requests->transformers) (3.3.2)\nRequirement already satisfied: idna<4,>=2.5 in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (from requests->transformers) (3.7)\nRequirement already satisfied: urllib3<3,>=1.21.1 in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (from requests->transformers) (1.26.18)\nNote: you may need to restart the kernel to use updated packages.\n"
}
],
"execution_count": 12,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724068942055
}
}
},
{
"cell_type": "code",
"source": [
"from transformers import AutoModelForCausalLM \n",
"from transformers import AutoProcessor\n",
"\n",
"\n",
"# from image_embedding_phi3_v import Phi3VImageProcessor \n",
"\n",
"# transformers.Phi3VImageProcessor = Phi3VImageProcessor "
],
"outputs": [],
"execution_count": 13,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724068942117
}
}
},
{
"cell_type": "code",
"source": [
"processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True, num_crops=4)"
],
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": "/anaconda/envs/azureml_py38/lib/python3.9/site-packages/transformers/models/auto/image_processing_auto.py:513: FutureWarning: The image_processor_class argument is deprecated and will be removed in v4.42. Please use `slow_image_processor_class`, or `fast_image_processor_class` instead\n warnings.warn(\n"
}
],
"execution_count": 14,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724068942746
}
}
},
{
"cell_type": "code",
"source": [
"pip install jinja2 -U"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "Requirement already satisfied: jinja2 in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (3.1.4)\nRequirement already satisfied: MarkupSafe>=2.0 in /anaconda/envs/azureml_py38/lib/python3.9/site-packages (from jinja2) (2.0.1)\nNote: you may need to restart the kernel to use updated packages.\n"
}
],
"execution_count": 15,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724068946299
}
}
},
{
"cell_type": "code",
"source": [
"prompt = processor.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)"
],
"outputs": [],
"execution_count": 16,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724068946385
}
}
},
{
"cell_type": "code",
"source": [
"inputs = processor(prompt, images, return_tensors=\"pt\").to(\"cuda:0\")"
],
"outputs": [],
"execution_count": 20,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724069088259
}
}
},
{
"cell_type": "code",
"source": [
"generation_args = { \"max_new_tokens\": 1000, \"temperature\": 0.0, \"do_sample\": False, }"
],
"outputs": [],
"execution_count": 21,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724069108248
}
}
},
{
"cell_type": "code",
"source": [
"generate_ids = model.generate(**inputs, eos_token_id=processor.tokenizer.eos_token_id, **generation_args)"
],
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": "/anaconda/envs/azureml_py38/lib/python3.9/site-packages/transformers/generation/configuration_utils.py:567: UserWarning: `do_sample` is set to `False`. However, `temperature` is set to `0.0` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `temperature`.\n warnings.warn(\nThe `seen_tokens` attribute is deprecated and will be removed in v4.41. Use the `cache_position` model input instead.\n"
}
],
"execution_count": 22,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724069135074
}
}
},
{
"cell_type": "code",
"source": [
"generate_ids = generate_ids[:, inputs['input_ids'].shape[1]:]"
],
"outputs": [],
"execution_count": 23,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724069138113
}
}
},
{
"cell_type": "code",
"source": [
"response = processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]"
],
"outputs": [],
"execution_count": 24,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724069148861
}
}
},
{
"cell_type": "code",
"source": [
"response"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 25,
"data": {
"text/plain": "\"The video appears to be a promotional or informational piece about a product or service called 'Copilot'. It showcases various individuals in different settings, such as an office and a home office, engaging with the product. The video includes text overlays that suggest the product can help with tasks like lowering production costs, managing meetings, and creating presentations. There are also screenshots of a chat interface and a summary of a product launch discussion, indicating that the product may be related to project management or collaboration. The video seems to emphasize the efficiency and effectiveness of the Copilot product in a professional context.\""
},
"metadata": {}
}
],
"execution_count": 25,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724069154901
}
}
}
],
"metadata": {
"kernelspec": {
"name": "python38-azureml",
"language": "python",
"display_name": "Python 3.8 - AzureML"
},
"language_info": {
"name": "python",
"version": "3.9.19",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"microsoft": {
"ms_spell_check": {
"ms_spell_check_language": "en"
},
"host": {
"AzureML": {
"notebookHasBeenCompleted": true
}
}
},
"kernel_info": {
"name": "python38-azureml"
},
"nteract": {
"version": "nteract-front-end@1.0.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
+609
View File
@@ -0,0 +1,609 @@
{
"cells": [
{
"cell_type": "code",
"source": [
"# pip install transformers"
],
"outputs": [],
"execution_count": 1,
"metadata": {
"gather": {
"logged": 1724129002162
}
}
},
{
"cell_type": "code",
"source": [
"# pip install torch torchvision torchaudio -U"
],
"outputs": [],
"execution_count": 2,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724129002227
}
}
},
{
"cell_type": "code",
"source": [
"# pip install flash-attn --no-build-isolation"
],
"outputs": [],
"execution_count": 3,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724129002308
}
}
},
{
"cell_type": "code",
"source": [
"# ! pip install flash_attn -U"
],
"outputs": [],
"execution_count": 4,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724129002392
}
}
},
{
"cell_type": "code",
"source": [
"from torch import bfloat16\n",
"import transformers"
],
"outputs": [],
"execution_count": 5,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724129010695
}
}
},
{
"cell_type": "code",
"source": [
"model_id = \"../Phi3MOE\""
],
"outputs": [],
"execution_count": 6,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724129010747
}
}
},
{
"cell_type": "code",
"source": [
"model = transformers.AutoModelForCausalLM.from_pretrained(\n",
" model_id,\n",
" trust_remote_code=True,\n",
" torch_dtype=bfloat16,\n",
" device_map='auto'\n",
")\n"
],
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "Loading checkpoint shards: 0%| | 0/17 [00:00<?, ?it/s]",
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "5cd80a487bad430ba0455753bdeff5fa"
}
},
"metadata": {}
},
{
"output_type": "stream",
"name": "stderr",
"text": "WARNING:root:Some parameters are on the meta device device because they were offloaded to the cpu.\n"
}
],
"execution_count": 7,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724129511030
}
}
},
{
"cell_type": "code",
"source": [
"model.eval()"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "PhiMoEForCausalLM(\n (model): PhiMoEModel(\n (embed_tokens): Embedding(32064, 4096)\n (layers): ModuleList(\n (0-31): 32 x PhiMoEDecoderLayer(\n (self_attn): PhiMoESdpaAttention(\n (q_proj): Linear(in_features=4096, out_features=4096, bias=True)\n (k_proj): Linear(in_features=4096, out_features=1024, bias=True)\n (v_proj): Linear(in_features=4096, out_features=1024, bias=True)\n (o_proj): Linear(in_features=4096, out_features=4096, bias=True)\n (rotary_emb): Phi3LongRoPEScaledRotaryEmbedding()\n )\n (block_sparse_moe): PhiMoESparseMoeBlock(\n (gate): Linear(in_features=4096, out_features=16, bias=False)\n (experts): ModuleList(\n (0-15): 16 x PhiMoEBlockSparseTop2MLP(\n (w1): Linear(in_features=4096, out_features=6400, bias=False)\n (w2): Linear(in_features=6400, out_features=4096, bias=False)\n (w3): Linear(in_features=4096, out_features=6400, bias=False)\n (act_fn): SiLU()\n )\n )\n )\n (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n )\n )\n (norm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n )\n (lm_head): Linear(in_features=4096, out_features=32064, bias=True)\n)"
},
"metadata": {}
}
],
"execution_count": 8,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724129511125
}
}
},
{
"cell_type": "code",
"source": [
"tokenizer = transformers.AutoTokenizer.from_pretrained(model_id)"
],
"outputs": [],
"execution_count": 9,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724129511238
}
}
},
{
"cell_type": "code",
"source": [
"# generate_text = transformers.pipeline(\n",
"# model=model, tokenizer=tokenizer,\n",
"# return_full_text=False, # if using langchain set True\n",
"# task=\"text-generation\",\n",
"# # we pass model parameters here too\n",
"# temperature=0.1, # 'randomness' of outputs, 0.0 is the min and 1.0 the max\n",
"# top_p=0.15, # select from top tokens whose probability add up to 15%\n",
"# top_k=0, # select from top 0 tokens (because zero, relies on top_p)\n",
"# max_new_tokens=2048, # max number of tokens to generate in the output\n",
"# repetition_penalty=1.1 # if output begins repeating increase\n",
"# )\n",
"\n",
"pipe = transformers.pipeline(\n",
"\n",
" \"text-generation\",\n",
"\n",
" model=model,\n",
"\n",
" tokenizer=tokenizer,\n",
"\n",
")"
],
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": "2024-08-20 04:51:54.431510: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory\n2024-08-20 04:51:54.431543: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.\nThe model 'PhiMoEForCausalLM' is not supported for text-generation. Supported models are ['BartForCausalLM', 'BertLMHeadModel', 'BertGenerationDecoder', 'BigBirdForCausalLM', 'BigBirdPegasusForCausalLM', 'BioGptForCausalLM', 'BlenderbotForCausalLM', 'BlenderbotSmallForCausalLM', 'BloomForCausalLM', 'CamembertForCausalLM', 'LlamaForCausalLM', 'CodeGenForCausalLM', 'CohereForCausalLM', 'CpmAntForCausalLM', 'CTRLLMHeadModel', 'Data2VecTextForCausalLM', 'DbrxForCausalLM', 'ElectraForCausalLM', 'ErnieForCausalLM', 'FalconForCausalLM', 'FuyuForCausalLM', 'GemmaForCausalLM', 'Gemma2ForCausalLM', 'GitForCausalLM', 'GPT2LMHeadModel', 'GPT2LMHeadModel', 'GPTBigCodeForCausalLM', 'GPTNeoForCausalLM', 'GPTNeoXForCausalLM', 'GPTNeoXJapaneseForCausalLM', 'GPTJForCausalLM', 'JambaForCausalLM', 'JetMoeForCausalLM', 'LlamaForCausalLM', 'MambaForCausalLM', 'Mamba2ForCausalLM', 'MarianForCausalLM', 'MBartForCausalLM', 'MegaForCausalLM', 'MegatronBertForCausalLM', 'MistralForCausalLM', 'MixtralForCausalLM', 'MptForCausalLM', 'MusicgenForCausalLM', 'MusicgenMelodyForCausalLM', 'MvpForCausalLM', 'NemotronForCausalLM', 'OlmoForCausalLM', 'OpenLlamaForCausalLM', 'OpenAIGPTLMHeadModel', 'OPTForCausalLM', 'PegasusForCausalLM', 'PersimmonForCausalLM', 'PhiForCausalLM', 'Phi3ForCausalLM', 'PLBartForCausalLM', 'ProphetNetForCausalLM', 'QDQBertLMHeadModel', 'Qwen2ForCausalLM', 'Qwen2MoeForCausalLM', 'RecurrentGemmaForCausalLM', 'ReformerModelWithLMHead', 'RemBertForCausalLM', 'RobertaForCausalLM', 'RobertaPreLayerNormForCausalLM', 'RoCBertForCausalLM', 'RoFormerForCausalLM', 'RwkvForCausalLM', 'Speech2Text2ForCausalLM', 'StableLmForCausalLM', 'Starcoder2ForCausalLM', 'TransfoXLLMHeadModel', 'TrOCRForCausalLM', 'WhisperForCausalLM', 'XGLMForCausalLM', 'XLMWithLMHeadModel', 'XLMProphetNetForCausalLM', 'XLMRobertaForCausalLM', 'XLMRobertaXLForCausalLM', 'XLNetLMHeadModel', 'XmodForCausalLM'].\n"
}
],
"execution_count": 10,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724129523437
}
}
},
{
"cell_type": "code",
"source": [
"generation_args = {\n",
"\n",
" \"max_new_tokens\": 512,\n",
"\n",
" \"return_full_text\": False,\n",
"\n",
" \"temperature\": 0.3,\n",
"\n",
" \"do_sample\": False,\n",
"\n",
"}"
],
"outputs": [],
"execution_count": 11,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724129523505
}
}
},
{
"cell_type": "code",
"source": [
"sys_msg = \"\"\"You are a helpful AI assistant, you are an agent capable of using a variety of tools to answer a question. Here are a few of the tools available to you:\n",
"\n",
"- Blog: This tool helps you describe a certain knowledge point and content, and finally write it into Twitter or Facebook style content\n",
"- Translate: This is a tool that helps you translate into any language, using plain language as required\n",
"\n",
"To use these tools you must always respond in JSON format containing `\"tool_name\"` and `\"input\"` key-value pairs. For example, to answer the question, \"Build Muliti Agents with MOE models\" you must use the calculator tool like so:\n",
"\n",
"```json\n",
"\n",
"{\n",
" \"tool_name\": \"Blog\",\n",
" \"input\": \"Build Muliti Agents with MOE models\"\n",
"}\n",
"\n",
"```\n",
"\n",
"Or to translate the question \"can you introduce yourself in Chinese\" you must respond:\n",
"\n",
"```json\n",
"\n",
"{\n",
" \"tool_name\": \"Search\",\n",
" \"input\": \"can you introduce yourself in Chinese\"\n",
"}\n",
"\n",
"```\n",
"\n",
"Remember just output the final result, output in JSON format containing `\"agentid\"`,`\"tool_name\"` , `\"input\"` and `\"output\"` key-value pairs .:\n",
"\n",
"```json\n",
"\n",
"[\n",
"\n",
"\n",
"{ \"agentid\": \"step1\",\n",
" \"tool_name\": \"Blog\",\n",
" \"input\": \"Build Muliti Agents with MOE models\",\n",
" \"output\": \".........\"\n",
"},\n",
"\n",
"{ \"agentid\": \"step2\",\n",
" \"tool_name\": \"Search\",\n",
" \"input\": \"can you introduce yourself in Chinese\",\n",
" \"output\": \".........\"\n",
"},\n",
"{\n",
" \"agentid\": \"final\"\n",
" \"tool_name\": \"Result\",\n",
" \"output\": \".........\"\n",
"}\n",
"]\n",
"\n",
"```\n",
"\n",
"The users answer is as follows.\n",
"\"\"\""
],
"outputs": [],
"execution_count": 12,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724129523768
}
}
},
{
"cell_type": "code",
"source": [
"def instruction_format(sys_message: str, query: str):\n",
" # note, don't \"</s>\" to the end\n",
" return f'<|system|> {sys_message} <|end|>\\n<|user|> {query} <|end|>\\n<|assistant|>'"
],
"outputs": [],
"execution_count": 13,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724129523822
}
}
},
{
"cell_type": "code",
"source": [
"query ='Write something about Generative AI with MOE , translate it to Chinese'"
],
"outputs": [],
"execution_count": 14,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724129523875
}
}
},
{
"cell_type": "code",
"source": [
"input_prompt = instruction_format(sys_msg, query)\n",
"\n"
],
"outputs": [],
"execution_count": 15,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724129523921
}
}
},
{
"cell_type": "code",
"source": [
"input_prompt"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 16,
"data": {
"text/plain": "'<|system|> You are a helpful AI assistant, you are an agent capable of using a variety of tools to answer a question. Here are a few of the tools available to you:\\n\\n- Blog: This tool helps you describe a certain knowledge point and content, and finally write it into Twitter or Facebook style content\\n- Translate: This is a tool that helps you translate into any language, using plain language as required\\n\\nTo use these tools you must always respond in JSON format containing `\"tool_name\"` and `\"input\"` key-value pairs. For example, to answer the question, \"Build Muliti Agents with MOE models\" you must use the calculator tool like so:\\n\\n```json\\n\\n{\\n \"tool_name\": \"Blog\",\\n \"input\": \"Build Muliti Agents with MOE models\"\\n}\\n\\n```\\n\\nOr to translate the question \"can you introduce yourself in Chinese\" you must respond:\\n\\n```json\\n\\n{\\n \"tool_name\": \"Search\",\\n \"input\": \"can you introduce yourself in Chinese\"\\n}\\n\\n```\\n\\nRemember just output the final result, output in JSON format containing `\"agentid\"`,`\"tool_name\"` , `\"input\"` and `\"output\"` key-value pairs .:\\n\\n```json\\n\\n[\\n\\n\\n{ \"agentid\": \"step1\",\\n \"tool_name\": \"Blog\",\\n \"input\": \"Build Muliti Agents with MOE models\",\\n \"output\": \".........\"\\n},\\n\\n{ \"agentid\": \"step2\",\\n \"tool_name\": \"Search\",\\n \"input\": \"can you introduce yourself in Chinese\",\\n \"output\": \".........\"\\n},\\n{\\n \"agentid\": \"final\"\\n \"tool_name\": \"Result\",\\n \"output\": \".........\"\\n}\\n]\\n\\n```\\n\\nThe users answer is as follows.\\n <|end|>\\n<|user|> Write something about Generative AI with MOE , translate it to Chinese <|end|>\\n<|assistant|>'"
},
"metadata": {}
}
],
"execution_count": 16,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724129523983
}
}
},
{
"cell_type": "code",
"source": [
"import torch\n",
"\n",
"torch.cuda.empty_cache() \n",
"\n",
"import os\n",
"\n",
"os.environ[\"PYTORCH_CUDA_ALLOC_CONF\"] = \"expandable_segments:True \""
],
"outputs": [],
"execution_count": 17,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724129524034
}
}
},
{
"cell_type": "code",
"source": [
"# res = generate_text(input_prompt)\n",
"\n",
"output = pipe(input_prompt, **generation_args)"
],
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": "/anaconda/envs/azureml_py38/lib/python3.9/site-packages/transformers/generation/configuration_utils.py:567: UserWarning: `do_sample` is set to `False`. However, `temperature` is set to `0.3` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `temperature`.\n warnings.warn(\nThe `seen_tokens` attribute is deprecated and will be removed in v4.41. Use the `cache_position` model input instead.\n"
}
],
"execution_count": 18,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724130076595
}
}
},
{
"cell_type": "code",
"source": [
"output[0]['generated_text']"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 19,
"data": {
"text/plain": "' ```json\\n\\n[\\n\\n{ \"agentid\": \"step1\",\\n \"tool_name\": \"Blog\",\\n \"input\": \"Generative AI with MOE\",\\n \"output\": \"Generative AI with MOE (Mixture of Experts) is a powerful approach that combines the strengths of generative models and the flexibility of MOE architecture. This hybrid model can generate high-quality, diverse, and contextually relevant content, making it suitable for various applications such as content creation, data augmentation, and more.\"\\n},\\n\\n{ \"agentid\": \"step2\",\\n \"tool_name\": \"Translate\",\\n \"input\": \"Generative AI with MOE is a powerful approach that combines the strengths of generative models and the flexibility of MOE architecture. This hybrid model can generate high-quality, diverse, and contextually relevant content, making it suitable for various applications such as content creation, data augmentation, and more.\",\\n \"output\": \"基于生成AI的MOEMixture of Experts)是一种强大的方法,它结合了生成模型的优势和MOE架构的灵活性。这种混合模型可以生成高质量、多样化且上下文相关的内容,使其适用于各种应用,如内容创建、数据增强等。\"\\n},\\n{\\n \"agentid\": \"final\",\\n \"tool_name\": \"Result\",\\n \"output\": \"基于生成AI的MOEMixture of Experts)是一种强大的方法,它结合了生成模型的优势和MOE架构的灵活性。这种混合模型可以生成高质量、多样化且上下文相关的内容,使其适用于各种应用,如内容创建、数据增强等。\"\\n}\\n]\\n```'"
},
"metadata": {}
}
],
"execution_count": 19,
"metadata": {
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"gather": {
"logged": 1724130076669
}
}
}
],
"metadata": {
"kernelspec": {
"name": "python38-azureml",
"language": "python",
"display_name": "Python 3.8 - AzureML"
},
"language_info": {
"name": "python",
"version": "3.9.19",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"microsoft": {
"ms_spell_check": {
"ms_spell_check_language": "en"
},
"host": {
"AzureML": {
"notebookHasBeenCompleted": true
}
}
},
"kernel_info": {
"name": "python38-azureml"
},
"nteract": {
"version": "nteract-front-end@1.0.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}