espejelomar commited on
Commit
7e85460
1 Parent(s): d1d09db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -13
app.py CHANGED
@@ -11,32 +11,34 @@ st.image(
11
  )
12
 
13
 
14
- st.subheader("Code Query Search: An Introduction to Semantic Search")
15
 
16
  st.markdown(
17
  """
18
- Suppose you have a database of texts and you want to find which entries best match the meaning of a single text you have. Example, we want to see which query on Stack Overflow best matches a question you have about Python. However, just word-for-word matching will not work due to the complexity of the programming questions. We need to do a "smart" search: that's what semantic search is for.
19
 
20
  *"**Semantic search seeks to improve search accuracy by understanding the content of the search query. In contrast to traditional search engines which only find documents based on lexical matches, semantic search can also find synonyms.**" - [SBert Documentation](https://www.sbert.net/examples/applications/semantic-search/README.html)*.
21
 
22
 
23
- Let's make this interactive and use the power of **[Streamlit](https://docs.streamlit.io/) + [HF Spaces](https://huggingface.co/spaces) + the '[sentence-transformers/all-mpnet-base-v2](https://huggingface.co/sentence-transformers/all-mpnet-base-v2)' model by creating the programming question search example right here**:
 
 
24
  """
25
  )
26
 
27
  anchor = st.text_input(
28
- "Please enter here your query about Python, we will look for similar ones",
29
- value="How to sort a list",
30
  )
31
 
32
  n_texts = st.number_input(
33
- f"""How many similar queries you want?""", value=2, min_value=1
34
  )
35
- if st.button("Find the most similar queries in the Stack Overflow query dataset."):
36
 
37
  st.markdown(
38
  """
39
- **Amazing! These are the entries in our Stack Overflow dataset that better match the language of our specific query.** The 'Description' column shows the description of the entry in our dataset, and the 'Code' column shows the code of the proposed solution. Sorry for the format of the Code!
40
  """
41
  )
42
 
@@ -51,9 +53,9 @@ st.subheader("What happens underneath? Obtaining embeddings")
51
 
52
  st.markdown(
53
  f"""
54
- First we embed our database of texts: we convert each of the Stack Overflow queries into a vector space. That is, convert query descriptions from words to numbers that provide an understanding of the language within each query. The understanding of each text will be reflected in a vector called embedding.
55
 
56
- The system would look like the following figure. We embed (1) our texts database (in this case, the Stack Overflow set of queries), and (2) our own query (in this case: '{anchor}') **with the same model**. Notice that our query could be a matrix of several elements.
57
 
58
  """
59
  )
@@ -70,11 +72,11 @@ st.subheader("Obtaining the closest observations in the vector space")
70
 
71
  st.markdown(
72
  f"""
73
- We now have two numerical representations of texts (embeddings): one for our original text database and one for our own query. Our goal: get the texts in the database that have the closest meaning to our query.
74
 
75
  Queries that are most similar to each other will be closer together in the vector space, and queries that differ most will be farther apart.
76
 
77
- The following figure (obtained from the [Sentence Transformers documentation](https://www.sbert.net/examples/applications/semantic-search/README.html)) shows in blue how we would represent the Stack Overflow query database, and in orange your '**{anchor}**' query which is outside the original database. The blue dot with the annotation 'Relevant Document' would be the most similar Stack Overflow query to our search.
78
 
79
  """
80
  )
