Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import torch | |
| from sentence_transformers import SentenceTransformer,util | |
| #from transformers import pipeline | |
| import pandas as pd | |
| import numpy as np | |
| # Load the pre-trained SentenceTransformer model | |
| #pipeline = pipeline(task="Sentence Similarity", model="all-MiniLM-L6-v2") | |
| model = SentenceTransformer('all-MiniLM-L6-v2') | |
| sentence_embed = pd.read_csv('Reference_file_2 (1).csv') | |
| #st.write(sentence_embed.head(5)) | |
| # Function to compute cosine similarity | |
| def cosine_similarity(v1, v2): | |
| """Compute cosine similarity between two vectors.""" | |
| return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2)) | |
| # Backend function for mapping | |
| def mapping_code(user_input): | |
| emb1 = model.encode(user_input, convert_to_tensor=True).astype(float) | |
| similarities = [] | |
| for sentence_emb in sentence_embed['embeds']: | |
| sentence_emb = np.array(sentence_emb).astype(float) | |
| similarity = cosine_similarity(sentence_emb, emb1) | |
| similarities.append(similarity) | |
| # Combine similarity scores with 'code' and 'description' | |
| result = list(zip(sentence_embed['SBS Code'], sentence_embed['Long Description'], similarities)) | |
| # Sort results by similarity scores | |
| result.sort(key=lambda x: x[2], reverse=True) | |
| # Return top 5 entries with 'code', 'description', and 'similarity_score' | |
| top_5_results = [] | |
| for i in range(5): | |
| code, description, similarity_score = result[i] | |
| top_5_results.append({"Code": code, "Description": description, "Similarity Score": similarity_score}) | |
| return top_5_results | |
| # Streamlit frontend interface | |
| def main(): | |
| st.title("CPT Description Mapping") | |
| # Input text box for user input | |
| user_input = st.text_input("Enter CPT description:") | |
| # Button to trigger mapping | |
| if st.button("Map"): | |
| if user_input: | |
| st.write("Please wait for a moment .... ") | |
| # Call backend function to get mapping results | |
| mapping_results = mapping_code(user_input) | |
| # Display top 5 similar sentences | |
| 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}") | |
| if __name__ == "__main__": | |
| main() |