Spaces:
Runtime error
Runtime error
Upload 7 files
Browse files- .gitattributes +4 -0
- Equinix-Big.jpg +3 -0
- GenAI.ipynb +433 -0
- GenAI_1.ipynb +315 -0
- RAG.pdf +3 -0
- Team.pdf +3 -0
- Team1.pdf +3 -0
- equinix-sign.jpg +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,7 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
Equinix-Big.jpg filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
RAG.pdf filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
Team.pdf filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
Team1.pdf filter=lfs diff=lfs merge=lfs -text
|
Equinix-Big.jpg
ADDED
|
Git LFS Details
|
GenAI.ipynb
ADDED
|
@@ -0,0 +1,433 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "3a800e93-1e4a-40de-a211-4244e8d1a161",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"#!pip install -qU langchain-google-genai"
|
| 11 |
+
]
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"cell_type": "code",
|
| 15 |
+
"execution_count": 1,
|
| 16 |
+
"id": "764db45e-0ed0-480b-b338-2d7747d7746d",
|
| 17 |
+
"metadata": {},
|
| 18 |
+
"outputs": [],
|
| 19 |
+
"source": [
|
| 20 |
+
"from langchain_google_genai import ChatGoogleGenerativeAI\n",
|
| 21 |
+
"from langchain.prompts import PromptTemplate\n",
|
| 22 |
+
"from langchain.chains import LLMChain\n",
|
| 23 |
+
"import os\n",
|
| 24 |
+
"#from google.colab import userdata "
|
| 25 |
+
]
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"cell_type": "code",
|
| 29 |
+
"execution_count": 3,
|
| 30 |
+
"id": "d5b8529b-7206-4f51-804c-0a49b3242310",
|
| 31 |
+
"metadata": {},
|
| 32 |
+
"outputs": [],
|
| 33 |
+
"source": [
|
| 34 |
+
"import os\n",
|
| 35 |
+
"os.environ[\"MY_SECRET_KEY\"] = \"AIzaSyDRj3wAgqOCjc_D45W_u-G3y9dk5YDgxEo\"\n"
|
| 36 |
+
]
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
"cell_type": "code",
|
| 40 |
+
"execution_count": 4,
|
| 41 |
+
"id": "f484e386-e7a8-44a9-9ad5-0ec977a8b618",
|
| 42 |
+
"metadata": {},
|
| 43 |
+
"outputs": [],
|
| 44 |
+
"source": [
|
| 45 |
+
"#pip install fastapi uvicorn google-generativeai"
|
| 46 |
+
]
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"cell_type": "code",
|
| 50 |
+
"execution_count": 5,
|
| 51 |
+
"id": "bcb32fd9-805c-409c-aa27-2745867daf41",
|
| 52 |
+
"metadata": {},
|
| 53 |
+
"outputs": [],
|
| 54 |
+
"source": [
|
| 55 |
+
"from fastapi import FastAPI\n",
|
| 56 |
+
"import google.generativeai as genai\n",
|
| 57 |
+
"from fastapi.middleware.cors import CORSMiddleware\n",
|
| 58 |
+
"\n",
|
| 59 |
+
"# Configure Google Gemini API\n",
|
| 60 |
+
"from langchain_google_genai import ChatGoogleGenerativeAI"
|
| 61 |
+
]
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"cell_type": "code",
|
| 65 |
+
"execution_count": 6,
|
| 66 |
+
"id": "34b28dd0-5675-48ac-b07a-5ee27b5a04dd",
|
| 67 |
+
"metadata": {},
|
| 68 |
+
"outputs": [],
|
| 69 |
+
"source": [
|
| 70 |
+
"google_api_key = os.environ[\"MY_SECRET_KEY\"]\n",
|
| 71 |
+
"\n",
|
| 72 |
+
"# Check if the API key was found\n",
|
| 73 |
+
"if google_api_key:\n",
|
| 74 |
+
" # Set the environment variable if the API key was found\n",
|
| 75 |
+
" os.environ[\"GOOGLE_API_KEY\"] = google_api_key\n",
|
| 76 |
+
"\n",
|
| 77 |
+
" llm = ChatGoogleGenerativeAI(\n",
|
| 78 |
+
" model=\"gemini-pro\", # Specify the model name\n",
|
| 79 |
+
" google_api_key=os.environ[\"GOOGLE_API_KEY\"]\n",
|
| 80 |
+
" )\n",
|
| 81 |
+
"else:\n",
|
| 82 |
+
" print(\"Error: GOOGLE_API_KEY not found in Colab secrets. Please store your API key.\")\n",
|
| 83 |
+
"\n",
|
| 84 |
+
"\n",
|
| 85 |
+
"\n",
|
| 86 |
+
"genai.configure(api_key=google_api_key)\n",
|
| 87 |
+
"model = genai.GenerativeModel(\"gemini-pro\")"
|
| 88 |
+
]
|
| 89 |
+
},
|
| 90 |
+
{
|
| 91 |
+
"cell_type": "code",
|
| 92 |
+
"execution_count": 7,
|
| 93 |
+
"id": "c452755f-fad6-455f-9822-7c66b36d724f",
|
| 94 |
+
"metadata": {},
|
| 95 |
+
"outputs": [],
|
| 96 |
+
"source": [
|
| 97 |
+
"# Initialize FastAPI\n",
|
| 98 |
+
"app = FastAPI()\n",
|
| 99 |
+
"\n",
|
| 100 |
+
"# Enable CORS for frontend access\n",
|
| 101 |
+
"app.add_middleware(\n",
|
| 102 |
+
" CORSMiddleware,\n",
|
| 103 |
+
" allow_origins=[\"*\"],\n",
|
| 104 |
+
" allow_credentials=True,\n",
|
| 105 |
+
" allow_methods=[\"*\"],\n",
|
| 106 |
+
" allow_headers=[\"*\"],\n",
|
| 107 |
+
")\n",
|
| 108 |
+
"\n",
|
| 109 |
+
"@app.get(\"/chat\")\n",
|
| 110 |
+
"def chat(query: str):\n",
|
| 111 |
+
" response = model.generate_content(query)\n",
|
| 112 |
+
" return {\"response\": response.text}\n",
|
| 113 |
+
"\n",
|
| 114 |
+
"# Run the server: uvicorn backend:app --reload"
|
| 115 |
+
]
|
| 116 |
+
},
|
| 117 |
+
{
|
| 118 |
+
"cell_type": "code",
|
| 119 |
+
"execution_count": 8,
|
| 120 |
+
"id": "cde9c8d0-80a4-4943-a78a-a75ca4825e34",
|
| 121 |
+
"metadata": {},
|
| 122 |
+
"outputs": [
|
| 123 |
+
{
|
| 124 |
+
"name": "stderr",
|
| 125 |
+
"output_type": "stream",
|
| 126 |
+
"text": [
|
| 127 |
+
"/var/folders/y9/krs1m7td1p33yj75p9f1s5740000gn/T/ipykernel_11301/2071011701.py:7: LangChainDeprecationWarning: The class `LLMChain` was deprecated in LangChain 0.1.17 and will be removed in 1.0. Use :meth:`~RunnableSequence, e.g., `prompt | llm`` instead.\n",
|
| 128 |
+
" chain = LLMChain(llm=llm, prompt=prompt)\n",
|
| 129 |
+
"/var/folders/y9/krs1m7td1p33yj75p9f1s5740000gn/T/ipykernel_11301/2071011701.py:11: LangChainDeprecationWarning: The method `Chain.run` was deprecated in langchain 0.1.0 and will be removed in 1.0. Use :meth:`~invoke` instead.\n",
|
| 130 |
+
" response = chain.run(query=query)\n"
|
| 131 |
+
]
|
| 132 |
+
},
|
| 133 |
+
{
|
| 134 |
+
"name": "stdout",
|
| 135 |
+
"output_type": "stream",
|
| 136 |
+
"text": [
|
| 137 |
+
"Chatbot Response: The capital of France is Paris.\n"
|
| 138 |
+
]
|
| 139 |
+
}
|
| 140 |
+
],
|
| 141 |
+
"source": [
|
| 142 |
+
"prompt = PromptTemplate.from_template(\"Answer the following query: {query}\")\n",
|
| 143 |
+
"\n",
|
| 144 |
+
"# Initialize LLM\n",
|
| 145 |
+
"llm = ChatGoogleGenerativeAI(model=\"gemini-2.0-flash\", temperature=0)\n",
|
| 146 |
+
"\n",
|
| 147 |
+
"# Create an LLM Chain\n",
|
| 148 |
+
"chain = LLMChain(llm=llm, prompt=prompt)\n",
|
| 149 |
+
"\n",
|
| 150 |
+
"# Run chatbot\n",
|
| 151 |
+
"query = \"What is the capital of France?\"\n",
|
| 152 |
+
"response = chain.run(query=query)\n",
|
| 153 |
+
"print(\"Chatbot Response:\", response)"
|
| 154 |
+
]
|
| 155 |
+
},
|
| 156 |
+
{
|
| 157 |
+
"cell_type": "code",
|
| 158 |
+
"execution_count": 9,
|
| 159 |
+
"id": "da1d9899-328b-47c2-87a5-4175519bacdc",
|
| 160 |
+
"metadata": {},
|
| 161 |
+
"outputs": [
|
| 162 |
+
{
|
| 163 |
+
"name": "stdout",
|
| 164 |
+
"output_type": "stream",
|
| 165 |
+
"text": [
|
| 166 |
+
"Chatbot Response: The capital of India is **New Delhi**.\n"
|
| 167 |
+
]
|
| 168 |
+
}
|
| 169 |
+
],
|
| 170 |
+
"source": [
|
| 171 |
+
"# Run chatbot\n",
|
| 172 |
+
"query = \"What is the capital of India?\"\n",
|
| 173 |
+
"response = chain.run(query=query)\n",
|
| 174 |
+
"print(\"Chatbot Response:\", response)"
|
| 175 |
+
]
|
| 176 |
+
},
|
| 177 |
+
{
|
| 178 |
+
"cell_type": "code",
|
| 179 |
+
"execution_count": 24,
|
| 180 |
+
"id": "d42c4cf2-8b96-4310-8692-350d0d9c85b4",
|
| 181 |
+
"metadata": {},
|
| 182 |
+
"outputs": [
|
| 183 |
+
{
|
| 184 |
+
"data": {
|
| 185 |
+
"text/plain": [
|
| 186 |
+
"'/Users/saurabhverma/GENAI'"
|
| 187 |
+
]
|
| 188 |
+
},
|
| 189 |
+
"execution_count": 24,
|
| 190 |
+
"metadata": {},
|
| 191 |
+
"output_type": "execute_result"
|
| 192 |
+
}
|
| 193 |
+
],
|
| 194 |
+
"source": [
|
| 195 |
+
"os.getcwd()"
|
| 196 |
+
]
|
| 197 |
+
},
|
| 198 |
+
{
|
| 199 |
+
"cell_type": "code",
|
| 200 |
+
"execution_count": null,
|
| 201 |
+
"id": "161cd070-2ac4-422a-8199-2a7cc42ac335",
|
| 202 |
+
"metadata": {},
|
| 203 |
+
"outputs": [],
|
| 204 |
+
"source": [
|
| 205 |
+
"# UI with Gradio\n",
|
| 206 |
+
"def chat_interface(question):\n",
|
| 207 |
+
" return rag_pipeline(question)\n",
|
| 208 |
+
"\n",
|
| 209 |
+
"ui = gr.Interface(fn=chat_interface, inputs=\"text\", outputs=\"text\", title=\"RAG Chat with Gemini\")\n",
|
| 210 |
+
"ui.launch()"
|
| 211 |
+
]
|
| 212 |
+
},
|
| 213 |
+
{
|
| 214 |
+
"cell_type": "code",
|
| 215 |
+
"execution_count": 9,
|
| 216 |
+
"id": "cc81ff71-c152-4780-bb90-31f1df623f7e",
|
| 217 |
+
"metadata": {},
|
| 218 |
+
"outputs": [
|
| 219 |
+
{
|
| 220 |
+
"name": "stdout",
|
| 221 |
+
"output_type": "stream",
|
| 222 |
+
"text": [
|
| 223 |
+
"Collecting gradio\n",
|
| 224 |
+
" Downloading gradio-5.20.1-py3-none-any.whl.metadata (16 kB)\n",
|
| 225 |
+
"Collecting aiofiles<24.0,>=22.0 (from gradio)\n",
|
| 226 |
+
" Downloading aiofiles-23.2.1-py3-none-any.whl.metadata (9.7 kB)\n",
|
| 227 |
+
"Requirement already satisfied: anyio<5.0,>=3.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (4.2.0)\n",
|
| 228 |
+
"Requirement already satisfied: fastapi<1.0,>=0.115.2 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (0.115.11)\n",
|
| 229 |
+
"Collecting ffmpy (from gradio)\n",
|
| 230 |
+
" Downloading ffmpy-0.5.0-py3-none-any.whl.metadata (3.0 kB)\n",
|
| 231 |
+
"Collecting gradio-client==1.7.2 (from gradio)\n",
|
| 232 |
+
" Downloading gradio_client-1.7.2-py3-none-any.whl.metadata (7.1 kB)\n",
|
| 233 |
+
"Collecting groovy~=0.1 (from gradio)\n",
|
| 234 |
+
" Downloading groovy-0.1.2-py3-none-any.whl.metadata (6.1 kB)\n",
|
| 235 |
+
"Requirement already satisfied: httpx>=0.24.1 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (0.27.0)\n",
|
| 236 |
+
"Collecting huggingface-hub>=0.28.1 (from gradio)\n",
|
| 237 |
+
" Downloading huggingface_hub-0.29.2-py3-none-any.whl.metadata (13 kB)\n",
|
| 238 |
+
"Requirement already satisfied: jinja2<4.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (3.1.4)\n",
|
| 239 |
+
"Requirement already satisfied: markupsafe~=2.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (2.1.3)\n",
|
| 240 |
+
"Requirement already satisfied: numpy<3.0,>=1.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (1.26.4)\n",
|
| 241 |
+
"Requirement already satisfied: orjson~=3.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (3.10.15)\n",
|
| 242 |
+
"Requirement already satisfied: packaging in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (23.2)\n",
|
| 243 |
+
"Requirement already satisfied: pandas<3.0,>=1.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (2.2.2)\n",
|
| 244 |
+
"Requirement already satisfied: pillow<12.0,>=8.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (10.3.0)\n",
|
| 245 |
+
"Requirement already satisfied: pydantic>=2.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (2.10.3)\n",
|
| 246 |
+
"Collecting pydub (from gradio)\n",
|
| 247 |
+
" Downloading pydub-0.25.1-py2.py3-none-any.whl.metadata (1.4 kB)\n",
|
| 248 |
+
"Collecting python-multipart>=0.0.18 (from gradio)\n",
|
| 249 |
+
" Downloading python_multipart-0.0.20-py3-none-any.whl.metadata (1.8 kB)\n",
|
| 250 |
+
"Requirement already satisfied: pyyaml<7.0,>=5.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (6.0.1)\n",
|
| 251 |
+
"Collecting ruff>=0.9.3 (from gradio)\n",
|
| 252 |
+
" Downloading ruff-0.9.10-py3-none-macosx_11_0_arm64.whl.metadata (25 kB)\n",
|
| 253 |
+
"Collecting safehttpx<0.2.0,>=0.1.6 (from gradio)\n",
|
| 254 |
+
" Downloading safehttpx-0.1.6-py3-none-any.whl.metadata (4.2 kB)\n",
|
| 255 |
+
"Collecting semantic-version~=2.0 (from gradio)\n",
|
| 256 |
+
" Downloading semantic_version-2.10.0-py2.py3-none-any.whl.metadata (9.7 kB)\n",
|
| 257 |
+
"Requirement already satisfied: starlette<1.0,>=0.40.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (0.46.1)\n",
|
| 258 |
+
"Collecting tomlkit<0.14.0,>=0.12.0 (from gradio)\n",
|
| 259 |
+
" Downloading tomlkit-0.13.2-py3-none-any.whl.metadata (2.7 kB)\n",
|
| 260 |
+
"Collecting typer<1.0,>=0.12 (from gradio)\n",
|
| 261 |
+
" Downloading typer-0.15.2-py3-none-any.whl.metadata (15 kB)\n",
|
| 262 |
+
"Requirement already satisfied: typing-extensions~=4.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (4.12.2)\n",
|
| 263 |
+
"Requirement already satisfied: uvicorn>=0.14.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (0.34.0)\n",
|
| 264 |
+
"Requirement already satisfied: fsspec in /opt/anaconda3/lib/python3.12/site-packages (from gradio-client==1.7.2->gradio) (2024.3.1)\n",
|
| 265 |
+
"Collecting websockets<16.0,>=10.0 (from gradio-client==1.7.2->gradio)\n",
|
| 266 |
+
" Downloading websockets-15.0.1-cp312-cp312-macosx_11_0_arm64.whl.metadata (6.8 kB)\n",
|
| 267 |
+
"Requirement already satisfied: idna>=2.8 in /opt/anaconda3/lib/python3.12/site-packages (from anyio<5.0,>=3.0->gradio) (3.7)\n",
|
| 268 |
+
"Requirement already satisfied: sniffio>=1.1 in /opt/anaconda3/lib/python3.12/site-packages (from anyio<5.0,>=3.0->gradio) (1.3.0)\n",
|
| 269 |
+
"Requirement already satisfied: certifi in /opt/anaconda3/lib/python3.12/site-packages (from httpx>=0.24.1->gradio) (2024.12.14)\n",
|
| 270 |
+
"Requirement already satisfied: httpcore==1.* in /opt/anaconda3/lib/python3.12/site-packages (from httpx>=0.24.1->gradio) (1.0.2)\n",
|
| 271 |
+
"Requirement already satisfied: h11<0.15,>=0.13 in /opt/anaconda3/lib/python3.12/site-packages (from httpcore==1.*->httpx>=0.24.1->gradio) (0.14.0)\n",
|
| 272 |
+
"Requirement already satisfied: filelock in /opt/anaconda3/lib/python3.12/site-packages (from huggingface-hub>=0.28.1->gradio) (3.13.1)\n",
|
| 273 |
+
"Requirement already satisfied: requests in /opt/anaconda3/lib/python3.12/site-packages (from huggingface-hub>=0.28.1->gradio) (2.32.2)\n",
|
| 274 |
+
"Requirement already satisfied: tqdm>=4.42.1 in /opt/anaconda3/lib/python3.12/site-packages (from huggingface-hub>=0.28.1->gradio) (4.66.4)\n",
|
| 275 |
+
"Requirement already satisfied: python-dateutil>=2.8.2 in /opt/anaconda3/lib/python3.12/site-packages (from pandas<3.0,>=1.0->gradio) (2.9.0.post0)\n",
|
| 276 |
+
"Requirement already satisfied: pytz>=2020.1 in /opt/anaconda3/lib/python3.12/site-packages (from pandas<3.0,>=1.0->gradio) (2024.1)\n",
|
| 277 |
+
"Requirement already satisfied: tzdata>=2022.7 in /opt/anaconda3/lib/python3.12/site-packages (from pandas<3.0,>=1.0->gradio) (2023.3)\n",
|
| 278 |
+
"Requirement already satisfied: annotated-types>=0.6.0 in /opt/anaconda3/lib/python3.12/site-packages (from pydantic>=2.0->gradio) (0.6.0)\n",
|
| 279 |
+
"Requirement already satisfied: pydantic-core==2.27.1 in /opt/anaconda3/lib/python3.12/site-packages (from pydantic>=2.0->gradio) (2.27.1)\n",
|
| 280 |
+
"Requirement already satisfied: click>=8.0.0 in /opt/anaconda3/lib/python3.12/site-packages (from typer<1.0,>=0.12->gradio) (8.1.7)\n",
|
| 281 |
+
"Requirement already satisfied: shellingham>=1.3.0 in /opt/anaconda3/lib/python3.12/site-packages (from typer<1.0,>=0.12->gradio) (1.5.0)\n",
|
| 282 |
+
"Requirement already satisfied: rich>=10.11.0 in /opt/anaconda3/lib/python3.12/site-packages (from typer<1.0,>=0.12->gradio) (13.3.5)\n",
|
| 283 |
+
"Requirement already satisfied: six>=1.5 in /opt/anaconda3/lib/python3.12/site-packages (from python-dateutil>=2.8.2->pandas<3.0,>=1.0->gradio) (1.16.0)\n",
|
| 284 |
+
"Requirement already satisfied: markdown-it-py<3.0.0,>=2.2.0 in /opt/anaconda3/lib/python3.12/site-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.2.0)\n",
|
| 285 |
+
"Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /opt/anaconda3/lib/python3.12/site-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.15.1)\n",
|
| 286 |
+
"Requirement already satisfied: charset-normalizer<4,>=2 in /opt/anaconda3/lib/python3.12/site-packages (from requests->huggingface-hub>=0.28.1->gradio) (2.0.4)\n",
|
| 287 |
+
"Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/anaconda3/lib/python3.12/site-packages (from requests->huggingface-hub>=0.28.1->gradio) (2.2.2)\n",
|
| 288 |
+
"Requirement already satisfied: mdurl~=0.1 in /opt/anaconda3/lib/python3.12/site-packages (from markdown-it-py<3.0.0,>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio) (0.1.0)\n",
|
| 289 |
+
"Downloading gradio-5.20.1-py3-none-any.whl (62.3 MB)\n",
|
| 290 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.3/62.3 MB\u001b[0m \u001b[31m11.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
|
| 291 |
+
"\u001b[?25hDownloading gradio_client-1.7.2-py3-none-any.whl (322 kB)\n",
|
| 292 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m322.1/322.1 kB\u001b[0m \u001b[31m12.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 293 |
+
"\u001b[?25hDownloading aiofiles-23.2.1-py3-none-any.whl (15 kB)\n",
|
| 294 |
+
"Downloading groovy-0.1.2-py3-none-any.whl (14 kB)\n",
|
| 295 |
+
"Downloading huggingface_hub-0.29.2-py3-none-any.whl (468 kB)\n",
|
| 296 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m468.1/468.1 kB\u001b[0m \u001b[31m12.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 297 |
+
"\u001b[?25hDownloading python_multipart-0.0.20-py3-none-any.whl (24 kB)\n",
|
| 298 |
+
"Downloading ruff-0.9.10-py3-none-macosx_11_0_arm64.whl (10.2 MB)\n",
|
| 299 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m10.2/10.2 MB\u001b[0m \u001b[31m14.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m0:01\u001b[0m\n",
|
| 300 |
+
"\u001b[?25hDownloading safehttpx-0.1.6-py3-none-any.whl (8.7 kB)\n",
|
| 301 |
+
"Downloading semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)\n",
|
| 302 |
+
"Downloading tomlkit-0.13.2-py3-none-any.whl (37 kB)\n",
|
| 303 |
+
"Downloading typer-0.15.2-py3-none-any.whl (45 kB)\n",
|
| 304 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m45.1/45.1 kB\u001b[0m \u001b[31m3.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 305 |
+
"\u001b[?25hDownloading ffmpy-0.5.0-py3-none-any.whl (6.0 kB)\n",
|
| 306 |
+
"Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n",
|
| 307 |
+
"Downloading websockets-15.0.1-cp312-cp312-macosx_11_0_arm64.whl (173 kB)\n",
|
| 308 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m173.3/173.3 kB\u001b[0m \u001b[31m12.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 309 |
+
"\u001b[?25hInstalling collected packages: pydub, websockets, tomlkit, semantic-version, ruff, python-multipart, groovy, ffmpy, aiofiles, huggingface-hub, typer, safehttpx, gradio-client, gradio\n",
|
| 310 |
+
" Attempting uninstall: tomlkit\n",
|
| 311 |
+
" Found existing installation: tomlkit 0.11.1\n",
|
| 312 |
+
" Uninstalling tomlkit-0.11.1:\n",
|
| 313 |
+
" Successfully uninstalled tomlkit-0.11.1\n",
|
| 314 |
+
" Attempting uninstall: typer\n",
|
| 315 |
+
" Found existing installation: typer 0.9.0\n",
|
| 316 |
+
" Uninstalling typer-0.9.0:\n",
|
| 317 |
+
" Successfully uninstalled typer-0.9.0\n",
|
| 318 |
+
"Successfully installed aiofiles-23.2.1 ffmpy-0.5.0 gradio-5.20.1 gradio-client-1.7.2 groovy-0.1.2 huggingface-hub-0.29.2 pydub-0.25.1 python-multipart-0.0.20 ruff-0.9.10 safehttpx-0.1.6 semantic-version-2.10.0 tomlkit-0.13.2 typer-0.15.2 websockets-15.0.1\n"
|
| 319 |
+
]
|
| 320 |
+
}
|
| 321 |
+
],
|
| 322 |
+
"source": [
|
| 323 |
+
"#pip install -U langchain-community\n",
|
| 324 |
+
"!pip install gradio"
|
| 325 |
+
]
|
| 326 |
+
},
|
| 327 |
+
{
|
| 328 |
+
"cell_type": "code",
|
| 329 |
+
"execution_count": 11,
|
| 330 |
+
"id": "8c816fe4-5454-4e30-bbf9-cda7214943f5",
|
| 331 |
+
"metadata": {},
|
| 332 |
+
"outputs": [
|
| 333 |
+
{
|
| 334 |
+
"name": "stderr",
|
| 335 |
+
"output_type": "stream",
|
| 336 |
+
"text": [
|
| 337 |
+
"/var/folders/y9/krs1m7td1p33yj75p9f1s5740000gn/T/ipykernel_16004/3095841870.py:27: LangChainDeprecationWarning: The class `OpenAIEmbeddings` was deprecated in LangChain 0.0.9 and will be removed in 1.0. An updated version of the class exists in the :class:`~langchain-openai package and should be used instead. To use it run `pip install -U :class:`~langchain-openai` and import as `from :class:`~langchain_openai import OpenAIEmbeddings``.\n",
|
| 338 |
+
" vector_store = Chroma.from_documents(docs, embedding=OpenAIEmbeddings())\n"
|
| 339 |
+
]
|
| 340 |
+
},
|
| 341 |
+
{
|
| 342 |
+
"ename": "ValidationError",
|
| 343 |
+
"evalue": "1 validation error for OpenAIEmbeddings\n Value error, Did not find openai_api_key, please add an environment variable `OPENAI_API_KEY` which contains it, or pass `openai_api_key` as a named parameter. [type=value_error, input_value={'model_kwargs': {}, 'cli...20, 'http_client': None}, input_type=dict]\n For further information visit https://errors.pydantic.dev/2.10/v/value_error",
|
| 344 |
+
"output_type": "error",
|
| 345 |
+
"traceback": [
|
| 346 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
| 347 |
+
"\u001b[0;31mValidationError\u001b[0m Traceback (most recent call last)",
|
| 348 |
+
"Cell \u001b[0;32mIn[11], line 27\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[38;5;66;03m# Create Vector Database\u001b[39;00m\n\u001b[1;32m 26\u001b[0m docs \u001b[38;5;241m=\u001b[39m load_docs()\n\u001b[0;32m---> 27\u001b[0m vector_store \u001b[38;5;241m=\u001b[39m Chroma\u001b[38;5;241m.\u001b[39mfrom_documents(docs, embedding\u001b[38;5;241m=\u001b[39mOpenAIEmbeddings())\n\u001b[1;32m 29\u001b[0m \u001b[38;5;66;03m# RAG Pipeline: Retrieve and Generate\u001b[39;00m\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrag_pipeline\u001b[39m(query):\n",
|
| 349 |
+
"File \u001b[0;32m/opt/anaconda3/lib/python3.12/site-packages/langchain_core/_api/deprecation.py:214\u001b[0m, in \u001b[0;36mdeprecated.<locals>.deprecate.<locals>.finalize.<locals>.warn_if_direct_instance\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 212\u001b[0m warned \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m 213\u001b[0m emit_warning()\n\u001b[0;32m--> 214\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m wrapped(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
|
| 350 |
+
"File \u001b[0;32m/opt/anaconda3/lib/python3.12/site-packages/pydantic/main.py:214\u001b[0m, in \u001b[0;36mBaseModel.__init__\u001b[0;34m(self, **data)\u001b[0m\n\u001b[1;32m 212\u001b[0m \u001b[38;5;66;03m# `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks\u001b[39;00m\n\u001b[1;32m 213\u001b[0m __tracebackhide__ \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m--> 214\u001b[0m validated_self \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m__pydantic_validator__\u001b[38;5;241m.\u001b[39mvalidate_python(data, self_instance\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m)\n\u001b[1;32m 215\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m validated_self:\n\u001b[1;32m 216\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(\n\u001b[1;32m 217\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mA custom validator is returning a value other than `self`.\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 218\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mReturning anything other than `self` from a top level model validator isn\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt supported when validating via `__init__`.\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 219\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mSee the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[1;32m 220\u001b[0m stacklevel\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m,\n\u001b[1;32m 221\u001b[0m )\n",
|
| 351 |
+
"\u001b[0;31mValidationError\u001b[0m: 1 validation error for OpenAIEmbeddings\n Value error, Did not find openai_api_key, please add an environment variable `OPENAI_API_KEY` which contains it, or pass `openai_api_key` as a named parameter. [type=value_error, input_value={'model_kwargs': {}, 'cli...20, 'http_client': None}, input_type=dict]\n For further information visit https://errors.pydantic.dev/2.10/v/value_error"
|
| 352 |
+
]
|
| 353 |
+
}
|
| 354 |
+
],
|
| 355 |
+
"source": [
|
| 356 |
+
"import google.generativeai as genai\n",
|
| 357 |
+
"from langchain.vectorstores import Chroma\n",
|
| 358 |
+
"from langchain.embeddings import OpenAIEmbeddings\n",
|
| 359 |
+
"from langchain.schema import Document\n",
|
| 360 |
+
"from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
|
| 361 |
+
"from langchain_community.document_loaders import TextLoader\n",
|
| 362 |
+
"import gradio as gr\n",
|
| 363 |
+
"\n",
|
| 364 |
+
"# Configure Google Gemini API\n",
|
| 365 |
+
"GOOGLE_API_KEY = \"AIzaSyDRj3wAgqOCjc_D45W_u-G3y9dk5YDgxEo\"\n",
|
| 366 |
+
"genai.configure(api_key=GOOGLE_API_KEY)\n",
|
| 367 |
+
"model = genai.GenerativeModel(\"gemini-pro\")\n",
|
| 368 |
+
"\n",
|
| 369 |
+
"# Load and process documents\n",
|
| 370 |
+
"def load_docs():\n",
|
| 371 |
+
" raw_text = \"\"\"\n",
|
| 372 |
+
" Machine learning is a branch of artificial intelligence (AI) focused on building applications that learn from data and improve their accuracy over time.\n",
|
| 373 |
+
" Supervised learning uses labeled data, while unsupervised learning finds hidden patterns.\n",
|
| 374 |
+
" Reinforcement learning is based on rewards and penalties.\n",
|
| 375 |
+
" \"\"\"\n",
|
| 376 |
+
" text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=10)\n",
|
| 377 |
+
" docs = [Document(page_content=text) for text in text_splitter.split_text(raw_text)]\n",
|
| 378 |
+
" return docs\n",
|
| 379 |
+
"\n",
|
| 380 |
+
"# Create Vector Database\n",
|
| 381 |
+
"docs = load_docs()\n",
|
| 382 |
+
"vector_store = Chroma.from_documents(docs, embedding=OpenAIEmbeddings())\n",
|
| 383 |
+
"\n",
|
| 384 |
+
"# RAG Pipeline: Retrieve and Generate\n",
|
| 385 |
+
"def rag_pipeline(query):\n",
|
| 386 |
+
" results = vector_store.similarity_search(query, k=2)\n",
|
| 387 |
+
" context = \" \".join([doc.page_content for doc in results])\n",
|
| 388 |
+
" \n",
|
| 389 |
+
" # Pass context + query to Gemini\n",
|
| 390 |
+
" full_prompt = f\"Context: {context}\\n\\nQuestion: {query}\\nAnswer:\"\n",
|
| 391 |
+
" response = model.generate_content(full_prompt)\n",
|
| 392 |
+
" \n",
|
| 393 |
+
" return response.text\n",
|
| 394 |
+
"\n",
|
| 395 |
+
"# UI with Gradio\n",
|
| 396 |
+
"def chat_interface(question):\n",
|
| 397 |
+
" return rag_pipeline(question)\n",
|
| 398 |
+
"\n",
|
| 399 |
+
"ui = gr.Interface(fn=chat_interface, inputs=\"text\", outputs=\"text\", title=\"RAG Chat with Gemini\")\n",
|
| 400 |
+
"ui.launch()\n"
|
| 401 |
+
]
|
| 402 |
+
},
|
| 403 |
+
{
|
| 404 |
+
"cell_type": "code",
|
| 405 |
+
"execution_count": null,
|
| 406 |
+
"id": "93bcb8cd-9202-45c1-98e3-9f9b06387fc2",
|
| 407 |
+
"metadata": {},
|
| 408 |
+
"outputs": [],
|
| 409 |
+
"source": []
|
| 410 |
+
}
|
| 411 |
+
],
|
| 412 |
+
"metadata": {
|
| 413 |
+
"kernelspec": {
|
| 414 |
+
"display_name": "Python 3 (ipykernel)",
|
| 415 |
+
"language": "python",
|
| 416 |
+
"name": "python3"
|
| 417 |
+
},
|
| 418 |
+
"language_info": {
|
| 419 |
+
"codemirror_mode": {
|
| 420 |
+
"name": "ipython",
|
| 421 |
+
"version": 3
|
| 422 |
+
},
|
| 423 |
+
"file_extension": ".py",
|
| 424 |
+
"mimetype": "text/x-python",
|
| 425 |
+
"name": "python",
|
| 426 |
+
"nbconvert_exporter": "python",
|
| 427 |
+
"pygments_lexer": "ipython3",
|
| 428 |
+
"version": "3.12.4"
|
| 429 |
+
}
|
| 430 |
+
},
|
| 431 |
+
"nbformat": 4,
|
| 432 |
+
"nbformat_minor": 5
|
| 433 |
+
}
|
GenAI_1.ipynb
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 2,
|
| 6 |
+
"id": "9633aea7-5c45-44f9-a78b-b5bc39984754",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"from langchain_google_genai import ChatGoogleGenerativeAI\n",
|
| 11 |
+
"from langchain.prompts import PromptTemplate\n",
|
| 12 |
+
"from langchain.chains import LLMChain\n",
|
| 13 |
+
"\n",
|
| 14 |
+
"import os\n",
|
| 15 |
+
"\n",
|
| 16 |
+
"import google.generativeai as genai\n",
|
| 17 |
+
"from langchain.document_loaders import PyPDFLoader\n",
|
| 18 |
+
"from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
|
| 19 |
+
"from langchain_google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings\n",
|
| 20 |
+
"from langchain.vectorstores import FAISS\n",
|
| 21 |
+
"import gradio as gr\n",
|
| 22 |
+
"\n",
|
| 23 |
+
"\n",
|
| 24 |
+
"os.environ[\"MY_SECRET_KEY\"] = \"AIzaSyDRj3wAgqOCjc_D45W_u-G3y9dk5YDgxEo\""
|
| 25 |
+
]
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"cell_type": "code",
|
| 29 |
+
"execution_count": 3,
|
| 30 |
+
"id": "41abde7b-366d-427e-8938-35ce7a4ed778",
|
| 31 |
+
"metadata": {},
|
| 32 |
+
"outputs": [],
|
| 33 |
+
"source": [
|
| 34 |
+
"#pip install pypdf\n",
|
| 35 |
+
"#!pip install faiss-cpu"
|
| 36 |
+
]
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
"cell_type": "code",
|
| 40 |
+
"execution_count": 4,
|
| 41 |
+
"id": "b7e3810f-c5fb-44d7-b4b7-a30ac507d78b",
|
| 42 |
+
"metadata": {},
|
| 43 |
+
"outputs": [],
|
| 44 |
+
"source": [
|
| 45 |
+
"google_api_key = os.environ[\"MY_SECRET_KEY\"]\n",
|
| 46 |
+
"\n",
|
| 47 |
+
"# Check if the API key was found\n",
|
| 48 |
+
"if google_api_key:\n",
|
| 49 |
+
" # Set the environment variable if the API key was found\n",
|
| 50 |
+
" os.environ[\"GOOGLE_API_KEY\"] = google_api_key\n",
|
| 51 |
+
"\n",
|
| 52 |
+
" llm = ChatGoogleGenerativeAI(\n",
|
| 53 |
+
" model=\"gemini-pro\", # Specify the model name\n",
|
| 54 |
+
" google_api_key=os.environ[\"GOOGLE_API_KEY\"]\n",
|
| 55 |
+
" )\n",
|
| 56 |
+
"else:\n",
|
| 57 |
+
" print(\"Error: GOOGLE_API_KEY not found in Colab secrets. Please store your API key.\")\n",
|
| 58 |
+
"\n",
|
| 59 |
+
"\n",
|
| 60 |
+
"\n",
|
| 61 |
+
"genai.configure(api_key=google_api_key)\n",
|
| 62 |
+
"model = genai.GenerativeModel(\"gemini-pro\")"
|
| 63 |
+
]
|
| 64 |
+
},
|
| 65 |
+
{
|
| 66 |
+
"cell_type": "code",
|
| 67 |
+
"execution_count": 5,
|
| 68 |
+
"id": "ef330936-8c45-4aff-b2cf-fe9dfaaf2764",
|
| 69 |
+
"metadata": {},
|
| 70 |
+
"outputs": [],
|
| 71 |
+
"source": [
|
| 72 |
+
"work_dir=os.getcwd()"
|
| 73 |
+
]
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"cell_type": "code",
|
| 77 |
+
"execution_count": 6,
|
| 78 |
+
"id": "a55af811-7758-4090-a5f8-748b6192971b",
|
| 79 |
+
"metadata": {},
|
| 80 |
+
"outputs": [
|
| 81 |
+
{
|
| 82 |
+
"name": "stdout",
|
| 83 |
+
"output_type": "stream",
|
| 84 |
+
"text": [
|
| 85 |
+
"Current Working Directory: /Users/saurabhverma/GENAI\n"
|
| 86 |
+
]
|
| 87 |
+
}
|
| 88 |
+
],
|
| 89 |
+
"source": [
|
| 90 |
+
"# Verify file existence\n",
|
| 91 |
+
"assert \"Team1.pdf\" in os.listdir(work_dir), \"Team1.pdf not found in the specified directory!\"\n",
|
| 92 |
+
"print(f\"Current Working Directory: {os.getcwd()}\")"
|
| 93 |
+
]
|
| 94 |
+
},
|
| 95 |
+
{
|
| 96 |
+
"cell_type": "code",
|
| 97 |
+
"execution_count": 7,
|
| 98 |
+
"id": "7a0a4457-2f9c-40db-9dd4-d57e3edf1fd0",
|
| 99 |
+
"metadata": {},
|
| 100 |
+
"outputs": [],
|
| 101 |
+
"source": [
|
| 102 |
+
"# Load PDF and split text\n",
|
| 103 |
+
"pdf_path = \"Team1.pdf\" # Ensure this file is uploaded to Colab\n",
|
| 104 |
+
"loader = PyPDFLoader(pdf_path)\n",
|
| 105 |
+
"documents = loader.load()\n",
|
| 106 |
+
"\n",
|
| 107 |
+
"# Split text into chunks\n",
|
| 108 |
+
"text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=10)\n",
|
| 109 |
+
"text_chunks = text_splitter.split_documents(documents)"
|
| 110 |
+
]
|
| 111 |
+
},
|
| 112 |
+
{
|
| 113 |
+
"cell_type": "code",
|
| 114 |
+
"execution_count": 8,
|
| 115 |
+
"id": "b5387499-a756-49de-86b0-96a5ce712ba7",
|
| 116 |
+
"metadata": {},
|
| 117 |
+
"outputs": [],
|
| 118 |
+
"source": [
|
| 119 |
+
"# Generate embeddings\n",
|
| 120 |
+
"embeddings = GoogleGenerativeAIEmbeddings(model=\"models/embedding-001\")\n",
|
| 121 |
+
"\n",
|
| 122 |
+
"# Store embeddings in FAISS index\n",
|
| 123 |
+
"vectorstore = FAISS.from_documents(text_chunks, embeddings)\n",
|
| 124 |
+
"retriever = vectorstore.as_retriever(search_kwargs={\"k\": 4})"
|
| 125 |
+
]
|
| 126 |
+
},
|
| 127 |
+
{
|
| 128 |
+
"cell_type": "code",
|
| 129 |
+
"execution_count": 9,
|
| 130 |
+
"id": "35554163-75cd-4f0b-a538-565a48700245",
|
| 131 |
+
"metadata": {},
|
| 132 |
+
"outputs": [],
|
| 133 |
+
"source": [
|
| 134 |
+
"# Set up Gemini model\n",
|
| 135 |
+
"llm = ChatGoogleGenerativeAI(model=\"gemini-2.0-flash-001\", temperature=0)\n",
|
| 136 |
+
"\n"
|
| 137 |
+
]
|
| 138 |
+
},
|
| 139 |
+
{
|
| 140 |
+
"cell_type": "code",
|
| 141 |
+
"execution_count": 10,
|
| 142 |
+
"id": "e95b424b-11c1-46f3-9b4e-9e2d42d1f05d",
|
| 143 |
+
"metadata": {},
|
| 144 |
+
"outputs": [],
|
| 145 |
+
"source": [
|
| 146 |
+
"import gradio as gr\n",
|
| 147 |
+
"from langchain.prompts import PromptTemplate\n",
|
| 148 |
+
"from langchain.chains import LLMChain\n",
|
| 149 |
+
"\n",
|
| 150 |
+
"def rag_query(query):\n",
|
| 151 |
+
" # Retrieve relevant documents\n",
|
| 152 |
+
" docs = retriever.get_relevant_documents(query)\n",
|
| 153 |
+
" \n",
|
| 154 |
+
" # Otherwise, use RAG\n",
|
| 155 |
+
" context = \"\\n\".join([doc.page_content for doc in docs])\n",
|
| 156 |
+
" prompt = f\"Context:\\n{context}\\n\\nQuestion: {query}\\nAnswer directly and concisely:\"\n",
|
| 157 |
+
"\n",
|
| 158 |
+
" try:\n",
|
| 159 |
+
" response = llm.invoke(prompt)\n",
|
| 160 |
+
" except Exception as e:\n",
|
| 161 |
+
" response = f\"Error in RAG processing: {str(e)}\"\n",
|
| 162 |
+
"\n",
|
| 163 |
+
" return response.content\n",
|
| 164 |
+
"\n",
|
| 165 |
+
"\n"
|
| 166 |
+
]
|
| 167 |
+
},
|
| 168 |
+
{
|
| 169 |
+
"cell_type": "code",
|
| 170 |
+
"execution_count": 11,
|
| 171 |
+
"id": "552ff2fa-3c70-4054-803e-633efc7601f4",
|
| 172 |
+
"metadata": {},
|
| 173 |
+
"outputs": [],
|
| 174 |
+
"source": [
|
| 175 |
+
"import gradio as gr\n",
|
| 176 |
+
"from langchain.prompts import PromptTemplate\n",
|
| 177 |
+
"from langchain.chains import LLMChain\n",
|
| 178 |
+
"from langchain_google_genai import ChatGoogleGenerativeAI\n",
|
| 179 |
+
"\n",
|
| 180 |
+
"# Initialize LLM once (avoid repeated initialization)\n",
|
| 181 |
+
"llm = ChatGoogleGenerativeAI(model=\"gemini-2.0-flash\", temperature=0)\n",
|
| 182 |
+
"\n",
|
| 183 |
+
"# Define the general query function\n",
|
| 184 |
+
"def general_query(query):\n",
|
| 185 |
+
" try:\n",
|
| 186 |
+
" # Define the prompt correctly\n",
|
| 187 |
+
" prompt = PromptTemplate.from_template(\"Answer the following query: {query}\")\n",
|
| 188 |
+
" \n",
|
| 189 |
+
" # Create an LLM Chain\n",
|
| 190 |
+
" chain = LLMChain(llm=llm, prompt=prompt)\n",
|
| 191 |
+
" \n",
|
| 192 |
+
" # Run chatbot and return response\n",
|
| 193 |
+
" response = chain.run(query=query)\n",
|
| 194 |
+
" \n",
|
| 195 |
+
" return response # Return response directly (not response.content)\n",
|
| 196 |
+
" \n",
|
| 197 |
+
" except Exception as e:\n",
|
| 198 |
+
" return f\"Error: {str(e)}\"\n",
|
| 199 |
+
"\n"
|
| 200 |
+
]
|
| 201 |
+
},
|
| 202 |
+
{
|
| 203 |
+
"cell_type": "code",
|
| 204 |
+
"execution_count": 12,
|
| 205 |
+
"id": "ab63a509-e927-405a-985b-d07039e05e9f",
|
| 206 |
+
"metadata": {},
|
| 207 |
+
"outputs": [
|
| 208 |
+
{
|
| 209 |
+
"name": "stdout",
|
| 210 |
+
"output_type": "stream",
|
| 211 |
+
"text": [
|
| 212 |
+
"* Running on local URL: http://127.0.0.1:7860\n",
|
| 213 |
+
"* Running on public URL: https://efeff91c52754b11ed.gradio.live\n",
|
| 214 |
+
"\n",
|
| 215 |
+
"This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n"
|
| 216 |
+
]
|
| 217 |
+
},
|
| 218 |
+
{
|
| 219 |
+
"data": {
|
| 220 |
+
"text/html": [
|
| 221 |
+
"<div><iframe src=\"https://efeff91c52754b11ed.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 222 |
+
],
|
| 223 |
+
"text/plain": [
|
| 224 |
+
"<IPython.core.display.HTML object>"
|
| 225 |
+
]
|
| 226 |
+
},
|
| 227 |
+
"metadata": {},
|
| 228 |
+
"output_type": "display_data"
|
| 229 |
+
},
|
| 230 |
+
{
|
| 231 |
+
"data": {
|
| 232 |
+
"text/plain": []
|
| 233 |
+
},
|
| 234 |
+
"execution_count": 12,
|
| 235 |
+
"metadata": {},
|
| 236 |
+
"output_type": "execute_result"
|
| 237 |
+
}
|
| 238 |
+
],
|
| 239 |
+
"source": [
|
| 240 |
+
"import gradio as gr\n",
|
| 241 |
+
"\n",
|
| 242 |
+
"\n",
|
| 243 |
+
"# Function to call the selected query method\n",
|
| 244 |
+
"def query_router(query, method):\n",
|
| 245 |
+
" if method == \"Team Query\": # Ensure exact match with dropdown options\n",
|
| 246 |
+
" return rag_query(query)\n",
|
| 247 |
+
" elif method == \"General Query\":\n",
|
| 248 |
+
" return general_query(query)\n",
|
| 249 |
+
" return \"Invalid selection!\"\n",
|
| 250 |
+
"\n",
|
| 251 |
+
"# Define local image paths\n",
|
| 252 |
+
"logo_path = \"equinix-sign.jpg\" # Ensure this file exists\n",
|
| 253 |
+
"\n",
|
| 254 |
+
"# Custom CSS for background styling\n",
|
| 255 |
+
"custom_css = \"\"\"\n",
|
| 256 |
+
".gradio-container {\n",
|
| 257 |
+
" background-color: #f0f0f0;\n",
|
| 258 |
+
" text-align: center;\n",
|
| 259 |
+
"}\n",
|
| 260 |
+
"#logo img {\n",
|
| 261 |
+
" display: block;\n",
|
| 262 |
+
" margin: 0 auto;\n",
|
| 263 |
+
" max-width: 200px; /* Adjust size */\n",
|
| 264 |
+
"}\n",
|
| 265 |
+
"\"\"\"\n",
|
| 266 |
+
"\n",
|
| 267 |
+
"# Create Gradio UI\n",
|
| 268 |
+
"with gr.Blocks(css=custom_css) as ui:\n",
|
| 269 |
+
" gr.Image(logo_path, elem_id=\"logo\", show_label=False, height=100, width=200) # Display Logo\n",
|
| 270 |
+
" \n",
|
| 271 |
+
" # Title & Description\n",
|
| 272 |
+
" gr.Markdown(\"<h1 style='text-align: center; color: black;'>Equinix Chatbot for Automation Team</h1>\")\n",
|
| 273 |
+
" gr.Markdown(\"<p style='text-align: center; color: black;'>Ask me anything!</p>\")\n",
|
| 274 |
+
"\n",
|
| 275 |
+
" # Input & Dropdown Section\n",
|
| 276 |
+
" with gr.Row():\n",
|
| 277 |
+
" query_input = gr.Textbox(label=\"Enter your query\")\n",
|
| 278 |
+
" query_method = gr.Dropdown([\"Team Query\", \"General Query\"], label=\"Select Query Type\")\n",
|
| 279 |
+
" \n",
|
| 280 |
+
" # Button for submitting query\n",
|
| 281 |
+
" submit_button = gr.Button(\"Submit\")\n",
|
| 282 |
+
"\n",
|
| 283 |
+
" # Output Textbox\n",
|
| 284 |
+
" output_box = gr.Textbox(label=\"Response\", interactive=False)\n",
|
| 285 |
+
"\n",
|
| 286 |
+
" # Button Click Event\n",
|
| 287 |
+
" submit_button.click(query_router, inputs=[query_input, query_method], outputs=output_box)\n",
|
| 288 |
+
"\n",
|
| 289 |
+
"# Launch UI\n",
|
| 290 |
+
"ui.launch(share=True)\n"
|
| 291 |
+
]
|
| 292 |
+
}
|
| 293 |
+
],
|
| 294 |
+
"metadata": {
|
| 295 |
+
"kernelspec": {
|
| 296 |
+
"display_name": "Python 3 (ipykernel)",
|
| 297 |
+
"language": "python",
|
| 298 |
+
"name": "python3"
|
| 299 |
+
},
|
| 300 |
+
"language_info": {
|
| 301 |
+
"codemirror_mode": {
|
| 302 |
+
"name": "ipython",
|
| 303 |
+
"version": 3
|
| 304 |
+
},
|
| 305 |
+
"file_extension": ".py",
|
| 306 |
+
"mimetype": "text/x-python",
|
| 307 |
+
"name": "python",
|
| 308 |
+
"nbconvert_exporter": "python",
|
| 309 |
+
"pygments_lexer": "ipython3",
|
| 310 |
+
"version": "3.12.4"
|
| 311 |
+
}
|
| 312 |
+
},
|
| 313 |
+
"nbformat": 4,
|
| 314 |
+
"nbformat_minor": 5
|
| 315 |
+
}
|
RAG.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5b88dad7546bc3b74af2f8fa99991933639d8ca6c9d9b29dc40f15f835e7d1ee
|
| 3 |
+
size 100412
|
Team.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:35732f9e343ec1705006de208fe38327524bf162a9ef3041c9942f1378f86511
|
| 3 |
+
size 295956
|
Team1.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:da809c3a654847a5923d8221a8e008649fd32d725b19c4bca51d138e0823f067
|
| 3 |
+
size 308946
|
equinix-sign.jpg
ADDED
|