File size: 1,031 Bytes
828c90a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbe9324
 
 
 
828c90a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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()