Spaces:
Runtime error
Runtime error
Delete GenAI.ipynb
Browse files- GenAI.ipynb +0 -433
GenAI.ipynb
DELETED
@@ -1,433 +0,0 @@
|
|
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 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|