similarity / app.py
Rajagopal's picture
Update app.py
e476748 verified
raw
history blame contribute delete
No virus
1.61 kB
"""
Basic similarity search example.
"""
import os
import streamlit as st
from sentence_transformers import SentenceTransformer, util
# Write directly to the app
st.title("IRIS - User Experience: : Getting the end-user to choose a similar cached question ")
st.write(
"""Type your question!
System will display **most similar questions**
.
"""
)
st.text_input("Type your question here", key="userquery")
#model = SentenceTransformer("all-MiniLM-L6-v2")
model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')
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"]
emb1 = model.encode(st.session_state.userquery )
maxscore = 0
bestmatch = ""
for i in listofCachedItems:
emb2 = model.encode(i)
cos_sim = util.cos_sim(emb1, emb2)
#print("Cosine-Similarity:" + str(cos_sim) + "\t\t Sentance " + str(i) )
if cos_sim > maxscore :
maxscore = cos_sim
bestmatch = i
#print("Final Result:-")
#print(bestmatch)
#print(maxscore)
#print(type(maxscore))
numericscore = maxscore[0].tolist()
numericscore = numericscore[0]
#print(numericscore)
listofanswer = []
if numericscore > 0.95:
# print(bestmatch)
# print(maxscore)
listofanswer.append(bestmatch)
st.write("Found a similar question that is already precomputed")
option = st.selectbox( 'We identified something similar. Try this?', listofanswer)
else:
st.write("That is a new question. There is no similar questions that is cached")