Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,134 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
import nltk
|
4 |
from nltk.corpus import stopwords
|
5 |
from nltk.tokenize import word_tokenize
|
6 |
|
7 |
-
#
|
8 |
-
nltk.download('punkt')
|
9 |
-
nltk.download('stopwords')
|
10 |
|
11 |
-
# Load the trained
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
#
|
26 |
-
def
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
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 |
+
"""
|