mishig HF staff commited on
Commit
38585cf
1 Parent(s): 069f50c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import os
3
+ import gradio as gr
4
+ import torch
5
+ from transformers import AutoModel, AutoTokenizer
6
+ import meilisearch
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained('Snowflake/snowflake-arctic-embed-m')
9
+ model = AutoModel.from_pretrained('Snowflake/snowflake-arctic-embed-m', add_pooling_layer=False)
10
+ model.eval()
11
+
12
+ cuda_available = torch.cuda.is_available()
13
+ print(f"CUDA available: {cuda_available}")
14
+
15
+ meilisearch_client = meilisearch.Client("https://edge.meilisearch.com", os.environ["MEILISEARCH_KEY"])
16
+ meilisearch_index_name = "docs-embed"
17
+ meilisearch_index = meilisearch_client.index(meilisearch_index_name)
18
+
19
+ def search_embeddings(query_text):
20
+ start_time_embedding = time.time()
21
+ query_prefix = 'Represent this sentence for searching code documentation: '
22
+ query_tokens = tokenizer(query_prefix + query_text, padding=True, truncation=True, return_tensors='pt', max_length=512)
23
+ # step1: tokenizer the query
24
+ with torch.no_grad():
25
+ # Compute token embeddings
26
+ query_embeddings = model(**query_tokens)[0][:, 0]
27
+ # normalize embeddings
28
+ query_embeddings = torch.nn.functional.normalize(query_embeddings, p=2, dim=1)
29
+ document_embeddings_list = query_embeddings[0].tolist()
30
+ elapsed_time_embedding = time.time() - start_time_embedding
31
+
32
+ # step2: search meilisearch
33
+ start_time_meilisearch = time.time()
34
+ response = meilisearch_index.search(
35
+ "", opt_params={"vector": document_embeddings_list, "hybrid": {"semanticRatio": 1.0}, "limit": 5, "attributesToRetrieve": ["text", "source", "library"]}
36
+ )
37
+ elapsed_time_meilisearch = time.time() - start_time_meilisearch
38
+ hits = response["hits"]
39
+
40
+ # step3: present the results in markdown
41
+ md = f"Stats:\n\nembedding time: {elapsed_time_embedding:.2f}s\n\nmeilisearch time: {elapsed_time_meilisearch:.2f}s\n\n---\n\n"
42
+ for hit in hits:
43
+ text, source, library = hit["text"], hit["source"], hit["library"]
44
+ source = f"[source](https://huggingface.co/docs/{library}/{source})"
45
+ md += text + f"\n\n{source}\n\n---\n\n"
46
+
47
+ return md
48
+
49
+
50
+ demo = gr.Interface(
51
+ fn=search_embeddings,
52
+ inputs=gr.Textbox(label="enter your query", placeholder="Type Markdown here...", lines=10),
53
+ outputs=gr.Markdown(),
54
+ title="HF Docs Emebddings Explorer",
55
+ allow_flagging="never"
56
+ )
57
+
58
+ if __name__ == "__main__":
59
+ demo.launch()