{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ " # Install Dependencies" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117 --upgrade" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install langchain einops accelerate transformers bitsandbytes" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Import Dependencies" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "d:\\YouTube\\6-06-2023 - Falcon\\falcon\\lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "from langchain import HuggingFacePipeline\n", "from langchain import PromptTemplate, LLMChain\n", "from transformers import AutoTokenizer, AutoModelForCausalLM\n", "import transformers\n", "import os \n", "import torch" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check if cuda is available \n", "torch.cuda.is_available()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Build the Pipeline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Define Model ID\n", "model_id = \"tiiuae/falcon-40b-instruct\"\n", "# Load Tokenizer\n", "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", "# Load Model \n", "model = AutoModelForCausalLM.from_pretrained(model_id, cache_dir='./workspace/', \n", " torch_dtype=torch.bfloat16, trust_remote_code=True, device_map=\"auto\", offload_folder=\"offload\")\n", "# Set PT model to inference mode\n", "model.eval()\n", "# Build HF Transformers pipeline \n", "pipeline = transformers.pipeline(\n", " \"text-generation\", \n", " model=model,\n", " tokenizer=tokenizer,\n", " device_map=\"auto\",\n", " max_length=400,\n", " do_sample=True,\n", " top_k=10,\n", " num_return_sequences=1,\n", " eos_token_id=tokenizer.eos_token_id\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Test out the pipeline\n", "pipeline('who is kim kardashian?')" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Pass it to Langchain" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Setup prompt template\n", "template = PromptTemplate(input_variables=['input'], template='{input}') \n", "# Pass hugging face pipeline to langchain class\n", "llm = HuggingFacePipeline(pipeline=pipeline) \n", "# Build stacked LLM chain i.e. prompt-formatting + LLM\n", "chain = LLMChain(llm=llm, prompt=template)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Test LLMChain \n", "response = chain.run('who is kim kardashian?')" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Build Gradio App" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Install Gradio for the UI component\n", "!pip install gradio" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Import gradio for UI\n", "import gradio as gr" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create generate function - this will be called when a user runs the gradio app \n", "def generate(prompt): \n", " # The prompt will get passed to the LLM Chain!\n", " return chain.run(prompt)\n", " # And will return responses " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Define a string variable to hold the title of the app\n", "title = '🦜🔗 Falcon-40b-Instruct'\n", "# Define another string variable to hold the description of the app\n", "description = 'This application demonstrates the use of the open-source `Falcon-40b-Instruct` LLM.'\n", "# pls subscribe 🙏" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Build gradio interface, define inputs and outputs...just text in this\n", "gr.Interface(fn=generate, inputs=[\"text\"], outputs=[\"text\"], \n", " # Pass through title and description\n", " title=title, description=description, \n", " # Set theme and launch parameters\n", " theme='finlaymacklon/boxy_violet').launch(server_port=8080, share=True)" ] } ], "metadata": { "kernelspec": { "display_name": "falcon", "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.9.12" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }