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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -129
app.py CHANGED
@@ -1,134 +1,116 @@
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
- """
 
1
  import streamlit as st
2
+ import joblib
3
+ import re
4
+ import string
 
5
 
6
+ # Load the trained model and TF-IDF vectorizer
7
+ knn_model = joblib.load('knn_model.joblib')
8
+ tfidf_vectorizer = joblib.load('tfidf_vectorizer.joblib')
9
 
10
+ # Preprocess the input text (same preprocessing as in the notebook)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def preprocess_text(text):
12
+ text = text.lower() # Convert to lowercase
13
+ text = re.sub(r'\d+', '', text) # Remove digits
14
+ text = text.translate(str.maketrans('', '', string.punctuation)) # Remove punctuation
15
+ return text
16
+
17
+ # Prediction function
18
+ def predict_disease(symptom):
19
+ preprocessed_symptom = preprocess_text(symptom)
20
+ tfidf_features = tfidf_vectorizer.transform([preprocessed_symptom]).toarray()
21
+ predicted_disease = knn_model.predict(tfidf_features)
22
+ return predicted_disease[0]
23
+
24
+ # Streamlit UI Design
25
+ st.set_page_config(page_title="Disease Prediction App", page_icon="🦠", layout="centered")
26
+
27
+ # Custom Styling
28
+ st.markdown("""
29
+ <style>
30
+ body {
31
+ background-color: #2E2E2E;
32
+ color: white;
33
+ font-family: 'Segoe UI', sans-serif;
34
+ }
35
+ .header {
36
+ font-size: 36px;
37
+ font-weight: bold;
38
+ color: #00BFFF;
39
+ text-align: center;
40
+ margin-top: 30px;
41
+ margin-bottom: 15px;
42
+ }
43
+ .description {
44
+ font-size: 16px;
45
+ color: #dcdcdc;
46
+ text-align: center;
47
+ margin-bottom: 20px;
48
+ }
49
+ .input-box {
50
+ background-color: #3E3E3E;
51
+ border-radius: 8px;
52
+ padding: 15px;
53
+ font-size: 16px;
54
+ font-family: 'Segoe UI', sans-serif;
55
+ border: none;
56
+ color: white;
57
+ }
58
+ .output {
59
+ background-color: #5F5F5F;
60
+ border-radius: 8px;
61
+ padding: 15px;
62
+ font-size: 18px;
63
+ color: #00BFFF;
64
+ font-weight: bold;
65
+ text-align: center;
66
+ }
67
+ .btn {
68
+ background-color: #00BFFF;
69
+ color: white;
70
+ font-size: 18px;
71
+ padding: 12px 24px;
72
+ border-radius: 8px;
73
+ border: none;
74
+ cursor: pointer;
75
+ width: 100%;
76
+ }
77
+ .btn:hover {
78
+ background-color: #008B8B;
79
+ }
80
+ footer {
81
+ margin-top: 40px;
82
+ text-align: center;
83
+ font-size: 14px;
84
+ color: #dcdcdc;
85
+ }
86
+ .container {
87
+ padding: 20px;
88
+ border-radius: 12px;
89
+ background-color: #383838;
90
+ max-width: 500px;
91
+ margin: auto;
92
+ }
93
+ </style>
94
+ """, unsafe_allow_html=True)
95
+
96
+ # Title and Description
97
+ st.markdown("<div class='header'>🦠 Disease Prediction</div>", unsafe_allow_html=True)
98
+ st.markdown("<div class='description'>Enter your symptoms, and the model will predict the possible disease based on the provided input.</div>", unsafe_allow_html=True)
99
+
100
+ # Input Box and Prediction Button in a centered container
101
+ with st.container():
102
+ symptom = st.text_area("Enter symptoms:", height=150, max_chars=500, placeholder="E.g., fever, cough, headache...", key="symptom", label_visibility="collapsed")
103
 
104
+ if st.button("Predict", key="predict_button"):
105
+ if symptom:
106
+ predicted_disease = predict_disease(symptom)
107
+ st.markdown(f"<div class='output'>**Predicted Disease: {predicted_disease}**</div>", unsafe_allow_html=True)
108
+ else:
109
+ st.warning("Please enter some symptoms to predict the disease.", icon="⚠️")
110
+
111
+ # Footer with minimalistic text
112
+ st.markdown("""
113
+ <footer>
114
+ <p>Powered by AI | Developed for Final Year Project </p>
115
+ </footer>
116
+ """, unsafe_allow_html=True)