Spaces:
Runtime error
Runtime error
akshayballal
commited on
Commit
•
0244db4
1
Parent(s):
95a35fc
feat: Add audio search functionality with Pinecone embeddings
Browse files- test.ipynb +0 -334
test.ipynb
DELETED
@@ -1,334 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"cells": [
|
3 |
-
{
|
4 |
-
"cell_type": "code",
|
5 |
-
"execution_count": 1,
|
6 |
-
"metadata": {},
|
7 |
-
"outputs": [
|
8 |
-
{
|
9 |
-
"name": "stderr",
|
10 |
-
"output_type": "stream",
|
11 |
-
"text": [
|
12 |
-
"C:\\Users\\arbal\\AppData\\Local\\Temp\\ipykernel_27040\\3276458500.py:3: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
13 |
-
" from tqdm.autonotebook import tqdm\n"
|
14 |
-
]
|
15 |
-
}
|
16 |
-
],
|
17 |
-
"source": [
|
18 |
-
"import embed_anything\n",
|
19 |
-
"from embed_anything import EmbedData\n",
|
20 |
-
"from tqdm.autonotebook import tqdm\n",
|
21 |
-
"from pinecone import Pinecone, ServerlessSpec\n",
|
22 |
-
"import numpy as np\n",
|
23 |
-
"import os\n",
|
24 |
-
"from pinecone import PineconeApiException\n",
|
25 |
-
"import uuid"
|
26 |
-
]
|
27 |
-
},
|
28 |
-
{
|
29 |
-
"cell_type": "code",
|
30 |
-
"execution_count": 2,
|
31 |
-
"metadata": {},
|
32 |
-
"outputs": [],
|
33 |
-
"source": [
|
34 |
-
"audio_files = [\"examples/samples_hp0.wav\", \"examples/samples_gb0.wav\"]"
|
35 |
-
]
|
36 |
-
},
|
37 |
-
{
|
38 |
-
"cell_type": "code",
|
39 |
-
"execution_count": 3,
|
40 |
-
"metadata": {},
|
41 |
-
"outputs": [],
|
42 |
-
"source": [
|
43 |
-
"embeddings: list[list[EmbedData]] = []"
|
44 |
-
]
|
45 |
-
},
|
46 |
-
{
|
47 |
-
"cell_type": "code",
|
48 |
-
"execution_count": 4,
|
49 |
-
"metadata": {},
|
50 |
-
"outputs": [],
|
51 |
-
"source": [
|
52 |
-
"for file in audio_files:\n",
|
53 |
-
" embedding = embed_anything.embed_file(file, \"Whisper-Jina\")\n",
|
54 |
-
" embeddings.append(embedding)"
|
55 |
-
]
|
56 |
-
},
|
57 |
-
{
|
58 |
-
"cell_type": "code",
|
59 |
-
"execution_count": 96,
|
60 |
-
"metadata": {},
|
61 |
-
"outputs": [],
|
62 |
-
"source": [
|
63 |
-
"pc = Pinecone(api_key=os.environ.get(\"PINECONE_API_KEY\"))\n",
|
64 |
-
"pc.delete_index(\"search-in-audio\")\n",
|
65 |
-
"try:\n",
|
66 |
-
" index = pc.create_index(\n",
|
67 |
-
" name=\"search-in-audio\",\n",
|
68 |
-
" dimension=768, # Replace with your model dimensions\n",
|
69 |
-
" metric=\"cosine\", # Replace with your model metric\n",
|
70 |
-
" spec=ServerlessSpec(\n",
|
71 |
-
" cloud=\"aws\",\n",
|
72 |
-
" region=\"us-east-1\"\n",
|
73 |
-
" ) \n",
|
74 |
-
" )\n",
|
75 |
-
" index = pc.Index('search-in-audio')\n",
|
76 |
-
"except(PineconeApiException) as e:\n",
|
77 |
-
" index = pc.Index('search-in-audio')\n",
|
78 |
-
" if e.status == 409:\n",
|
79 |
-
" print(\"Index already exists\")\n",
|
80 |
-
" \n",
|
81 |
-
" else: print(e)"
|
82 |
-
]
|
83 |
-
},
|
84 |
-
{
|
85 |
-
"cell_type": "code",
|
86 |
-
"execution_count": 97,
|
87 |
-
"metadata": {},
|
88 |
-
"outputs": [],
|
89 |
-
"source": [
|
90 |
-
"## convert embeddings which is of the form EmbedData : text, embedding, metadata to the form required by pinecone which is id, values, metadata\n",
|
91 |
-
"import re\n",
|
92 |
-
"def convert_to_pinecone_format(embeddings: list[list[EmbedData]]):\n",
|
93 |
-
" data = []\n",
|
94 |
-
" for i, embedding in enumerate(embeddings):\n",
|
95 |
-
" for j, emb in enumerate(embedding):\n",
|
96 |
-
" data.append({\"id\": str(uuid.uuid4()), \"values\": emb.embedding, \"metadata\": {\"text\": emb.text, \"start\": emb.metadata[\"start\"], \"end\": emb.metadata[\"end\"], \"file\": re.split(r'/|\\\\',emb.metadata[\"file_name\"])[-1]}})\n",
|
97 |
-
" return data"
|
98 |
-
]
|
99 |
-
},
|
100 |
-
{
|
101 |
-
"cell_type": "code",
|
102 |
-
"execution_count": 98,
|
103 |
-
"metadata": {},
|
104 |
-
"outputs": [
|
105 |
-
{
|
106 |
-
"data": {
|
107 |
-
"text/plain": [
|
108 |
-
"{'upserted_count': 15}"
|
109 |
-
]
|
110 |
-
},
|
111 |
-
"execution_count": 98,
|
112 |
-
"metadata": {},
|
113 |
-
"output_type": "execute_result"
|
114 |
-
}
|
115 |
-
],
|
116 |
-
"source": [
|
117 |
-
"data = convert_to_pinecone_format(embeddings)\n",
|
118 |
-
"index.upsert(data)"
|
119 |
-
]
|
120 |
-
},
|
121 |
-
{
|
122 |
-
"cell_type": "code",
|
123 |
-
"execution_count": 99,
|
124 |
-
"metadata": {},
|
125 |
-
"outputs": [],
|
126 |
-
"source": [
|
127 |
-
"query = embed_anything.embed_query(['united states'], \"Jina\")[0]"
|
128 |
-
]
|
129 |
-
},
|
130 |
-
{
|
131 |
-
"cell_type": "code",
|
132 |
-
"execution_count": 100,
|
133 |
-
"metadata": {},
|
134 |
-
"outputs": [],
|
135 |
-
"source": [
|
136 |
-
"result = index.query(vector=query.embedding, top_k=5, include_metadata=True,)\n"
|
137 |
-
]
|
138 |
-
},
|
139 |
-
{
|
140 |
-
"cell_type": "code",
|
141 |
-
"execution_count": 101,
|
142 |
-
"metadata": {},
|
143 |
-
"outputs": [
|
144 |
-
{
|
145 |
-
"name": "stdout",
|
146 |
-
"output_type": "stream",
|
147 |
-
"text": [
|
148 |
-
"{'end': '120', 'file': 'samples_gb0.wav', 'start': '90', 'text': \" Young democracy from Georgia and Ukraine to Afghanistan and Iraq can look to the United States for proof that self-government can endure and nations that still live under tyranny and oppression can find hope and inspiration in our commitment to liberty. For more than two centuries, Americans have demonstrated the ability of free people to choose their own leaders. Our nation has flourished because of its commitment to trusting the wisdom of our citizenry. In this year's election, we will see this tradition continue and we will be reminded once again\"}\n",
|
149 |
-
"{'end': '60', 'file': 'samples_gb0.wav', 'start': '30', 'text': ' The United States was founded on the belief that all men are created equal. Every election day, millions of Americans of all races, religions and backgrounds step into voting booths throughout the nation, whether they are richer, poor, older, young. Each of them has an equal share in choosing the path that our country will take. And every ballot they cast is a reminder that our founding principles are alive and well. Voting is one of the great privileges of a'}\n",
|
150 |
-
"{'end': '30', 'file': 'samples_gb0.wav', 'start': '0', 'text': \" Good morning. This Tuesday is Election Day. After months of spirited debate in vigorous campaigning, the time has come for Americans to make important decisions about our nation's future and encourage all Americans to go to the polls and vote. Election season brings out the spirit of competition between our political parties. And that competition is an essential part of a healthy democracy. But as the campaigns come to a close, Republicans, Democrats, and independents can find common ground on at least one point. Our system of\"}\n",
|
151 |
-
"{'end': '300', 'file': 'samples_hp0.wav', 'start': '270', 'text': ' TML'}\n",
|
152 |
-
"{'end': '30', 'file': 'samples_hp0.wav', 'start': '0', 'text': ' Henry F. Phillips from Wikipedia, the free encyclopedia at en.wickipedia.org.'}\n"
|
153 |
-
]
|
154 |
-
}
|
155 |
-
],
|
156 |
-
"source": [
|
157 |
-
"for res in result.matches:\n",
|
158 |
-
" print(res.metadata)"
|
159 |
-
]
|
160 |
-
},
|
161 |
-
{
|
162 |
-
"cell_type": "code",
|
163 |
-
"execution_count": 104,
|
164 |
-
"metadata": {},
|
165 |
-
"outputs": [
|
166 |
-
{
|
167 |
-
"name": "stdout",
|
168 |
-
"output_type": "stream",
|
169 |
-
"text": [
|
170 |
-
"Running on local URL: http://127.0.0.1:7899\n",
|
171 |
-
"\n",
|
172 |
-
"To create a public link, set `share=True` in `launch()`.\n"
|
173 |
-
]
|
174 |
-
},
|
175 |
-
{
|
176 |
-
"data": {
|
177 |
-
"text/html": [
|
178 |
-
"<div><iframe src=\"http://127.0.0.1:7899/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
179 |
-
],
|
180 |
-
"text/plain": [
|
181 |
-
"<IPython.core.display.HTML object>"
|
182 |
-
]
|
183 |
-
},
|
184 |
-
"metadata": {},
|
185 |
-
"output_type": "display_data"
|
186 |
-
},
|
187 |
-
{
|
188 |
-
"data": {
|
189 |
-
"text/plain": []
|
190 |
-
},
|
191 |
-
"execution_count": 104,
|
192 |
-
"metadata": {},
|
193 |
-
"output_type": "execute_result"
|
194 |
-
}
|
195 |
-
],
|
196 |
-
"source": [
|
197 |
-
"import gradio as gr\n",
|
198 |
-
"files = [\"samples_hp0.wav\", \"samples_gb0.wav\"]\n",
|
199 |
-
"def search(query, audio):\n",
|
200 |
-
"\n",
|
201 |
-
" results = []\n",
|
202 |
-
" query = embed_anything.embed_query([query], \"Jina\")[0]\n",
|
203 |
-
"\n",
|
204 |
-
" if re.split(r'/|\\\\',audio)[-1] not in files:\n",
|
205 |
-
" print(file, re.split(r'/|\\\\',audio)[-1])\n",
|
206 |
-
" embeddings = embed_anything.embed_file(audio, \"Whisper-Jina\")\n",
|
207 |
-
" embeddings = convert_to_pinecone_format([embeddings])\n",
|
208 |
-
" index.upsert(embeddings)\n",
|
209 |
-
"\n",
|
210 |
-
" files.append(re.split(r'/|\\\\',audio)[-1])\n",
|
211 |
-
"\n",
|
212 |
-
"\n",
|
213 |
-
" result = index.query(vector=query.embedding, top_k=5, include_metadata=True,)\n",
|
214 |
-
" for res in result.matches:\n",
|
215 |
-
" results.append(res.metadata)\n",
|
216 |
-
" \n",
|
217 |
-
" formatted_results = []\n",
|
218 |
-
" for result in results:\n",
|
219 |
-
" display_text = f'''\n",
|
220 |
-
" \n",
|
221 |
-
" `File: {result['file']}`\n",
|
222 |
-
" \n",
|
223 |
-
" `Start: {result['start']}`\n",
|
224 |
-
" \n",
|
225 |
-
" `End: {result['end']}`\n",
|
226 |
-
" \n",
|
227 |
-
" Text: {result['text']}''' \n",
|
228 |
-
" formatted_results.append(display_text)\n",
|
229 |
-
"\n",
|
230 |
-
" return formatted_results[0], results[0]['file'], formatted_results[1], results[1]['file'], formatted_results[2], results[2]['file']\n",
|
231 |
-
"\n",
|
232 |
-
"demo = gr.Interface(\n",
|
233 |
-
" fn=search,\n",
|
234 |
-
" inputs=[\"text\", gr.Audio(label='Audio', type = 'filepath')],\n",
|
235 |
-
" outputs=[\n",
|
236 |
-
" \n",
|
237 |
-
" gr.Markdown(label=\"Text\"), gr.Audio(label=\"Audio\", type=\"filepath\"),\n",
|
238 |
-
" gr.Markdown(label=\"Text\"), gr.Audio(label=\"Audio\", type=\"filepath\"),\n",
|
239 |
-
" gr.Markdown(label=\"Text\"), gr.Audio(label=\"Audio\", type=\"filepath\")\n",
|
240 |
-
" ],\n",
|
241 |
-
" examples=[[\"screwdriver\", \"examples/samples_hp0.wav\"], [\"united states\", \"examples/samples_gb0.wav\"], [\"united states\", \"examples/samples_hp0.wav\"]],\n",
|
242 |
-
")\n",
|
243 |
-
"demo.launch()"
|
244 |
-
]
|
245 |
-
},
|
246 |
-
{
|
247 |
-
"cell_type": "code",
|
248 |
-
"execution_count": 89,
|
249 |
-
"metadata": {},
|
250 |
-
"outputs": [
|
251 |
-
{
|
252 |
-
"data": {
|
253 |
-
"text/plain": [
|
254 |
-
"False"
|
255 |
-
]
|
256 |
-
},
|
257 |
-
"execution_count": 89,
|
258 |
-
"metadata": {},
|
259 |
-
"output_type": "execute_result"
|
260 |
-
}
|
261 |
-
],
|
262 |
-
"source": [
|
263 |
-
"audio = \"C:/Users/arbal/AppData/Local/Temp/gradio/c0490405f0fef485a043991c6a9d6b83f006db51/samples_hp0.wav\"\n",
|
264 |
-
"\"samples_hp0.wav\" not in audio"
|
265 |
-
]
|
266 |
-
},
|
267 |
-
{
|
268 |
-
"cell_type": "code",
|
269 |
-
"execution_count": 94,
|
270 |
-
"metadata": {},
|
271 |
-
"outputs": [
|
272 |
-
{
|
273 |
-
"ename": "SyntaxError",
|
274 |
-
"evalue": "(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \\UXXXXXXXX escape (1955374795.py, line 1)",
|
275 |
-
"output_type": "error",
|
276 |
-
"traceback": [
|
277 |
-
"\u001b[1;36m Cell \u001b[1;32mIn[94], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m if \"samples_hp0.wav\" not in \"C:\\Users\\arbal\\AppData\\Local\\Temp\\gradio\\c0490405f0fef485a043991c6a9d6b83f006db51\\samples_hp0.wav\"\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \\UXXXXXXXX escape\n"
|
278 |
-
]
|
279 |
-
}
|
280 |
-
],
|
281 |
-
"source": [
|
282 |
-
"if \"samples_hp0.wav\" not in \"C:\\Users\\arbal\\AppData\\Local\\Temp\\gradio\\c0490405f0fef485a043991c6a9d6b83f006db51\\samples_hp0.wav\""
|
283 |
-
]
|
284 |
-
},
|
285 |
-
{
|
286 |
-
"cell_type": "code",
|
287 |
-
"execution_count": 93,
|
288 |
-
"metadata": {},
|
289 |
-
"outputs": [
|
290 |
-
{
|
291 |
-
"data": {
|
292 |
-
"text/plain": [
|
293 |
-
"'samples_hp0.wav'"
|
294 |
-
]
|
295 |
-
},
|
296 |
-
"execution_count": 93,
|
297 |
-
"metadata": {},
|
298 |
-
"output_type": "execute_result"
|
299 |
-
}
|
300 |
-
],
|
301 |
-
"source": [
|
302 |
-
"re.split(r'/|\\\\',audio)[-1]"
|
303 |
-
]
|
304 |
-
},
|
305 |
-
{
|
306 |
-
"cell_type": "code",
|
307 |
-
"execution_count": null,
|
308 |
-
"metadata": {},
|
309 |
-
"outputs": [],
|
310 |
-
"source": []
|
311 |
-
}
|
312 |
-
],
|
313 |
-
"metadata": {
|
314 |
-
"kernelspec": {
|
315 |
-
"display_name": "embedAnything",
|
316 |
-
"language": "python",
|
317 |
-
"name": "python3"
|
318 |
-
},
|
319 |
-
"language_info": {
|
320 |
-
"codemirror_mode": {
|
321 |
-
"name": "ipython",
|
322 |
-
"version": 3
|
323 |
-
},
|
324 |
-
"file_extension": ".py",
|
325 |
-
"mimetype": "text/x-python",
|
326 |
-
"name": "python",
|
327 |
-
"nbconvert_exporter": "python",
|
328 |
-
"pygments_lexer": "ipython3",
|
329 |
-
"version": "3.11.9"
|
330 |
-
}
|
331 |
-
},
|
332 |
-
"nbformat": 4,
|
333 |
-
"nbformat_minor": 2
|
334 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|