File size: 11,506 Bytes
95a35fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "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",
      "  from tqdm.autonotebook import tqdm\n"
     ]
    }
   ],
   "source": [
    "import embed_anything\n",
    "from embed_anything import EmbedData\n",
    "from tqdm.autonotebook import tqdm\n",
    "from pinecone import Pinecone, ServerlessSpec\n",
    "import numpy as np\n",
    "import os\n",
    "from pinecone import PineconeApiException\n",
    "import uuid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "audio_files = [\"examples/samples_hp0.wav\", \"examples/samples_gb0.wav\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "embeddings: list[list[EmbedData]] = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "for file in audio_files:\n",
    "    embedding = embed_anything.embed_file(file, \"Whisper-Jina\")\n",
    "    embeddings.append(embedding)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [],
   "source": [
    "pc = Pinecone(api_key=os.environ.get(\"PINECONE_API_KEY\"))\n",
    "pc.delete_index(\"search-in-audio\")\n",
    "try:\n",
    "   index =  pc.create_index(\n",
    "        name=\"search-in-audio\",\n",
    "        dimension=768, # Replace with your model dimensions\n",
    "        metric=\"cosine\", # Replace with your model metric\n",
    "        spec=ServerlessSpec(\n",
    "            cloud=\"aws\",\n",
    "            region=\"us-east-1\"\n",
    "        ) \n",
    "    )\n",
    "   index = pc.Index('search-in-audio')\n",
    "except(PineconeApiException) as e:\n",
    "    index = pc.Index('search-in-audio')\n",
    "    if e.status == 409:\n",
    "        print(\"Index already exists\")\n",
    "    \n",
    "    else: print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "## convert embeddings which is of the form EmbedData : text, embedding, metadata to the form required by pinecone which is id, values, metadata\n",
    "import re\n",
    "def convert_to_pinecone_format(embeddings: list[list[EmbedData]]):\n",
    "    data = []\n",
    "    for i, embedding in enumerate(embeddings):\n",
    "        for j, emb in enumerate(embedding):\n",
    "            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",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'upserted_count': 15}"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = convert_to_pinecone_format(embeddings)\n",
    "index.upsert(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [],
   "source": [
    "query = embed_anything.embed_query(['united states'], \"Jina\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = index.query(vector=query.embedding, top_k=5, include_metadata=True,)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'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",
      "{'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",
      "{'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",
      "{'end': '300', 'file': 'samples_hp0.wav', 'start': '270', 'text': ' TML'}\n",
      "{'end': '30', 'file': 'samples_hp0.wav', 'start': '0', 'text': ' Henry F. Phillips from Wikipedia, the free encyclopedia at en.wickipedia.org.'}\n"
     ]
    }
   ],
   "source": [
    "for res in result.matches:\n",
    "    print(res.metadata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7899\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<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>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import gradio as gr\n",
    "files = [\"samples_hp0.wav\", \"samples_gb0.wav\"]\n",
    "def search(query, audio):\n",
    "\n",
    "    results = []\n",
    "    query = embed_anything.embed_query([query], \"Jina\")[0]\n",
    "\n",
    "    if re.split(r'/|\\\\',audio)[-1] not in files:\n",
    "            print(file, re.split(r'/|\\\\',audio)[-1])\n",
    "            embeddings = embed_anything.embed_file(audio, \"Whisper-Jina\")\n",
    "            embeddings = convert_to_pinecone_format([embeddings])\n",
    "            index.upsert(embeddings)\n",
    "\n",
    "    files.append(re.split(r'/|\\\\',audio)[-1])\n",
    "\n",
    "\n",
    "    result = index.query(vector=query.embedding, top_k=5, include_metadata=True,)\n",
    "    for res in result.matches:\n",
    "        results.append(res.metadata)\n",
    "    \n",
    "    formatted_results = []\n",
    "    for result in results:\n",
    "        display_text = f'''\n",
    "        \n",
    "        `File: {result['file']}`\n",
    "        \n",
    "        `Start: {result['start']}`\n",
    "        \n",
    "        `End: {result['end']}`\n",
    "        \n",
    "        Text: {result['text']}'''  \n",
    "        formatted_results.append(display_text)\n",
    "\n",
    "    return formatted_results[0], results[0]['file'], formatted_results[1],  results[1]['file'], formatted_results[2], results[2]['file']\n",
    "\n",
    "demo = gr.Interface(\n",
    "    fn=search,\n",
    "    inputs=[\"text\", gr.Audio(label='Audio', type = 'filepath')],\n",
    "    outputs=[\n",
    "        \n",
    "        gr.Markdown(label=\"Text\"), gr.Audio(label=\"Audio\", type=\"filepath\"),\n",
    "        gr.Markdown(label=\"Text\"), gr.Audio(label=\"Audio\", type=\"filepath\"),\n",
    "        gr.Markdown(label=\"Text\"), gr.Audio(label=\"Audio\", type=\"filepath\")\n",
    "    ],\n",
    "    examples=[[\"screwdriver\", \"examples/samples_hp0.wav\"], [\"united states\", \"examples/samples_gb0.wav\"], [\"united states\", \"examples/samples_hp0.wav\"]],\n",
    ")\n",
    "demo.launch()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "audio = \"C:/Users/arbal/AppData/Local/Temp/gradio/c0490405f0fef485a043991c6a9d6b83f006db51/samples_hp0.wav\"\n",
    "\"samples_hp0.wav\" not in audio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \\UXXXXXXXX escape (1955374795.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\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"
     ]
    }
   ],
   "source": [
    "if \"samples_hp0.wav\" not in \"C:\\Users\\arbal\\AppData\\Local\\Temp\\gradio\\c0490405f0fef485a043991c6a9d6b83f006db51\\samples_hp0.wav\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'samples_hp0.wav'"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.split(r'/|\\\\',audio)[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "embedAnything",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}