Spaces:
Runtime error
Runtime error
Commit ·
828c90a
1
Parent(s): 9b562bd
vector search demo
Browse files- app.py +42 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import cohere
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import pinecone
|
| 6 |
+
|
| 7 |
+
co = cohere.Client(os.environ.get('COHERE_API', ''))
|
| 8 |
+
pinecone.init(
|
| 9 |
+
api_key=os.environ.get('PINECONE_API', ''),
|
| 10 |
+
environment=os.environ.get('PINECONE_ENV', '')
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def list_me(matches):
|
| 14 |
+
result = ''
|
| 15 |
+
for match in matches:
|
| 16 |
+
result += '<li><a target="_blank" href="https://reddit.com/r/AskNYC/comments/' + match['id'] + '">'
|
| 17 |
+
result += match['metadata']['question']
|
| 18 |
+
result += '</a></li>'
|
| 19 |
+
return result
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def query(question):
|
| 23 |
+
response = co.embed(
|
| 24 |
+
model='large',
|
| 25 |
+
texts=[question],
|
| 26 |
+
)
|
| 27 |
+
index = pinecone.Index("gptnyc")
|
| 28 |
+
closest = index.query(
|
| 29 |
+
top_k=2,
|
| 30 |
+
include_metadata=True,
|
| 31 |
+
vector=response.embeddings[0],
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
return '<ul>' + list_me(closest['matches']) + '</ul>'
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
iface = gr.Interface(
|
| 38 |
+
fn=query,
|
| 39 |
+
inputs="text",
|
| 40 |
+
outputs="html"
|
| 41 |
+
)
|
| 42 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
cohere==3.10.0
|
| 2 |
+
pinecone-client==2.2.1
|