AI Healthcare Bot System
Dependencies:
- spaCy: NLP library for natural language understanding.
- textblob: Library for processing textual data.
- flask: Web framework for creating a web application.
- requests: HTTP library for making requests to external APIs.
- flask-ngrok: Used to expose a Flask web server to the internet for testing.
Installation:
You can install the required libraries using pip:
pip install spacy textblob flask requests flask-ngrok
Source Code :
from flask import Flask, request, jsonify
from textblob import TextBlob
import requests
import spacy
app = Flask(__name__)
# Load English language model for spaCy
nlp = spacy.load("en_core_web_sm")
# Sample medical data (can be extended with real medical databases)
medical_data = {
"headache": "You might be stressed. Try to relax and take a break.",
"cough": "It could be a symptom of a cold or flu. Drink plenty of fluids and rest.",
"fever": "You may have a fever. Rest and drink fluids. If it persists, consult a doctor.",
"sore throat": "Gargle with warm salt water. If it persists, consult a doctor.",
"fatigue": "Make sure you are getting enough sleep and eating a balanced diet.",
"stomach ache": "Avoid spicy and oily foods. If it continues, consult a doctor.",
"back pain": "Improving your posture and stretching may help. If it continues, consult a doctor."
}
# Define endpoint for receiving messages
@app.route("/ask", methods=["POST"])
def ask_bot():
data = request.json
message = data["message"]
# Perform text analysis with TextBlob
analysis = TextBlob(message)
if analysis.polarity > 0:
sentiment = "positive"
elif analysis.polarity < 0:
sentiment = "negative"
else:
sentiment = "neutral"
# Perform entity recognition with spaCy
doc = nlp(message)
entities = [ent.text for ent in doc.ents]
# Check for medical conditions in entities
response = ""
for entity in entities:
if entity.lower() in medical_data:
response += medical_data[entity.lower()] + "\n"
# If no specific condition found, provide general advice
if not response:
response = "I'm sorry, I don't have information about that. Please consult a doctor for advice."
# Return response in JSON format
return jsonify({
"response": response,
"sentiment": sentiment
})
if __name__ == "__main__":
app.run(debug=True)
0 Comments