CurryMindFree commited on
Commit
6cc2eec
·
verified ·
1 Parent(s): bf6239f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -8
app.py CHANGED
@@ -7,18 +7,33 @@ import torch
7
 
8
 
9
 
10
- index = faiss.read_index("index.faiss")
11
 
12
- with open("texts_long.pkl", "rb") as f:
13
  texts = pickle.load(f)
14
 
 
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  model_name = "BAAI/bge-small-en-v1.5"
17
  tokenizer = AutoTokenizer.from_pretrained(model_name)
18
- model = AutoModel.from_pretrained(model_name)
19
 
20
 
21
- def embed(texts):
22
 
23
  inputs = tokenizer(
24
  texts,
@@ -28,7 +43,7 @@ def embed(texts):
28
  )
29
 
30
  with torch.no_grad():
31
- outputs = model(**inputs)
32
 
33
  token_embeddings = outputs.last_hidden_state
34
  attention_mask = inputs["attention_mask"]
@@ -49,13 +64,13 @@ def embed(texts):
49
 
50
 
51
 
52
- def retrieve(query, k=3):
53
 
54
  q = embed([query])
55
 
56
  scores, ids = index.search(q, k)
57
 
58
- return "\n".join(str(i) +": " + texts[i] for i in ids[0])
59
  #return [str(i) +": " + texts[i] + "\n" for i in ids[0]]
60
 
61
 
@@ -67,7 +82,7 @@ with gr.Blocks() as demo:
67
  )
68
 
69
  output = gr.Textbox(
70
- label="Top3 most relevant news:"
71
  )
72
 
73
  topic_input.submit(
 
7
 
8
 
9
 
10
+ index = faiss.read_index("index503.faiss")
11
 
12
+ with open("texts503.pkl", "rb") as f:
13
  texts = pickle.load(f)
14
 
15
+ print(texts[500])
16
 
17
+ # chunking
18
+ chunks = []
19
+ chunk_size = 25
20
+ overlap_int = 2
21
+ for text in texts:
22
+ text_words = text.split()
23
+ for i in range(0, len(text_words), chunk_size):
24
+ chunk_words = text_words[max(i - overlap_int, 0): i + chunk_size]
25
+ chunk = " ".join(chunk_words)
26
+ chunks.append(chunk)
27
+
28
+ print(len(chunks))
29
+
30
+ # emb model
31
  model_name = "BAAI/bge-small-en-v1.5"
32
  tokenizer = AutoTokenizer.from_pretrained(model_name)
33
+ emb_model = AutoModel.from_pretrained(model_name)
34
 
35
 
36
+ def embed(query):
37
 
38
  inputs = tokenizer(
39
  texts,
 
43
  )
44
 
45
  with torch.no_grad():
46
+ outputs = emb_model(**inputs)
47
 
48
  token_embeddings = outputs.last_hidden_state
49
  attention_mask = inputs["attention_mask"]
 
64
 
65
 
66
 
67
+ def retrieve(query, k=5):
68
 
69
  q = embed([query])
70
 
71
  scores, ids = index.search(q, k)
72
 
73
+ return "\n".join(str(i) +": " + chunks[i] for i in ids[0])
74
  #return [str(i) +": " + texts[i] + "\n" for i in ids[0]]
75
 
76
 
 
82
  )
83
 
84
  output = gr.Textbox(
85
+ label="Top5 most relevant news:"
86
  )
87
 
88
  topic_input.submit(