├── FILE1 ├── ai_symptom_checker.py └── README.md /FILE1: -------------------------------------------------------------------------------- 1 | pip install transformers torch nltk scikit-learn 2 | import random 3 | import nltk 4 | from transformers import pipeline 5 | from sklearn.feature_extraction.text import TfidfVectorizer 6 | from sklearn.metrics.pairwise import cosine_similarity 7 | 8 | nltk.download('punkt') 9 | nltk.download('stopwords') 10 | from nltk.corpus import stopwords 11 | from nltk.tokenize import word_tokenize 12 | 13 | # Sample symptom-condition knowledge base 14 | conditions_db = { 15 | "fever, cough, fatigue": "You may have the flu or COVID-19.", 16 | "headache, nausea, sensitivity to light": "These are signs of a migraine.", 17 | "abdominal pain, diarrhea, vomiting": "Sounds like a gastrointestinal infection.", 18 | "sneezing, runny nose, itchy eyes": "You might have an allergy or the common cold.", 19 | "chest pain, shortness of breath, dizziness": "These could be signs of a cardiac issue. Seek urgent care.", 20 | "joint pain, swelling, stiffness": "These symptoms may suggest arthritis.", 21 | } 22 | 23 | # Preprocessing 24 | stop_words = set(stopwords.words('english')) 25 | 26 | def preprocess(text): 27 | words = word_tokenize(text.lower()) 28 | filtered = [w for w in words if w.isalnum() and w not in stop_words] 29 | return " ".join(filtered) 30 | 31 | # Convert DB keys to TF-IDF vectors 32 | vectorizer = TfidfVectorizer() 33 | db_symptoms = list(conditions_db.keys()) 34 | processed_symptoms = [preprocess(s) for s in db_symptoms] 35 | tfidf_matrix = vectorizer.fit_transform(processed_symptoms) 36 | 37 | # Sentiment analysis (for user empathy) 38 | sentiment_analyzer = pipeline("sentiment-analysis") 39 | 40 | # Chatbot response 41 | def get_condition(user_input): 42 | user_symptoms = preprocess(user_input) 43 | user_vec = vectorizer.transform([user_symptoms]) 44 | similarities = cosine_similarity(user_vec, tfidf_matrix) 45 | best_match_index = similarities.argmax() 46 | confidence = similarities[0][best_match_index] 47 | 48 | if confidence > 0.3: 49 | return conditions_db[db_symptoms[best_match_index]] 50 | else: 51 | return "I'm sorry, I couldn't determine your condition. Please consult a healthcare professional." 52 | 53 | # Main chatbot loop 54 | def chatbot(): 55 | print("🤖 Hi! I'm your AI Symptom Checker. Describe your symptoms (or type 'exit' to quit):") 56 | 57 | while True: 58 | user_input = input("You: ") 59 | if user_input.lower() in ["exit", "quit", "bye"]: 60 | print("🤖 Take care! Wishing you good health.") 61 | break 62 | 63 | # Show empathy if user seems distressed 64 | sentiment = sentiment_analyzer(user_input)[0] 65 | if sentiment['label'] == 'NEGATIVE': 66 | print("🤖 I'm sorry you're feeling this way. Let's see if we can understand your symptoms.") 67 | 68 | response = get_condition(user_input) 69 | print(f"🤖 {response}") 70 | 71 | # Run the chatbot 72 | if __name__ == "__main__": 73 | chatbot() 74 | -------------------------------------------------------------------------------- /ai_symptom_checker.py: -------------------------------------------------------------------------------- 1 | # file: ai_symptom_checker.py 2 | # Purpose: Simple AI Symptom Checker using TF-IDF similarity + sentiment analysis 3 | 4 | # Install dependencies (run once before using): 5 | # pip install transformers torch nltk scikit-learn 6 | 7 | import random 8 | import nltk 9 | from transformers import pipeline 10 | from sklearn.feature_extraction.text import TfidfVectorizer 11 | from sklearn.metrics.pairwise import cosine_similarity 12 | 13 | nltk.download('punkt') 14 | nltk.download('stopwords') 15 | from nltk.corpus import stopwords 16 | from nltk.tokenize import word_tokenize 17 | 18 | # Sample symptom-condition knowledge base 19 | conditions_db = { 20 | "fever, cough, fatigue": "You may have the flu or COVID-19.", 21 | "headache, nausea, sensitivity to light": "These are signs of a migraine.", 22 | "abdominal pain, diarrhea, vomiting": "Sounds like a gastrointestinal infection.", 23 | "sneezing, runny nose, itchy eyes": "You might have an allergy or the common cold.", 24 | "chest pain, shortness of breath, dizziness": "These could be signs of a cardiac issue. Seek urgent care.", 25 | "joint pain, swelling, stiffness": "These symptoms may suggest arthritis.", 26 | } 27 | 28 | # Preprocessing 29 | stop_words = set(stopwords.words('english')) 30 | 31 | def preprocess(text): 32 | words = word_tokenize(text.lower()) 33 | filtered = [w for w in words if w.isalnum() and w not in stop_words] 34 | return " ".join(filtered) 35 | 36 | # Convert DB keys to TF-IDF vectors 37 | vectorizer = TfidfVectorizer() 38 | db_symptoms = list(conditions_db.keys()) 39 | processed_symptoms = [preprocess(s) for s in db_symptoms] 40 | tfidf_matrix = vectorizer.fit_transform(processed_symptoms) 41 | 42 | # Sentiment analysis (for user empathy) 43 | sentiment_analyzer = pipeline("sentiment-analysis") 44 | 45 | # Chatbot response 46 | def get_condition(user_input): 47 | user_symptoms = preprocess(user_input) 48 | user_vec = vectorizer.transform([user_symptoms]) 49 | similarities = cosine_similarity(user_vec, tfidf_matrix) 50 | best_match_index = similarities.argmax() 51 | confidence = similarities[0][best_match_index] 52 | 53 | if confidence > 0.3: 54 | return conditions_db[db_symptoms[best_match_index]] 55 | else: 56 | return "I'm sorry, I couldn't determine your condition. Please consult a healthcare professional." 57 | 58 | # Main chatbot loop 59 | def chatbot(): 60 | print("🤖 Hi! I'm your AI Symptom Checker. Describe your symptoms (or type 'exit' to quit):") 61 | 62 | while True: 63 | user_input = input("You: ") 64 | if user_input.lower() in ["exit", "quit", "bye"]: 65 | print("🤖 Take care! Wishing you good health.") 66 | break 67 | 68 | # Show empathy if user seems distressed 69 | sentiment = sentiment_analyzer(user_input)[0] 70 | if sentiment['label'] == 'NEGATIVE': 71 | print("🤖 I'm sorry you're feeling this way. Let's see if we can understand your symptoms.") 72 | 73 | response = get_condition(user_input) 74 | print(f"🤖 {response}") 75 | 76 | # Run the chatbot 77 | if __name__ == "__main__": 78 | chatbot() 79 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | AI-Powered Symptom Checker Chatbot 🤖🩺 2 | 3 | https://img.shields.io/badge/Python-3.8%252B-blue 4 | https://img.shields.io/badge/License-MIT-green 5 | https://img.shields.io/badge/Powered%2520by-AI-orange 6 | 7 | An intelligent chatbot that analyzes symptoms and suggests possible medical conditions using machine learning. This prototype demonstrates how AI can assist in preliminary health assessments with a synthetic dataset of 150+ patient records. 8 | Features 9 | 10 | 🗣️ Natural language symptom analysis 11 | 12 | 🧠 Machine learning-based condition prediction 13 | 14 | 📊 Top 3 probable conditions with confidence scores 15 | 16 | ⚕️ Basic medical recommendations 17 | 18 | 📁 Synthetic dataset generator with 150+ records 19 | 20 | 📈 TF-IDF vectorization and SVM classification 21 | 22 | Installation 23 | bash 24 | 25 | # Clone the repository 26 | git clone https://github.com/GTVSOFT/AI-powered-Symptom-Checker-Chatbot.git 27 | cd AI-powered-Symptom-Checker-Chatbot 28 | 29 | # Install dependencies 30 | pip install -r requirements.txt 31 | 32 | Usage 33 | 1. Generate Synthetic Dataset 34 | bash 35 | 36 | python generate_data.py 37 | 38 | Generates symptom_dataset.xlsx with 150+ patient records 39 | 2. Run the Symptom Checker Chatbot 40 | bash 41 | 42 | python symptom_checker.py 43 | 44 | Example Interaction 45 | text 46 | 47 | 🤖 Hello! I'm your AI health assistant. 48 | Describe your symptoms (e.g., 'I have headache and fever'): 49 | Type 'quit' to exit at any time 50 | 51 | 👤 You: I have fever, cough, and shortness of breath 52 | 53 | 🤖 Possible conditions based on your symptoms: 54 | - COVID-19 (92.3% probability) 55 | - Pneumonia (6.1% probability) 56 | - Influenza (1.6% probability) 57 | 58 | 🚨 Recommendation: Consult a doctor immediately and get tested 59 | 60 | Dataset Details 61 | 62 | The synthetic dataset includes: 63 | 64 | 150+ patient records 65 | 66 | 30+ symptoms 67 | 68 | 20+ medical conditions 69 | 70 | Patient demographics (age, gender) 71 | 72 | Symptom severity and duration 73 | 74 | Columns: 75 | 76 | Patient ID 77 | 78 | Name 79 | 80 | Age 81 | 82 | Gender 83 | 84 | Symptoms 85 | 86 | Likely Condition 87 | 88 | Severity (Mild/Moderate/Severe) 89 | 90 | Duration (Days) 91 | 92 | View Sample Dataset 93 | Project Structure 94 | text 95 | 96 | AI-powered-Symptom-Checker-Chatbot/ 97 | ├── symptom_checker.py # Main chatbot application 98 | ├── generate_data.py # Synthetic dataset generator 99 | ├── symptom_dataset.xlsx # Generated dataset (after running generate_data.py) 100 | ├── requirements.txt # Python dependencies 101 | └── README.md # This file 102 | 103 | Important Notes 104 | 105 | ⚠️ This is NOT a medical diagnostic tool ⚠️ 106 | 107 | Uses synthetic data for demonstration only 108 | 109 | Predictions are based on pattern matching in synthetic data 110 | 111 | Always consult real healthcare professionals for medical concerns 112 | 113 | Not suitable for actual medical diagnosis or treatment 114 | 115 | License 116 | 117 | This project is licensed under the MIT License - see the LICENSE file for details. 118 | Contributing 119 | 120 | Contributions are welcome! Please open an issue or submit a pull request for any improvements. 121 | 122 | Fork the repository 123 | 124 | Create your feature branch (git checkout -b feature/your-feature) 125 | 126 | Commit your changes (git commit -am 'Add some feature') 127 | 128 | Push to the branch (git push origin feature/your-feature) 129 | 130 | Open a pull request 131 | 132 | Author: Okes Imoni 133 | Email: jennyimoni@gmail.com 134 | --------------------------------------------------------------------------------