Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import re
|
3 |
+
import pandas as pd
|
4 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
5 |
+
from sklearn.naive_bayes import MultinomialNB
|
6 |
+
from sklearn.model_selection import train_test_split
|
7 |
+
|
8 |
+
# Load your symptom-disease data
|
9 |
+
data = pd.read_csv("Symptom2Disease.csv")
|
10 |
+
|
11 |
+
# Initialize the TF-IDF vectorizer
|
12 |
+
tfidf_vectorizer = TfidfVectorizer()
|
13 |
+
|
14 |
+
# Apply TF-IDF vectorization to the preprocessed text data
|
15 |
+
X = tfidf_vectorizer.fit_transform(data['text'])
|
16 |
+
|
17 |
+
# Split the dataset into a training set and a testing set
|
18 |
+
X_train, X_test, y_train, y_test = train_test_split(X, data['label'], test_size=0.2, random_state=42)
|
19 |
+
|
20 |
+
# Initialize the Multinomial Naive Bayes model
|
21 |
+
model = MultinomialNB()
|
22 |
+
|
23 |
+
# Train the model on the training data
|
24 |
+
model.fit(X_train, y_train)
|
25 |
+
|
26 |
+
# Set Streamlit app title with emojis
|
27 |
+
st.title("Health Symptom-to-Disease Predictor 🏥👨⚕️")
|
28 |
+
|
29 |
+
# Define a sidebar
|
30 |
+
st.sidebar.title("Tool Definition")
|
31 |
+
st.sidebar.markdown("__Tool Definition__")
|
32 |
+
st.sidebar.markdown("This tool helps you identify possible diseases based on the symptoms you provide.")
|
33 |
+
st.sidebar.markdown("This is my project for the KaggleX program.")
|
34 |
+
st.sidebar.markdown("Please note that this tool is for informational purposes only. Always consult a healthcare professional for accurate medical advice.")
|
35 |
+
|
36 |
+
# Initialize chat history
|
37 |
+
if "messages" not in st.session_state:
|
38 |
+
st.session_state.messages = []
|
39 |
+
|
40 |
+
# Function to preprocess user input
|
41 |
+
def preprocess_input(user_input):
|
42 |
+
user_input = user_input.lower() # Convert to lowercase
|
43 |
+
user_input = re.sub(r"[^a-zA-Z\s]", "", user_input) # Remove special characters and numbers
|
44 |
+
user_input = " ".join(user_input.split()) # Remove extra spaces
|
45 |
+
return user_input
|
46 |
+
|
47 |
+
# Function to predict diseases based on user input
|
48 |
+
def predict_diseases(user_clean_text):
|
49 |
+
user_input_vector = tfidf_vectorizer.transform([user_cleaned_text]) # Vectorize the cleaned user input
|
50 |
+
predictions = model.predict(user_input_vector) # Make predictions using the trained model
|
51 |
+
return predictions
|
52 |
+
|
53 |
+
# Add user input section
|
54 |
+
user_input = st.text_area("Enter your symptoms (how you feel):", key="user_input")
|
55 |
+
|
56 |
+
# Add button to predict disease
|
57 |
+
if st.button("Predict Disease"):
|
58 |
+
# Display loading message
|
59 |
+
with st.spinner("Diagnosing patient..."):
|
60 |
+
# Check if user input is not empty
|
61 |
+
if user_input:
|
62 |
+
cleaned_input = preprocess_input(user_input)
|
63 |
+
predicted_diseases = predict_diseases(cleaned_input)
|
64 |
+
|
65 |
+
# Display predicted diseases
|
66 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
67 |
+
st.session_state.messages.append({"role": "assistant", "content": f"Based on your symptoms, you might have {', '.join(predicted_diseases)}."})
|
68 |
+
|
69 |
+
st.write("Based on your symptoms, you might have:")
|
70 |
+
for disease in predicted_diseases:
|
71 |
+
st.write(f"- {disease}")
|
72 |
+
else:
|
73 |
+
st.warning("Please enter your symptoms before predicting.")
|
74 |
+
|
75 |
+
# Display a warning message
|
76 |
+
st.warning("Please note that this tool is for informational purposes only. Always consult a healthcare professional for accurate medical advice.")
|
77 |
+
|
78 |
+
# Create FAQs section
|
79 |
+
show_faqs = st.checkbox("Show FAQs")
|
80 |
+
if show_faqs:
|
81 |
+
st.markdown("## Frequently Asked Questions")
|
82 |
+
st.markdown("**Q: How does this tool work?**")
|
83 |
+
st.markdown("A: The tool uses a machine learning model to analyze the symptoms you enter and predicts possible diseases based on a pre-trained dataset.")
|
84 |
+
|
85 |
+
st.markdown("**Q: Is this a substitute for a doctor's advice?**")
|
86 |
+
st.markdown("A: No, this tool is for informational purposes only. It's essential to consult a healthcare professional for accurate medical advice.")
|
87 |
+
|
88 |
+
st.markdown("**Q: Can I trust the predictions?**")
|
89 |
+
st.markdown("A: While the tool provides predictions, it's not a guarantee of accuracy. It's always best to consult a healthcare expert for a reliable diagnosis.")
|
90 |
+
|
91 |
+
# Add attribution
|
92 |
+
st.markdown("Created with ❤️ by Joas")
|