Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +55 -0
- import pytest.py +29 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pinecone
|
| 3 |
+
from pinecone import PineconeGRPC, GRPCClientConfig
|
| 4 |
+
|
| 5 |
+
# Initialize Pinecone connection
|
| 6 |
+
pinecone.init(api_key="your-pinecone-api-key", environment="us-west1-gcp")
|
| 7 |
+
pc = PineconeGRPC(api_key="your-pinecone-api-key")
|
| 8 |
+
index = pc.Index(host="localhost:5081", grpc_config=GRPCClientConfig(secure=False))
|
| 9 |
+
|
| 10 |
+
# Function to upsert a vector to Pinecone
|
| 11 |
+
def upsert_vector(vector_id, vector_values, genre):
|
| 12 |
+
index.upsert(
|
| 13 |
+
vectors=[
|
| 14 |
+
{
|
| 15 |
+
"id": vector_id,
|
| 16 |
+
"values": vector_values,
|
| 17 |
+
"metadata": {"genre": genre}
|
| 18 |
+
}
|
| 19 |
+
],
|
| 20 |
+
namespace="example-namespace"
|
| 21 |
+
)
|
| 22 |
+
return f"Vector {vector_id} upserted with genre {genre}"
|
| 23 |
+
|
| 24 |
+
# Function to query vectors based on genre
|
| 25 |
+
def query_vector(query_vector, genre_filter):
|
| 26 |
+
query = index.query(
|
| 27 |
+
vector=query_vector,
|
| 28 |
+
filter={"genre": {"$eq": genre_filter}},
|
| 29 |
+
top_k=1,
|
| 30 |
+
include_values=True,
|
| 31 |
+
include_metadata=True,
|
| 32 |
+
namespace="example-namespace"
|
| 33 |
+
)
|
| 34 |
+
return query
|
| 35 |
+
|
| 36 |
+
# Create Gradio Interface
|
| 37 |
+
with gr.Blocks() as app:
|
| 38 |
+
gr.Markdown("### Pinecone Vector Search with Gradio")
|
| 39 |
+
|
| 40 |
+
with gr.Row():
|
| 41 |
+
vector_id = gr.Textbox(label="Vector ID")
|
| 42 |
+
vector_values = gr.Textbox(label="Vector Values (comma separated)", placeholder="e.g. 1.0, 1.5")
|
| 43 |
+
genre = gr.Textbox(label="Genre")
|
| 44 |
+
|
| 45 |
+
submit_button = gr.Button("Upsert Vector")
|
| 46 |
+
submit_button.click(upsert_vector, inputs=[vector_id, vector_values, genre], outputs="status")
|
| 47 |
+
|
| 48 |
+
with gr.Row():
|
| 49 |
+
query_vector = gr.Textbox(label="Query Vector (comma separated)", placeholder="e.g. 1.0, 1.5")
|
| 50 |
+
genre_filter = gr.Textbox(label="Filter Genre")
|
| 51 |
+
|
| 52 |
+
search_button = gr.Button("Search")
|
| 53 |
+
search_button.click(query_vector, inputs=[query_vector, genre_filter], outputs="query_result")
|
| 54 |
+
|
| 55 |
+
app.launch()
|
import pytest.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
import pinecone
|
| 3 |
+
|
| 4 |
+
def test_upsert_vector():
|
| 5 |
+
# Initialize Pinecone connection
|
| 6 |
+
pinecone.init(api_key="your-pinecone-api-key", environment="us-west1-gcp")
|
| 7 |
+
index = pinecone.Index("example-index")
|
| 8 |
+
|
| 9 |
+
# Upsert a vector
|
| 10 |
+
response = index.upsert(
|
| 11 |
+
vectors=[{"id": "test1", "values": [1.0, 2.0], "metadata": {"genre": "comedy"}}],
|
| 12 |
+
namespace="example-namespace"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
assert response["status"] == "success"
|
| 16 |
+
|
| 17 |
+
def test_query_vector():
|
| 18 |
+
# Initialize Pinecone connection
|
| 19 |
+
pinecone.init(api_key="your-pinecone-api-key", environment="us-west1-gcp")
|
| 20 |
+
index = pinecone.Index("example-index")
|
| 21 |
+
|
| 22 |
+
# Query a vector
|
| 23 |
+
query_result = index.query(
|
| 24 |
+
vector=[1.0, 2.0],
|
| 25 |
+
top_k=1,
|
| 26 |
+
namespace="example-namespace"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
assert len(query_result["matches"]) > 0
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pinecone-client
|
| 3 |
+
pytest
|