Spaces:
Runtime error
Runtime error
File size: 4,021 Bytes
dab8f94 9722fe8 4521643 6ffa4a5 dab8f94 9722fe8 4521643 8c200ab dab8f94 9722fe8 8c200ab dab8f94 9722fe8 dab8f94 9722fe8 4521643 dab8f94 9722fe8 dab8f94 4521643 dab8f94 9722fe8 4521643 dab8f94 9722fe8 4521643 dab8f94 9722fe8 dab8f94 9722fe8 4521643 dab8f94 4521643 dab8f94 4521643 dab8f94 4521643 dab8f94 4521643 dab8f94 9722fe8 dab8f94 9722fe8 dab8f94 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | import os
import json
import re
import numpy as np
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
import torch
from torch.nn.functional import cosine_similarity
from sentence_transformers import SentenceTransformer
import gradio as gr
# Download necessary NLTK data
nltk.download('punkt_tab')
nltk.download('stopwords')
nltk.download('wordnet')
# Load dataset
dataset_path = "dataset.json"
if not os.path.exists(dataset_path):
raise FileNotFoundError(f"{dataset_path} not found. Please ensure the file is in the current directory.")
with open(dataset_path, "r") as f:
data = json.load(f)
disease_data = {d["name"]: d for d in data["diseases"]}
# Preprocessing
lemmatizer = WordNetLemmatizer()
stop_words = set(stopwords.words('english'))
medical_synonyms = {
"itchy": "itch", "pruritic": "itch", "rash": "eruption",
"redness": "red", "swollen": "swelling", "bumps": "lesion"
}
def advanced_preprocess_text(text):
text = text.lower()
text = re.sub(r'[^a-zA-Z\s]', ' ', text)
tokens = word_tokenize(text)
tokens = [medical_synonyms.get(token, token) for token in tokens]
tokens = [lemmatizer.lemmatize(token) for token in tokens if token not in stop_words]
return " ".join(tokens)
# Prepare disease descriptions
disease_names = []
disease_descriptions = []
for disease_name, details in disease_data.items():
disease_names.append(disease_name)
description = ""
if "symptoms" in details:
description += "Symptoms: " + " ".join(details["symptoms"]) + ". "
if "causes" in details:
description += "Causes: " + " ".join(details["causes"]) + ". "
if "cure" in details:
description += "Cure: " + " ".join(details["cure"]) + ". "
if "home_remedies" in details:
description += "Home Remedies: " + " ".join(details["home_remedies"]) + ". "
processed_desc = advanced_preprocess_text(description)
disease_descriptions.append(processed_desc)
# Load BERT model
bert_model = SentenceTransformer('all-MiniLM-L6-v2')
disease_embeddings = bert_model.encode(disease_descriptions, convert_to_tensor=True)
def gradio_chatbot(primary_symptom, location, associated_symptoms, duration, severity, additional_info):
user_input = " ".join([
primary_symptom, location, associated_symptoms, duration, severity, additional_info
])
processed_input = advanced_preprocess_text(user_input)
user_embedding = bert_model.encode(processed_input, convert_to_tensor=True)
similarities = cosine_similarity(user_embedding, disease_embeddings)
best_match_idx = torch.argmax(similarities).item()
predicted_disease = disease_names[best_match_idx]
details = disease_data.get(predicted_disease, {})
causes = ", ".join(details.get("causes", ["Not available"]))
cure = ", ".join(details.get("cure", ["Not available"]))
home_remedies = ", ".join(details.get("home_remedies", ["Not available"]))
response = (f"🩺 **Predicted Disease:** {predicted_disease}\n\n"
f"**Causes:** {causes}\n\n"
f"**Cure:** {cure}\n\n"
f"**Home Remedies:** {home_remedies}")
return response
iface = gr.Interface(
fn=gradio_chatbot,
inputs=[
gr.Textbox(label="1. Primary Symptom", placeholder="e.g., itching, rash"),
gr.Textbox(label="2. Location on Body", placeholder="e.g., face, arms"),
gr.Textbox(label="3. Associated Symptoms", placeholder="e.g., swelling, redness"),
gr.Textbox(label="4. Duration", placeholder="e.g., 2 days, 1 week"),
gr.Textbox(label="5. Severity (scale 1-10)", placeholder="e.g., 5"),
gr.Textbox(label="6. Additional Observations", placeholder="e.g., recent exposure, diet changes")
],
outputs="markdown",
title="Advanced Skin Disease AI Chatbot",
description="Answer a few questions about your symptoms and get a predicted skin disease along with recommended solutions."
)
iface.launch()
|