|
|
|
"""VirtualHealth.ipynb |
|
|
|
Automatically generated by Colab. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/1yVSYiPz-WUlO7U0uAKe9LmfMXHd5eyAA |
|
""" |
|
!pip install streamlit |
|
|
|
import xgboost as xgb |
|
import pickle |
|
import numpy as np |
|
import pandas as pd |
|
|
|
|
|
model = xgb.XGBClassifier() |
|
model.load_model("symptom_disease_model.json") |
|
|
|
|
|
label_encoder = pickle.load(open("label_encoder.pkl", "rb")) |
|
|
|
|
|
X_train = pd.read_csv("X_train.csv") |
|
symptom_list = X_train.columns.tolist() |
|
|
|
|
|
def predict_disease(user_symptoms): |
|
|
|
input_vector = np.zeros(len(symptom_list)) |
|
|
|
for symptom in user_symptoms: |
|
if symptom in symptom_list: |
|
input_vector[symptom_list.index(symptom)] = 1 |
|
|
|
input_vector = input_vector.reshape(1, -1) |
|
|
|
|
|
predicted_class = model.predict(input_vector)[0] |
|
|
|
|
|
predicted_disease = label_encoder.inverse_transform([predicted_class])[0] |
|
|
|
return predicted_disease |
|
|
|
|
|
user_symptoms = ["itching", "skin_rash", "nodal_skin_eruptions"] |
|
predicted_disease = predict_disease(user_symptoms) |
|
print(f"Predicted Disease: {predicted_disease}") |
|
|
|
!pip install zipfile36 |
|
import sys |
|
if sys.version_info >= (3, 6): |
|
import zipfile |
|
else: |
|
import zipfile36 as zipfile |
|
import os |
|
|
|
zip_file_path = '/content/disease symptom.zip' |
|
extracted_dir = '/content' |
|
|
|
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref: |
|
zip_ref.extractall(extracted_dir) |
|
|
|
|
|
precaution_df = pd.read_csv("Disease precaution.csv") |
|
|
|
|
|
precaution_dict = {} |
|
for _, row in precaution_df.iterrows(): |
|
disease = row["Disease"].strip().lower() |
|
precautions = [row[f"Precaution_{i}"] for i in range(1, 5) if pd.notna(row[f"Precaution_{i}"])] |
|
precaution_dict[disease] = precautions |
|
|
|
|
|
def get_precautions(disease_name): |
|
disease_name = disease_name.strip().lower() |
|
return precaution_dict.get(disease_name, ["No precautions found"]) |
|
|
|
|
|
precautions = get_precautions(predicted_disease) |
|
print(f"Precautions for {predicted_disease}: {precautions}") |
|
|
|
!pip install nltk |
|
|
|
import re |
|
import nltk |
|
from nltk.corpus import stopwords |
|
from nltk.tokenize import word_tokenize |
|
|
|
|
|
nltk.download("stopwords") |
|
nltk.download("punkt") |
|
|
|
|
|
stop_words = set(stopwords.words("english")) |
|
nltk.download('punkt_tab') |
|
|
|
import xgboost as xgb |
|
import pickle |
|
import numpy as np |
|
import pandas as pd |
|
import torch |
|
from transformers import AutoTokenizer, AutoModelForQuestionAnswering |
|
import re |
|
|
|
|
|
|
|
|
|
qa_model_name = "deepset/roberta-base-squad2" |
|
tokenizer = AutoTokenizer.from_pretrained(qa_model_name) |
|
qa_model = AutoModelForQuestionAnswering.from_pretrained(qa_model_name) |
|
|
|
|
|
|
|
|
|
|
|
model = xgb.XGBClassifier() |
|
model.load_model("symptom_disease_model.json") |
|
common_symptoms = ["fever", "cough", "headache", "pain", "vomiting", "fatigue", "nausea", "rash", "chills", "dizziness", "sore throat", "diarrhea"] |
|
|
|
|
|
label_encoder = pickle.load(open("label_encoder.pkl", "rb")) |
|
|
|
|
|
X_train = pd.read_csv("X_train.csv") |
|
symptom_list = X_train.columns.tolist() |
|
|
|
|
|
|
|
|
|
precaution_df = pd.read_csv("Disease precaution.csv") |
|
precaution_dict = { |
|
row["Disease"].strip().lower(): [row[f"Precaution_{i}"] for i in range(1, 5) if pd.notna(row[f"Precaution_{i}"])] |
|
for _, row in precaution_df.iterrows() |
|
} |
|
|
|
|
|
|
|
|
|
def load_medical_context(): |
|
with open("medical_context.txt", "r", encoding="utf-8") as file: |
|
return file.read() |
|
|
|
medical_context = load_medical_context() |
|
|
|
|
|
|
|
|
|
doctor_database = { |
|
"malaria": [{"name": "Dr. Rajesh Kumar", "specialty": "Infectious Diseases", "location": "Apollo Hospital", "contact": "9876543210"}], |
|
"diabetes": [{"name": "Dr. Anil Mehta", "specialty": "Endocrinologist", "location": "AIIMS Delhi", "contact": "9876543233"}], |
|
"heart attack": [{"name": "Dr. Vikram Singh", "specialty": "Cardiologist", "location": "Medanta Hospital", "contact": "9876543255"}], |
|
"hepatitis e": [{"name": "Dr. Sunil Agarwal", "specialty": "Hepatologist", "location": "Fortis Hospital", "contact": "9876543266"}], |
|
"pneumonia": [{"name": "Dr. Priya Sharma", "specialty": "Pulmonologist", "location": "Max Healthcare", "contact": "9876543277"}], |
|
"heartattack": [{"name": "Dr. Vikram Singh", "specialty": "Cardiologist", "location": "Medanta Hospital", "contact": "9876543255"}], |
|
} |
|
|
|
|
|
|
|
|
|
def predict_disease(user_symptoms): |
|
"""Predicts the disease based on user symptoms using the trained XGBoost model.""" |
|
input_vector = np.zeros(len(symptom_list)) |
|
|
|
for symptom in user_symptoms: |
|
if symptom in symptom_list: |
|
input_vector[symptom_list.index(symptom)] = 1 |
|
|
|
input_vector = input_vector.reshape(1, -1) |
|
|
|
|
|
predicted_class = model.predict(input_vector)[0] |
|
|
|
|
|
predicted_disease = label_encoder.inverse_transform([predicted_class])[0] |
|
|
|
return predicted_disease |
|
|
|
|
|
|
|
|
|
def get_precautions(disease): |
|
"""Returns the precautions for a given disease.""" |
|
return precaution_dict.get(disease.lower(), ["No precautions available"]) |
|
|
|
|
|
|
|
|
|
def get_medical_answer(question): |
|
"""Uses the pre-trained Q&A model to answer general medical questions.""" |
|
inputs = tokenizer(question, medical_context, return_tensors="pt", truncation=True, max_length=512) |
|
with torch.no_grad(): |
|
outputs = qa_model(**inputs) |
|
|
|
answer_start = torch.argmax(outputs.start_logits) |
|
answer_end = torch.argmax(outputs.end_logits) + 1 |
|
|
|
answer = tokenizer.convert_tokens_to_string( |
|
tokenizer.convert_ids_to_tokens(inputs["input_ids"][0][answer_start:answer_end]) |
|
) |
|
|
|
return answer if answer.strip() and answer != "[CLS]" else "No reliable answer found." |
|
|
|
|
|
|
|
|
|
def book_appointment(disease): |
|
"""Finds a doctor for the given disease and returns appointment details.""" |
|
disease = disease.lower().strip() |
|
doctors = doctor_database.get(disease, []) |
|
if not doctors: |
|
return f"Sorry, no available doctors found for {disease}." |
|
|
|
doctor = doctors[0] |
|
return f"Appointment booked with **{doctor['name']}** ({doctor['specialty']}) at **{doctor['location']}**.\nContact: {doctor['contact']}" |
|
|
|
|
|
|
|
|
|
def extract_treatment_from_context(disease): |
|
"""Extracts treatment details for a given disease from `medical_context.txt`.""" |
|
with open("medical_context.txt", "r", encoding="utf-8") as file: |
|
lines = file.readlines() |
|
|
|
treatment_section = [] |
|
found_disease = False |
|
found_treatment = False |
|
|
|
for line in lines: |
|
line = line.strip() |
|
|
|
|
|
if f"## {disease.lower()}" in line.lower(): |
|
found_disease = True |
|
|
|
|
|
if found_disease and "**Treatment**" in line: |
|
found_treatment = True |
|
continue |
|
|
|
|
|
if found_treatment: |
|
|
|
if line == "" or line.startswith("## "): |
|
break |
|
treatment_section.append(line) |
|
|
|
return "\n".join(treatment_section) if treatment_section else None |
|
|
|
|
|
def extract_disease_name(user_query): |
|
"""Extracts the disease name by removing unnecessary words, but keeps medical terms.""" |
|
user_query_cleaned = re.sub(r"[^\w\s]", "", user_query.lower()) |
|
words = word_tokenize(user_query_cleaned) |
|
|
|
|
|
filtered_words = [word for word in words if word not in stop_words or word in common_symptoms] |
|
|
|
return " ".join(filtered_words).strip() |
|
|
|
def find_best_match(query, database): |
|
"""Finds the best matching disease from the database based on query words.""" |
|
query_words = query.split() |
|
|
|
|
|
if query in database: |
|
return query |
|
|
|
|
|
for disease in database: |
|
for word in query_words: |
|
if word in disease: |
|
return disease |
|
|
|
return None |
|
|
|
|
|
def handle_user_query(user_query): |
|
"""Handles user queries related to symptoms, diseases, and doctor appointments.""" |
|
|
|
user_query = user_query.lower().strip() |
|
|
|
|
|
if "i have" in user_query or "experiencing" in user_query: |
|
symptoms = user_query.replace("I have", "").replace("experiencing", "").strip() |
|
disease = predict_disease(symptoms.split(", ")) |
|
precautions = get_precautions(disease) |
|
return f"**Predicted Disease:** {disease}\n**Precautions:** {', '.join(precautions)}\n{book_appointment(disease)}" |
|
|
|
|
|
user_query_cleaned = extract_disease_name(user_query) |
|
|
|
|
|
if "who should i see " in user_query: |
|
disease_query = user_query.replace("who should i see", "").strip() |
|
disease = find_best_match(disease_query, doctor_database) |
|
|
|
if disease: |
|
doctor = doctor_database[disease][0] |
|
return f"You should see a **{doctor['specialty']}** for {disease}.\nExample: {doctor['name']} at {doctor['location']}." |
|
else: |
|
return "I'm not sure. Please consult a general physician for more guidance." |
|
|
|
|
|
elif "book appointment" in user_query_cleaned: |
|
disease_query = user_query_cleaned.replace("book appointment", "").strip() |
|
disease = find_best_match(disease_query, doctor_database) |
|
return book_appointment(disease) if disease else "Sorry, no matching doctor found." |
|
|
|
|
|
elif "symptoms" in user_query_cleaned or "signs" in user_query_cleaned: |
|
disease = user_query_cleaned.replace("symptoms", "").replace("signs", "").strip() |
|
return get_medical_answer(f"What are the symptoms of {disease}?") |
|
|
|
|
|
elif "precautions" in user_query_cleaned or "prevent" in user_query_cleaned: |
|
disease = user_query_cleaned.replace("precautions", "").replace("prevent", "").strip() |
|
return ", ".join(get_precautions(disease)) |
|
|
|
|
|
if "treatment" in user_query_cleaned or "treat" in user_query_cleaned: |
|
disease = user_query_cleaned.replace("treatment", "").replace("treat", "").strip() |
|
|
|
|
|
treatment_answer = extract_treatment_from_context(disease) |
|
if treatment_answer: |
|
return treatment_answer |
|
|
|
|
|
model_answer = get_medical_answer(f"What is the treatment for {disease}?") |
|
if model_answer in ["<s>", "", "No reliable answer found."]: |
|
return f"I'm not sure, but common treatments for {disease} include medication, therapy, or consulting a specialist." |
|
return model_answer |
|
|
|
|
|
else: |
|
response = get_medical_answer(user_query) |
|
if response in ["<s>", "", "No reliable answer found."]: |
|
return "I'm not sure, but you may consult a specialist for better guidance." |
|
return response |
|
|
|
|
|
|
|
|
|
print(handle_user_query("I have fever, chills, and muscle aches")) |
|
print(handle_user_query("What are the symptoms of pneumonia?")) |
|
print(handle_user_query("Book an appointment for diabetes")) |
|
print(handle_user_query("Who should I see for heart attack")) |
|
print(handle_user_query("what is the treatment for tuberculosis")) |
|
|
|
print(handle_user_query("What is the treatment for tuberculosis?")) |
|
print(handle_user_query("What is the treatment for malaria?")) |
|
print(handle_user_query("What is the treatment for cancer?")) |
|
|
|
print(handle_user_query("What is the treatment for tuberculosis?")) |
|
print(handle_user_query("What is the treatment for malaria?")) |
|
print(handle_user_query("What is the treatment for cancer?")) |
|
print(handle_user_query("How to treat diabetes?")) |
|
print(handle_user_query("Tell me the cure for pneumonia?")) |
|
print(handle_user_query("Treatment for typhoid?")) |
|
|
|
print(handle_user_query("What are the symptoms of pneumonia?")) |
|
print(handle_user_query("Signs of heart attack?")) |
|
print(handle_user_query("How do I know if I have typhoid?")) |
|
print(handle_user_query("What symptoms should I check for tuberculosis?")) |
|
print(handle_user_query("Symptoms of dengue?")) |
|
|
|
print(handle_user_query("Who should I see for a heart attack?")) |
|
print(handle_user_query("Which doctor should I visit for diabetes?")) |
|
print(handle_user_query("Who should I consult for a skin rash?")) |
|
print(handle_user_query("What kind of doctor treats pneumonia?")) |
|
print(handle_user_query("Who specializes in treating migraines?")) |
|
|
|
print(handle_user_query("Book an appointment for malaria")) |
|
print(handle_user_query("I need a doctor for high blood pressure")) |
|
print(handle_user_query("Schedule a consultation for fever")) |
|
print(handle_user_query("Find a doctor for diabetes treatment")) |
|
print(handle_user_query("Book an appointment for pneumonia treatment")) |
|
|
|
print(handle_user_query("I have fever, cough, and chills")) |
|
print(handle_user_query("Experiencing blurry vision and excessive thirst")) |
|
print(handle_user_query("I am experiencing severe chest pain and difficulty breathing")) |
|
print(handle_user_query("Feeling tired, cold, and gaining weight")) |
|
print(handle_user_query("I have rash, joint pain, and headache")) |
|
|
|
print(handle_user_query("What does a doctor do?")) |
|
print(handle_user_query("What are antibiotics?")) |
|
print(handle_user_query("How does the immune system work?")) |
|
print(handle_user_query("What is the function of the liver?")) |
|
print(handle_user_query("Explain how blood pressure works?")) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""β
Steps to Deploy on Hugging Face Spaces |
|
π Step 1: Create a Hugging Face Space |
|
1οΈβ£ Go to Hugging Face Spaces |
|
2οΈβ£ Click "New Space" |
|
3οΈβ£ Name the Space (e.g., AI-Health-Assistant) |
|
4οΈβ£ Select "Streamlit" as the SDK |
|
5οΈβ£ Click "Create Space" β
|
|
|
|
π Step 2: Clone the Repository Locally |
|
After creating the Space, clone it to your local machine or Google Colab: |
|
|
|
```bash |
|
git clone https://huggingface.co/spaces/YOUR_USERNAME/AI-Health-Assistant |
|
cd AI-Health-Assistant |
|
``` |
|
Replace YOUR_USERNAME with your Hugging Face username! |
|
|
|
π Step 3: Add app.py (Your Streamlit Chatbot) |
|
Inside the cloned folder, create app.py and paste the following: |
|
|
|
π Step 4: Create requirements.txt |
|
Create a new file requirements.txt inside the same folder and add: |
|
```bash |
|
streamlit |
|
requests |
|
``` |
|
|
|
π Step 5: Push Your Code to Hugging Face |
|
Run these commands to push the code: |
|
|
|
```bash |
|
git add . |
|
git commit -m "Initial commit" |
|
git push |
|
``` |
|
π Your Space will automatically start building! |
|
""" |
|
|
|
|