Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,134 +1,116 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
from nltk.tokenize import word_tokenize
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
|
11 |
-
#
|
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 |
-
#
|
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 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|