|
import streamlit as st |
|
import torch |
|
from sentence_transformers import SentenceTransformer, util |
|
from spellchecker import SpellChecker |
|
import pickle |
|
|
|
|
|
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 check_misspelled_words(user_input): |
|
spell = SpellChecker() |
|
|
|
words = user_input.split() |
|
|
|
misspelled = spell.unknown(words) |
|
return misspelled |
|
|
|
|
|
def mapping_code(user_input): |
|
if len(user_input.split()) < 5: |
|
raise ValueError("Input sentence should be at least 5 words long.") |
|
emb1 = model.encode(user_input.lower()) |
|
similarities = util.pytorch_cos_sim(emb1, stored_embeddings)[0] |
|
|
|
result = [(code, description, float(sim)) for code, description, sim in zip(stored_data["SBS_code"], stored_data["Description"], similarities)] |
|
|
|
result.sort(key=lambda x: x[2], reverse=True) |
|
|
|
num_results = min(5, len(result)) |
|
top_5_results = [{"Code": code, "Description": description, "Similarity Score": sim} for code, description, sim in result[:num_results]] |
|
return top_5_results |
|
|
|
|
|
def main(): |
|
st.title("CPT Description Mapping") |
|
|
|
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.") |
|
else: |
|
st.write("Please wait for a moment .... ") |
|
|
|
try: |
|
misspelled_words = check_misspelled_words(user_input) |
|
if misspelled_words: |
|
st.write("Please enter a detailed correct full description") |
|
st.write(misspelled_words) |
|
else: |
|
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: {result['Similarity Score']:.4f}") |
|
except ValueError as e: |
|
st.error(str(e)) |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|