Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from sentence_transformers import SentenceTransformer,util
|
| 4 |
+
#from transformers import pipeline
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import numpy as np
|
| 7 |
+
import pickle
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# Load the pre-trained SentenceTransformer model
|
| 11 |
+
#pipeline = pipeline(task="Sentence Similarity", model="all-MiniLM-L6-v2")
|
| 12 |
+
model = SentenceTransformer('neuml/pubmedbert-base-embeddings')
|
| 13 |
+
#sentence_embed = pd.read_csv('Reference_file.csv')
|
| 14 |
+
with open("embeddings_1.pkl", "rb") as fIn:
|
| 15 |
+
stored_data = pickle.load(fIn)
|
| 16 |
+
stored_code = stored_data["SBS_code"]
|
| 17 |
+
stored_sentences = stored_data["Description"]
|
| 18 |
+
stored_embeddings = stored_data["embeddings"]
|
| 19 |
+
|
| 20 |
+
import streamlit as st
|
| 21 |
+
|
| 22 |
+
# Define the function for mapping code
|
| 23 |
+
def mapping_code(user_input):
|
| 24 |
+
emb1 = model.encode(user_input.lower())
|
| 25 |
+
similarities = []
|
| 26 |
+
for sentence in stored_embeddings:
|
| 27 |
+
similarity = util.cos_sim(sentence, emb1)
|
| 28 |
+
similarities.append(similarity)
|
| 29 |
+
|
| 30 |
+
# Combine similarity scores with 'code' and 'description'
|
| 31 |
+
result = list(zip(stored_data["SBS_code"],stored_data["Description"], similarities))
|
| 32 |
+
|
| 33 |
+
# Sort results by similarity scores
|
| 34 |
+
result.sort(key=lambda x: x[2], reverse=True)
|
| 35 |
+
|
| 36 |
+
num_results = min(5, len(result))
|
| 37 |
+
|
| 38 |
+
# Return top 5 entries with 'code', 'description', and 'similarity_score'
|
| 39 |
+
top_5_results = []
|
| 40 |
+
if num_results > 0:
|
| 41 |
+
for i in range(num_results):
|
| 42 |
+
code, description, similarity_score = result[i]
|
| 43 |
+
top_5_results.append({"Code": code, "Description": description, "Similarity Score": similarity_score})
|
| 44 |
+
else:
|
| 45 |
+
top_5_results.append({"Code": "", "Description": "No similar sentences found", "Similarity Score": 0.0})
|
| 46 |
+
|
| 47 |
+
return top_5_results
|
| 48 |
+
# Streamlit frontend interface
|
| 49 |
+
def main():
|
| 50 |
+
st.title("CPT Description Mapping")
|
| 51 |
+
|
| 52 |
+
# Input text box for user input
|
| 53 |
+
user_input = st.text_input("Enter CPT description:")
|
| 54 |
+
|
| 55 |
+
# Button to trigger mapping
|
| 56 |
+
if st.button("Map"):
|
| 57 |
+
if user_input:
|
| 58 |
+
st.write("Please wait for a moment .... ")
|
| 59 |
+
|
| 60 |
+
# Call backend function to get mapping results
|
| 61 |
+
mapping_results = mapping_code(user_input)
|
| 62 |
+
|
| 63 |
+
# Display top 5 similar sentences
|
| 64 |
+
st.write("Top 5 similar sentences:")
|
| 65 |
+
for i, result in enumerate(mapping_results, 1):
|
| 66 |
+
st.write(f"{i}. Code: {result['Code']}, Description: {result['Description']}, Similarity Score: {float(result['Similarity Score']):.4f}")
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
main()
|