rajsecrets0 commited on
Commit
e1cc8c0
·
verified ·
1 Parent(s): f8233d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -49
app.py CHANGED
@@ -1,61 +1,134 @@
1
  import streamlit as st
2
- import joblib
3
  import nltk
4
  from nltk.corpus import stopwords
5
  from nltk.tokenize import word_tokenize
6
 
7
- # Ensure NLTK resources are downloaded
8
- nltk.download('punkt')
9
- nltk.download('stopwords')
10
 
11
- # Load the trained KNN model
12
- knn_classifier = joblib.load('knn_model.pkl')
13
-
14
- # Load the TF-IDF vectorizer
15
- tfidf_vectorizer = joblib.load('tfidf_vectorizer.pkl')
16
-
17
- # Text Preprocessing Function
18
- stop_words = set(stopwords.words('english'))
 
 
 
 
 
 
 
 
19
 
 
20
  def preprocess_text(text):
 
 
21
  words = word_tokenize(text.lower())
22
  words = [word for word in words if word.isalpha() and word not in stop_words]
23
  return ' '.join(words)
24
 
25
- # Inference function
26
- def predict_disease(symptom):
27
- preprocessed_symptom = preprocess_text(symptom)
28
- symptom_tfidf = tfidf_vectorizer.transform([preprocessed_symptom])
29
- predicted_disease = knn_classifier.predict(symptom_tfidf)
30
- return predicted_disease[0]
31
-
32
- # Streamlit UI
33
- st.title("Disease Classification using Symptoms")
34
- st.markdown("Enter your symptoms to predict the disease.")
35
-
36
- # Input box for symptoms
37
- symptom = st.text_input("Enter symptoms:", "")
38
-
39
- # Predict button
40
- if st.button("Predict Disease"):
41
- if symptom:
42
- predicted_disease = predict_disease(symptom)
43
- st.success(f"Predicted Disease: {predicted_disease}")
44
- else:
45
- st.warning("Please enter symptoms.")
46
-
47
- # Add some styling
48
- st.markdown(
49
- """
50
- <style>
51
- .main .block-container {
52
- max-width: 600px;
53
- padding-top: 2rem;
54
- padding-right: 2rem;
55
- padding-left: 2rem;
56
- padding-bottom: 2rem;
57
- }
58
- </style>
59
- """,
60
- unsafe_allow_html=True
61
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import pickle
3
  import nltk
4
  from nltk.corpus import stopwords
5
  from nltk.tokenize import word_tokenize
6
 
7
+ # Download NLTK resources
8
+ nltk.download('punkt', quiet=True)
9
+ nltk.download('stopwords', quiet=True)
10
 
11
+ # Load the pre-trained model and vectorizer
12
+ @st.cache_resource
13
+ def load_model():
14
+ try:
15
+ # Load TF-IDF Vectorizer
16
+ with open('tfidf_vectorizer.pkl', 'rb') as vectorizer_file:
17
+ tfidf_vectorizer = pickle.load(vectorizer_file)
18
+
19
+ # Load KNN Classifier
20
+ with open('knn_model.pkl', 'rb') as model_file:
21
+ knn_classifier = pickle.load(model_file)
22
+
23
+ return tfidf_vectorizer, knn_classifier
24
+ except Exception as e:
25
+ st.error(f"Error loading model: {e}")
26
+ return None, None
27
 
28
+ # Preprocess text function
29
  def preprocess_text(text):
30
+ # Tokenization and preprocessing
31
+ stop_words = set(stopwords.words('english'))
32
  words = word_tokenize(text.lower())
33
  words = [word for word in words if word.isalpha() and word not in stop_words]
34
  return ' '.join(words)
35
 
36
+ # Main Streamlit App
37
+ def main():
38
+ # Set page title and favicon
39
+ st.set_page_config(
40
+ page_title="Disease Classification by Symptoms",
41
+ page_icon=":medical_symbol:",
42
+ layout="centered"
43
+ )
44
+
45
+ # Title and description
46
+ st.title("🩺 Disease Classification Predictor")
47
+ st.markdown("""
48
+ ### Predict Potential Diseases Based on Symptoms
49
+
50
+ Enter your symptoms below, and our AI model will help predict possible diseases.
51
+ """)
52
+
53
+ # Load model and vectorizer
54
+ tfidf_vectorizer, knn_classifier = load_model()
55
+
56
+ # Input form for symptoms
57
+ with st.form(key='symptom_form'):
58
+ symptoms = st.text_area(
59
+ "Enter your symptoms:",
60
+ placeholder="Example: low appetite, fever, headache",
61
+ help="Provide a detailed description of your symptoms"
62
+ )
63
+
64
+ submit_button = st.form_submit_button(label="Predict Disease")
65
+
66
+ # Prediction logic
67
+ if submit_button:
68
+ if not symptoms:
69
+ st.warning("Please enter some symptoms.")
70
+ return
71
+
72
+ try:
73
+ # Preprocess input symptoms
74
+ preprocessed_symptoms = preprocess_text(symptoms)
75
+
76
+ # Transform symptoms using TF-IDF vectorizer
77
+ symptoms_tfidf = tfidf_vectorizer.transform([preprocessed_symptoms])
78
+
79
+ # Predict disease
80
+ predicted_disease = knn_classifier.predict(symptoms_tfidf)
81
+
82
+ # Display prediction
83
+ st.success(f"Predicted Disease: {predicted_disease[0]}")
84
+
85
+ # Additional information (optional)
86
+ st.info("""
87
+ ### Disclaimer
88
+ - This is an AI-based prediction and should not replace professional medical advice
89
+ - Always consult with a healthcare professional for accurate diagnosis
90
+ - The prediction is based on machine learning analysis of symptom patterns
91
+ """)
92
+
93
+ except Exception as e:
94
+ st.error(f"An error occurred during prediction: {e}")
95
+
96
+ # Sidebar with additional information
97
+ st.sidebar.title("About the Model")
98
+ st.sidebar.markdown("""
99
+ ### Disease Classification Model
100
+ - **Algorithm**: K-Nearest Neighbors (KNN)
101
+ - **Features**: TF-IDF Vectorization
102
+ - **Trained on**: Symptom to Disease Dataset
103
+
104
+ #### How it works:
105
+ 1. Transform symptoms into numerical features
106
+ 2. Compare with known disease patterns
107
+ 3. Predict most likely disease
108
+ """)
109
+
110
+ # Run the app
111
+ if __name__ == "__main__":
112
+ main()
113
+
114
+ # Additional requirements.txt content
115
+ """
116
+ streamlit
117
+ scikit-learn
118
+ nltk
119
+ pickle5
120
+ """
121
+
122
+ # Deployment Notes
123
+ """
124
+ Deployment Steps:
125
+ 1. Install requirements:
126
+ pip install -r requirements.txt
127
+
128
+ 2. Download NLTK resources:
129
+ python -m nltk.downloader punkt
130
+ python -m nltk.downloader stopwords
131
+
132
+ 3. Run the Streamlit app:
133
+ streamlit run app.py
134
+ """