{ "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": [ "" ] }, "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 }