{ "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": [ "
" ], "text/plain": [ "