Team Ai
Apppublic

Ubaidbhat/DatabaseTuned

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
experimenting.ipynb483 linesDownload Raw Back to root
1{2 "cells": [3  {4   "cell_type": "code",5   "execution_count": 2,6   "id": "108ad76c-9502-40d3-86b2-7309affc926e",7   "metadata": {},8   "outputs": [],9   "source": [10    "import shutil\n",11    "import requests\n",12    "import sys\n",13    "from typing import Optional, List, Tuple\n",14    "import json\n",15    "from langchain_community.llms import HuggingFaceHub"16   ]17  },18  {19   "cell_type": "code",20   "execution_count": 3,21   "id": "27579e99-9637-4fe5-902c-05c4969ea3aa",22   "metadata": {},23   "outputs": [24    {25     "data": {26      "application/vnd.jupyter.widget-view+json": {27       "model_id": "21ac52ca52fa4910a0e06e3286813c57",28       "version_major": 2,29       "version_minor": 030      },31      "text/plain": [32       "adapter_config.json:   0%|          | 0.00/701 [00:00<?, ?B/s]"33      ]34     },35     "metadata": {},36     "output_type": "display_data"37    },38    {39     "name": "stdout",40     "output_type": "stream",41     "text": [42      "HuggingFaceH4/zephyr-7b-beta\n"43     ]44    },45    {46     "data": {47      "application/vnd.jupyter.widget-view+json": {48       "model_id": "d5c705f48d0d40fbb1dd3d409b7f4d7f",49       "version_major": 2,50       "version_minor": 051      },52      "text/plain": [53       "config.json:   0%|          | 0.00/638 [00:00<?, ?B/s]"54      ]55     },56     "metadata": {},57     "output_type": "display_data"58    },59    {60     "data": {61      "application/vnd.jupyter.widget-view+json": {62       "model_id": "f209655a6cc743a99cdfa9e0fe40b9f3",63       "version_major": 2,64       "version_minor": 065      },66      "text/plain": [67       "model.safetensors.index.json:   0%|          | 0.00/23.9k [00:00<?, ?B/s]"68      ]69     },70     "metadata": {},71     "output_type": "display_data"72    },73    {74     "data": {75      "application/vnd.jupyter.widget-view+json": {76       "model_id": "4b5d3521692d4dee9a6989a37b97d7bf",77       "version_major": 2,78       "version_minor": 079      },80      "text/plain": [81       "Downloading shards:   0%|          | 0/8 [00:00<?, ?it/s]"82      ]83     },84     "metadata": {},85     "output_type": "display_data"86    },87    {88     "data": {89      "application/vnd.jupyter.widget-view+json": {90       "model_id": "f66b9cd6a8b04bddb99bbc34fafd6e2e",91       "version_major": 2,92       "version_minor": 093      },94      "text/plain": [95       "model-00001-of-00008.safetensors:   0%|          | 0.00/1.89G [00:00<?, ?B/s]"96      ]97     },98     "metadata": {},99     "output_type": "display_data"100    },101    {102     "data": {103      "application/vnd.jupyter.widget-view+json": {104       "model_id": "3469f59063564f2f87022dbb69bc893e",105       "version_major": 2,106       "version_minor": 0107      },108      "text/plain": [109       "model-00002-of-00008.safetensors:   0%|          | 0.00/1.95G [00:00<?, ?B/s]"110      ]111     },112     "metadata": {},113     "output_type": "display_data"114    },115    {116     "data": {117      "application/vnd.jupyter.widget-view+json": {118       "model_id": "2b9a7fd2ce904339b08fe41c4f912237",119       "version_major": 2,120       "version_minor": 0121      },122      "text/plain": [123       "model-00003-of-00008.safetensors:   0%|          | 0.00/1.98G [00:00<?, ?B/s]"124      ]125     },126     "metadata": {},127     "output_type": "display_data"128    },129    {130     "data": {131      "application/vnd.jupyter.widget-view+json": {132       "model_id": "04318a351ff3418685c823f9cf77f2cd",133       "version_major": 2,134       "version_minor": 0135      },136      "text/plain": [137       "model-00004-of-00008.safetensors:   0%|          | 0.00/1.95G [00:00<?, ?B/s]"138      ]139     },140     "metadata": {},141     "output_type": "display_data"142    },143    {144     "data": {145      "application/vnd.jupyter.widget-view+json": {146       "model_id": "87d710cb385e4db2ba8ffaf670841d40",147       "version_major": 2,148       "version_minor": 0149      },150      "text/plain": [151       "model-00005-of-00008.safetensors:   0%|          | 0.00/1.98G [00:00<?, ?B/s]"152      ]153     },154     "metadata": {},155     "output_type": "display_data"156    },157    {158     "data": {159      "application/vnd.jupyter.widget-view+json": {160       "model_id": "dcdf133396924c2b84c4a86e5badfabe",161       "version_major": 2,162       "version_minor": 0163      },164      "text/plain": [165       "model-00006-of-00008.safetensors:   0%|          | 0.00/1.95G [00:00<?, ?B/s]"166      ]167     },168     "metadata": {},169     "output_type": "display_data"170    },171    {172     "data": {173      "application/vnd.jupyter.widget-view+json": {174       "model_id": "1256b467e1a04d75a4bd969e081dff88",175       "version_major": 2,176       "version_minor": 0177      },178      "text/plain": [179       "model-00007-of-00008.safetensors:   0%|          | 0.00/1.98G [00:00<?, ?B/s]"180      ]181     },182     "metadata": {},183     "output_type": "display_data"184    },185    {186     "data": {187      "application/vnd.jupyter.widget-view+json": {188       "model_id": "40e036179dcf43bbac1e2c783638e086",189       "version_major": 2,190       "version_minor": 0191      },192      "text/plain": [193       "model-00008-of-00008.safetensors:   0%|          | 0.00/816M [00:00<?, ?B/s]"194      ]195     },196     "metadata": {},197     "output_type": "display_data"198    },199    {200     "data": {201      "application/vnd.jupyter.widget-view+json": {202       "model_id": "ff862b62cc0c4b3fa6c2d49e617d8dd2",203       "version_major": 2,204       "version_minor": 0205      },206      "text/plain": [207       "Loading checkpoint shards:   0%|          | 0/8 [00:00<?, ?it/s]"208      ]209     },210     "metadata": {},211     "output_type": "display_data"212    },213    {214     "data": {215      "application/vnd.jupyter.widget-view+json": {216       "model_id": "09733da86a4c429db42a5d99d34f7035",217       "version_major": 2,218       "version_minor": 0219      },220      "text/plain": [221       "generation_config.json:   0%|          | 0.00/111 [00:00<?, ?B/s]"222      ]223     },224     "metadata": {},225     "output_type": "display_data"226    },227    {228     "data": {229      "application/vnd.jupyter.widget-view+json": {230       "model_id": "a4951f876a47494aade4c74786c51b5b",231       "version_major": 2,232       "version_minor": 0233      },234      "text/plain": [235       "tokenizer_config.json:   0%|          | 0.00/1.43k [00:00<?, ?B/s]"236      ]237     },238     "metadata": {},239     "output_type": "display_data"240    },241    {242     "data": {243      "application/vnd.jupyter.widget-view+json": {244       "model_id": "267a73bd9a494d43899a2ffc75f19953",245       "version_major": 2,246       "version_minor": 0247      },248      "text/plain": [249       "tokenizer.model:   0%|          | 0.00/493k [00:00<?, ?B/s]"250      ]251     },252     "metadata": {},253     "output_type": "display_data"254    },255    {256     "data": {257      "application/vnd.jupyter.widget-view+json": {258       "model_id": "3536d4a8071041ec9137bc931366e77b",259       "version_major": 2,260       "version_minor": 0261      },262      "text/plain": [263       "tokenizer.json:   0%|          | 0.00/1.80M [00:00<?, ?B/s]"264      ]265     },266     "metadata": {},267     "output_type": "display_data"268    },269    {270     "data": {271      "application/vnd.jupyter.widget-view+json": {272       "model_id": "a66e862cfb374441b2caa7ac5b232435",273       "version_major": 2,274       "version_minor": 0275      },276      "text/plain": [277       "added_tokens.json:   0%|          | 0.00/42.0 [00:00<?, ?B/s]"278      ]279     },280     "metadata": {},281     "output_type": "display_data"282    },283    {284     "data": {285      "application/vnd.jupyter.widget-view+json": {286       "model_id": "ca714bb5a9c743219223f2163715e396",287       "version_major": 2,288       "version_minor": 0289      },290      "text/plain": [291       "special_tokens_map.json:   0%|          | 0.00/168 [00:00<?, ?B/s]"292      ]293     },294     "metadata": {},295     "output_type": "display_data"296    },297    {298     "data": {299      "application/vnd.jupyter.widget-view+json": {300       "model_id": "a1b80a7393944f06a43db4ba4419c293",301       "version_major": 2,302       "version_minor": 0303      },304      "text/plain": [305       "adapter_model.safetensors:   0%|          | 0.00/83.9M [00:00<?, ?B/s]"306      ]307     },308     "metadata": {},309     "output_type": "display_data"310    },311    {312     "name": "stderr",313     "output_type": "stream",314     "text": [315      "/usr/local/lib/python3.10/dist-packages/peft/tuners/lora/bnb.py:272: UserWarning: Merge lora module to 4-bit linear may get different generations due to rounding errors.\n",316      "  warnings.warn(\n"317     ]318    }319   ],320   "source": [321    "##Loading the Model to answer questions\n",322    "import torch\n",323    "from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n",324    "from peft import PeftModel, PeftConfig\n",325    "\n",326    "\n",327    "peft_model_id = \"Ubaidbhat/zephr_database_finetuned\"\n",328    "config = PeftConfig.from_pretrained(peft_model_id)\n",329    "print(config.base_model_name_or_path)\n",330    "bnb_config = BitsAndBytesConfig(\n",331    "    load_in_4bit = True,\n",332    "    bnb_4bit_use_double_quant=True,\n",333    "    bnb_4bit_quant_type=\"nf4\",\n",334    "    bnb_4bit_compute_dtype=torch.bfloat16\n",335    ")\n",336    "\n",337    "d_map = {\"\": torch.cuda.current_device()} if torch.cuda.is_available() else None\n",338    "\n",339    "model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, quantization_config=bnb_config, device_map=d_map)\n",340    "tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)\n",341    "model = PeftModel.from_pretrained(model, peft_model_id)\n",342    "model = model.merge_and_unload()"343   ]344  },345  {346   "cell_type": "code",347   "execution_count": 4,348   "id": "be4a09d2-6cff-4937-a6fb-4f58e4f895ff",349   "metadata": {},350   "outputs": [],351   "source": [352    "##Creating base Model Chain\n",353    "from langchain.llms import HuggingFacePipeline\n",354    "from langchain.prompts import PromptTemplate\n",355    "from transformers import pipeline\n",356    "from langchain_core.output_parsers import StrOutputParser\n",357    "from langchain.chains import LLMChain\n",358    "\n",359    "text_generation_pipeline = pipeline(\n",360    "    model=model,\n",361    "    tokenizer=tokenizer,\n",362    "    task=\"text-generation\",\n",363    "    temperature=0.2,\n",364    "    do_sample=True,\n",365    "    repetition_penalty=1.1,\n",366    "    return_full_text=True,\n",367    "    max_new_tokens=400,\n",368    "    pad_token_id=tokenizer.eos_token_id,\n",369    ")\n",370    "\n",371    "llm = HuggingFacePipeline(pipeline=text_generation_pipeline)\n",372    "\n",373    "prompt_template = \"\"\"\n",374    "<|system|>\n",375    "Answer the question based on your knowledge.\n",376    "</s>\n",377    "<|user|>\n",378    "{question}\n",379    "</s>\n",380    "<|assistant|>\n",381    "\"\"\"\n",382    "\n",383    "prompt = PromptTemplate(\n",384    "    input_variables=[\"question\"],\n",385    "    template=prompt_template,\n",386    ")\n",387    "\n",388    "llm_chain = prompt | llm | StrOutputParser()\n",389    "\n",390    "def inference(question):\n",391    "    llmAnswer = llm_chain.invoke({\"question\": question})\n",392    "    llmAnswer = llmAnswer.rstrip()\n",393    "    return llmAnswer"394   ]395  },396  {397   "cell_type": "code",398   "execution_count": 8,399   "id": "9e4c410a-5fdf-4b52-96e2-6745b874cb16",400   "metadata": {},401   "outputs": [402    {403     "name": "stdout",404     "output_type": "stream",405     "text": [406      "Running on local URL:  http://127.0.0.1:7864\n",407      "Running on public URL: https://98d1369a3563cae95e.gradio.live\n",408      "\n",409      "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"410     ]411    },412    {413     "data": {414      "text/html": [415       "<div><iframe src=\"https://98d1369a3563cae95e.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"416      ],417      "text/plain": [418       "<IPython.core.display.HTML object>"419      ]420     },421     "metadata": {},422     "output_type": "display_data"423    },424    {425     "data": {426      "text/plain": []427     },428     "execution_count": 8,429     "metadata": {},430     "output_type": "execute_result"431    }432   ],433   "source": [434    "import gradio as gr\n",435    "from langchain_core.runnables import RunnablePassthrough\n",436    "\n",437    "def predict(question):\n",438    "    return question\n",439    "    \n",440    "pred = gr.Interface(\n",441    "    fn=predict,\n",442    "    inputs=[\n",443    "        gr.Textbox(label=\"Question\", value = \"Your Question here......\"),\n",444    "    ],\n",445    "    outputs=\"text\",\n",446    "    title=\"Finetuned Zephr Model in the Database Management Domaain\"\n",447    ")\n",448    "\n",449    "pred.launch(share=True)"450   ]451  },452  {453   "cell_type": "code",454   "execution_count": null,455   "id": "c7be3ade-8cd6-448b-b608-0182a9743315",456   "metadata": {},457   "outputs": [],458   "source": []459  }460 ],461 "metadata": {462  "kernelspec": {463   "display_name": "Python 3 (ipykernel)",464   "language": "python",465   "name": "python3"466  },467  "language_info": {468   "codemirror_mode": {469    "name": "ipython",470    "version": 3471   },472   "file_extension": ".py",473   "mimetype": "text/x-python",474   "name": "python",475   "nbconvert_exporter": "python",476   "pygments_lexer": "ipython3",477   "version": "3.10.12"478  }479 },480 "nbformat": 4,481 "nbformat_minor": 5482}483