Rajagopal commited on
Commit
73473a7
1 Parent(s): 04ca4b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -48
app.py CHANGED
@@ -1,70 +1,50 @@
1
  """
2
- Basic similarity search example. Used in the original txtai demo.
3
  """
4
 
5
  import os
6
 
7
  import streamlit as st
8
 
9
- from txtai.embeddings import Embeddings
10
 
 
11
 
12
- class Application:
13
- """
14
- Main application.
15
- """
16
 
17
- def __init__(self):
18
- """
19
- Creates a new application.
20
- """
21
 
22
- # Create embeddings model, backed by sentence-transformers & transformers
23
- self.embeddings = Embeddings({"path": "sentence-transformers/nli-mpnet-base-v2"})
24
 
25
- def run(self):
26
- """
27
- Runs a Streamlit application.
28
- """
29
 
30
- st.title("Similarity Search")
31
- st.markdown("This application runs a basic similarity search that identifies the best matching row for a query.")
32
 
33
- data = [
34
- "US tops 5 million confirmed virus cases",
35
- "Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg",
36
- "Beijing mobilises invasion craft along coast as Taiwan tensions escalate",
37
- "The National Park Service warns against sacrificing slower friends in a bear attack",
38
- "Maine man wins $1M from $25 lottery ticket",
39
- "Make huge profits without work, earn up to $100,000 a day",
40
- ]
41
 
42
- data = st.text_area("Data", value="\n".join(data))
43
- query = st.text_input("Query")
44
 
45
- data = data.split("\n")
 
46
 
47
- if query:
48
- # Get index of best section that best matches query
49
- uid = self.embeddings.similarity(query, data)[0][0]
50
- st.write(data[uid])
51
 
 
 
52
 
53
- @st.cache(allow_output_mutation=True)
54
- def create():
55
- """
56
- Creates and caches a Streamlit application.
57
 
58
- Returns:
59
- Application
60
- """
 
 
61
 
62
- return Application()
63
 
64
-
65
- if __name__ == "__main__":
66
- os.environ["TOKENIZERS_PARALLELISM"] = "false"
67
-
68
- # Create and run application
69
- app = create()
70
- app.run()
 
1
  """
2
+ Basic similarity search example.
3
  """
4
 
5
  import os
6
 
7
  import streamlit as st
8
 
9
+ from sentence_transformers import SentenceTransformer, util
10
 
11
+ #model = SentenceTransformer("all-MiniLM-L6-v2")
12
 
13
+ model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')
 
 
 
14
 
 
 
 
 
15
 
16
+ listofCachedItems = ["what was the revenue for FIFA 23", "what was the revenue for ApexLegends", "What was the revenue for FIFA 23 in Aug 2023", "What was the revenue for ApexLegends in Aug 2023"]
 
17
 
18
+ emb1 = model.encode("what was the revenue for FIFA 23 for UK" )
 
 
 
19
 
20
+ maxscore = 0
21
+ bestmatch = ""
22
 
23
+ for i in listofCachedItems:
24
+ emb2 = model.encode(i)
25
+ cos_sim = util.cos_sim(emb1, emb2)
26
+ print("Cosine-Similarity:" + str(cos_sim) + "\t\t Sentance " + str(i) )
27
+ if cos_sim > maxscore :
28
+ maxscore = cos_sim
29
+ bestmatch = i
 
30
 
31
+ print("Final Result:-")
 
32
 
33
+ print(bestmatch)
34
+ print(maxscore)
35
 
36
+ print(type(maxscore))
 
 
 
37
 
38
+ numericscore = maxscore[0].tolist()
39
+ numericscore = numericscore[0]
40
 
41
+ print(numericscore)
 
 
 
42
 
43
+ if numericscore > 0.45:
44
+ print(bestmatch)
45
+ print(maxscore)
46
+ else:
47
+ print("No matches")
48
 
 
49
 
50
+ st.write(bestmatch)