Spaces:
Runtime error
Runtime error
import os | |
import cohere | |
import gradio as gr | |
import pinecone | |
co = cohere.Client(os.environ.get('COHERE_API', '')) | |
pinecone.init( | |
api_key=os.environ.get('PINECONE_API', ''), | |
environment=os.environ.get('PINECONE_ENV', '') | |
) | |
def list_me(matches): | |
result = '' | |
for match in matches: | |
result += '<li><a target="_blank" href="https://reddit.com/r/AskNYC/comments/' + match['id'] + '">' | |
result += match['metadata']['question'] | |
result += '</a>' | |
if 'body' in match['metadata']: | |
result += '<br/>' + match['metadata']['body'] | |
result += '</li>' | |
return result | |
def query(question): | |
response = co.embed( | |
model='large', | |
texts=[question], | |
) | |
index = pinecone.Index("gptnyc") | |
closest = index.query( | |
top_k=2, | |
include_metadata=True, | |
vector=response.embeddings[0], | |
) | |
return '<ul>' + list_me(closest['matches']) + '</ul>' | |
iface = gr.Interface( | |
fn=query, | |
inputs="text", | |
outputs="html" | |
) | |
iface.launch() | |