shaik78 commited on
Commit
5fc4958
Β·
verified Β·
1 Parent(s): 3feab70

Upload 2 files

Browse files
Files changed (2) hide show
  1. text-summarizer (1).ipynb +1 -0
  2. whisper.ipynb +1 -0
text-summarizer (1).ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"cells":[{"cell_type":"code","execution_count":1,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T11:21:48.184387Z","iopub.status.busy":"2024-05-20T11:21:48.183665Z","iopub.status.idle":"2024-05-20T11:24:45.446401Z","shell.execute_reply":"2024-05-20T11:24:45.444922Z","shell.execute_reply.started":"2024-05-20T11:21:48.184353Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["Collecting transformers==4.33.0\n"," Using cached transformers-4.33.0-py3-none-any.whl.metadata (119 kB)\n","Collecting accelerate==0.22.0\n"," Using cached accelerate-0.22.0-py3-none-any.whl.metadata (17 kB)\n","Collecting einops==0.6.1\n"," Using cached einops-0.6.1-py3-none-any.whl.metadata (12 kB)\n","Collecting langchain==0.0.300\n"," Using cached langchain-0.0.300-py3-none-any.whl.metadata (15 kB)\n","Collecting xformers==0.0.21\n"," Using cached xformers-0.0.21.tar.gz (22.3 MB)\n"]},{"name":"stderr","output_type":"stream","text":["ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: 'C:\\\\Users\\\\siddi\\\\AppData\\\\Local\\\\Temp\\\\pip-install-cyo5pvb9\\\\xformers_0b8bc7bb638a4071b04db9b2990bef40\\\\third_party/flash-attention/csrc/cutlass/docs/classcutlass_1_1epilogue_1_1thread_1_1LinearCombinationRelu_3_01ElementOutput___00_01Count_00_014d4e40c4295be6a8d8778d86e94fe14a.html'\n","HINT: This error might have occurred since this system does not have Windows Long Path support enabled. You can find information on how to enable this at https://pip.pypa.io/warnings/enable-long-paths\n","\n"]}],"source":["!pip install transformers==4.33.0 accelerate==0.22.0 einops==0.6.1 langchain==0.0.300 xformers==0.0.21 \\\n","bitsandbytes==0.41.1 sentence_transformers==2.2.2 chromadb==0.4.12"]},{"cell_type":"code","execution_count":2,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T11:25:39.463684Z","iopub.status.busy":"2024-05-20T11:25:39.462969Z","iopub.status.idle":"2024-05-20T11:25:47.108271Z","shell.execute_reply":"2024-05-20T11:25:47.107327Z","shell.execute_reply.started":"2024-05-20T11:25:39.463651Z"},"trusted":true},"outputs":[],"source":["from torch import cuda, bfloat16\n","import torch\n","import transformers\n","from transformers import AutoTokenizer\n","from time import time\n","#import chromadb\n","#from chromadb.config import Settings\n","from langchain.llms import HuggingFacePipeline\n","from langchain.document_loaders import TextLoader\n","from langchain.text_splitter import RecursiveCharacterTextSplitter\n","from langchain.embeddings import HuggingFaceEmbeddings\n","from langchain.chains import RetrievalQA\n","from langchain.vectorstores import Chroma\n","from langchain import PromptTemplate, LLMChain"]},{"cell_type":"code","execution_count":3,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T11:25:48.508728Z","iopub.status.busy":"2024-05-20T11:25:48.507725Z","iopub.status.idle":"2024-05-20T11:26:01.796161Z","shell.execute_reply":"2024-05-20T11:26:01.794844Z","shell.execute_reply.started":"2024-05-20T11:25:48.508689Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["Note: you may need to restart the kernel to use updated packages.\n"]}],"source":["pip install -q accelerate bitsandbytes\n"]},{"cell_type":"code","execution_count":4,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T11:26:04.152407Z","iopub.status.busy":"2024-05-20T11:26:04.152021Z","iopub.status.idle":"2024-05-20T11:26:04.201802Z","shell.execute_reply":"2024-05-20T11:26:04.200747Z","shell.execute_reply.started":"2024-05-20T11:26:04.152376Z"},"trusted":true},"outputs":[],"source":["model_id = '/kaggle/input/llama-2/pytorch/7b-chat-hf/1'\n","\n","device = f'cuda:{cuda.current_device()}' if cuda.is_available() else 'cpu'\n","\n","# set quantization configuration to load large model with less GPU memory\n","# this requires the `bitsandbytes` library\n","bnb_config = transformers.BitsAndBytesConfig(\n"," load_in_4bit=True,\n"," bnb_4bit_quant_type='nf4',\n"," bnb_4bit_use_double_quant=True,\n"," bnb_4bit_compute_dtype=bfloat16\n",")"]},{"cell_type":"code","execution_count":5,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T11:26:09.157715Z","iopub.status.busy":"2024-05-20T11:26:09.156819Z","iopub.status.idle":"2024-05-20T11:29:10.539621Z","shell.execute_reply":"2024-05-20T11:29:10.538631Z","shell.execute_reply.started":"2024-05-20T11:26:09.157684Z"},"trusted":true},"outputs":[{"name":"stderr","output_type":"stream","text":["2024-05-20 11:26:12.744027: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n","2024-05-20 11:26:12.744143: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n","2024-05-20 11:26:12.880595: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n"]},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"e3757c76baca4aa4a33b67066b3a9607","version_major":2,"version_minor":0},"text/plain":["Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"]},"metadata":{},"output_type":"display_data"},{"name":"stderr","output_type":"stream","text":["/opt/conda/lib/python3.10/site-packages/transformers/generation/configuration_utils.py:362: UserWarning: `do_sample` is set to `False`. However, `temperature` is set to `0.9` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `temperature`. This was detected when initializing the generation config instance, which means the corresponding file may hold incorrect parameterization and should be fixed.\n"," warnings.warn(\n","/opt/conda/lib/python3.10/site-packages/transformers/generation/configuration_utils.py:367: UserWarning: `do_sample` is set to `False`. However, `top_p` is set to `0.6` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_p`. This was detected when initializing the generation config instance, which means the corresponding file may hold incorrect parameterization and should be fixed.\n"," warnings.warn(\n"]},{"name":"stdout","output_type":"stream","text":["Prepare model, tokenizer: 181.376 sec.\n"]}],"source":["time_1 = time()\n","model_config = transformers.AutoConfig.from_pretrained(\n"," model_id,\n",")\n","model = transformers.AutoModelForCausalLM.from_pretrained(\n"," model_id,\n"," trust_remote_code=True,\n"," config=model_config,\n"," quantization_config=bnb_config,\n"," device_map='auto',\n",")\n","tokenizer = AutoTokenizer.from_pretrained(model_id)\n","time_2 = time()\n","print(f\"Prepare model, tokenizer: {round(time_2-time_1, 3)} sec.\")"]},{"cell_type":"code","execution_count":6,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T11:33:37.803044Z","iopub.status.busy":"2024-05-20T11:33:37.802575Z","iopub.status.idle":"2024-05-20T11:33:39.377722Z","shell.execute_reply":"2024-05-20T11:33:39.376630Z","shell.execute_reply.started":"2024-05-20T11:33:37.803010Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["Prepare pipeline: 1.569 sec.\n"]}],"source":["time_1 = time()\n","summarization_pipeline = transformers.pipeline(\n"," \"text-generation\",\n"," model=model,\n"," tokenizer=tokenizer,\n"," torch_dtype=torch.float16,\n"," trust_remote_code=True,\n"," device_map=\"auto\",)\n","time_2 = time()\n","print(f\"Prepare pipeline: {round(time_2-time_1, 3)} sec.\")"]},{"cell_type":"code","execution_count":7,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T11:33:42.068650Z","iopub.status.busy":"2024-05-20T11:33:42.068273Z","iopub.status.idle":"2024-05-20T11:33:42.075261Z","shell.execute_reply":"2024-05-20T11:33:42.073785Z","shell.execute_reply.started":"2024-05-20T11:33:42.068622Z"},"trusted":true},"outputs":[],"source":["llm = HuggingFacePipeline(pipeline=summarization_pipeline, model_kwargs={'temperature': 0})\n","\n","template = \"\"\"\n","Write a concise summary of the following text in bullet points:\n","'''{text}'''\n","SUMMARY:\n","\n","\"\"\"\n","\n","prompt = PromptTemplate(template=template, input_variables=[\"text\"])\n","\n","llm_chain = LLMChain(prompt=prompt, llm=llm)"]},{"cell_type":"code","execution_count":8,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T11:33:44.308393Z","iopub.status.busy":"2024-05-20T11:33:44.307561Z","iopub.status.idle":"2024-05-20T11:33:44.313472Z","shell.execute_reply":"2024-05-20T11:33:44.312379Z","shell.execute_reply.started":"2024-05-20T11:33:44.308363Z"},"trusted":true},"outputs":[],"source":["def test_model(text):\n"," \"\"\"\n"," This function summarizes a given text string and returns a bulleted list of key points.\n"," Args:\n"," text: The text string to be summarized.\n"," Returns:\n"," A string containing the bulleted list summary of the text, or None if there's an error.\n"," \"\"\"\n"," time_1 = time()\n"," summary = llm_chain.run(text=text)\n"," time_2 = time()\n"," print(f\"Test inference: {round(time_2-time_1, 3)} sec.\")\n"," print(f\"Summary: {summary}\")"]},{"cell_type":"code","execution_count":9,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T11:33:48.278268Z","iopub.status.busy":"2024-05-20T11:33:48.277890Z","iopub.status.idle":"2024-05-20T11:33:57.251039Z","shell.execute_reply":"2024-05-20T11:33:57.250115Z","shell.execute_reply.started":"2024-05-20T11:33:48.278238Z"},"trusted":true},"outputs":[{"name":"stderr","output_type":"stream","text":["/opt/conda/lib/python3.10/site-packages/transformers/generation/utils.py:1417: UserWarning: You have modified the pretrained model configuration to control generation. This is a deprecated strategy to control generation and will be removed soon, in a future version. Please use a generation configuration file (see https://huggingface.co/docs/transformers/main_classes/text_generation )\n"," warnings.warn(\n"]},{"name":"stdout","output_type":"stream","text":["Test inference: 8.968 sec.\n","Summary: β€’ AI is a branch of computer science focused on developing intelligent machines.\n","β€’ AI research has made significant progress in recent years.\n","β€’ AI applications are now widespread and impact various sectors.\n","β€’ AI raises ethical considerations regarding automation, bias, and human control.\n","β€’ It's crucial to develop responsible AI practices to ensure benefits for society.\n"]}],"source":["test_model(\"Artificial intelligence (AI) is a branch of computer science focused on developing intelligent machines that can mimic human cognitive functions like learning, problem-solving, anddecision-making. AI research has made significant progress in recent years, with advancementsin machine learning algorithms and the increasing availability of data. AI applications are nowwidespread, impacting various sectors like healthcare, finance, transportation, and manufacturing.From medical diagnosis tools to self-driving cars, AI is transforming industries and raisingethical considerations regarding automation, bias, and human control. As AI continues to evolve,it's crucial to develop responsible AI practices that ensure its benefits are harnessed for thebetterment of society.\")"]},{"cell_type":"code","execution_count":10,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T11:36:55.992781Z","iopub.status.busy":"2024-05-20T11:36:55.992348Z","iopub.status.idle":"2024-05-20T11:37:21.176443Z","shell.execute_reply":"2024-05-20T11:37:21.175306Z","shell.execute_reply.started":"2024-05-20T11:36:55.992750Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["Test inference: 25.157 sec.\n","Summary: * Hyperthyroidism occurs when the thyroid gland produces too much thyroid hormone, leading to symptoms such as weight loss, rapid heartbeat, and anxiety.\n","* Hypothyroidism occurs when the thyroid gland produces too little thyroid hormone, leading to symptoms such as fatigue, weight gain, and cold sensitivity.\n","* The pituitary gland plays a crucial role in regulating thyroid hormone production by producing thyroid-stimulating hormone (TSH).\n","* Disorders related to the pituitary gland include hypopituitarism, hyperpituitarism, and pituitary tumors.\n","* There are three types of hypothyroidism: primary, secondary, and compensated.\n","* Lithium can have effects on the thyroid gland, including hypothyroidism and goitre.\n","* The Free Thyroxine Index (FTI) is a calculated value used to estimate the level of free thyroxine (T4) in the blood.\n","\n","\n","\n","\n","\n","\n"]}],"source":["with open('/kaggle/input/testing-dataset/thyroid.txt', \"r\") as f:\n"," extracted_text = f.read()\n","text = extracted_text\n","test_model(text)"]},{"cell_type":"code","execution_count":11,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T12:00:56.732276Z","iopub.status.busy":"2024-05-20T12:00:56.731876Z","iopub.status.idle":"2024-05-20T12:01:10.809630Z","shell.execute_reply":"2024-05-20T12:01:10.808362Z","shell.execute_reply.started":"2024-05-20T12:00:56.732237Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n","To disable this warning, you can either:\n","\t- Avoid using `tokenizers` before the fork if possible\n","\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n","Collecting pdfminer.six\n"," Downloading pdfminer.six-20231228-py3-none-any.whl.metadata (4.2 kB)\n","Requirement already satisfied: charset-normalizer>=2.0.0 in /opt/conda/lib/python3.10/site-packages (from pdfminer.six) (3.3.2)\n","Requirement already satisfied: cryptography>=36.0.0 in /opt/conda/lib/python3.10/site-packages (from pdfminer.six) (41.0.7)\n","Requirement already satisfied: cffi>=1.12 in /opt/conda/lib/python3.10/site-packages (from cryptography>=36.0.0->pdfminer.six) (1.16.0)\n","Requirement already satisfied: pycparser in /opt/conda/lib/python3.10/site-packages (from cffi>=1.12->cryptography>=36.0.0->pdfminer.six) (2.21)\n","Downloading pdfminer.six-20231228-py3-none-any.whl (5.6 MB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.6/5.6 MB\u001b[0m \u001b[31m49.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n","\u001b[?25hInstalling collected packages: pdfminer.six\n","Successfully installed pdfminer.six-20231228\n","Note: you may need to restart the kernel to use updated packages.\n"]}],"source":["pip install pdfminer.six"]},{"cell_type":"code","execution_count":12,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T12:01:17.301998Z","iopub.status.busy":"2024-05-20T12:01:17.301573Z","iopub.status.idle":"2024-05-20T12:01:31.132363Z","shell.execute_reply":"2024-05-20T12:01:31.131008Z","shell.execute_reply.started":"2024-05-20T12:01:17.301961Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n","To disable this warning, you can either:\n","\t- Avoid using `tokenizers` before the fork if possible\n","\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n","Collecting PyPdf2\n"," Downloading pypdf2-3.0.1-py3-none-any.whl.metadata (6.8 kB)\n","Downloading pypdf2-3.0.1-py3-none-any.whl (232 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m232.6/232.6 kB\u001b[0m \u001b[31m5.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n","\u001b[?25hInstalling collected packages: PyPdf2\n","Successfully installed PyPdf2-3.0.1\n"]}],"source":["!pip install PyPdf2"]},{"cell_type":"code","execution_count":13,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T12:01:36.180129Z","iopub.status.busy":"2024-05-20T12:01:36.179145Z","iopub.status.idle":"2024-05-20T12:01:36.364110Z","shell.execute_reply":"2024-05-20T12:01:36.363199Z","shell.execute_reply.started":"2024-05-20T12:01:36.180092Z"},"trusted":true},"outputs":[],"source":["from PyPDF2 import PdfReader\n","pdfreader = PdfReader('/kaggle/input/ai-dataset/ai.pdf')"]},{"cell_type":"code","execution_count":14,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T12:01:41.112256Z","iopub.status.busy":"2024-05-20T12:01:41.111345Z","iopub.status.idle":"2024-05-20T12:01:41.390940Z","shell.execute_reply":"2024-05-20T12:01:41.389877Z","shell.execute_reply.started":"2024-05-20T12:01:41.112221Z"},"trusted":true},"outputs":[],"source":["from typing_extensions import Concatenate\n","text = \"\"\n","for i,page in enumerate(pdfreader.pages):\n"," content = page.extract_text()\n"," if content:\n"," text += content"]},{"cell_type":"code","execution_count":15,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T12:01:45.081869Z","iopub.status.busy":"2024-05-20T12:01:45.081446Z","iopub.status.idle":"2024-05-20T12:01:45.086668Z","shell.execute_reply":"2024-05-20T12:01:45.085563Z","shell.execute_reply.started":"2024-05-20T12:01:45.081823Z"},"trusted":true},"outputs":[],"source":["from langchain.docstore.document import Document "]},{"cell_type":"code","execution_count":16,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T12:01:49.722451Z","iopub.status.busy":"2024-05-20T12:01:49.722053Z","iopub.status.idle":"2024-05-20T12:01:49.727910Z","shell.execute_reply":"2024-05-20T12:01:49.726775Z","shell.execute_reply.started":"2024-05-20T12:01:49.722417Z"},"trusted":true},"outputs":[],"source":["docs = [Document(page_content=text)]"]},{"cell_type":"code","execution_count":17,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T12:01:52.763686Z","iopub.status.busy":"2024-05-20T12:01:52.762590Z","iopub.status.idle":"2024-05-20T12:01:52.770967Z","shell.execute_reply":"2024-05-20T12:01:52.769916Z","shell.execute_reply.started":"2024-05-20T12:01:52.763639Z"},"trusted":true},"outputs":[],"source":["text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=20)\n","chunks = text_splitter.create_documents([text])"]},{"cell_type":"code","execution_count":18,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T12:01:57.337216Z","iopub.status.busy":"2024-05-20T12:01:57.336430Z","iopub.status.idle":"2024-05-20T12:01:57.344190Z","shell.execute_reply":"2024-05-20T12:01:57.343219Z","shell.execute_reply.started":"2024-05-20T12:01:57.337185Z"},"trusted":true},"outputs":[{"data":{"text/plain":["40"]},"execution_count":18,"metadata":{},"output_type":"execute_result"}],"source":["len(chunks)"]},{"cell_type":"markdown","metadata":{"execution":{"iopub.execute_input":"2024-05-19T07:59:54.586537Z","iopub.status.busy":"2024-05-19T07:59:54.586203Z","iopub.status.idle":"2024-05-19T07:59:54.592012Z","shell.execute_reply":"2024-05-19T07:59:54.590994Z","shell.execute_reply.started":"2024-05-19T07:59:54.586512Z"}},"source":["loader = TextLoader(\"/kaggle/input/testing-dataset/thyroid.txt\",\n"," encoding=\"utf8\")\n","documents = loader.load()"]},{"cell_type":"code","execution_count":19,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T12:02:07.752498Z","iopub.status.busy":"2024-05-20T12:02:07.751596Z","iopub.status.idle":"2024-05-20T12:02:15.517927Z","shell.execute_reply":"2024-05-20T12:02:15.516798Z","shell.execute_reply.started":"2024-05-20T12:02:07.752464Z"},"trusted":true},"outputs":[{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"9732b0cdc83049f0b91347dd5b95b2ec","version_major":2,"version_minor":0},"text/plain":[".gitattributes: 0%| | 0.00/1.23k [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"d9a4ed3c78e5413eb2441f1385a27e12","version_major":2,"version_minor":0},"text/plain":["1_Pooling/config.json: 0%| | 0.00/190 [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"e5c32088cb6a47e0bd93dee4bed8f869","version_major":2,"version_minor":0},"text/plain":["README.md: 0%| | 0.00/10.6k [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"5cc9741ae09b49a5a92a1e7f789c6e42","version_major":2,"version_minor":0},"text/plain":["config.json: 0%| | 0.00/571 [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"6219113174984f5b8650c56ba94c36bd","version_major":2,"version_minor":0},"text/plain":["config_sentence_transformers.json: 0%| | 0.00/116 [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"4536fabbea1a4e7893a4b5831319ec3f","version_major":2,"version_minor":0},"text/plain":["data_config.json: 0%| | 0.00/39.3k [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"36398b98c62d4a21bc6b528b0784cfe3","version_major":2,"version_minor":0},"text/plain":["model.safetensors: 0%| | 0.00/438M [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"fa8f0460889e4906b38f967024ef6dd3","version_major":2,"version_minor":0},"text/plain":["pytorch_model.bin: 0%| | 0.00/438M [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"40ed26a26fae40afbe3ca10c76e7884f","version_major":2,"version_minor":0},"text/plain":["sentence_bert_config.json: 0%| | 0.00/53.0 [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"53a84f33242944e88efce7b441f462f2","version_major":2,"version_minor":0},"text/plain":["special_tokens_map.json: 0%| | 0.00/239 [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"71473bd6f7664eaf872f7019407ebf2d","version_major":2,"version_minor":0},"text/plain":["tokenizer.json: 0%| | 0.00/466k [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"85b3f77ff2b94415a0394c7194aa2352","version_major":2,"version_minor":0},"text/plain":["tokenizer_config.json: 0%| | 0.00/363 [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"1b3c8188f2a949af9489603dc27801c5","version_major":2,"version_minor":0},"text/plain":["train_script.py: 0%| | 0.00/13.1k [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"e5a643a658b140299a8af1f00a7443ca","version_major":2,"version_minor":0},"text/plain":["vocab.txt: 0%| | 0.00/232k [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"1c1b7aa1b8654246a79fa6d646ef86b5","version_major":2,"version_minor":0},"text/plain":["modules.json: 0%| | 0.00/349 [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"}],"source":["model_name = \"sentence-transformers/all-mpnet-base-v2\"\n","model_kwargs = {\"device\": \"cuda\"}\n","\n","embeddings = HuggingFaceEmbeddings(model_name=model_name, model_kwargs=model_kwargs)"]},{"cell_type":"code","execution_count":20,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T12:02:19.447374Z","iopub.status.busy":"2024-05-20T12:02:19.446667Z","iopub.status.idle":"2024-05-20T12:02:20.561798Z","shell.execute_reply":"2024-05-20T12:02:20.560916Z","shell.execute_reply.started":"2024-05-20T12:02:19.447340Z"},"trusted":true},"outputs":[{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"a06cb0122e5b4ad99593c8ee9fb536f7","version_major":2,"version_minor":0},"text/plain":["Batches: 0%| | 0/2 [00:00<?, ?it/s]"]},"metadata":{},"output_type":"display_data"}],"source":["vectordb = Chroma.from_documents(documents=chunks, embedding=embeddings, persist_directory=\"chroma_db\")"]},{"cell_type":"code","execution_count":21,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T12:02:23.501875Z","iopub.status.busy":"2024-05-20T12:02:23.501477Z","iopub.status.idle":"2024-05-20T12:02:23.506466Z","shell.execute_reply":"2024-05-20T12:02:23.505475Z","shell.execute_reply.started":"2024-05-20T12:02:23.501833Z"},"trusted":true},"outputs":[],"source":["retriever = vectordb.as_retriever()"]},{"cell_type":"code","execution_count":22,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T12:02:29.386911Z","iopub.status.busy":"2024-05-20T12:02:29.386507Z","iopub.status.idle":"2024-05-20T12:02:29.392439Z","shell.execute_reply":"2024-05-20T12:02:29.391363Z","shell.execute_reply.started":"2024-05-20T12:02:29.386882Z"},"trusted":true},"outputs":[],"source":["qa = RetrievalQA.from_chain_type(\n"," llm=llm, \n"," chain_type=\"stuff\", \n"," retriever=retriever, \n"," verbose=True\n",")"]},{"cell_type":"code","execution_count":23,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T12:02:32.512385Z","iopub.status.busy":"2024-05-20T12:02:32.511694Z","iopub.status.idle":"2024-05-20T12:02:32.518296Z","shell.execute_reply":"2024-05-20T12:02:32.517318Z","shell.execute_reply.started":"2024-05-20T12:02:32.512353Z"},"trusted":true},"outputs":[],"source":["def test_rag(qa, **kwargs):\n"," print(f\"Query: {kwargs['query']}\\n\")\n"," time_1 = time()\n"," result = qa.run(**kwargs)\n"," time_2 = time()\n"," print(f\"Inference time: {round(time_2-time_1, 3)} sec.\")\n","\n"," if isinstance(result, list):\n"," print(\"\\nResult: \", result[0]['summary_text'])\n"," else:\n"," print(\"\\nResult: \", result)"]},{"cell_type":"code","execution_count":24,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T12:02:35.863714Z","iopub.status.busy":"2024-05-20T12:02:35.862905Z","iopub.status.idle":"2024-05-20T12:02:55.462313Z","shell.execute_reply":"2024-05-20T12:02:55.460731Z","shell.execute_reply.started":"2024-05-20T12:02:35.863674Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["Query: Can you summarize this long piece of text into a concise summary in not more than 300 words?\n","\n","\n","\n","\u001b[1m> Entering new RetrievalQA chain...\u001b[0m\n"]},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"b8ba6970fc6444f5abe133dbeb56b189","version_major":2,"version_minor":0},"text/plain":["Batches: 0%| | 0/1 [00:00<?, ?it/s]"]},"metadata":{},"output_type":"display_data"},{"name":"stdout","output_type":"stream","text":["\n","\u001b[1m> Finished chain.\u001b[0m\n","Inference time: 19.592 sec.\n","\n","Result: Sure, here is a summary of the text in 300 words or less:\n","\n","The text discusses the history and evolution of artificial intelligence (AI) and its impact on society. AI can be classified into different types, such as analytical, human-inspired, and humanized AI, depending on its cognitive, emotional, and social intelligence. The author argues that when AI reaches mainstream usage, it is frequently no longer considered as such, and this phenomenon is known as the AI effect. The text also mentions that advances in AI have led to the development of image recognition, smart speakers, and self-driving cars, and it has entered the business environment and public conversation. The author concludes that AI has the potential to revolutionize many aspects of our lives, but it also raises important ethical and societal questions that need to be addressed.\n"]}],"source":["test_rag(qa, query=\"Can you summarize this long piece of text into a concise summary in not more than 300 words?\", \n"," num_outputs=1, max_length=300, min_length=30, do_sample=False)"]},{"cell_type":"code","execution_count":25,"metadata":{"execution":{"iopub.execute_input":"2024-05-20T12:03:08.232387Z","iopub.status.busy":"2024-05-20T12:03:08.231964Z","iopub.status.idle":"2024-05-20T12:03:43.576910Z","shell.execute_reply":"2024-05-20T12:03:43.575941Z","shell.execute_reply.started":"2024-05-20T12:03:08.232353Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["Query: Can you summarize this long piece of text into a concise summary in not more than 500 words?\n","\n","\n","\n","\u001b[1m> Entering new RetrievalQA chain...\u001b[0m\n"]},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"b38405c7565443d19d86bdad156b6747","version_major":2,"version_minor":0},"text/plain":["Batches: 0%| | 0/1 [00:00<?, ?it/s]"]},"metadata":{},"output_type":"display_data"},{"name":"stdout","output_type":"stream","text":["\n","\u001b[1m> Finished chain.\u001b[0m\n","Inference time: 35.34 sec.\n","\n","Result: Of course! Here is a summary of the text in 475 words:\n","\n","The article discusses the history and future of artificial intelligence (AI). The author divides AI's history into four seasons: spring, summer, fall, and winter. Spring represents the birth of AI, summer its growth and development, fall its challenges and limitations, and winter its potential future advancements.\n","\n","In the spring season, AI was born out of the need to automate tasks and solve complex problems. The first AI program was developed in the 1950s, and since then, AI has evolved rapidly, with the development of new algorithms, models, and techniques. Summer represents the growth and development of AI, where it became more sophisticated and capable of performing a wide range of tasks.\n","\n","In the fall season, AI faced challenges and limitations, such as the lack of data, the difficulty of interpreting results, and the need for better integration with human systems. Despite these challenges, AI continued to advance, with the development of new technologies such as deep learning and natural language processing. Winter represents the potential future advancements of AI, where it is expected to reach new heights, such as the development of Artificial General Intelligence (AGI) and Superintelligence.\n","\n","The author notes that the AI effect occurs when onlookers discount the behavior of an AI program, arguing that it is not real intelligence. However, as the British science fiction writer Arthur Clarke once said, \"Any sufficiently advanced technology is indistinguishable from magic.\" The author concludes by emphasizing the importance of understanding AI's past, present, and future to prepare for the challenges ahead.\n"]}],"source":["test_rag(qa, query=\"Can you summarize this long piece of text into a concise summary in not more than 500 words?\", \n"," num_outputs=1, max_length=500, min_length=30, do_sample=False)"]}],"metadata":{"kaggle":{"accelerator":"gpu","dataSources":[{"datasetId":1332934,"sourceId":2219619,"sourceType":"datasetVersion"},{"datasetId":4884056,"sourceId":8234785,"sourceType":"datasetVersion"},{"datasetId":4889139,"sourceId":8241861,"sourceType":"datasetVersion"},{"datasetId":4889150,"sourceId":8241874,"sourceType":"datasetVersion"},{"datasetId":4889169,"sourceId":8241895,"sourceType":"datasetVersion"},{"datasetId":4910415,"sourceId":8270647,"sourceType":"datasetVersion"},{"datasetId":5045202,"sourceId":8463148,"sourceType":"datasetVersion"},{"datasetId":5048074,"sourceId":8466953,"sourceType":"datasetVersion"},{"modelInstanceId":3093,"sourceId":4298,"sourceType":"modelInstanceVersion"},{"isSourceIdPinned":true,"modelInstanceId":1462,"sourceId":1728,"sourceType":"modelInstanceVersion"}],"dockerImageVersionId":30699,"isGpuEnabled":true,"isInternetEnabled":true,"language":"python","sourceType":"notebook"},"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.12.1"}},"nbformat":4,"nbformat_minor":4}
whisper.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"cells":[{"cell_type":"code","execution_count":1,"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2024-06-04T08:55:15.098965Z","iopub.status.busy":"2024-06-04T08:55:15.098160Z","iopub.status.idle":"2024-06-04T08:55:48.500990Z","shell.execute_reply":"2024-06-04T08:55:48.499909Z","shell.execute_reply.started":"2024-06-04T08:55:15.098931Z"},"trusted":true},"outputs":[],"source":["!pip install git+https://github.com/openai/whisper.git -q"]},{"cell_type":"code","execution_count":2,"metadata":{"execution":{"iopub.execute_input":"2024-06-04T08:57:31.775720Z","iopub.status.busy":"2024-06-04T08:57:31.774874Z","iopub.status.idle":"2024-06-04T08:57:48.102234Z","shell.execute_reply":"2024-06-04T08:57:48.101124Z","shell.execute_reply.started":"2024-06-04T08:57:31.775682Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["Collecting git+https://github.com/openai/whisper.git\n"," Cloning https://github.com/openai/whisper.git to /tmp/pip-req-build-6zhf_1k6\n"," Running command git clone --filter=blob:none --quiet https://github.com/openai/whisper.git /tmp/pip-req-build-6zhf_1k6\n"," Resolved https://github.com/openai/whisper.git to commit ba3f3cd54b0e5b8ce1ab3de13e32122d0d5f98ab\n"," Installing build dependencies ... \u001b[?25ldone\n","\u001b[?25h Getting requirements to build wheel ... \u001b[?25ldone\n","\u001b[?25h Preparing metadata (pyproject.toml) ... \u001b[?25ldone\n","\u001b[?25hBuilding wheels for collected packages: openai-whisper\n"," Building wheel for openai-whisper (pyproject.toml) ... \u001b[?25ldone\n","\u001b[?25h Created wheel for openai-whisper: filename=openai_whisper-20231117-py3-none-any.whl size=802826 sha256=7e734f0646ceed2b6daea1520be26c9c400c331fea44b72923336fd08fa85521\n"," Stored in directory: /tmp/pip-ephem-wheel-cache-pkgr03u7/wheels/8b/6c/d0/622666868c179f156cf595c8b6f06f88bc5d80c4b31dccaa03\n","Successfully built openai-whisper\n","Installing collected packages: openai-whisper\n"," Attempting uninstall: openai-whisper\n"," Found existing installation: openai-whisper 20231117\n"," Uninstalling openai-whisper-20231117:\n"," Successfully uninstalled openai-whisper-20231117\n","Successfully installed openai-whisper-20231117\n","Note: you may need to restart the kernel to use updated packages.\n"]}],"source":["pip install --upgrade --no-deps --force-reinstall git+https://github.com/openai/whisper.git"]},{"cell_type":"code","execution_count":3,"metadata":{"execution":{"iopub.execute_input":"2024-06-04T08:58:24.118963Z","iopub.status.busy":"2024-06-04T08:58:24.118228Z","iopub.status.idle":"2024-06-04T08:58:36.403896Z","shell.execute_reply":"2024-06-04T08:58:36.402788Z","shell.execute_reply.started":"2024-06-04T08:58:24.118928Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["Collecting setuptools-rust\n"," Downloading setuptools_rust-1.9.0-py3-none-any.whl.metadata (9.3 kB)\n","Requirement already satisfied: setuptools>=62.4 in /opt/conda/lib/python3.10/site-packages (from setuptools-rust) (69.0.3)\n","Collecting semantic-version<3,>=2.8.2 (from setuptools-rust)\n"," Downloading semantic_version-2.10.0-py2.py3-none-any.whl.metadata (9.7 kB)\n","Requirement already satisfied: tomli>=1.2.1 in /opt/conda/lib/python3.10/site-packages (from setuptools-rust) (2.0.1)\n","Downloading setuptools_rust-1.9.0-py3-none-any.whl (26 kB)\n","Downloading semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)\n","Installing collected packages: semantic-version, setuptools-rust\n","Successfully installed semantic-version-2.10.0 setuptools-rust-1.9.0\n","Note: you may need to restart the kernel to use updated packages.\n"]}],"source":["pip install setuptools-rust"]},{"cell_type":"code","execution_count":4,"metadata":{"execution":{"iopub.execute_input":"2024-06-04T08:59:01.169412Z","iopub.status.busy":"2024-06-04T08:59:01.168516Z","iopub.status.idle":"2024-06-04T08:59:08.573688Z","shell.execute_reply":"2024-06-04T08:59:08.572849Z","shell.execute_reply.started":"2024-06-04T08:59:01.169378Z"},"trusted":true},"outputs":[{"name":"stderr","output_type":"stream","text":["100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 139M/139M [00:01<00:00, 99.8MiB/s]\n"]}],"source":["import whisper\n","\n","model = whisper.load_model(\"base\")"]},{"cell_type":"code","execution_count":5,"metadata":{"execution":{"iopub.execute_input":"2024-06-04T08:59:30.959417Z","iopub.status.busy":"2024-06-04T08:59:30.958657Z","iopub.status.idle":"2024-06-04T08:59:30.966523Z","shell.execute_reply":"2024-06-04T08:59:30.965508Z","shell.execute_reply.started":"2024-06-04T08:59:30.959387Z"},"trusted":true},"outputs":[{"data":{"text/plain":["device(type='cuda', index=0)"]},"execution_count":5,"metadata":{},"output_type":"execute_result"}],"source":["model.device"]},{"cell_type":"code","execution_count":6,"metadata":{"execution":{"iopub.execute_input":"2024-06-04T08:59:34.578364Z","iopub.status.busy":"2024-06-04T08:59:34.578011Z","iopub.status.idle":"2024-06-04T08:59:50.105776Z","shell.execute_reply":"2024-06-04T08:59:50.104637Z","shell.execute_reply.started":"2024-06-04T08:59:34.578336Z"},"trusted":true},"outputs":[],"source":["!pip install yt_dlp -q"]},{"cell_type":"code","execution_count":7,"metadata":{"execution":{"iopub.execute_input":"2024-06-04T09:00:17.851749Z","iopub.status.busy":"2024-06-04T09:00:17.850792Z","iopub.status.idle":"2024-06-04T09:00:18.289383Z","shell.execute_reply":"2024-06-04T09:00:18.288625Z","shell.execute_reply.started":"2024-06-04T09:00:17.851690Z"},"trusted":true},"outputs":[],"source":["import yt_dlp\n","\n","def extract_audio(url):\n"," \"\"\"Extracts audio from a YouTube video using yt-dlp.\n","\n"," Args:\n"," url: The URL of the YouTube video.\n","\n"," Returns:\n"," The downloaded audio file path.\n"," \"\"\"\n"," ydl_opts = {\n"," 'format': 'bestaudio/best',\n"," 'postprocessors': [{\n"," 'key': 'FFmpegExtractAudio',\n"," 'preferredcodec': 'mp3',\n"," 'preferredquality': '192',\n"," }],\n"," 'outtmpl': '%(title)s.%(ext)s'\n"," }\n","\n"," with yt_dlp.YoutubeDL(ydl_opts) as ydl:\n"," info_dict = ydl.extract_info(url, download=True)\n"," audio_file = ydl.prepare_filename(info_dict)\n"," print(f\"Audio extracted successfully! Downloaded file: {audio_file}\")\n"," return audio_file\n"]},{"cell_type":"code","execution_count":42,"metadata":{"execution":{"iopub.execute_input":"2024-06-04T10:30:54.992944Z","iopub.status.busy":"2024-06-04T10:30:54.992459Z","iopub.status.idle":"2024-06-04T10:31:00.608265Z","shell.execute_reply":"2024-06-04T10:31:00.607324Z","shell.execute_reply.started":"2024-06-04T10:30:54.992907Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["[youtube] Extracting URL: https://www.youtube.com/shorts/vjeEOTI3tXU\n","[youtube] vjeEOTI3tXU: Downloading webpage\n","[youtube] vjeEOTI3tXU: Downloading ios player API JSON\n","[youtube] vjeEOTI3tXU: Downloading m3u8 information\n","[info] vjeEOTI3tXU: Downloading 1 format(s): 251\n","[download] Destination: Puzzles for Software Engineers | Amazon #18.webm\n","[download] 100% of 853.91KiB in 00:00:00 at 2.17MiB/s \n","[ExtractAudio] Destination: Puzzles for Software Engineers | Amazon #18.mp3\n","Deleting original file Puzzles for Software Engineers | Amazon #18.webm (pass -k to keep)\n","Audio extracted successfully! Downloaded file: Puzzles for Software Engineers | Amazon #18.webm\n"]},{"data":{"text/plain":["'Puzzles for Software Engineers | Amazon #18.webm'"]},"execution_count":42,"metadata":{},"output_type":"execute_result"}],"source":["url = \"https://www.youtube.com/shorts/vjeEOTI3tXU\"\n","extract_audio(url)"]},{"cell_type":"code","execution_count":9,"metadata":{"execution":{"iopub.execute_input":"2024-06-04T09:01:19.218820Z","iopub.status.busy":"2024-06-04T09:01:19.218085Z","iopub.status.idle":"2024-06-04T09:01:22.972902Z","shell.execute_reply":"2024-06-04T09:01:22.971882Z","shell.execute_reply.started":"2024-06-04T09:01:19.218787Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["Detected language: en\n","If you're watching this video, and I'm pretty sure you are, well, I'm going to have to guess and say that you've at least interacted with a large language model or an LLM. LLMs can quickly answer natural language questions, provide summarisation and follow complex instructions. But have you thought about the operational side of these models? LLMs need\n"]}],"source":["# load audio and pad/trim it to fit 30 seconds\n","audio = whisper.load_audio(\"/kaggle/working/Large Language Model Operations (LLMOps) Explained.mp3\")\n","audio = whisper.pad_or_trim(audio)\n","\n","# make log-Mel spectrogram and move to the same device as the model\n","mel = whisper.log_mel_spectrogram(audio).to(model.device)\n","\n","# detect the spoken language\n","_, probs = model.detect_language(mel)\n","print(f\"Detected language: {max(probs, key=probs.get)}\")\n","\n","# decode the audio\n","options = whisper.DecodingOptions()\n","result = whisper.decode(model, mel, options)\n","\n","# print the recognized text\n","print(result.text)"]},{"cell_type":"code","execution_count":43,"metadata":{"execution":{"iopub.execute_input":"2024-06-04T10:31:43.063599Z","iopub.status.busy":"2024-06-04T10:31:43.062594Z","iopub.status.idle":"2024-06-04T10:32:14.281894Z","shell.execute_reply":"2024-06-04T10:32:14.280707Z","shell.execute_reply.started":"2024-06-04T10:31:43.063558Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["Detecting language using up to the first 30 seconds. Use `--language` to specify the language\n","Detected language: Hindi\n","[00:00.000 --> 00:04.200] This question was asked in an Amazon software engineering interview. Let's see if you can solve it.\n","[00:04.200 --> 00:11.480] You have a bag with 21 blue and 23 red balls.\n","[00:11.480 --> 00:15.160] There are also 22 red balls outside the bag.\n","[00:15.160 --> 00:18.400] Now you have to randomly remove 2 balls from the bag.\n","[00:18.400 --> 00:24.160] If both the balls are of different colors, then you have to put the blue ball back in the bag.\n","[00:24.160 --> 00:31.120] And if the balls are of the same color, then you have to remove them and put a red ball back in the bag.\n","[00:31.120 --> 00:38.240] Now you repeat this process and remove 2 balls every time until there is only one ball left in the bag.\n","[00:38.240 --> 00:41.760] You have to tell me what color will be the last ball that will be left.\n","[00:41.760 --> 00:46.840] You will get the answer in the last 1 second, but if you recognize it before that, then please comment below and let me know.\n","[00:46.840 --> 00:49.960] That's all for today. See you in the next video. Till then, keep learning and keep exploring.\n"]}],"source":["!whisper \"/kaggle/working/Puzzles for Software Engineers | Amazon #18.mp3\" --model medium --task translate"]},{"cell_type":"code","execution_count":19,"metadata":{"execution":{"iopub.execute_input":"2024-06-04T09:25:44.266756Z","iopub.status.busy":"2024-06-04T09:25:44.266322Z","iopub.status.idle":"2024-06-04T09:26:41.948852Z","shell.execute_reply":"2024-06-04T09:26:41.947797Z","shell.execute_reply.started":"2024-06-04T09:25:44.266724Z"},"trusted":true},"outputs":[{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"1bfaaee2125b4d648d453900c3076360","version_major":2,"version_minor":0},"text/plain":["model.safetensors: 44%|####4 | 1.36G/3.09G [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"bc0bf7a6f8744526943e6e4c36d9a273","version_major":2,"version_minor":0},"text/plain":["generation_config.json: 0%| | 0.00/3.90k [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"b245ae86ae184dc2ab25ef79af1381f1","version_major":2,"version_minor":0},"text/plain":["preprocessor_config.json: 0%| | 0.00/340 [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"6a5566605bb04638a00159b8d4b66844","version_major":2,"version_minor":0},"text/plain":["tokenizer_config.json: 0%| | 0.00/283k [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"bc61182a78d3432280cc70a70ea5ee56","version_major":2,"version_minor":0},"text/plain":["vocab.json: 0%| | 0.00/1.04M [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"07f5c74444be4f1482652d696177cc79","version_major":2,"version_minor":0},"text/plain":["tokenizer.json: 0%| | 0.00/2.48M [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"18e2aa0a81fc4b73ae23d2ffde0bde75","version_major":2,"version_minor":0},"text/plain":["merges.txt: 0%| | 0.00/494k [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"f81b387c058b4966b0971916a3920387","version_major":2,"version_minor":0},"text/plain":["normalizer.json: 0%| | 0.00/52.7k [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"406f8a6b456a4ee1aa9127883cc500b6","version_major":2,"version_minor":0},"text/plain":["added_tokens.json: 0%| | 0.00/34.6k [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"3647d809a8b9436c843dc99b44b44e63","version_major":2,"version_minor":0},"text/plain":["special_tokens_map.json: 0%| | 0.00/2.07k [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"name":"stderr","output_type":"stream","text":["Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n"]},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"d1553743a3a740d1b5eb1e32ae84b4f9","version_major":2,"version_minor":0},"text/plain":["Downloading readme: 0%| | 0.00/480 [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"a601be0743af4e82a0ef57efc0e8ff02","version_major":2,"version_minor":0},"text/plain":["Downloading data: 0%| | 0.00/1.98M [00:00<?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"f441e03ce4114ceea8c580c6a192a015","version_major":2,"version_minor":0},"text/plain":["Generating validation split: 0%| | 0/1 [00:00<?, ? examples/s]"]},"metadata":{},"output_type":"display_data"},{"name":"stderr","output_type":"stream","text":["/opt/conda/lib/python3.10/site-packages/transformers/models/whisper/generation_whisper.py:480: FutureWarning: The input name `inputs` is deprecated. Please make sure to use `input_features` instead.\n"," warnings.warn(\n","Due to a bug fix in https://github.com/huggingface/transformers/pull/28687 transcription using a multilingual Whisper will default to language detection followed by transcription instead of translation to English.This might be a breaking change for your use case. If you want to instead always translate your audio to English, make sure to pass `language='en'`.\n"]},{"name":"stdout","output_type":"stream","text":[" If you're watching this video, and I'm pretty sure you are, well, I'm going to hazard a guess and say that you've at least interacted with a large language model or an LLM. LLMs can quickly answer natural language questions, provide summarization, and follow complex instructions. But have you thought about the operational side of these models? LLMs need deployment, monitoring, and maintenance just like anything else, and that's what LLM Ops addresses, Large Language Model Operations. It's a collaboration of data scientists, DevOps engineers, and IT professionals in an environment for data exploration, prompt engineering and pipeline management. LLM Ops automates the operational and monitoring tasks in the machine learning lifecycle. Oh yes, machine learning, because LLM Ops falls within the scope of machine learning operations, it might be tempting to think of LLMs as just another model for something called ML Ops. Now if you're not familiar, ML Ops is about streamlining the process of taking machine learning models in production and then maintaining and monitoring them. So the difference here is that LLM Ops addresses the specifics of LLM machine learning models that traditional MLOps does not. Now an MLOps lifecycle might look a bit like this. So we have exploratory data analysis and some development here as one stage. Then beneath that we have a couple of CICD pipelines. What's that? That's continuous integration and continuous delivery. And in fact, we would probably have one here for deployment. And we would have another one over here for actually training our model. So training CI-CD here. And then finally, this all filters into one last stage, which is effectively the monitor stage for monitoring the model. But LLMs, they introduce additional requirements over other ML models. So, for example, let's consider transfer learning. Many traditional ML models are created and trained from scratch, but that's typically not the case with most LLMs. Building new LLMs from scratch, well, that would be a very expensive operation. Many LLMs actually start from an existing foundation model, and that model is then fine-tuned with new data to improve model performance in a given domain. Or consider hyperparameter tuning. In ML, hyperparameter tuning often focuses on improving metrics like accuracy. For LLMs, tuning also becomes important for reducing the cost and computational power requirements of training and influence. Another difference, that is performance metrics. Now ML models most often have clearly defined and easy to calculate performance metrics like accuracy or AUC, that's area under curve, and an F1 score. But when evaluating LLMs, a different set of standard benchmarks and scoring are needed. Bilingual evaluation understudy or BLEU and recall oriented understudy for gisting evaluation, I think that's right, for Rogue. These are all things that require additional consideration during implementation. So the components of LLM Ops look something like this. So at the top here, we have EDA or exploratory data analysis to iteratively explore and share data for use in the LLM model. That moves us in to data prep that transforms, aggregates and deduplicates data. We have prompt engineering that's used to develop prompts for structured, reliable queries to LLMs. Now, as we've discussed, it's likely that this model will actually be fine tuned to improve its performance to the domain where it's operating. There's also a model review and governance process to track the model and pipeline versions and then manage their complete lifecycle. There is model inference and serving, and that can manage the production specifics of testing and QA, such as frequency of model refresh and inference request times. And finally, an LLM ops lifecycle is likely to include a stage for model monitoring that includes human feedback to your LLM applications. This stage can identify potential malicious attacks, model drift and potential areas for improvement. Ultimately, LLM development consists of many components, and some of these components are specific to LLMs, not other machine learning models. And those developed LLM models need to be deployed and they need to be monitored. And all of this requires collaboration and handoffs across various teams. An LLM Ops platform things like efficiency throughout the entire lifecycle. And then when it comes to risk, we can reduce the risk through improved security and privacy by using advanced enterprise-grade LLM Ops to prioritize the protection of sensitive information. And LLM Ops enable easier scalability, and that's through the management of the data, which is important when we're talking about multiple models that need to be overseen, controlled, and monitored for continuous integration, delivery, and deployment. controlled and monitored for continuous integration, delivery and deployment. So that's LLM Ops in a nutshell. It's a set of practices, techniques and tools used for the operational management of large language models in production environments. And unlike the broader ML Ops, it addresses the unique approaches required to train and deploy LLMs. If you have any questions, please drop us a line below. required to train and deploy LLMs.\n"]}],"source":["import torch\n","from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline\n","from datasets import load_dataset\n","\n","\n","device = \"cuda:0\" if torch.cuda.is_available() else \"cpu\"\n","torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32\n","\n","model_id = \"openai/whisper-large-v3\"\n","\n","model = AutoModelForSpeechSeq2Seq.from_pretrained(\n"," model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True\n",")\n","model.to(device)\n","\n","processor = AutoProcessor.from_pretrained(model_id)\n","\n","pipe = pipeline(\n"," \"automatic-speech-recognition\",\n"," model=model,\n"," tokenizer=processor.tokenizer,\n"," feature_extractor=processor.feature_extractor,\n"," max_new_tokens=128,\n"," chunk_length_s=30,\n"," batch_size=16,\n"," return_timestamps=True,\n"," torch_dtype=torch_dtype,\n"," device=device,\n",")\n","\n","dataset = load_dataset(\"distil-whisper/librispeech_long\", \"clean\", split=\"validation\")\n","sample = dataset[0][\"audio\"]\n","\n","result = pipe(\"/kaggle/working/Large Language Model Operations (LLMOps) Explained.mp3\")\n","print(result[\"text\"])\n"]},{"cell_type":"code","execution_count":21,"metadata":{"execution":{"iopub.execute_input":"2024-06-04T09:41:54.277540Z","iopub.status.busy":"2024-06-04T09:41:54.277123Z","iopub.status.idle":"2024-06-04T09:42:09.434514Z","shell.execute_reply":"2024-06-04T09:42:09.433368Z","shell.execute_reply.started":"2024-06-04T09:41:54.277507Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["Collecting whisper\n"," Downloading whisper-1.1.10.tar.gz (42 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m42.8/42.8 kB\u001b[0m \u001b[31m1.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25ldone\n","\u001b[?25hRequirement already satisfied: six in /opt/conda/lib/python3.10/site-packages (from whisper) (1.16.0)\n","Building wheels for collected packages: whisper\n"," Building wheel for whisper (setup.py) ... \u001b[?25ldone\n","\u001b[?25h Created wheel for whisper: filename=whisper-1.1.10-py3-none-any.whl size=41120 sha256=5875605233d9ce887ee905d59bb8485c0be76df94b88cddfbf9d55fa92a39daa\n"," Stored in directory: /root/.cache/pip/wheels/aa/7c/1d/015619716e2facae6631312503baf3c3220e6a9a3508cb14b6\n","Successfully built whisper\n","Installing collected packages: whisper\n","Successfully installed whisper-1.1.10\n"]}],"source":["!pip install whisper"]},{"cell_type":"code","execution_count":29,"metadata":{"execution":{"iopub.execute_input":"2024-06-04T09:51:00.663340Z","iopub.status.busy":"2024-06-04T09:51:00.662458Z","iopub.status.idle":"2024-06-04T09:51:21.924856Z","shell.execute_reply":"2024-06-04T09:51:21.923896Z","shell.execute_reply.started":"2024-06-04T09:51:00.663307Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":[" Now stop me if you've heard this one before, but there are a lot of large language models available today, and they have their own capabilities and specialities. What if I prefer to use one LLM to interpret some user queries in my business application, but a whole other LLM to author a response to those queries? Well, that scenario is exactly what Langchain Caters 2. Langchain is an open source orchestration framework for the development of applications that use large language models, and it comes in both Python and JavaScript libraries. It's essentially a generic interface for nearly any LLM, so you have a centralized development environment to build your large language model applications, and then integrate them with stuff like data sources and software workflows. Now, when it was launched by Harrison Chase in October 2022, Langchain enjoyed a meteoric rise, and by June of the following year, it was the single fastest growing open source projects on GitHub. And while the Langchain hype train has slightly cooled a little bit, there's plenty of utility here, so let's take a look at its components. So what makes up Langchain? Well, Langchain streamlines the programming of LLM applications through something called abstractions. What do I mean by that? Well, your thermostat that allows you to control the temperature in your home without needing to understand all the complex circuitry that this entails, we just set the temperature, that's an abstraction. So Langchain's abstractions represent common steps and concepts necessary to work with language models, and they can be chained together to create applications, minimizing the amount of code required to execute complex NLP tasks. So let's start with the LLM module. Now nearly any LLM can be used in Langchain, you just need an API key. The LLM class is designed to provide a standard interface for all models, so pick an LLM of your choice, be that a closed source one like GPT-4 or an open source one like LRMA-2, or this being Langchain, pick both. Okay, what else do we got? We have prompts. Now, prompts are the instructions given to a large language model, and the prompt template class in Langchain formalizes the composition of prompts without the need to manually hard code context and queries. A prompt template can contain instructions like do not use technical terms in your response, that would be a good one, or it could be a set of examples to guide its responses, that's called fuchsia prompting, or it could specify an output format. Now, chains as the name implies are the core of the Langchain's workflows. They combine LLMs with other components, creating applications by executing a sequence of functions. So let's say our application that needs to, first of all, retrieve data from a website, then it needs to summarize the text that gets back, and then finally it needs to use that summary to answer user submitted questions. That's a sequential chain where the output of one function acts as the input to the next, and each function in the chain could use different prompts, different parameters, and even different models. Now, to achieve certain tasks, LLMs might need to access specific external data sources that are not included in the training data set of the LLM itself. So things like internal documents or emails, that sort of thing. Now, Langchain collectively refers to this sort of documentation, as indexes, and there are a number of them. So let's take a look at a few. Now, one of them is called a document loader. Now, document loaders, they work with third-party applications for importing data sources from sources like fast storage services, I think dropbox or Google Drive, or web content from YouTube transcripts, or collaboration tools like air table or databases like pandas and MongoDB. There's also support for vector databases as well. Now, unlike traditional structured databases, vector databases represent data points by converting them into something called vector embeddings, which are numerical representations in the form of vectors with a fixed number of dimensions. And you can store a lot of information in this format as it's a very efficient means of retrieval. There are also something called text splitters, which can be very useful as well, because they can split text up into small, semantically meaningful chunks that can then be combined using the methods and parameters of your choosing. Now, LLMs by default don't really have any long-term memory of prior conversations, unless you happen to pass the chat history in as an input to your query. But LLMshain solves this problem with simple utilities for adding in memory into your application. And you have options for retaining like the entire conversations through two options to just retain a summarization of the conversation that we've had so far. And then finally, the last one we'll look at agents. Now, agents can use a given language model as a reasoning engine to determine which actions to take. And when building a chain for an agent, you'll want to include inputs like a list of the available tools that it should use, the user input, like the prompts and queries, and then any other relevant previously executed steps. So how can we put all of this to work for our applications? Well, let's talk about a few LLMshain use cases. Now, obviously we have chat bots. LLHain can be used to provide proper context for the specific use of a chat bot and to integrate chat bots into existing communication channels and workflows with their own APIs. We also have summarization. Language models can be tasked with summarizing many types of text from breaking down complex academic papers and transcripts to providing just a digest of incoming emails. We'll also see lots of examples where this is used for question answering. So using specific documents or specialized knowledge bases, LLMshain can retrieve the relevant information from the storage and then articulate helpful answers using the information that would otherwise not have been in their training data set. And yeah, this is a good one, data augmentation. LLMshain can be used to generate synthetic data for use of machine learning. So for example, the LLHain can be trained to generate additional samples that closely resemble the real data points in a training data set. And there are of course virtual agents as we already started to discuss. Integrated with the right workflows, LLHain's agent modules can use an LLM to autonomously determine the next steps and then take the action that it needs to complete that step using something called RPA or robotic process automation. LLHain is open source and free to use. There are also related frameworks like LLHain serve for creating chains as REST APIs and LLHain Smith, which provides tools to monitor a value and debug applications. Essentially LLHain's tools and APIs simplify the process of building applications that make use of large language models. If you have any questions, please drop us a line below and if you want to see more videos like this in the future, please like and subscribe. Thanks for watching.\n"]}],"source":["import whisper\n","\n","model = whisper.load_model('base')\n","result = model.transcribe('/kaggle/working/What is LangChain?.mp3')\n","print(result['text'])"]}],"metadata":{"kaggle":{"accelerator":"gpu","dataSources":[],"dockerImageVersionId":30699,"isGpuEnabled":true,"isInternetEnabled":true,"language":"python","sourceType":"notebook"},"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.13"}},"nbformat":4,"nbformat_minor":4}