Spaces:
Sleeping
Sleeping
import streamlit as st | |
import joblib | |
import nltk | |
from nltk.corpus import stopwords | |
from nltk.tokenize import word_tokenize | |
# Load the trained KNN model | |
knn_classifier = joblib.load('knn_model.pkl') | |
# Load the TF-IDF vectorizer | |
tfidf_vectorizer = joblib.load('tfidf_vectorizer.pkl') | |
# Download nltk resources | |
nltk.download('punkt') | |
nltk.download('stopwords') | |
# Text Preprocessing Function | |
stop_words = set(stopwords.words('english')) | |
def preprocess_text(text): | |
words = word_tokenize(text.lower()) | |
words = [word for word in words if word.isalpha() and word not in stop_words] | |
return ' '.join(words) | |
# Inference function | |
def predict_disease(symptom): | |
preprocessed_symptom = preprocess_text(symptom) | |
symptom_tfidf = tfidf_vectorizer.transform([preprocessed_symptom]) | |
predicted_disease = knn_classifier.predict(symptom_tfidf) | |
return predicted_disease[0] | |
# Streamlit UI | |
st.title("Disease Classification using Symptoms") | |
st.markdown("Enter your symptoms to predict the disease.") | |
# Input box for symptoms | |
symptom = st.text_input("Enter symptoms:", "") | |
# Predict button | |
if st.button("Predict Disease"): | |
if symptom: | |
predicted_disease = predict_disease(symptom) | |
st.success(f"Predicted Disease: {predicted_disease}") | |
else: | |
st.warning("Please enter symptoms.") | |
# Add some styling | |
st.markdown( | |
""" | |
<style> | |
.main .block-container { | |
max-width: 600px; | |
padding-top: 2rem; | |
padding-right: 2rem; | |
padding-left: 2rem; | |
padding-bottom: 2rem; | |
} | |
</style> | |
""", | |
unsafe_allow_html=True | |
) | |