rajsecrets0's picture
Update app.py
e1cc8c0 verified
raw
history blame
3.92 kB
import streamlit as st
import pickle
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
# Download NLTK resources
nltk.download('punkt', quiet=True)
nltk.download('stopwords', quiet=True)
# Load the pre-trained model and vectorizer
@st.cache_resource
def load_model():
try:
# Load TF-IDF Vectorizer
with open('tfidf_vectorizer.pkl', 'rb') as vectorizer_file:
tfidf_vectorizer = pickle.load(vectorizer_file)
# Load KNN Classifier
with open('knn_model.pkl', 'rb') as model_file:
knn_classifier = pickle.load(model_file)
return tfidf_vectorizer, knn_classifier
except Exception as e:
st.error(f"Error loading model: {e}")
return None, None
# Preprocess text function
def preprocess_text(text):
# Tokenization and preprocessing
stop_words = set(stopwords.words('english'))
words = word_tokenize(text.lower())
words = [word for word in words if word.isalpha() and word not in stop_words]
return ' '.join(words)
# Main Streamlit App
def main():
# Set page title and favicon
st.set_page_config(
page_title="Disease Classification by Symptoms",
page_icon=":medical_symbol:",
layout="centered"
)
# Title and description
st.title("🩺 Disease Classification Predictor")
st.markdown("""
### Predict Potential Diseases Based on Symptoms
Enter your symptoms below, and our AI model will help predict possible diseases.
""")
# Load model and vectorizer
tfidf_vectorizer, knn_classifier = load_model()
# Input form for symptoms
with st.form(key='symptom_form'):
symptoms = st.text_area(
"Enter your symptoms:",
placeholder="Example: low appetite, fever, headache",
help="Provide a detailed description of your symptoms"
)
submit_button = st.form_submit_button(label="Predict Disease")
# Prediction logic
if submit_button:
if not symptoms:
st.warning("Please enter some symptoms.")
return
try:
# Preprocess input symptoms
preprocessed_symptoms = preprocess_text(symptoms)
# Transform symptoms using TF-IDF vectorizer
symptoms_tfidf = tfidf_vectorizer.transform([preprocessed_symptoms])
# Predict disease
predicted_disease = knn_classifier.predict(symptoms_tfidf)
# Display prediction
st.success(f"Predicted Disease: {predicted_disease[0]}")
# Additional information (optional)
st.info("""
### Disclaimer
- This is an AI-based prediction and should not replace professional medical advice
- Always consult with a healthcare professional for accurate diagnosis
- The prediction is based on machine learning analysis of symptom patterns
""")
except Exception as e:
st.error(f"An error occurred during prediction: {e}")
# Sidebar with additional information
st.sidebar.title("About the Model")
st.sidebar.markdown("""
### Disease Classification Model
- **Algorithm**: K-Nearest Neighbors (KNN)
- **Features**: TF-IDF Vectorization
- **Trained on**: Symptom to Disease Dataset
#### How it works:
1. Transform symptoms into numerical features
2. Compare with known disease patterns
3. Predict most likely disease
""")
# Run the app
if __name__ == "__main__":
main()
# Additional requirements.txt content
"""
streamlit
scikit-learn
nltk
pickle5
"""
# Deployment Notes
"""
Deployment Steps:
1. Install requirements:
pip install -r requirements.txt
2. Download NLTK resources:
python -m nltk.downloader punkt
python -m nltk.downloader stopwords
3. Run the Streamlit app:
streamlit run app.py
"""