Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
import numpy as np
|
| 7 |
+
from sklearn.model_selection import train_test_split
|
| 8 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 9 |
+
from sklearn.metrics import accuracy_score, classification_report
|
| 10 |
+
import tensorflow as tf
|
| 11 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 12 |
+
from sklearn.naive_bayes import MultinomialNB
|
| 13 |
+
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
|
| 14 |
+
import nltk
|
| 15 |
+
from nltk.corpus import stopwords
|
| 16 |
+
from nltk.stem import PorterStemmer
|
| 17 |
+
from gensim.models import Word2Vec
|
| 18 |
+
import matplotlib.pyplot as plt
|
| 19 |
+
import seaborn as sns
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# for using TensorFlow for deep learning
|
| 23 |
+
from tensorflow.keras.models import Sequential
|
| 24 |
+
from tensorflow.keras.layers import Dense
|
| 25 |
+
from tensorflow.keras.optimizers import Adam
|
| 26 |
+
from tensorflow.keras.losses import categorical_crossentropy
|
| 27 |
+
|
| 28 |
+
# for using PyTorch for deep learning
|
| 29 |
+
import torch
|
| 30 |
+
import torch.nn as nn
|
| 31 |
+
import torch.optim as optim
|
| 32 |
+
import torch.nn.functional as F
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# Load your symptom-disease data
|
| 36 |
+
data = pd.read_csv("Symptom2Disease.csv")
|
| 37 |
+
|
| 38 |
+
# Initialize the TF-IDF vectorizer
|
| 39 |
+
tfidf_vectorizer = TfidfVectorizer()
|
| 40 |
+
|
| 41 |
+
# Apply TF-IDF vectorization to the preprocessed text data
|
| 42 |
+
X = tfidf_vectorizer.fit_transform(data['text'])
|
| 43 |
+
|
| 44 |
+
# Split the dataset into a training set and a testing set
|
| 45 |
+
X_train, X_test, y_train, y_test = train_test_split(X, data['label'], test_size=0.2, random_state=42)
|
| 46 |
+
|
| 47 |
+
# Initialize the Multinomial Naive Bayes model
|
| 48 |
+
model = MultinomialNB()
|
| 49 |
+
|
| 50 |
+
# Train the model on the training data
|
| 51 |
+
model.fit(X_train, y_train)
|
| 52 |
+
|
| 53 |
+
# Set Streamlit app title with emojis
|
| 54 |
+
st.title("Healthcare Symptom-to-Disease Recommender 🏥👨⚕️")
|
| 55 |
+
|
| 56 |
+
# Define a sidebar
|
| 57 |
+
st.sidebar.title("Tool Definition")
|
| 58 |
+
st.sidebar.markdown("This tool helps you identify possible diseases based on the symptoms you provide. It is not a substitute for professional medical advice. Always consult a healthcare professional for accurate diagnosis and treatment.")
|
| 59 |
+
|
| 60 |
+
# Initialize chat history
|
| 61 |
+
if "messages" not in st.session_state:
|
| 62 |
+
st.session_state.messages = []
|
| 63 |
+
|
| 64 |
+
# Function to preprocess user input
|
| 65 |
+
def preprocess_input(user_input):
|
| 66 |
+
user_input = user_input.lower() # Convert to lowercase
|
| 67 |
+
user_input = re.sub(r"[^a-zA-Z\s]", "", user_input) # Remove special characters and numbers
|
| 68 |
+
user_input = " ".join(user_input.split()) # Remove extra spaces
|
| 69 |
+
return user_input
|
| 70 |
+
|
| 71 |
+
# Function to predict diseases based on user input
|
| 72 |
+
def predict_diseases(user_clean_text):
|
| 73 |
+
user_input_vector = tfidf_vectorizer.transform([user_clean_text]) # Vectorize the cleaned user input
|
| 74 |
+
predictions = model.predict(user_input_vector) # Make predictions using the trained model
|
| 75 |
+
return predictions
|
| 76 |
+
|
| 77 |
+
# Add user input section
|
| 78 |
+
user_input = st.text_area("Enter your symptoms (how you feel):", key="user_input")
|
| 79 |
+
|
| 80 |
+
# Add button to predict disease
|
| 81 |
+
if st.button("Predict Disease"):
|
| 82 |
+
# Display loading message
|
| 83 |
+
with st.spinner("Diagnosing patient..."):
|
| 84 |
+
# Check if user input is not empty
|
| 85 |
+
if user_input:
|
| 86 |
+
cleaned_input = preprocess_input(user_input)
|
| 87 |
+
predicted_diseases = predict_diseases(cleaned_input)
|
| 88 |
+
|
| 89 |
+
# Display predicted diseases
|
| 90 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 91 |
+
st.session_state.messages.append({"role": "assistant", "content": f"Based on your symptoms, you might have {', '.join(predicted_diseases)}."})
|
| 92 |
+
|
| 93 |
+
st.write("Based on your symptoms, you might have:")
|
| 94 |
+
for disease in predicted_diseases:
|
| 95 |
+
st.write(f"- {disease}")
|
| 96 |
+
else:
|
| 97 |
+
st.warning("Please enter your symptoms before predicting.")
|
| 98 |
+
|
| 99 |
+
# Display a warning message
|
| 100 |
+
st.warning("Please note that this tool is for informational purposes only. Always consult a healthcare professional for accurate medical advice.")
|
| 101 |
+
|
| 102 |
+
# Add attribution
|
| 103 |
+
st.markdown("Created with ❤️ by Richard Dorglo")
|