|
import os |
|
import streamlit as st |
|
import pandas as pd |
|
from datasets import load_from_disk |
|
from transformers import AutoTokenizer, TFAutoModel |
|
from dotenv import load_dotenv |
|
|
|
load_dotenv() |
|
|
|
|
|
|
|
Drugs = ['Amitriptyline', 'Ambien', 'Aripiprazole', 'Actos', 'Aubra', |
|
'Amphetamine / dextroamphetamine', 'Afrezza', |
|
'Aluminum chloride hexahydrate', 'Acetaminophen / hydrocodone', |
|
'Aviane', 'Aldesleukin', 'Acamprosate', 'Azathioprine', |
|
'Adapalene / benzoyl peroxide', |
|
'Acetaminophen / butalbital / caffeine', 'Accutane', |
|
'Ayr Saline Nasal', 'Asenapine', 'Adipex-P', 'Augmentin', 'Apri', |
|
'Alesse', 'Aspirin / carisoprodol', 'Azelaic acid', 'Azithromycin', |
|
'Alpha 1-proteinase inhibitor', 'Abilify', 'Amlodipine', |
|
'Arimidex', 'Acetaminophen / codeine', 'Advair Diskus', |
|
'Alprazolam', 'Alcaftadine', |
|
'Acetaminophen / dexbrompheniramine / pseudoephedrine', 'AndroGel', |
|
'Avinza', 'Ativan', 'Atomoxetine', 'Alphagan P', 'Apremilast', |
|
'Advair HFA', 'Anastrozole', 'Azor', 'Azelastine / fluticasone', |
|
'Amerge', 'Avonex Pen', 'Acetaminophen / oxycodone', |
|
'Abacavir / dolutegravir / lamivudine', 'Adderall', 'Atorvastatin', |
|
'Aptensio XR', |
|
'Acetaminophen / dichloralphenazone / isometheptene mucate', |
|
'Acyclovir', 'Armodafinil', 'Adderall XR', |
|
'Antipyrine / benzocaine', 'Aczone', 'Amoxicillin / clavulanate', |
|
'Adalimumab', 'Aranesp', 'Ammonium lactate / halobetasol', |
|
'Atenolol', 'Avelox', 'Azithromycin Dose Pack', 'Adapalene', |
|
'Amoxicillin', 'Acetaminophen / aspirin / caffeine', 'Azelastine', |
|
'Albuterol', 'Amethyst', 'Asacol', 'Aleve', 'Alprostadil', |
|
'Astelin', 'Atropine / hyoscyamine / phenobarbital / scopolamine','Ascorbic acid', 'Asthmanefrin', 'Allegra-D 12 Hour', 'Amikacin', |
|
'Arixtra', 'Apokyn', 'AbobotulinumtoxinA', 'Axid', 'Accolate', |
|
'Adoxa', 'Aspirin / caffeine', 'Aliskiren / hydrochlorothiazide', |
|
'Afinitor', 'Acetaminophen / phenylephrine', 'Atacand', |
|
'Allergy DN PE','Bactrim', 'Belviq', 'Blisovi Fe 1 / 20', |
|
'Benzoyl peroxide / clindamycin', 'BuSpar', 'Bupropion', |
|
'Bacitracin / neomycin / polymyxin b', 'Brisdelle', 'Beyaz', |
|
'Buprenorphine / naloxone', 'Benzonatate', 'Bisacodyl', 'Buprenex', |
|
'Benzoyl peroxide / erythromycin', 'Budeprion SR', |
|
'Bupropion / naltrexone', 'Belsomra', 'Bevacizumab', |
|
'Barium sulfate', 'Bismuth subsalicylate', 'Brovana', 'Buspirone', |
|
'Bronkaid', 'Benzocaine', 'Butrans', 'Bunavail', 'Benicar', |
|
'Benadryl Allergy', 'Benadryl', 'Biotin', 'Bromfed DM', |
|
'Breo Ellipta', 'Biaxin', 'Botox', 'Buprenorphine', |
|
'Benzoic acid / salicylic acid', 'Brimonidine / timolol', |
|
'Beclomethasone', 'Bydureon', 'Bonine', 'Brilinta', 'Bactrim DS', |
|
'Brexpiprazole', 'Baclofen', 'Benazepril', 'Biaxin XL', 'Basaglar', |
|
'Bismuth subcitrate potassium / metronidazole / tetracycline', |
|
'Belbuca', 'Bromocriptine', 'Budesonide / formoterol', |
|
'Blisovi 24 Fe', 'Bystolic', 'Banzel', 'Balacet', 'Balsalazide', |
|
'Benlysta', 'Belimumab', 'Byetta', |
|
'Bisoprolol / hydrochlorothiazide', 'Butorphanol', 'Boniva', |
|
'Briviact', 'Brimonidine', 'Biafine', 'Betaseron', |
|
'Benazepril / hydrochlorothiazide', 'Benzoyl peroxide / sulfur', |
|
'Bisacodyl / polyethylene glycol 3350 / potassium chloride / sodium bicarbonate / sodium chloride', |
|
'Balsam peru / castor oil / trypsin', |
|
'Betamethasone / calcipotriene', 'Budeprion XL', 'Benicar HCT', |
|
'Bepotastine', 'Bentyl', 'Brompheniramine', 'Benzaclin', |
|
'Bisoprolol', 'Budesonide', 'Bontril Slow Release', 'Bepreve','Berinert', 'Benzoyl peroxide', 'Belladonna / opium', |
|
'Butabarbital', 'Bioflavonoids / zinc glycinate', 'Brodalumab', |
|
'Bicillin L-A', 'Bimatoprost', 'Benztropine', 'Bethanechol', |
|
'Bupivacaine', 'Bosutinib', 'Bismuth subgallate', 'BenzEFoam', |
|
'Brivaracetam', 'Blistex','Contrave', 'Cyclafem 1 / 35', 'Copper', 'Chantix', |
|
'Ciprofloxacin', 'Cyclosporine', 'Clonazepam', 'Ciclopirox', |
|
'Campral', 'Cryselle', 'Clindamycin', 'Clonidine', 'Celecoxib', |
|
'Caffeine', 'Clarithromycin', 'Clomiphene', 'Clotrimazole', |
|
'Celexa', 'Codeine / guaifenesin', 'Cefuroxime', 'Cymbalta', |
|
'Canagliflozin', 'Citalopram', 'Corticotropin', 'Cefdinir', |
|
'Carvedilol', 'Chlordiazepoxide', 'CellCept', |
|
'Cobicistat / elvitegravir / emtricitabine / tenofovir alafenamide', |
|
'Chateal', 'Cyclobenzaprine', 'Cialis', 'Cholestyramine', 'Cozaar', |
|
'Catapres', 'Carbidopa / levodopa', 'Carisoprodol', |
|
'Citric acid / magnesium oxide / sodium picosulfate', 'Cetirizine', |
|
'Cambia', 'Clindamycin / tretinoin', 'Crestor', 'Copaxone', |
|
'Chlorpheniramine / hydrocodone / pseudoephedrine', |
|
'Cobicistat / elvitegravir / emtricitabine / tenofovir', |
|
'Cogentin', 'Cosentyx', 'Cipro', 'Cytomel', 'Camrese', 'Clomid', |
|
'Celebrex', 'Colesevelam', 'Clozapine', 'Cutivate', 'Cyred', |
|
'Concerta', 'Chlorpheniramine / hydrocodone', 'Claravis', |
|
'Cyanocobalamin', 'Clopidogrel', 'Cyproheptadine', |
|
'Chlordiazepoxide / clidinium', 'Carbamazepine', 'Crisaborole', |
|
'Colchicine', 'Ciprofloxacin / dexamethasone', 'Clomipramine', |
|
'Cabergoline', 'Carac', 'Cephalexin', 'Cariprazine', 'Correctol', |
|
'Celestone', 'Creon', 'Clobetasol', 'Colazal', 'Chlorzoxazone', |
|
'Cenestin', 'Casodex', 'Cinryze', 'Claritin-D', 'Chlorhexidine', |
|
'Complera', 'Cervidil', 'Cefixime', 'Coreg', 'Camila', |
|
'Clorazepate', 'Cevimeline', 'Cosopt', 'Chondroitin / glucosamine','Citrate of Magnesia', 'Coricidin HBP Cold & Flu', 'Cefditoren', |
|
'Ceftibuten', 'Cyclessa', 'Cortef', 'Calcium acetate', 'Cyclizine', |
|
'Coagulation factor ix', 'Colace', 'Carmol 20', 'Calan SR', |
|
'Cyklokapron', 'Coal tar', 'Cobicistat / darunavir', 'Calamine', |
|
'CoQ10','Duloxetine', 'Depakote', 'Drospirenone / estradiol', |
|
'Depo-Provera', 'Desyrel', 'Desvenlafaxine', |
|
'Drospirenone / ethinyl estradiol', 'Doxylamine / pyridoxine', |
|
'Demerol', 'Dextromethorphan', 'Diazepam', 'Diphenhydramine', |
|
'Denosumab', 'Dulaglutide', 'Drysol', 'Divalproex sodium', |
|
'Doxycycline', 'Desogestrel / ethinyl estradiol', 'Duofilm', |
|
'Dicyclomine', 'Dexmethylphenidate', 'Diltiazem', 'Dapsone', |
|
'Dalfampridine', 'Dilantin', 'Dienogest / estradiol', 'Diclofenac', |
|
'Donnatal', 'Depakote ER', 'Donepezil', 'Dulcolax', 'Dulera', |
|
'Dapagliflozin', 'Duexis', 'Differin', 'Doxepin', 'Docosanol', |
|
'Diclegis', 'Desloratadine', |
|
'Drospirenone / ethinyl estradiol / levomefolate calcium', 'Duac', |
|
'Deplin', 'Doryx', 'Dilaudid', 'Dimenhydrinate', 'Delsym', |
|
'Denavir', 'D.H.E. 45', 'Disulfiram', 'Droperidol', 'Dasatinib', |
|
'Dextrostat', 'Dymista', 'Dextroamphetamine', 'DDAVP Rhinal Tube', |
|
'Dabigatran', 'Dasabuvir / ombitasvir / paritaprevir / ritonavir', |
|
'Dextromethorphan / guaifenesin', 'Diflucan', 'Debrox', |
|
'Diphenhydramine / naproxen', 'Daklinza', 'Daliresp', |
|
'Dihydroergotamine', 'Dinoprostone', 'Dermal filler', 'Doxylamine', |
|
'Daytrana', 'Diprivan', 'Dexlansoprazole', 'Dovonex', 'Doral', |
|
'Desquam-X Wash', 'Dexilant', 'Dofetilide', 'Diovan HCT', 'Detrol', |
|
"Dimetapp Children's Cold & Cough", 'Delatestryl', 'Desipramine', |
|
'Daclatasvir', 'Depo-Testosterone', 'Dulcolax Laxative', |
|
'Dexamethasone', 'Dimethyl fumarate', 'Dronabinol', 'Duragesic', |
|
'Dexedrine', 'Dupixent', 'Dramamine','Daypro', 'Dyazide', 'Deltasone', 'Depo-Medrol', |
|
'Dapagliflozin / metformin', 'Dilaudid-HP', 'Doxorubicin', |
|
'Deoxycholic acid', |
|
'Dextromethorphan / phenylephrine / pyrilamine', |
|
'Diphenhydramine / ibuprofen', 'Divigel', 'Dermatop'] |
|
|
|
model_ckpt = "sentence-transformers/multi-qa-mpnet-base-dot-v1" |
|
tokenizer = AutoTokenizer.from_pretrained(model_ckpt) |
|
model = TFAutoModel.from_pretrained(model_ckpt, from_pt=True) |
|
|
|
def cls_pooling(model_output): |
|
return model_output.last_hidden_state[:, 0] |
|
|
|
def get_embeddings(text_list): |
|
encoded_input = tokenizer( |
|
text_list, padding=True, truncation=True, return_tensors="tf" |
|
) |
|
encoded_input = {k: v for k, v in encoded_input.items()} |
|
model_output = model(**encoded_input) |
|
return cls_pooling(model_output) |
|
|
|
embeddings_dataset = load_from_disk("data") |
|
embeddings_dataset.add_faiss_index(column="embeddings") |
|
|
|
def recommendations(question): |
|
question_embedding = get_embeddings([question]).numpy() |
|
scores, samples = embeddings_dataset.get_nearest_examples( |
|
"embeddings", question_embedding, k=5 |
|
) |
|
samples_df = pd.DataFrame.from_dict(samples) |
|
samples_df["scores"] = scores |
|
samples_df.sort_values("scores", ascending=False, inplace=True,ignore_index=True) |
|
return samples_df[['drugName', 'review', 'scores']] |
|
|
|
|
|
st.title("Drug Recommendation System") |
|
|
|
st.markdown( |
|
""" |
|
<style> |
|
#MainMenu {visibility: hidden;} |
|
footer {visibility: hidden;} |
|
</style> |
|
""", |
|
unsafe_allow_html=True |
|
) |
|
|
|
|
|
|
|
st.sidebar.title("Choose or Enter a Question:") |
|
selection_type = st.sidebar.radio("Select type:", ("Select Default", "Enter Custom")) |
|
|
|
if selection_type == "Select Default": |
|
selected_question = st.sidebar.selectbox("Select a question", Drugs) |
|
if st.sidebar.button("Show Recommendations"): |
|
recommendation_result = recommendations(selected_question) |
|
st.header(f"Top 5 Recommended Drugs for '{selected_question}':") |
|
st.table(recommendation_result) |
|
else: |
|
default_question = "I've acne problem" |
|
custom_question = st.sidebar.text_input("Enter your question:", default_question) |
|
if st.sidebar.button("Get Recommendations"): |
|
if custom_question: |
|
custom_recommendation_result = recommendations(custom_question) |
|
st.header("Top 5 Recommended Drugs for Your Question:") |
|
st.table(custom_recommendation_result) |
|
else: |
|
st.warning("Please enter a question to get recommendations.") |