Spaces:
Sleeping
Sleeping
import streamlit as st | |
import joblib | |
import re | |
import string | |
# Load the trained model and TF-IDF vectorizer | |
knn_model = joblib.load('knn_model.joblib') | |
tfidf_vectorizer = joblib.load('tfidf_vectorizer.joblib') | |
# Preprocess the input text (same preprocessing as in the notebook) | |
def preprocess_text(text): | |
text = text.lower() # Convert to lowercase | |
text = re.sub(r'\d+', '', text) # Remove digits | |
text = text.translate(str.maketrans('', '', string.punctuation)) # Remove punctuation | |
return text | |
# Prediction function | |
def predict_disease(symptom): | |
preprocessed_symptom = preprocess_text(symptom) | |
tfidf_features = tfidf_vectorizer.transform([preprocessed_symptom]).toarray() | |
predicted_disease = knn_model.predict(tfidf_features) | |
return predicted_disease[0] | |
# Streamlit UI Design | |
st.set_page_config(page_title="Disease Prediction App", page_icon="🦠", layout="centered") | |
# Custom Styling | |
st.markdown(""" | |
<style> | |
body { | |
background-color: #2E2E2E; | |
color: white; | |
font-family: 'Segoe UI', sans-serif; | |
} | |
.header { | |
font-size: 36px; | |
font-weight: bold; | |
color: #00BFFF; | |
text-align: center; | |
margin-top: 30px; | |
margin-bottom: 15px; | |
} | |
.description { | |
font-size: 16px; | |
color: #dcdcdc; | |
text-align: center; | |
margin-bottom: 20px; | |
} | |
.input-box { | |
background-color: #3E3E3E; | |
border-radius: 8px; | |
padding: 15px; | |
font-size: 16px; | |
font-family: 'Segoe UI', sans-serif; | |
border: none; | |
color: white; | |
} | |
.output { | |
background-color: #5F5F5F; | |
border-radius: 8px; | |
padding: 15px; | |
font-size: 18px; | |
color: #00BFFF; | |
font-weight: bold; | |
text-align: center; | |
} | |
.btn { | |
background-color: #00BFFF; | |
color: white; | |
font-size: 18px; | |
padding: 12px 24px; | |
border-radius: 8px; | |
border: none; | |
cursor: pointer; | |
width: 100%; | |
} | |
.btn:hover { | |
background-color: #008B8B; | |
} | |
footer { | |
margin-top: 40px; | |
text-align: center; | |
font-size: 14px; | |
color: #dcdcdc; | |
} | |
.container { | |
padding: 20px; | |
border-radius: 12px; | |
background-color: #383838; | |
max-width: 500px; | |
margin: auto; | |
} | |
</style> | |
""", unsafe_allow_html=True) | |
# Title and Description | |
st.markdown("<div class='header'>🦠 Disease Prediction</div>", unsafe_allow_html=True) | |
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) | |
# Input Box and Prediction Button in a centered container | |
with st.container(): | |
symptom = st.text_area("Enter symptoms:", height=150, max_chars=500, placeholder="E.g., fever, cough, headache...", key="symptom", label_visibility="collapsed") | |
if st.button("Predict", key="predict_button"): | |
if symptom: | |
predicted_disease = predict_disease(symptom) | |
st.markdown(f"<div class='output'>**Predicted Disease: {predicted_disease}**</div>", unsafe_allow_html=True) | |
else: | |
st.warning("Please enter some symptoms to predict the disease.", icon="⚠️") | |
# Footer with minimalistic text | |
st.markdown(""" | |
<footer> | |
<p>Powered by AI | Developed for Final Year Project </p> | |
</footer> | |
""", unsafe_allow_html=True) | |