# Import necessary libraries 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 # Download NLTK resources nltk.download('wordnet') nltk.download('stopwords') # Define file paths MODEL_PATH = 'model/passmodel.pkl' TOKENIZER_PATH = 'model/tfidfvectorizer.pkl' DATA_PATH = 'data/custom_dataset.csv' # Load vectorizer and model vectorizer = joblib.load(TOKENIZER_PATH) model = joblib.load(MODEL_PATH) # Initialize NLTK tools stop = stopwords.words('english') lemmatizer = WordNetLemmatizer() # Configure Streamlit page st.set_page_config( page_title='DPDR', page_icon='๐จโโ๏ธ', layout='wide' ) # Custom CSS for styling st.markdown(""" """, unsafe_allow_html=True) # Title and header st.title("๐ An Intelligent System for Disease Prediction and Drug Recommendation ๐") st.markdown("---") # Sidebar for additional options 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') # Predefined list of symptoms 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"] # Input section with multiselect for symptoms st.header("๐ Enter Patient Symptoms") # Radio button to choose input method input_method = st.radio( "Choose how you want to enter symptoms:", options=["Select from predefined list", "Type your own text"], index=0 # Default to the first option ) # Initialize raw_text raw_text = "" # Option 1: Select from predefined list if input_method == "Select from predefined list": selected_symptoms = st.multiselect("Choose the symptoms:", symptoms) raw_text = ", ".join(selected_symptoms) # Option 2: Type your own text elif input_method == "Type your own text": raw_text = st.text_area("Describe the patient's symptoms or condition here:", height=100) # Display the final input text (for debugging or confirmation) st.markdown(f"**Input Text:** {raw_text}") # Function to clean and preprocess text def clean_text(raw_review): # 1. Remove HTML tags review_text = BeautifulSoup(raw_review, 'html.parser').get_text() # 2. Keep only letters and replace non-alphabetic characters with spaces letters_only = re.sub('[^a-zA-Z]', ' ', review_text) # 3. Convert to lowercase and split into words words = letters_only.lower().split() # 4. Remove stopwords meaningful_words = [w for w in words if w not in stop] # 5. Lemmatize words lemmatized_words = [lemmatizer.lemmatize(w) for w in meaningful_words] # 6. Join words back into a single string return ' '.join(lemmatized_words) # Function to extract top recommended drugs def top_drugs_extractor(condition, df): # Filter and sort drugs based on rating and usefulness df_top = df[(df['rating'] >= 9) & (df['usefulCount'] >= 90)].sort_values( by=['rating', 'usefulCount'], ascending=[False, False] ) # Extract top 4 unique drugs for the condition drug_lst = df_top[df_top['condition'] == condition]['drugName'].head(4).tolist() drug_lst = list(set(drug_lst)) return drug_lst # Function to predict condition and recommend drugs def predict(raw_text): global predicted_cond, top_drugs if raw_text != "": # Clean and preprocess input text clean_text_result = clean_text(raw_text) clean_lst = [clean_text_result] # Transform text using the vectorizer tfidf_vect = vectorizer.transform(clean_lst) # Predict condition prediction = model.predict(tfidf_vect) predicted_cond = prediction[0] # Load data and extract top drugs df = pd.read_csv(DATA_PATH) top_drugs = top_drugs_extractor(predicted_cond, df) # Predict button predict_button = st.button("๐ Predict") # Display results when the button is clicked if predict_button: # Check if input is empty 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"