@@ -90,7 +92,7 @@ st.markdown(
90
 
91
  We compare the embedding of our query with the embeddings of each of the texts in the database (there are easier ways to do it but in this case it won't be necessary) using the cosine similarity function, better explained in the [Pytorch documentation](https://pytorch.org/docs/stable/generated/torch.nn.functional.cosine_similarity.html). The results of the cosine similarity function will detect which of the texts in the database are closest to our query in vector space.
92
 
93
- This is what we did un the dynamic example above!
94
  """
95
  )
96
 
 
11
  )
12
 
13
 
14
+ st.subheader("Code Search: An Introduction to Semantic Search")
15
 
16
  st.markdown(
17
  """
18
+ Suppose you have a database of texts and you want to find which entries best match the meaning of a single text you have. Example, we want to see which function on Github best matches a description of a function we want to create in Python. However, just word-for-word matching will not work due to the complexity of the programming questions. We need to do a "smart" search: that's what semantic search is for.
19
 
20
  *"**Semantic search seeks to improve search accuracy by understanding the content of the search query. In contrast to traditional search engines which only find documents based on lexical matches, semantic search can also find synonyms.**" - [SBert Documentation](https://www.sbert.net/examples/applications/semantic-search/README.html)*.
21
 
22
 
23
+ Let's make this interactive and use the power of **[Streamlit](https://docs.streamlit.io/) + [HF Spaces](https://huggingface.co/spaces) + the '[sentence-transformers/all-mpnet-base-v2](https://huggingface.co/sentence-transformers/all-mpnet-base-v2)' model + the [Code Search Net Dataset](https://huggingface.co/datasets/code_search_net) in Hugging Face Datasets** by giving the description of a function we want to create and search what function in Github already is similar to our search. Right here!:
24
+
25
+ Disclaimer: For agility, we will use a sample dataset of just 10,000 Python functions on Github. These are very few observations for such a broad search. Therefore, the results may not be optimal.
26
  """
27
  )
28
 
29
  anchor = st.text_input(
30
+ "Please enter here the description of a Python function you want to create, we will look for similar ones in Github.",
31
+ value="Create a dictionary",
32
  )
33
 
34
  n_texts = st.number_input(
35
+ f"""How many similar functions you want?""", value=2, min_value=1
36
  )
37
+ if st.button("Find them."):
38
 
39
  st.markdown(
40
  """
41
+ **Amazing! These are the functions in the code search net dataset that better match the language of our specific description.** The 'func_documentation_string' column shows the documentation description of the function in Github, the 'repository_name' column shows the name of the repository where the function is, and 'func_code_url' takes you the function.
42
  """
43
  )
44
 
 
53
 
54
  st.markdown(
55
  f"""
56
+ First we embed our text database: we convert each of the function descriptions from the code search net dataset (the 'func_documentation_string' column) into a vector space. That is, convert function descriptions from words to numbers that provide an understanding of the language within each description. The understanding of each text will be reflected in a vector called embedding.
57
 
58
+ The system would look like the following figure. We embed (1) our texts database (in this case, the Github set of function descriptions), and (2) our own description (in this case: '{anchor}') **with the same model**. Notice that our descriptions could be a matrix of several elements.
59
 
60
  """
61
  )
 
72
 
73
  st.markdown(
74
  f"""
75
+ We now have two numerical representations of texts (embeddings): one for our original text database and one for our own query (here, the description of a python function). Our goal: get the texts in the database that have the closest meaning to our query.
76
 
77
  Queries that are most similar to each other will be closer together in the vector space, and queries that differ most will be farther apart.
78
 
79
+ The following figure (obtained from the [Sentence Transformers documentation](https://www.sbert.net/examples/applications/semantic-search/README.html)) shows in blue how we would represent the code search net dataset, and in orange your '**{anchor}**' query which is outside the original dataset. The blue dot with the annotation 'Relevant Document' would be the most similar Github function to our search.
80
 
81
  """
82
  )
 
92
 
93
  We compare the embedding of our query with the embeddings of each of the texts in the database (there are easier ways to do it but in this case it won't be necessary) using the cosine similarity function, better explained in the [Pytorch documentation](https://pytorch.org/docs/stable/generated/torch.nn.functional.cosine_similarity.html). The results of the cosine similarity function will detect which of the texts in the database are closest to our query in vector space.
94
 
95
+ This is what we did in the dynamic example above!
96
  """
97
  )
98