import gradio as gr import tensorflow as tf from transformers import TFAutoModel, AutoTokenizer import os import numpy as np import shap import scipy.special model_name = 'cardiffnlp/twitter-roberta-base-sentiment-latest' tokenizer = AutoTokenizer.from_pretrained(model_name) model = tf.keras.models.load_model( "model.h5", custom_objects={ 'TFRobertaModel': TFAutoModel.from_pretrained(model_name) } ) labels = [ 'Cardiologist', 'Dermatologist', 'ENT Specialist', 'Gastro-enterologist', 'General-Physicians', 'Neurologist/Gastro-enterologist', 'Ophthalmologist', 'Orthopedist', 'Psychiatrist', 'Respirologist', 'Rheumatologist', 'Rheumatologist/Gastro-enterologist', 'Rheumatologist/Orthopedist', 'Surgeon' ] seq_len = 152 def prep_data(text): tokens = tokenizer( text, max_length=seq_len, truncation=True, padding='max_length', add_special_tokens=True, return_tensors='tf' ) return { 'input_ids': tokens['input_ids'], 'attention_mask': tokens['attention_mask'] } def inference(text): encoded_text = prep_data(text) probs = model.predict_on_batch(encoded_text) probabilities = {i:j for i,j in zip(labels, list(probs.flatten()))} return probabilities def predictor(x): input_ids = tokenizer(x, max_length=seq_len, truncation=True, padding='max_length', add_special_tokens=True, return_tensors='tf')['input_ids'] attention_mask = tokenizer(x, max_length=seq_len, truncation=True, padding='max_length', add_special_tokens=True, return_tensors='tf')['attention_mask'] outputs = model.predict([input_ids, attention_mask]) probas = tf.nn.softmax(outputs).numpy() val = scipy.special.logit(probas[:,1]) return val def f_batch(x): val = np.array([]) for i in x: val = np.append(val, predictor(i)) return val explainer_roberta = shap.Explainer(f_batch, tokenizer) shap_values = explainer_roberta(["When I remember her I feel down"]) def get_shap_data(input_text): shap_values = explainer_roberta([input_text]) html_shap_content = shap.plots.text(shap_values, display=False) return html_shap_content all_options = { 'Heart hurts': "Immediate medical attention is required. Please seek emergency care.", 'Acne': "Consider dermatological treatment options such as topical creams, medications, or lifestyle changes.", 'Hair falling out': "Consult a dermatologist to address hair-related concerns.", 'Infected wound': "Keep the wound clean and dry. If signs of infection develop, seek immediate medical attention. Consult with a surgeon for evaluation.", 'Skin issue': "Seek advice from a dermatologist for personalized treatment options.", 'Cough': "Stay hydrated, get plenty of rest, and consider over-the-counter cough medications.", 'Ear ache': "Avoid inserting anything into the ear and consult an ENT specialist for proper examination and treatment.", 'Feeling cold': "Rest, stay warm, and drink plenty of fluids. Seek medical attention if symptoms worsen.", 'Stomach ache': "Avoid spicy or fatty foods, and consider over-the-counter antacids for temporary relief.", 'Internal pain': "It's important to get a proper diagnosis. Please consult with a gastroenterologist for further evaluation.", 'Open wound': "Keep the wound clean and dry. If signs of infection develop, seek immediate medical attention. Consult with a surgeon for evaluation.", 'Feeling dizzy': "Ensure you are well-hydrated and rested. If symptoms persist, consult with a general physician for evaluation.", 'Body feels weak': "Get plenty of rest, eat a balanced diet, and consider consulting with a general physician for further evaluation.", 'Head ache': "Ensure you are well-hydrated and rested. If headaches persist, consult with a neurologist for evaluation.", 'Blurry vision': "Ensure you are well-rested and take regular breaks from screens. If vision problems persist, consult with an ophthalmologist for evaluation.", 'Joint pain': "Rest, gentle stretching, and over-the-counter pain relievers may provide relief. If symptoms persist, consult with an orthopedist for evaluation.", 'Knee pain': "Rest, gentle stretching, and over-the-counter pain relievers may provide relief. If symptoms persist, consult with an orthopedist for evaluation.", 'Back pain': "Rest, gentle stretching, and over-the-counter pain relievers may provide relief. If symptoms persist, consult with an orthopedist for evaluation.", 'Emotional pain': "Seek support from friends and family, and consider speaking with a therapist or psychiatrist for evaluation and support.", 'Hard to breath': "If experiencing difficulty breathing, seek immediate medical attention. Consult with a respirologist for evaluation.", 'Muscle pain': "Rest, gentle exercise, and over-the-counter pain relievers may provide relief. If symptoms persist, consult with a rheumatologist for evaluation.", 'Injury from sports': "Rest, gentle exercise, and over-the-counter pain relievers may provide relief. If symptoms persist, consult with a rheumatologist for evaluation.", 'Foot ache': "Rest, gentle exercise, and over-the-counter pain relievers may provide relief. If symptoms persist, consult with a rheumatologist for evaluation.", 'Shoulder pain': "Rest, gentle stretching, and over-the-counter pain relievers may provide relief. If symptoms persist, consult with a rheumatologist or gastroenterologist for evaluation.", 'Neck pain': "Rest, gentle stretching, and over-the-counter pain relievers may provide relief. If symptoms persist, consult with a rheumatologist or orthopedist for evaluation.", } def pt_f(x): return all_options.get(x) css = """ textarea { background-color: #00000000; border: 1px solid #6366f160; } """ with gr.Blocks(title="SpecX", css=css, theme=gr.themes.Soft()) as demo: with gr.Column(): hsdjif = gr.HTML(''' specx-logo ''') textmd = gr.Markdown('''

SpecX: Find the Right Specialist For Your Symptoms!

''') with gr.Row(): with gr.Column(scale=1, min_width=600): text_box = gr.Textbox(label="Explain your problem in one sentence.") with gr.Row(): submit_btn = gr.Button("Submit", elem_id="warningk", variant='primary') submit_btn_shap = gr.Button("Get SHAP Analysis", variant='secondary') examples = gr.Examples(examples=[ "When I remember her I feel down", "The area around my heart doesn't feel good.", "I have a split on my thumb that will not heal." ], inputs=text_box) with gr.Column(): label = gr.Label(num_top_classes=4, label="Recommended Specialist") gr.Markdown("## SHAP Analysis") shap_content_box = gr.HTML() submit_btn.click(inference, inputs=text_box, outputs=label) submit_btn_shap.click(get_shap_data, inputs=text_box, outputs=shap_content_box) with gr.Row(): textmd1 = gr.Markdown('''
''') with gr.Column(): textmd1 = gr.Markdown(''' ## Personalised Treatment ''') with gr.Row(): with gr.Column(): specialist_symptom = gr.Dropdown( label="Select Specialist Type", choices=list(all_options.keys()), ) submit_btn2 = gr.Button("Submit", elem_id="warningk", variant='primary') with gr.Column(): pt_text = gr.Textbox(label="Recommendation") submit_btn2.click(pt_f, inputs=specialist_symptom, outputs=pt_text) with gr.Row(): textmd1 = gr.Markdown('''
''') with gr.Column(): textmd1 = gr.Markdown(''' ## Telemedicine Appointment ''') with gr.Row(): gr.HTML("""""") with gr.Column(): textmd1 = gr.Markdown(''' ## Download SpecX App ''') serdger = gr.HTML(""" QR for SpecX App """) with gr.Row(): with gr.Column(): textmd1 = gr.Markdown('''

98% Success Rate

''') with gr.Column(): textmd2 = gr.Markdown('''

99.9% Model uptime

''') with gr.Column(): textmd3 = gr.Markdown('''

77,223 Monthly active users

''') with gr.Column(): textmd3 = gr.Markdown('''

990,224 total model predictions made

''') demo.launch()