|
|
|
import os |
|
import joblib |
|
import pandas as pd |
|
import re |
|
import nltk |
|
import streamlit as st |
|
import numpy as np |
|
from nltk.stem import WordNetLemmatizer |
|
from nltk.corpus import stopwords |
|
from bs4 import BeautifulSoup |
|
import plotly.express as px |
|
|
|
|
|
nltk.download('wordnet') |
|
nltk.download('stopwords') |
|
|
|
|
|
MODEL_PATH = 'model/passmodel.pkl' |
|
TOKENIZER_PATH = 'model/tfidfvectorizer.pkl' |
|
DATA_PATH = 'data/custom_dataset.csv' |
|
|
|
|
|
vectorizer = joblib.load(TOKENIZER_PATH) |
|
model = joblib.load(MODEL_PATH) |
|
|
|
|
|
stop = stopwords.words('english') |
|
lemmatizer = WordNetLemmatizer() |
|
|
|
|
|
st.set_page_config( |
|
page_title='DPDR', |
|
page_icon='π¨ββοΈ', |
|
layout='wide' |
|
) |
|
|
|
|
|
st.markdown(""" |
|
<style> |
|
/* Predict button styling */ |
|
.stButton button { |
|
background-color: #89b4fa; /* Blue */ |
|
color: #1e1e2e; /* Base (dark text) */ |
|
font-size: 18px; |
|
padding: 10px 24px; |
|
border-radius: 8px; |
|
border: none; |
|
transition: background-color 0.3s ease; |
|
} |
|
.stButton button:hover { |
|
background-color: #000000; /* Dark Black */ |
|
color: #ffffff; /* White text for contrast */ |
|
} |
|
.stButton button:active { |
|
background-color: #89dceb; /* Sky */ |
|
} |
|
|
|
/* Condition Predicted styling */ |
|
.condition-card { |
|
padding: 20px; |
|
border-radius: 10px; |
|
background-color: #FFA500; /* Vibrant Orange */ |
|
color: #1e1e2e; /* Base (dark text) */ |
|
margin: 10px 0; |
|
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2); |
|
font-weight: bold; /* Make text bold for emphasis */ |
|
} |
|
|
|
/* Top Recommended Drugs styling */ |
|
.drug-card-1 { |
|
padding: 15px; |
|
border-radius: 8px; |
|
background-color: #b4befe; /* Lavender */ |
|
color: #1e1e2e; /* Base (dark text) */ |
|
margin: 10px 0; |
|
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.1); |
|
} |
|
.drug-card-2 { |
|
padding: 15px; |
|
border-radius: 8px; |
|
background-color: #a6e3a1; /* Green */ |
|
color: #1e1e2e; /* Base (dark text) */ |
|
margin: 10px 0; |
|
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.1); |
|
} |
|
.drug-card-3 { |
|
padding: 15px; |
|
border-radius: 8px; |
|
background-color: #f38ba8; /* Red */ |
|
color: #1e1e2e; /* Base (dark text) */ |
|
margin: 10px 0; |
|
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.1); |
|
} |
|
.drug-card-4 { |
|
padding: 15px; |
|
border-radius: 8px; |
|
background-color: #f2cdcd; /* Flamingo */ |
|
color: #1e1e2e; /* Base (dark text) */ |
|
margin: 10px 0; |
|
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.1); |
|
} |
|
</style> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
st.title("π An Intelligent System for Disease Prediction and Drug Recommendation π") |
|
st.markdown("---") |
|
|
|
|
|
with st.sidebar: |
|
st.header("βοΈ Settings") |
|
st.markdown("Configure the model and data paths here.") |
|
MODEL_PATH = st.text_input("Model Path", value='model/passmodel.pkl') |
|
TOKENIZER_PATH = st.text_input("Tokenizer Path", value='model/tfidfvectorizer.pkl') |
|
DATA_PATH = st.text_input("Data Path", value='data/custom_dataset.csv') |
|
|
|
|
|
symptoms = ["Acne", "Anxiety", "Depression", "High blood pressure", "Diabetes", "Migraine", |
|
"Cough", "Wheezing", "Shortness of breath", "Fever", "Fatigue", "Frequent urination", |
|
"Painful urination", "Chest pain", "Headache", "Nausea", "Dizziness", "Skin rash", |
|
"Insomnia", "Difficulty concentrating", "Hyperactivity", "Irritability", "Weight gain", |
|
"Weight loss", "Blurred vision", "Increased thirst", "Increased hunger", "Joint pain", |
|
"Swelling", "Stress", "Mood swings", "Back pain", "Abdominal pain", "Irregular menstruation", |
|
"Heavy menstrual bleeding", "Missed periods", "Breast tenderness", "Nausea after eating", |
|
"Heart palpitations", "Cold sweats", "Night sweats", "Dry mouth", "Frequent infections", |
|
"Slow healing wounds", "Tingling in hands or feet", "Numbness in extremities", "Loss of appetite", |
|
"Excessive sweating", "Rapid heartbeat", "Chest tightness", "Nasal congestion", "Sinus pressure", |
|
"Sore throat", "Runny nose", "Chills", "Muscle aches", "Confusion", "Memory problems", |
|
"Low energy", "Feeling hopeless", "Loss of interest in activities", "Suicidal thoughts", |
|
"Restlessness", "Aggression", "Impulsivity", "Poor coordination", "Frequent urination at night", |
|
"Blood in urine", "Cloudy urine", "Pelvic pain", "Lower back pain", "Burning sensation during urination", |
|
"Foul-smelling urine", "Itching or irritation in the genital area", "Redness or swelling in the genital area", |
|
"White patches on the skin", "Blackheads", "Whiteheads", "Oily skin", "Dry skin", "Skin discoloration", |
|
"Flaky skin", "Sensitivity to light", "Sensitivity to sound", "Aura (visual disturbances)", "Neck pain", |
|
"Stiff neck", "Sensitivity to smells", "Loss of balance", "Ringing in the ears", "Frequent yawning", |
|
"Excessive thirst at night", "Frequent nighttime urination", "Dark patches on the skin", "Frequent headaches", |
|
"Eye pain", "Blurry vision", "Double vision", "Eye redness", "Eye swelling", "Eye discharge", "Ear pain", |
|
"Ear discharge", "Hearing loss", "Tinnitus", "Jaw pain", "Tooth sensitivity", "Gum bleeding", "Bad breath", |
|
"Swollen lymph nodes", "Hoarseness", "Difficulty swallowing", "Chronic cough", "Blood in sputum", |
|
"Rapid breathing", "Shallow breathing", "Chest congestion", "Phlegm production", "Nasal discharge", |
|
"Postnasal drip", "Facial pain", "Facial swelling", "Tenderness in the face", "Toothache", "Jaw stiffness", |
|
"Difficulty opening the mouth", "Difficulty chewing", "Difficulty speaking", "Difficulty breathing through the nose", |
|
"Loss of taste", "Loss of smell", "Metallic taste in the mouth", "Dry eyes", "Watery eyes", "Itchy eyes", |
|
"Itchy skin", "Hives", "Swollen joints", "Stiff joints", "Redness in joints", "Warmth in joints", |
|
"Difficulty moving joints", "Cracking or popping in joints", "Weakness in muscles", "Muscle cramps", |
|
"Muscle spasms", "Tremors", "Seizures", "Fainting", "Lightheadedness", "Pale skin", "Yellowing of the skin", |
|
"Bruising easily", "Slow heart rate", "High heart rate", "Irregular heartbeat", "Low blood pressure", |
|
"High blood sugar", "Low blood sugar", "Excessive hunger", "Excessive thirst", "Frequent infections in the urinary tract", |
|
"Frequent infections in the respiratory tract", "Frequent infections in the skin", "Frequent infections in the ears", |
|
"Frequent infections in the eyes", "Frequent infections in the mouth", "Frequent infections in the throat", |
|
"Frequent infections in the sinuses", "Frequent infections in the lungs", "Frequent infections in the digestive system", |
|
"Frequent infections in the reproductive system", "Frequent infections in the urinary system", |
|
"Frequent infections in the nervous system", "Frequent infections in the circulatory system", |
|
"Frequent infections in the immune system"] |
|
|
|
|
|
|
|
st.header("π Enter Patient Symptoms") |
|
|
|
|
|
input_method = st.radio( |
|
"Choose how you want to enter symptoms:", |
|
options=["Select from predefined list", "Type your own text"], |
|
index=0 |
|
) |
|
|
|
|
|
raw_text = "" |
|
|
|
|
|
if input_method == "Select from predefined list": |
|
selected_symptoms = st.multiselect("Choose the symptoms:", symptoms) |
|
raw_text = ", ".join(selected_symptoms) |
|
|
|
|
|
elif input_method == "Type your own text": |
|
raw_text = st.text_area("Describe the patient's symptoms or condition here:", height=100) |
|
|
|
|
|
st.markdown(f"**Input Text:** {raw_text}") |
|
|
|
|
|
def clean_text(raw_review): |
|
|
|
review_text = BeautifulSoup(raw_review, 'html.parser').get_text() |
|
|
|
|
|
letters_only = re.sub('[^a-zA-Z]', ' ', review_text) |
|
|
|
|
|
words = letters_only.lower().split() |
|
|
|
|
|
meaningful_words = [w for w in words if w not in stop] |
|
|
|
|
|
lemmatized_words = [lemmatizer.lemmatize(w) for w in meaningful_words] |
|
|
|
|
|
return ' '.join(lemmatized_words) |
|
|
|
|
|
def top_drugs_extractor(condition, df): |
|
|
|
df_top = df[(df['rating'] >= 9) & (df['usefulCount'] >= 90)].sort_values( |
|
by=['rating', 'usefulCount'], ascending=[False, False] |
|
) |
|
|
|
|
|
drug_lst = df_top[df_top['condition'] == condition]['drugName'].head(4).tolist() |
|
drug_lst = list(set(drug_lst)) |
|
|
|
return drug_lst |
|
|
|
|
|
def predict(raw_text): |
|
global predicted_cond, top_drugs |
|
|
|
if raw_text != "": |
|
|
|
clean_text_result = clean_text(raw_text) |
|
clean_lst = [clean_text_result] |
|
|
|
|
|
tfidf_vect = vectorizer.transform(clean_lst) |
|
|
|
|
|
prediction = model.predict(tfidf_vect) |
|
predicted_cond = prediction[0] |
|
|
|
|
|
df = pd.read_csv(DATA_PATH) |
|
top_drugs = top_drugs_extractor(predicted_cond, df) |
|
|
|
|
|
predict_button = st.button("π Predict") |
|
|
|
|
|
if predict_button: |
|
|
|
if not raw_text.strip(): |
|
st.warning("β οΈ Please enter symptoms or select from the predefined list before predicting.") |
|
else: |
|
with st.spinner("π§ Analyzing the condition and generating recommendations..."): |
|
predict(raw_text) |
|
|
|
st.markdown("---") |
|
st.markdown("### π― Condition Predicted") |
|
st.markdown(f"<div class='condition-card'><h3>{predicted_cond}</h3></div>", unsafe_allow_html=True) |
|
|
|
st.markdown("---") |
|
st.markdown("### π Top Recommended Drugs") |
|
|
|
|
|
if len(top_drugs) < 4: |
|
st.warning(f"β οΈ Only {len(top_drugs)} recommended drugs are available for this condition.") |
|
|
|
|
|
for i, drug in enumerate(top_drugs): |
|
if i == 0: |
|
st.markdown(f"<div class='drug-card-1'><h4>{i + 1}. {drug}</h4></div>", unsafe_allow_html=True) |
|
elif i == 1: |
|
st.markdown(f"<div class='drug-card-2'><h4>{i + 1}. {drug}</h4></div>", unsafe_allow_html=True) |
|
elif i == 2: |
|
st.markdown(f"<div class='drug-card-3'><h4>{i + 1}. {drug}</h4></div>", unsafe_allow_html=True) |
|
elif i == 3: |
|
st.markdown(f"<div class='drug-card-4'><h4>{i + 1}. {drug}</h4></div>", unsafe_allow_html=True) |
|
|
|
|
|
st.markdown("---") |
|
st.markdown("### π Drug Recommendations Visualization") |
|
df_drugs = pd.DataFrame({"Drug": top_drugs, "Rank": range(len(top_drugs), 0, -1)}) |
|
color_map = {} |
|
if len(top_drugs) >= 1: |
|
color_map[top_drugs[0]] = "#b4befe" |
|
if len(top_drugs) >= 2: |
|
color_map[top_drugs[1]] = "#a6e3a1" |
|
if len(top_drugs) >= 3: |
|
color_map[top_drugs[2]] = "#f38ba8" |
|
if len(top_drugs) >= 4: |
|
color_map[top_drugs[3]] = "#f2cdcd" |
|
|
|
fig = px.bar(df_drugs, x="Rank", y="Drug", title="Top Recommended Drugs", |
|
labels={"Drug": "Drug Name", "Rank": "Rank"}, |
|
orientation='h', |
|
color="Drug", |
|
color_discrete_map=color_map) |
|
|
|
fig.update_layout(yaxis={'categoryorder':'total ascending'}) |
|
st.plotly_chart(fig, use_container_width=True) |
|
|
|
|
|
st.markdown("---") |
|
st.markdown("### β οΈπ§ **Disclaimer** π§β οΈ") |
|
st.warning(""" |
|
**This is just a prototype and not a substitute for professional medical advice.** |
|
The predictions and recommendations provided by this app are based on a machine learning model and may not always be accurate. |
|
Always consult a qualified healthcare provider for medical diagnosis and treatment. |
|
""") |
|
st.markdown("---") |
|
|
|
|
|
with st.expander("βΉοΈ About This App"): |
|
st.write(""" |
|
This app uses a machine learning model to predict diseases based on symptoms and recommends the top drugs for the predicted condition. |
|
- **Symptoms**: Select from a predefined list of symptoms. |
|
- **Prediction**: The model analyzes the symptoms and predicts the most likely condition. |
|
- **Drug Recommendations**: Based on the predicted condition, the app recommends the top drugs with high ratings and usefulness. |
|
""") |