Spaces:
Runtime error
Runtime error
File size: 9,267 Bytes
72b7a20 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"id": "76BpiP5vMhpG"
},
"outputs": [],
"source": [
"# !pip install openai langchain python-dotenv -q"
]
},
{
"cell_type": "code",
"source": [
"# !pip install chromadb==0.3.22 tiktoken -q"
],
"metadata": {
"id": "ASD5ljxgNNbs"
},
"execution_count": 24,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# !pip install chromadb -U"
],
"metadata": {
"id": "8IWdv5UgNP6c"
},
"execution_count": 25,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# !pip install gradio"
],
"metadata": {
"id": "DliXsYaZOtAH"
},
"execution_count": 26,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from langchain.embeddings.openai import OpenAIEmbeddings\n",
"from langchain.vectorstores import Chroma\n",
"from langchain.text_splitter import CharacterTextSplitter\n",
"from langchain.chains.question_answering import load_qa_chain\n",
"from langchain.llms import OpenAI\n",
"import os\n"
],
"metadata": {
"id": "jGEXeboZNAb9"
},
"execution_count": 27,
"outputs": []
},
{
"cell_type": "code",
"source": [
"with open(\"/content/Data_Engineering.txt\") as f:\n",
" hitchhikersguide = f.read()"
],
"metadata": {
"id": "h4QnGIJYNjeM"
},
"execution_count": 28,
"outputs": []
},
{
"cell_type": "code",
"source": [
"text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0, separator = \"\\n\")\n",
"texts = text_splitter.split_text(hitchhikersguide)\n",
"print(f\"Final lenght: {len(texts)}\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "RmfWIfclN4DP",
"outputId": "58e3ffcf-b56a-4120-bcd9-718396bfa49c"
},
"execution_count": 29,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Final lenght: 1\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"### Setting up the OpenAI env\n",
"\n",
"!echo OPENAI_API_KEY=\"\" > .env"
],
"metadata": {
"id": "4Y4-ZTsZONsZ"
},
"execution_count": 30,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import os\n",
"import openai\n",
"from dotenv import load_dotenv\n",
"\n",
"load_dotenv(\".env\")\n",
"\n",
"openai.api_key = os.environ.get(\"OPENAI_API_KEY\")"
],
"metadata": {
"id": "PPYw5waOOT0D"
},
"execution_count": 31,
"outputs": []
},
{
"cell_type": "code",
"source": [
"embeddings = OpenAIEmbeddings()"
],
"metadata": {
"id": "pj-lRr3UODGm"
},
"execution_count": 32,
"outputs": []
},
{
"cell_type": "code",
"source": [
"docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{\"source\": str(i)} for i in range(len(texts))]).as_retriever()"
],
"metadata": {
"id": "DcDeDj9HOFgI"
},
"execution_count": 33,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Creating the Chain Model\n",
"chain = load_qa_chain(OpenAI(temperature=0), chain_type=\"stuff\")"
],
"metadata": {
"id": "7Sh5PEFoOcF9"
},
"execution_count": 34,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def make_inference(query):\n",
" docs = docsearch.get_relevant_documents(query)\n",
" return(chain.run(input_documents=docs, question=query))"
],
"metadata": {
"id": "meb-lvSsOgsM"
},
"execution_count": 35,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import gradio\n",
"\n",
"if __name__ == \"__main__\":\n",
" # make a gradio interface\n",
" import gradio as gr\n",
"\n",
" gr.Interface(\n",
" make_inference,\n",
" [\n",
" gr.inputs.Textbox(lines=2, label=\"Query\"),\n",
" ],\n",
" gr.outputs.Textbox(label=\"Response\"),\n",
" title=\"🗣️TalkToMyDoc📄\",\n",
" description=\"🗣️TalkToMyDoc📄 is a tool that allows you to ask questions about a document. In this case - Hitch Hitchhiker's Guide to the Galaxy.\",\n",
" ).launch()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 781
},
"id": "-btP40G1OkgI",
"outputId": "062d6b92-d8c2-4256-deef-023bb9b0292a"
},
"execution_count": 36,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"<ipython-input-36-636b02531079>:10: GradioDeprecationWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n",
" gr.inputs.Textbox(lines=2, label=\"Query\"),\n",
"<ipython-input-36-636b02531079>:10: GradioDeprecationWarning: `optional` parameter is deprecated, and it has no effect\n",
" gr.inputs.Textbox(lines=2, label=\"Query\"),\n",
"<ipython-input-36-636b02531079>:10: GradioDeprecationWarning: `numeric` parameter is deprecated, and it has no effect\n",
" gr.inputs.Textbox(lines=2, label=\"Query\"),\n",
"<ipython-input-36-636b02531079>:12: GradioDeprecationWarning: Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components\n",
" gr.outputs.Textbox(label=\"Response\"),\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n",
"Note: opening Chrome Inspector may crash demo inside Colab notebooks.\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Javascript object>"
],
"application/javascript": [
"(async (port, path, width, height, cache, element) => {\n",
" if (!google.colab.kernel.accessAllowed && !cache) {\n",
" return;\n",
" }\n",
" element.appendChild(document.createTextNode(''));\n",
" const url = await google.colab.kernel.proxyPort(port, {cache});\n",
"\n",
" const external_link = document.createElement('div');\n",
" external_link.innerHTML = `\n",
" <div style=\"font-family: monospace; margin-bottom: 0.5rem\">\n",
" Running on <a href=${new URL(path, url).toString()} target=\"_blank\">\n",
" https://localhost:${port}${path}\n",
" </a>\n",
" </div>\n",
" `;\n",
" element.appendChild(external_link);\n",
"\n",
" const iframe = document.createElement('iframe');\n",
" iframe.src = new URL(path, url).toString();\n",
" iframe.height = height;\n",
" iframe.allow = \"autoplay; camera; microphone; clipboard-read; clipboard-write;\"\n",
" iframe.width = width;\n",
" iframe.style.border = 0;\n",
" element.appendChild(iframe);\n",
" })(7861, \"/\", \"100%\", 500, false, window.element)"
]
},
"metadata": {}
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "fqFPXldYOm0X"
},
"execution_count": 36,
"outputs": []
}
]
} |