monsoon-nlp commited on
Commit
0d78964
1 Parent(s): bbe9324

query sgpt

Browse files
Files changed (2) hide show
  1. app.py +42 -2
  2. requirements.txt +2 -0
app.py CHANGED
@@ -2,7 +2,10 @@ 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(
@@ -10,6 +13,10 @@ pinecone.init(
10
  environment=os.environ.get('PINECONE_ENV', '')
11
  )
12
 
 
 
 
 
13
  def list_me(matches):
14
  result = ''
15
  for match in matches:
@@ -19,10 +26,11 @@ def list_me(matches):
19
  if 'body' in match['metadata']:
20
  result += '<br/>' + match['metadata']['body']
21
  result += '</li>'
22
- return result
23
 
24
 
25
  def query(question):
 
26
  response = co.embed(
27
  model='large',
28
  texts=[question],
@@ -34,7 +42,39 @@ def query(question):
34
  vector=response.embeddings[0],
35
  )
36
 
37
- return '<ul>' + list_me(closest['matches']) + '</ul>'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
 
40
  iface = gr.Interface(
 
2
 
3
  import cohere
4
  import gradio as gr
5
+ import numpy as np
6
  import pinecone
7
+ import torch
8
+ from transformers import AutoModel, AutoTokenizer
9
 
10
  co = cohere.Client(os.environ.get('COHERE_API', ''))
11
  pinecone.init(
 
13
  environment=os.environ.get('PINECONE_ENV', '')
14
  )
15
 
16
+ model = AutoModel.from_pretrained('monsoon-nlp/gpt-nyc')
17
+ tokenizer = AutoTokenizer.from_pretrained('monsoon-nlp/gpt-nyc')
18
+ zos = np.zeros(4096-1024).tolist()
19
+
20
  def list_me(matches):
21
  result = ''
22
  for match in matches:
 
26
  if 'body' in match['metadata']:
27
  result += '<br/>' + match['metadata']['body']
28
  result += '</li>'
29
+ return result.replace('/mini', '/')
30
 
31
 
32
  def query(question):
33
+ # Cohere search
34
  response = co.embed(
35
  model='large',
36
  texts=[question],
 
42
  vector=response.embeddings[0],
43
  )
44
 
45
+ # SGPT search
46
+ batch_tokens = tokenizer(
47
+ [question],
48
+ padding=True,
49
+ truncation=True,
50
+ return_tensors="pt"
51
+ )
52
+ with torch.no_grad():
53
+ last_hidden_state = model(**batch_tokens, output_hidden_states=True, return_dict=True).last_hidden_state
54
+ weights = (
55
+ torch.arange(start=1, end=last_hidden_state.shape[1] + 1)
56
+ .unsqueeze(0)
57
+ .unsqueeze(-1)
58
+ .expand(last_hidden_state.size())
59
+ .float().to(last_hidden_state.device)
60
+ )
61
+ input_mask_expanded = (
62
+ batch_tokens["attention_mask"]
63
+ .unsqueeze(-1)
64
+ .expand(last_hidden_state.size())
65
+ .float()
66
+ )
67
+ sum_embeddings = torch.sum(last_hidden_state * input_mask_expanded * weights, dim=1)
68
+ sum_mask = torch.sum(input_mask_expanded * weights, dim=1)
69
+ embeddings = sum_embeddings / sum_mask
70
+ closest_sgpt = index.query(
71
+ top_k=2,
72
+ include_metadata=True,
73
+ namespace="mini",
74
+ vector=embeddings[0].tolist() + zos,
75
+ )
76
+
77
+ return '<h3>Cohere</h3><ul>' + list_me(closest['matches']) + '</ul><h3>SGPT</h3><ul>' + list_me(closest_sgpt['matches']) + '</ul>'
78
 
79
 
80
  iface = gr.Interface(
requirements.txt CHANGED
@@ -1,2 +1,4 @@
1
  cohere==3.10.0
2
  pinecone-client==2.2.1
 
 
 
1
  cohere==3.10.0
2
  pinecone-client==2.2.1
3
+ torch
4
+ transformers==4.26.1