Dorglorich commited on
Commit
a1a0c86
1 Parent(s): 991e38b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("This tool helps you identify possible diseases based on the symptoms you provide.")
32
+ st.sidebar.markdown("the tool may aid healthcare professionals in the initial assessment of potential conditions, facilitating quicker decision-making and improving patient care")
33
+
34
+ st.sidebar.title("⚠️ **Limitation**")
35
+ st.sidebar.markdown("This tool's predictions are based solely on symptom descriptions and may not account for other critical factors,")
36
+ st.sidebar.markdown("such as a patient's medical history or laboratory tests,")
37
+ st.sidebar.markdown("As such,it should be used as an initial reference and not as a sole diagnostic tool. 👩‍⚕️")
38
+
39
+ st.warning("Please note that this tool is for informational purposes only. Always consult a healthcare professional for accurate medical advice.")
40
+ show_faqs = st.sidebar.checkbox("Show FAQs")
41
+
42
+ # Initialize chat history
43
+ if "messages" not in st.session_state:
44
+ st.session_state.messages = []
45
+
46
+ # Function to preprocess user input
47
+ def preprocess_input(user_input):
48
+ user_input = user_input.lower() # Convert to lowercase
49
+ user_input = re.sub(r"[^a-zA-Z\s]", "", user_input) # Remove special characters and numbers
50
+ user_input = " ".join(user_input.split()) # Remove extra spaces
51
+ return user_input
52
+
53
+ # Function to predict diseases based on user input
54
+ def predict_diseases(user_clean_text):
55
+ user_input_vector = tfidf_vectorizer.transform([user_clean_text]) # Vectorize the cleaned user input
56
+ predictions = model.predict(user_input_vector) # Make predictions using the trained model
57
+ return predictions
58
+
59
+ # Add user input section
60
+ user_input = st.text_area("Enter your symptoms (how you feel):", key="user_input")
61
+
62
+ # Add button to predict disease
63
+ if st.button("Predict Disease"):
64
+ # Display loading message
65
+ with st.spinner("Diagnosing patient..."):
66
+ # Check if user input is not empty
67
+ if user_input:
68
+ cleaned_input = preprocess_input(user_input)
69
+ predicted_diseases = predict_diseases(cleaned_input)
70
+
71
+ # Display predicted diseases
72
+ st.session_state.messages.append({"role": "user", "content": user_input})
73
+ st.session_state.messages.append({"role": "assistant", "content": f"Based on your symptoms, you might have {', '.join(predicted_diseases)}."})
74
+
75
+ st.write("Based on your symptoms, you might have:")
76
+ for disease in predicted_diseases:
77
+ st.write(f"- {disease}")
78
+ else:
79
+ st.warning("Please enter your symptoms before predicting.")
80
+
81
+ # Create FAQs section
82
+ if show_faqs:
83
+ st.markdown("## Frequently Asked Questions")
84
+ st.markdown("**Q: How does this tool work?**")
85
+ 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.")
86
+
87
+ st.markdown("**Q: Is this a substitute for a doctor's advice?**")
88
+ st.markdown("A: No, this tool is for informational purposes only. It's essential to consult a healthcare professional for accurate medical advice.")
89
+
90
+ st.markdown("**Q: Can I trust the predictions?**")
91
+ 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.")
92
+
93
+ # Add attribution
94
+ st.markdown("Created ❤️ by Richard Dorglo")