|
import streamlit as st |
|
import torch |
|
from sentence_transformers import SentenceTransformer, util |
|
|
|
import pickle |
|
import re |
|
|
|
|
|
model = SentenceTransformer('neuml/pubmedbert-base-embeddings') |
|
|
|
|
|
with open("embeddings_1.pkl", "rb") as fIn: |
|
stored_data = pickle.load(fIn) |
|
stored_embeddings = stored_data["embeddings"] |
|
|
|
def validate_input(input_string): |
|
|
|
pattern = r'^[a-zA-Z0-9]+$|^[a-zA-Z]+$' |
|
|
|
|
|
if re.match(pattern, input_string): |
|
return True |
|
else: |
|
return False |
|
|
|
|
|
def mapping_code(user_input,user_slider_input_number): |
|
|
|
emb1 = model.encode(user_input.lower()) |
|
similarities = [] |
|
for sentence in stored_embeddings: |
|
similarity = util.cos_sim(sentence, emb1) |
|
similarities.append(similarity) |
|
|
|
|
|
result = [(code, desc, sim) for (code, desc, sim) in zip(stored_data["SBS_code"], stored_data["Description"], similarities) if sim > user_slider_input_number] |
|
|
|
|
|
result.sort(key=lambda x: x[2], reverse=True) |
|
|
|
num_results = min(5, len(result)) |
|
|
|
|
|
top_5_results = [] |
|
if num_results > 0: |
|
for i in range(num_results): |
|
code, description, similarity_score = result[i] |
|
top_5_results.append({"Code": code, "Description": description, "Similarity Score": similarity_score}) |
|
else: |
|
top_5_results.append({"Code": "", "Description": "No match", "Similarity Score": 0.0}) |
|
|
|
return top_5_results |
|
|
|
|
|
def main(): |
|
st.title("CPT Description Mapping") |
|
st.markdown("<font color='red'>**⚠️ Please ensure the accuracy of your input spellings.**</font>", unsafe_allow_html=True) |
|
|
|
st.markdown("<font color='blue'>**💡 Note:** Please note that the similarity scores provided are not indicative of accuracy . Top 5 code description provided should be verified with CPT description by User.</font>", unsafe_allow_html=True) |
|
|
|
|
|
|
|
|
|
|
|
user_input = st.text_input("Enter CPT description:", placeholder="Please enter a full description for better search results.") |
|
|
|
|
|
if st.button("Map"): |
|
if not user_input.strip(): |
|
st.error("Input box cannot be empty.") |
|
elif not validate_input(user_input): |
|
st.warning("Please input correct description containing only letters and numbers, or letters only.") |
|
else: |
|
st.write("Please wait for a moment .... ") |
|
|
|
try: |
|
mapping_results = mapping_code(user_input) |
|
|
|
st.write("Top 5 similar sentences:") |
|
for i, result in enumerate(mapping_results, 1): |
|
st.write(f"{i}. Code: {result['Code']}, Description: {result['Description']}, Similarity Score: {float(result['Similarity Score']):.4f}") |
|
except ValueError as e: |
|
st.error(str(e)) |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|