Advertisement

Python Project With Source Code - AI Healthcare Bot System using Python

Python Project With Source Code - AI Healthcare Bot System using Python

AI Healthcare Bot System in Python involves integrating natural language processing (NLP) with healthcare-related information to provide users with medical advice, symptom checking, or general health information.

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)


How it Works:

  1. Dependencies: The code imports necessary libraries (flask, textblob, requests, spacy).
  2. SpaCy Model: Loads the spaCy English model (en_core_web_sm) for natural language processing.
  3. Medical Data: Defines a simple dictionary medical_data containing sample medical advice for common symptoms.
  4. Flask Web Server: Starts a Flask web server with a /ask endpoint to receive POST requests containing user messages.
  5. Text Analysis: Uses TextBlob to perform sentiment analysis on the user message and categorize it as positive, negative, or neutral.
  6. Entity Recognition: Utilizes spaCy to extract entities (medical conditions) from the user message.
  7. Response Generation: Checks if any extracted entity matches a key in medical_data and constructs a response with relevant advice. If no specific condition matches, a default response is provided.
  8. JSON Response: Returns a JSON object containing the response and sentiment analysis result.

How to Run:

  • Save the code to a file (healthcare_bot.py).
  • Run the Flask web server: python healthcare_bot.py.
  • Use tools like Postman or send HTTP POST requests to http://localhost:5000/ask with JSON data {"message": "I have a headache"} to interact with the bot.

Notes:

  • Expandability: This example uses a simple static dictionary for medical advice. For a real-world application, you would integrate with medical databases or APIs for up-to-date information.
  • Security: Ensure proper validation and sanitization of user inputs, especially when dealing with healthcare data.
  • Deployment: For production deployment, consider hosting on a secure server and using HTTPS.

This basic AI Healthcare Bot System demonstrates how to integrate natural language processing with healthcare information to provide users with personalized medical advice based on their symptoms.

Post a Comment

0 Comments