Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,50 @@
|
|
1 |
"""
|
2 |
-
Basic similarity search example.
|
3 |
"""
|
4 |
|
5 |
import os
|
6 |
|
7 |
import streamlit as st
|
8 |
|
9 |
-
from
|
10 |
|
|
|
11 |
|
12 |
-
|
13 |
-
"""
|
14 |
-
Main application.
|
15 |
-
"""
|
16 |
|
17 |
-
def __init__(self):
|
18 |
-
"""
|
19 |
-
Creates a new application.
|
20 |
-
"""
|
21 |
|
22 |
-
|
23 |
-
self.embeddings = Embeddings({"path": "sentence-transformers/nli-mpnet-base-v2"})
|
24 |
|
25 |
-
|
26 |
-
"""
|
27 |
-
Runs a Streamlit application.
|
28 |
-
"""
|
29 |
|
30 |
-
|
31 |
-
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
]
|
41 |
|
42 |
-
|
43 |
-
query = st.text_input("Query")
|
44 |
|
45 |
-
|
|
|
46 |
|
47 |
-
|
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 |
-
|
54 |
-
def create():
|
55 |
-
"""
|
56 |
-
Creates and caches a Streamlit application.
|
57 |
|
58 |
-
|
59 |
-
|
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)
|
|
|
|
|
|
|
|
|
|
|
|