import streamlit as st
import pickle
import numpy as np
import pandas as pd
from transformers import AutoTokenizer,AutoModel
import torch
import tensorflow as tf
from keras.models import load_model
def predict(new_data):
tokens = tokenizer(new_data.split(), padding=True, truncation=True, max_length=128, return_tensors='pt')
with torch.no_grad():
embeddings = model(tokens['input_ids'], attention_mask=tokens['attention_mask'])[0][:, 0, :].numpy()
y_pred = rf.predict(embeddings)
prev_label=" "
text=new_data.split()
data=[]
labels=[]
for i,(word,label) in enumerate(zip(text,y_pred)):
if label!="Other":
label=label.split('-')[1]
if prev_label==label:
data[-1]=data[-1]+" "+word
else:
data.append(word)
labels.append(label)
prev_label=label
return(data,labels)
def highlight(sentence):
highlighted_text = ""
entity_colors = {"Symptom":"#87cefa","Medical Condition":"#ffb6c1"}
words, labels = predict(sentence)
for words, label in zip(words, labels):
prev_label=""
if label!="Other" and words!="a":
if label in ["Medical Condition","Symptom"]:
word_color = entity_colors.get(label, "yellow")
label_color = entity_colors.get(label + '-label', "black")
highlighted_text += f'{words}{label} '
else:
highlighted_text += f'{words} '
else:
highlighted_text += f'{words} '
st.markdown(highlighted_text, unsafe_allow_html=True)
#Load the trained model
with open("biobert_rf.pkl", 'rb') as f:
rf = pickle.load(f)
# Load the BioBERT model and tokenizer
model_name = "dmis-lab/biobert-base-cased-v1.1"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)
st.title('Oral Medicine Meets NLP')
st.subheader('Named Entity Recoginition System For Oral Medicine ')
sentence = st.text_area('Enter a sentence:')
st.write("OR")
selected_options = st.selectbox(
'Choose a text from dropdown: ',
(" ",
'Anemia and gingival bleeding are connected in that anemia can be a contributing cause to the occurrence of gingival bleeding. Anemia is a condition characterized by a shortage in the number or quality of red blood cells, which can lead to a reduced ability of the blood to carry oxygen throughout the body.',
'Hemophilia is a genetic illness that mainly affects the blood ability to clot properly. Individuals with significant hemophilia are at an elevated possibility of experiencing unforeseen bleeding episodes, which can occur in various parts of the body, including the mouth. Oral bleeding can be a sign of hemophilia and can present as gum bleeding or mouth sores.',
"Von Willebrand disease VWD is a genetic condition that impairs the blood's ability to clot properly. One of the symptoms of VWD is spontaneous gingival bleeding , which can occur without any apparent cause or trauma")) # set default to None
# Define the colors for each label
if st.button('Analyze'):
if sentence:
highlight(sentence)
elif selected_options:
highlight(selected_options)
else:
st.write("Please enter a text or select an example to analyze")