Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from sentence_transformers import SentenceTransformer, util
|
4 |
+
|
5 |
+
# Load the pre-trained SentenceTransformer model
|
6 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
7 |
+
|
8 |
+
# Define the backend function
|
9 |
+
def mapping_code(user_input):
|
10 |
+
emb1 = model.encode(user_input.lower())
|
11 |
+
similarities = []
|
12 |
+
for sentence_embed in sentences['embeds']:
|
13 |
+
similarity = util.cos_sim(sentence_embed, emb1)
|
14 |
+
similarities.append(similarity)
|
15 |
+
|
16 |
+
# Combine similarity scores with 'code' and 'description'
|
17 |
+
result = list(zip(sentences['SBS Code'], sentences['Long Description'], similarities))
|
18 |
+
|
19 |
+
# Sort results by similarity scores
|
20 |
+
result.sort(key=lambda x: x[2], reverse=True)
|
21 |
+
|
22 |
+
# Return top 5 entries with 'code', 'description', and 'similarity_score'
|
23 |
+
top_5_results = []
|
24 |
+
for i in range(5):
|
25 |
+
code, description, similarity_score = result[i]
|
26 |
+
top_5_results.append({"Code": code, "Description": description, "Similarity Score": similarity_score})
|
27 |
+
return top_5_results
|
28 |
+
|
29 |
+
# Streamlit frontend interface
|
30 |
+
def main():
|
31 |
+
st.title("CPT Description Mapping")
|
32 |
+
|
33 |
+
# Input text box for user input
|
34 |
+
user_input = st.text_input("Enter CPT description:")
|
35 |
+
|
36 |
+
# Button to trigger mapping
|
37 |
+
if st.button("Map"):
|
38 |
+
if user_input:
|
39 |
+
st.write("Please wait for a moment .... ")
|
40 |
+
|
41 |
+
# Call backend function to get mapping results
|
42 |
+
mapping_results = mapping_code(user_input)
|
43 |
+
|
44 |
+
# Display top 5 similar sentences
|
45 |
+
st.write("Top 5 similar sentences:")
|
46 |
+
for i, result in enumerate(mapping_results, 1):
|
47 |
+
st.write(f"{i}. Code: {result['Code']}, Description: {result['Description']}, Similarity Score: {result['Similarity Score']:.4f}")
|
48 |
+
|
49 |
+
# Run the app
|
50 |
+
if __name__ == "__main__":
|
51 |
+
main()
|