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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"nbformat": 4,
|
3 |
+
"nbformat_minor": 0,
|
4 |
+
"metadata": {
|
5 |
+
"colab": {
|
6 |
+
"provenance": []
|
7 |
+
},
|
8 |
+
"kernelspec": {
|
9 |
+
"name": "python3",
|
10 |
+
"display_name": "Python 3"
|
11 |
+
},
|
12 |
+
"language_info": {
|
13 |
+
"name": "python"
|
14 |
+
}
|
15 |
+
},
|
16 |
+
"cells": [
|
17 |
+
{
|
18 |
+
"cell_type": "code",
|
19 |
+
"execution_count": 57,
|
20 |
+
"metadata": {
|
21 |
+
"colab": {
|
22 |
+
"base_uri": "https://localhost:8080/"
|
23 |
+
},
|
24 |
+
"id": "JUwKXagI8Vkj",
|
25 |
+
"outputId": "c9f6a127-a95e-48d4-d0d8-3725f62cca12"
|
26 |
+
},
|
27 |
+
"outputs": [
|
28 |
+
{
|
29 |
+
"output_type": "stream",
|
30 |
+
"name": "stdout",
|
31 |
+
"text": [
|
32 |
+
"Predicted Disease: Fungal infection\n"
|
33 |
+
]
|
34 |
+
}
|
35 |
+
],
|
36 |
+
"source": [
|
37 |
+
"import xgboost as xgb\n",
|
38 |
+
"import pickle\n",
|
39 |
+
"import numpy as np\n",
|
40 |
+
"import pandas as pd\n",
|
41 |
+
"\n",
|
42 |
+
"# Load the trained model\n",
|
43 |
+
"model = xgb.XGBClassifier()\n",
|
44 |
+
"model.load_model(\"symptom_disease_model.json\")\n",
|
45 |
+
"\n",
|
46 |
+
"# Load the label encoder\n",
|
47 |
+
"label_encoder = pickle.load(open(\"label_encoder.pkl\", \"rb\"))\n",
|
48 |
+
"\n",
|
49 |
+
"# Load symptom names (from preprocessed training data)\n",
|
50 |
+
"X_train = pd.read_csv(\"X_train.csv\") # Get feature names\n",
|
51 |
+
"symptom_list = X_train.columns.tolist()\n",
|
52 |
+
"\n",
|
53 |
+
"# Function to Predict Disease\n",
|
54 |
+
"def predict_disease(user_symptoms):\n",
|
55 |
+
" # Convert user symptoms into one-hot encoded format\n",
|
56 |
+
" input_vector = np.zeros(len(symptom_list))\n",
|
57 |
+
"\n",
|
58 |
+
" for symptom in user_symptoms:\n",
|
59 |
+
" if symptom in symptom_list:\n",
|
60 |
+
" input_vector[symptom_list.index(symptom)] = 1\n",
|
61 |
+
"\n",
|
62 |
+
" input_vector = input_vector.reshape(1, -1) # Reshape for model\n",
|
63 |
+
"\n",
|
64 |
+
" # Predict disease (returns a numerical class)\n",
|
65 |
+
" predicted_class = model.predict(input_vector)[0]\n",
|
66 |
+
"\n",
|
67 |
+
" # Convert number to disease name\n",
|
68 |
+
" predicted_disease = label_encoder.inverse_transform([predicted_class])[0]\n",
|
69 |
+
"\n",
|
70 |
+
" return predicted_disease\n",
|
71 |
+
"\n",
|
72 |
+
"# Example Usage\n",
|
73 |
+
"user_symptoms = [\"itching\", \"skin_rash\", \"nodal_skin_eruptions\"]\n",
|
74 |
+
"predicted_disease = predict_disease(user_symptoms)\n",
|
75 |
+
"print(f\"Predicted Disease: {predicted_disease}\")\n"
|
76 |
+
]
|
77 |
+
},
|
78 |
+
{
|
79 |
+
"cell_type": "code",
|
80 |
+
"source": [
|
81 |
+
"!pip install zipfile36\n",
|
82 |
+
"import sys\n",
|
83 |
+
"if sys.version_info >= (3, 6):\n",
|
84 |
+
" import zipfile\n",
|
85 |
+
"else:\n",
|
86 |
+
" import zipfile36 as zipfile\n",
|
87 |
+
"import os\n",
|
88 |
+
"\n",
|
89 |
+
"zip_file_path = '/content/disease symptom.zip' # Update with your path\n",
|
90 |
+
"extracted_dir = '/content' # Where to extract the files\n",
|
91 |
+
"\n",
|
92 |
+
"with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:\n",
|
93 |
+
" zip_ref.extractall(extracted_dir)"
|
94 |
+
],
|
95 |
+
"metadata": {
|
96 |
+
"colab": {
|
97 |
+
"base_uri": "https://localhost:8080/"
|
98 |
+
},
|
99 |
+
"id": "_cmO1ieS8rcS",
|
100 |
+
"outputId": "51db88e4-f9a6-4404-be06-a161afb80c29"
|
101 |
+
},
|
102 |
+
"execution_count": 2,
|
103 |
+
"outputs": [
|
104 |
+
{
|
105 |
+
"output_type": "stream",
|
106 |
+
"name": "stdout",
|
107 |
+
"text": [
|
108 |
+
"Collecting zipfile36\n",
|
109 |
+
" Downloading zipfile36-0.1.3-py3-none-any.whl.metadata (736 bytes)\n",
|
110 |
+
"Downloading zipfile36-0.1.3-py3-none-any.whl (20 kB)\n",
|
111 |
+
"Installing collected packages: zipfile36\n",
|
112 |
+
"Successfully installed zipfile36-0.1.3\n"
|
113 |
+
]
|
114 |
+
}
|
115 |
+
]
|
116 |
+
},
|
117 |
+
{
|
118 |
+
"cell_type": "code",
|
119 |
+
"source": [
|
120 |
+
"# Load the precaution dataset\n",
|
121 |
+
"precaution_df = pd.read_csv(\"Disease precaution.csv\")\n",
|
122 |
+
"\n",
|
123 |
+
"# Convert to dictionary for fast lookup\n",
|
124 |
+
"precaution_dict = {}\n",
|
125 |
+
"for _, row in precaution_df.iterrows():\n",
|
126 |
+
" disease = row[\"Disease\"].strip().lower()\n",
|
127 |
+
" precautions = [row[f\"Precaution_{i}\"] for i in range(1, 5) if pd.notna(row[f\"Precaution_{i}\"])]\n",
|
128 |
+
" precaution_dict[disease] = precautions\n",
|
129 |
+
"\n",
|
130 |
+
"# Function to Get Precautions\n",
|
131 |
+
"def get_precautions(disease_name):\n",
|
132 |
+
" disease_name = disease_name.strip().lower()\n",
|
133 |
+
" return precaution_dict.get(disease_name, [\"No precautions found\"])\n",
|
134 |
+
"\n",
|
135 |
+
"# Example Usage\n",
|
136 |
+
"precautions = get_precautions(predicted_disease)\n",
|
137 |
+
"print(f\"Precautions for {predicted_disease}: {precautions}\")\n"
|
138 |
+
],
|
139 |
+
"metadata": {
|
140 |
+
"colab": {
|
141 |
+
"base_uri": "https://localhost:8080/"
|
142 |
+
},
|
143 |
+
"id": "Q-by1c_x8lqq",
|
144 |
+
"outputId": "19461912-49d0-48e4-9d9a-2b7e535df0e0"
|
145 |
+
},
|
146 |
+
"execution_count": 3,
|
147 |
+
"outputs": [
|
148 |
+
{
|
149 |
+
"output_type": "stream",
|
150 |
+
"name": "stdout",
|
151 |
+
"text": [
|
152 |
+
"Precautions for Fungal infection: ['bath twice', 'use detol or neem in bathing water', 'keep infected area dry', 'use clean cloths']\n"
|
153 |
+
]
|
154 |
+
}
|
155 |
+
]
|
156 |
+
},
|
157 |
+
{
|
158 |
+
"cell_type": "code",
|
159 |
+
"source": [
|
160 |
+
"!pip install nltk"
|
161 |
+
],
|
162 |
+
"metadata": {
|
163 |
+
"colab": {
|
164 |
+
"base_uri": "https://localhost:8080/"
|
165 |
+
},
|
166 |
+
"id": "Z9dYwI-Cjzz3",
|
167 |
+
"outputId": "1564c447-3876-4979-9c6c-44832e5ab1b7"
|
168 |
+
},
|
169 |
+
"execution_count": 80,
|
170 |
+
"outputs": [
|
171 |
+
{
|
172 |
+
"output_type": "stream",
|
173 |
+
"name": "stdout",
|
174 |
+
"text": [
|
175 |
+
"Requirement already satisfied: nltk in /usr/local/lib/python3.11/dist-packages (3.9.1)\n",
|
176 |
+
"Requirement already satisfied: click in /usr/local/lib/python3.11/dist-packages (from nltk) (8.1.8)\n",
|
177 |
+
"Requirement already satisfied: joblib in /usr/local/lib/python3.11/dist-packages (from nltk) (1.4.2)\n",
|
178 |
+
"Requirement already satisfied: regex>=2021.8.3 in /usr/local/lib/python3.11/dist-packages (from nltk) (2024.11.6)\n",
|
179 |
+
"Requirement already satisfied: tqdm in /usr/local/lib/python3.11/dist-packages (from nltk) (4.67.1)\n"
|
180 |
+
]
|
181 |
+
}
|
182 |
+
]
|
183 |
+
},
|
184 |
+
{
|
185 |
+
"cell_type": "code",
|
186 |
+
"source": [
|
187 |
+
"import re\n",
|
188 |
+
"import nltk\n",
|
189 |
+
"from nltk.corpus import stopwords\n",
|
190 |
+
"from nltk.tokenize import word_tokenize\n",
|
191 |
+
"\n",
|
192 |
+
"# Download stopwords if not already downloaded\n",
|
193 |
+
"nltk.download(\"stopwords\")\n",
|
194 |
+
"nltk.download(\"punkt\")\n",
|
195 |
+
"\n",
|
196 |
+
"# Load English stopwords\n",
|
197 |
+
"stop_words = set(stopwords.words(\"english\"))\n",
|
198 |
+
"nltk.download('punkt_tab')"
|
199 |
+
],
|
200 |
+
"metadata": {
|
201 |
+
"colab": {
|
202 |
+
"base_uri": "https://localhost:8080/"
|
203 |
+
},
|
204 |
+
"id": "BbkxCwC3j766",
|
205 |
+
"outputId": "318849f5-8596-44ac-878e-91e154ef5e2d"
|
206 |
+
},
|
207 |
+
"execution_count": 83,
|
208 |
+
"outputs": [
|
209 |
+
{
|
210 |
+
"output_type": "stream",
|
211 |
+
"name": "stderr",
|
212 |
+
"text": [
|
213 |
+
"[nltk_data] Downloading package stopwords to /root/nltk_data...\n",
|
214 |
+
"[nltk_data] Package stopwords is already up-to-date!\n",
|
215 |
+
"[nltk_data] Downloading package punkt to /root/nltk_data...\n",
|
216 |
+
"[nltk_data] Package punkt is already up-to-date!\n",
|
217 |
+
"[nltk_data] Downloading package punkt_tab to /root/nltk_data...\n",
|
218 |
+
"[nltk_data] Unzipping tokenizers/punkt_tab.zip.\n"
|
219 |
+
]
|
220 |
+
},
|
221 |
+
{
|
222 |
+
"output_type": "execute_result",
|
223 |
+
"data": {
|
224 |
+
"text/plain": [
|
225 |
+
"True"
|
226 |
+
]
|
227 |
+
},
|
228 |
+
"metadata": {},
|
229 |
+
"execution_count": 83
|
230 |
+
}
|
231 |
+
]
|
232 |
+
},
|
233 |
+
{
|
234 |
+
"cell_type": "code",
|
235 |
+
"source": [
|
236 |
+
"import xgboost as xgb\n",
|
237 |
+
"import pickle\n",
|
238 |
+
"import numpy as np\n",
|
239 |
+
"import pandas as pd\n",
|
240 |
+
"import torch\n",
|
241 |
+
"from transformers import AutoTokenizer, AutoModelForQuestionAnswering\n",
|
242 |
+
"import re # Import regex module for better input processing\n",
|
243 |
+
"\n",
|
244 |
+
"# ============================\n",
|
245 |
+
"# πΉ 1. Load Pretrained Medical Q&A Model\n",
|
246 |
+
"# ============================\n",
|
247 |
+
"qa_model_name = \"deepset/roberta-base-squad2\" # Better model for medical Q&A\n",
|
248 |
+
"tokenizer = AutoTokenizer.from_pretrained(qa_model_name)\n",
|
249 |
+
"qa_model = AutoModelForQuestionAnswering.from_pretrained(qa_model_name)\n",
|
250 |
+
"\n",
|
251 |
+
"# ============================\n",
|
252 |
+
"# πΉ 2. Load Symptom Checker Model & Label Encoder (Fixed)\n",
|
253 |
+
"# ============================\n",
|
254 |
+
"# Load trained XGBoost model from JSON\n",
|
255 |
+
"model = xgb.XGBClassifier()\n",
|
256 |
+
"model.load_model(\"symptom_disease_model.json\")\n",
|
257 |
+
"common_symptoms = [\"fever\", \"cough\", \"headache\", \"pain\", \"vomiting\", \"fatigue\", \"nausea\", \"rash\", \"chills\", \"dizziness\", \"sore throat\", \"diarrhea\"]\n",
|
258 |
+
"\n",
|
259 |
+
"# Load Corrected Label Encoder\n",
|
260 |
+
"label_encoder = pickle.load(open(\"label_encoder.pkl\", \"rb\"))\n",
|
261 |
+
"\n",
|
262 |
+
"# Load symptom names from training data\n",
|
263 |
+
"X_train = pd.read_csv(\"X_train.csv\") # Get feature names\n",
|
264 |
+
"symptom_list = X_train.columns.tolist()\n",
|
265 |
+
"\n",
|
266 |
+
"# ============================\n",
|
267 |
+
"# πΉ 3. Load Precaution Data\n",
|
268 |
+
"# ============================\n",
|
269 |
+
"precaution_df = pd.read_csv(\"Disease precaution.csv\")\n",
|
270 |
+
"precaution_dict = {\n",
|
271 |
+
" row[\"Disease\"].strip().lower(): [row[f\"Precaution_{i}\"] for i in range(1, 5) if pd.notna(row[f\"Precaution_{i}\"])]\n",
|
272 |
+
" for _, row in precaution_df.iterrows()\n",
|
273 |
+
"}\n",
|
274 |
+
"\n",
|
275 |
+
"# ============================\n",
|
276 |
+
"# πΉ 4. Load Medical Context\n",
|
277 |
+
"# ============================\n",
|
278 |
+
"def load_medical_context():\n",
|
279 |
+
" with open(\"medical_context.txt\", \"r\", encoding=\"utf-8\") as file:\n",
|
280 |
+
" return file.read()\n",
|
281 |
+
"\n",
|
282 |
+
"medical_context = load_medical_context()\n",
|
283 |
+
"\n",
|
284 |
+
"# ============================\n",
|
285 |
+
"# πΉ 5. Doctor Database (For Appointments)\n",
|
286 |
+
"# ============================\n",
|
287 |
+
"doctor_database = {\n",
|
288 |
+
" \"malaria\": [{\"name\": \"Dr. Rajesh Kumar\", \"specialty\": \"Infectious Diseases\", \"location\": \"Apollo Hospital\", \"contact\": \"9876543210\"}],\n",
|
289 |
+
" \"diabetes\": [{\"name\": \"Dr. Anil Mehta\", \"specialty\": \"Endocrinologist\", \"location\": \"AIIMS Delhi\", \"contact\": \"9876543233\"}],\n",
|
290 |
+
" \"heart attack\": [{\"name\": \"Dr. Vikram Singh\", \"specialty\": \"Cardiologist\", \"location\": \"Medanta Hospital\", \"contact\": \"9876543255\"}],\n",
|
291 |
+
" \"hepatitis e\": [{\"name\": \"Dr. Sunil Agarwal\", \"specialty\": \"Hepatologist\", \"location\": \"Fortis Hospital\", \"contact\": \"9876543266\"}],\n",
|
292 |
+
" \"pneumonia\": [{\"name\": \"Dr. Priya Sharma\", \"specialty\": \"Pulmonologist\", \"location\": \"Max Healthcare\", \"contact\": \"9876543277\"}],\n",
|
293 |
+
" \"heartattack\": [{\"name\": \"Dr. Vikram Singh\", \"specialty\": \"Cardiologist\", \"location\": \"Medanta Hospital\", \"contact\": \"9876543255\"}],\n",
|
294 |
+
"}\n",
|
295 |
+
"\n",
|
296 |
+
"# ============================\n",
|
297 |
+
"# πΉ 6. Predict Disease from Symptoms (Fully Fixed)\n",
|
298 |
+
"# ============================\n",
|
299 |
+
"def predict_disease(user_symptoms):\n",
|
300 |
+
" \"\"\"Predicts the disease based on user symptoms using the trained XGBoost model.\"\"\"\n",
|
301 |
+
" input_vector = np.zeros(len(symptom_list))\n",
|
302 |
+
"\n",
|
303 |
+
" for symptom in user_symptoms:\n",
|
304 |
+
" if symptom in symptom_list:\n",
|
305 |
+
" input_vector[symptom_list.index(symptom)] = 1 # One-hot encoding\n",
|
306 |
+
"\n",
|
307 |
+
" input_vector = input_vector.reshape(1, -1) # Reshape for model input\n",
|
308 |
+
"\n",
|
309 |
+
" # Predict disease (returns a numerical class)\n",
|
310 |
+
" predicted_class = model.predict(input_vector)[0]\n",
|
311 |
+
"\n",
|
312 |
+
" # Convert number to disease name\n",
|
313 |
+
" predicted_disease = label_encoder.inverse_transform([predicted_class])[0]\n",
|
314 |
+
"\n",
|
315 |
+
" return predicted_disease\n",
|
316 |
+
"\n",
|
317 |
+
"# ============================\n",
|
318 |
+
"# πΉ 7. Get Precautions for a Disease\n",
|
319 |
+
"# ============================\n",
|
320 |
+
"def get_precautions(disease):\n",
|
321 |
+
" \"\"\"Returns the precautions for a given disease.\"\"\"\n",
|
322 |
+
" return precaution_dict.get(disease.lower(), [\"No precautions available\"])\n",
|
323 |
+
"\n",
|
324 |
+
"# ============================\n",
|
325 |
+
"# πΉ 8. Answer Medical Questions (Q&A Model)\n",
|
326 |
+
"# ============================\n",
|
327 |
+
"def get_medical_answer(question):\n",
|
328 |
+
" \"\"\"Uses the pre-trained Q&A model to answer general medical questions.\"\"\"\n",
|
329 |
+
" inputs = tokenizer(question, medical_context, return_tensors=\"pt\", truncation=True, max_length=512)\n",
|
330 |
+
" with torch.no_grad():\n",
|
331 |
+
" outputs = qa_model(**inputs)\n",
|
332 |
+
"\n",
|
333 |
+
" answer_start = torch.argmax(outputs.start_logits)\n",
|
334 |
+
" answer_end = torch.argmax(outputs.end_logits) + 1\n",
|
335 |
+
"\n",
|
336 |
+
" answer = tokenizer.convert_tokens_to_string(\n",
|
337 |
+
" tokenizer.convert_ids_to_tokens(inputs[\"input_ids\"][0][answer_start:answer_end])\n",
|
338 |
+
" )\n",
|
339 |
+
"\n",
|
340 |
+
" return answer if answer.strip() and answer != \"[CLS]\" else \"No reliable answer found.\"\n",
|
341 |
+
"\n",
|
342 |
+
"# ============================\n",
|
343 |
+
"# πΉ 9. Book a Doctor's Appointment\n",
|
344 |
+
"# ============================\n",
|
345 |
+
"def book_appointment(disease):\n",
|
346 |
+
" \"\"\"Finds a doctor for the given disease and returns appointment details.\"\"\"\n",
|
347 |
+
" disease = disease.lower().strip()\n",
|
348 |
+
" doctors = doctor_database.get(disease, [])\n",
|
349 |
+
" if not doctors:\n",
|
350 |
+
" return f\"Sorry, no available doctors found for {disease}.\"\n",
|
351 |
+
"\n",
|
352 |
+
" doctor = doctors[0]\n",
|
353 |
+
" return f\"Appointment booked with **{doctor['name']}** ({doctor['specialty']}) at **{doctor['location']}**.\\nContact: {doctor['contact']}\"\n",
|
354 |
+
"\n",
|
355 |
+
"# ============================\n",
|
356 |
+
"# πΉ 10. Handle User Queries\n",
|
357 |
+
"# ============================\n",
|
358 |
+
"def extract_treatment_from_context(disease):\n",
|
359 |
+
" \"\"\"Extracts treatment details for a given disease from `medical_context.txt`.\"\"\"\n",
|
360 |
+
" with open(\"medical_context.txt\", \"r\", encoding=\"utf-8\") as file:\n",
|
361 |
+
" lines = file.readlines()\n",
|
362 |
+
"\n",
|
363 |
+
" treatment_section = []\n",
|
364 |
+
" found_disease = False\n",
|
365 |
+
" found_treatment = False\n",
|
366 |
+
"\n",
|
367 |
+
" for line in lines:\n",
|
368 |
+
" line = line.strip()\n",
|
369 |
+
"\n",
|
370 |
+
" # Check if we found the disease name\n",
|
371 |
+
" if f\"## {disease.lower()}\" in line.lower():\n",
|
372 |
+
" found_disease = True\n",
|
373 |
+
"\n",
|
374 |
+
" # If we found the disease, now look for \"Treatment\"\n",
|
375 |
+
" if found_disease and \"**Treatment**\" in line:\n",
|
376 |
+
" found_treatment = True\n",
|
377 |
+
" continue # Skip the \"**Treatment**:\" line itself\n",
|
378 |
+
"\n",
|
379 |
+
" # If found, keep extracting treatment details\n",
|
380 |
+
" if found_treatment:\n",
|
381 |
+
" # Stop at blank line or the next section (## New Disease Name)\n",
|
382 |
+
" if line == \"\" or line.startswith(\"## \"):\n",
|
383 |
+
" break\n",
|
384 |
+
" treatment_section.append(line)\n",
|
385 |
+
"\n",
|
386 |
+
" return \"\\n\".join(treatment_section) if treatment_section else None\n",
|
387 |
+
"\n",
|
388 |
+
"\n",
|
389 |
+
"def extract_disease_name(user_query):\n",
|
390 |
+
" \"\"\"Extracts the disease name by removing unnecessary words, but keeps medical terms.\"\"\"\n",
|
391 |
+
" user_query_cleaned = re.sub(r\"[^\\w\\s]\", \"\", user_query.lower()) # Remove punctuation\n",
|
392 |
+
" words = word_tokenize(user_query_cleaned)\n",
|
393 |
+
"\n",
|
394 |
+
" # Remove stopwords but keep diseases/symptoms\n",
|
395 |
+
" filtered_words = [word for word in words if word not in stop_words or word in common_symptoms]\n",
|
396 |
+
"\n",
|
397 |
+
" return \" \".join(filtered_words).strip()\n",
|
398 |
+
"\n",
|
399 |
+
"def find_best_match(query, database):\n",
|
400 |
+
" \"\"\"Finds the best matching disease from the database based on query words.\"\"\"\n",
|
401 |
+
" query_words = query.split() # Split query into words\n",
|
402 |
+
"\n",
|
403 |
+
" # Check for exact match first\n",
|
404 |
+
" if query in database:\n",
|
405 |
+
" return query # Exact match found\n",
|
406 |
+
"\n",
|
407 |
+
" # Check if any word in query exists in database keys\n",
|
408 |
+
" for disease in database:\n",
|
409 |
+
" for word in query_words:\n",
|
410 |
+
" if word in disease: # Partial match found\n",
|
411 |
+
" return disease\n",
|
412 |
+
"\n",
|
413 |
+
" return None # No match found\n",
|
414 |
+
"\n",
|
415 |
+
"\n",
|
416 |
+
"def handle_user_query(user_query):\n",
|
417 |
+
" \"\"\"Handles user queries related to symptoms, diseases, and doctor appointments.\"\"\"\n",
|
418 |
+
"\n",
|
419 |
+
" user_query = user_query.lower().strip()\n",
|
420 |
+
"\n",
|
421 |
+
" # Skip Cleaning for \"I have...\" and \"experiencing...\" Cases\n",
|
422 |
+
" if \"i have\" in user_query or \"experiencing\" in user_query:\n",
|
423 |
+
" symptoms = user_query.replace(\"I have\", \"\").replace(\"experiencing\", \"\").strip()\n",
|
424 |
+
" disease = predict_disease(symptoms.split(\", \")) # Convert to list\n",
|
425 |
+
" precautions = get_precautions(disease)\n",
|
426 |
+
" return f\"**Predicted Disease:** {disease}\\n**Precautions:** {', '.join(precautions)}\\n{book_appointment(disease)}\"\n",
|
427 |
+
"\n",
|
428 |
+
" # Extract Disease Name for Queries\n",
|
429 |
+
" user_query_cleaned = extract_disease_name(user_query)\n",
|
430 |
+
"\n",
|
431 |
+
" # Handle \"Who should I see for...\" Queries (Improved with Partial Matching)\n",
|
432 |
+
" if \"who should i see \" in user_query:\n",
|
433 |
+
" disease_query = user_query.replace(\"who should i see\", \"\").strip()\n",
|
434 |
+
" disease = find_best_match(disease_query, doctor_database) # Get best match\n",
|
435 |
+
"\n",
|
436 |
+
" if disease:\n",
|
437 |
+
" doctor = doctor_database[disease][0]\n",
|
438 |
+
" return f\"You should see a **{doctor['specialty']}** for {disease}.\\nExample: {doctor['name']} at {doctor['location']}.\"\n",
|
439 |
+
" else:\n",
|
440 |
+
" return \"I'm not sure. Please consult a general physician for more guidance.\"\n",
|
441 |
+
"\n",
|
442 |
+
" # Book Appointment (Improved with Partial Matching)\n",
|
443 |
+
" elif \"book appointment\" in user_query_cleaned:\n",
|
444 |
+
" disease_query = user_query_cleaned.replace(\"book appointment\", \"\").strip()\n",
|
445 |
+
" disease = find_best_match(disease_query, doctor_database)\n",
|
446 |
+
" return book_appointment(disease) if disease else \"Sorry, no matching doctor found.\"\n",
|
447 |
+
"\n",
|
448 |
+
" # Symptoms Query\n",
|
449 |
+
" elif \"symptoms\" in user_query_cleaned or \"signs\" in user_query_cleaned:\n",
|
450 |
+
" disease = user_query_cleaned.replace(\"symptoms\", \"\").replace(\"signs\", \"\").strip()\n",
|
451 |
+
" return get_medical_answer(f\"What are the symptoms of {disease}?\")\n",
|
452 |
+
"\n",
|
453 |
+
" # Precautions Query\n",
|
454 |
+
" elif \"precautions\" in user_query_cleaned or \"prevent\" in user_query_cleaned:\n",
|
455 |
+
" disease = user_query_cleaned.replace(\"precautions\", \"\").replace(\"prevent\", \"\").strip()\n",
|
456 |
+
" return \", \".join(get_precautions(disease))\n",
|
457 |
+
"\n",
|
458 |
+
" # Treatment Query\n",
|
459 |
+
" if \"treatment\" in user_query_cleaned or \"treat\" in user_query_cleaned:\n",
|
460 |
+
" disease = user_query_cleaned.replace(\"treatment\", \"\").replace(\"treat\", \"\").strip()\n",
|
461 |
+
"\n",
|
462 |
+
" # πΉ First, try to extract treatment from `medical_context.txt`\n",
|
463 |
+
" treatment_answer = extract_treatment_from_context(disease)\n",
|
464 |
+
" if treatment_answer:\n",
|
465 |
+
" return treatment_answer # Use direct extraction first\n",
|
466 |
+
"\n",
|
467 |
+
" # πΉ If no treatment info found, use the Q&A Model\n",
|
468 |
+
" model_answer = get_medical_answer(f\"What is the treatment for {disease}?\")\n",
|
469 |
+
" if model_answer in [\"<s>\", \"\", \"No reliable answer found.\"]:\n",
|
470 |
+
" return f\"I'm not sure, but common treatments for {disease} include medication, therapy, or consulting a specialist.\"\n",
|
471 |
+
" return model_answer\n",
|
472 |
+
"\n",
|
473 |
+
" # General Medical Questions (Fallback)\n",
|
474 |
+
" else:\n",
|
475 |
+
" response = get_medical_answer(user_query)\n",
|
476 |
+
" if response in [\"<s>\", \"\", \"No reliable answer found.\"]:\n",
|
477 |
+
" return \"I'm not sure, but you may consult a specialist for better guidance.\"\n",
|
478 |
+
" return response\n",
|
479 |
+
"\n",
|
480 |
+
"# ============================\n",
|
481 |
+
"# πΉ 11. Test Cases (Run Examples)\n",
|
482 |
+
"# ============================\n",
|
483 |
+
"print(handle_user_query(\"I have fever, chills, and muscle aches\")) # Should predict disease & precautions\n",
|
484 |
+
"print(handle_user_query(\"What are the symptoms of pneumonia?\")) # Should return pneumonia symptoms\n",
|
485 |
+
"print(handle_user_query(\"Book an appointment for diabetes\")) # Should book a diabetes specialist\n",
|
486 |
+
"print(handle_user_query(\"Who should I see for heart attack\")) # Should return \"Cardiologist\"\n",
|
487 |
+
"print(handle_user_query(\"what is the treatment for tuberculosis\")) # Should return correct treatment\n"
|
488 |
+
],
|
489 |
+
"metadata": {
|
490 |
+
"colab": {
|
491 |
+
"base_uri": "https://localhost:8080/"
|
492 |
+
},
|
493 |
+
"id": "ccAK0hD9WcZy",
|
494 |
+
"outputId": "d36ea7da-1ca1-4de0-e39a-9ad35a0a0b31"
|
495 |
+
},
|
496 |
+
"execution_count": 110,
|
497 |
+
"outputs": [
|
498 |
+
{
|
499 |
+
"output_type": "stream",
|
500 |
+
"name": "stdout",
|
501 |
+
"text": [
|
502 |
+
"**Predicted Disease:** Hepatitis E\n",
|
503 |
+
"**Precautions:** stop alcohol consumption, rest, consult doctor, medication\n",
|
504 |
+
"Appointment booked with **Dr. Sunil Agarwal** (Hepatologist) at **Fortis Hospital**.\n",
|
505 |
+
"Contact: 9876543266\n",
|
506 |
+
" Fever, cough, chest pain, difficulty breathing\n",
|
507 |
+
"Appointment booked with **Dr. Anil Mehta** (Endocrinologist) at **AIIMS Delhi**.\n",
|
508 |
+
"Contact: 9876543233\n",
|
509 |
+
"You should see a **Cardiologist** for heart attack.\n",
|
510 |
+
"Example: Dr. Vikram Singh at Medanta Hospital.\n",
|
511 |
+
"- **Doctor to consult**: Infectious Disease Specialist\n"
|
512 |
+
]
|
513 |
+
}
|
514 |
+
]
|
515 |
+
},
|
516 |
+
{
|
517 |
+
"cell_type": "code",
|
518 |
+
"source": [
|
519 |
+
"print(handle_user_query(\"What is the treatment for tuberculosis?\")) # Should return correct treatment\n",
|
520 |
+
"print(handle_user_query(\"What is the treatment for malaria?\")) # Should also work\n",
|
521 |
+
"print(handle_user_query(\"What is the treatment for cancer?\")) # Should return something useful"
|
522 |
+
],
|
523 |
+
"metadata": {
|
524 |
+
"colab": {
|
525 |
+
"base_uri": "https://localhost:8080/"
|
526 |
+
},
|
527 |
+
"id": "ybyh64xuq1ih",
|
528 |
+
"outputId": "4bc9e030-8a3a-469d-d5b3-9d0e8aec6367"
|
529 |
+
},
|
530 |
+
"execution_count": 111,
|
531 |
+
"outputs": [
|
532 |
+
{
|
533 |
+
"output_type": "stream",
|
534 |
+
"name": "stdout",
|
535 |
+
"text": [
|
536 |
+
"- **Doctor to consult**: Infectious Disease Specialist\n",
|
537 |
+
"I'm not sure, but common treatments for malaria include medication, therapy, or consulting a specialist.\n",
|
538 |
+
"- **Doctor to consult**: Oncologist\n"
|
539 |
+
]
|
540 |
+
}
|
541 |
+
]
|
542 |
+
},
|
543 |
+
{
|
544 |
+
"cell_type": "code",
|
545 |
+
"source": [
|
546 |
+
"print(handle_user_query(\"What is the treatment for tuberculosis?\")) # Should return correct treatment\n",
|
547 |
+
"print(handle_user_query(\"What is the treatment for malaria?\")) # Should also work\n",
|
548 |
+
"print(handle_user_query(\"What is the treatment for cancer?\")) # Should return something useful\n",
|
549 |
+
"print(handle_user_query(\"How to treat diabetes?\")) # Should return proper treatment\n",
|
550 |
+
"print(handle_user_query(\"Tell me the cure for pneumonia?\")) # Should return treatment\n",
|
551 |
+
"print(handle_user_query(\"Treatment for typhoid?\")) # Should extract treatment\n"
|
552 |
+
],
|
553 |
+
"metadata": {
|
554 |
+
"colab": {
|
555 |
+
"base_uri": "https://localhost:8080/"
|
556 |
+
},
|
557 |
+
"id": "6DQ8XH11gwGZ",
|
558 |
+
"outputId": "5a8cbacc-05d5-4c9e-cb4c-6b93666971b2"
|
559 |
+
},
|
560 |
+
"execution_count": 112,
|
561 |
+
"outputs": [
|
562 |
+
{
|
563 |
+
"output_type": "stream",
|
564 |
+
"name": "stdout",
|
565 |
+
"text": [
|
566 |
+
"- **Doctor to consult**: Infectious Disease Specialist\n",
|
567 |
+
"I'm not sure, but common treatments for malaria include medication, therapy, or consulting a specialist.\n",
|
568 |
+
"- **Doctor to consult**: Oncologist\n",
|
569 |
+
"I'm not sure, but common treatments for diabetes include medication, therapy, or consulting a specialist.\n",
|
570 |
+
"I'm not sure, but you may consult a specialist for better guidance.\n",
|
571 |
+
"I'm not sure, but common treatments for typhoid include medication, therapy, or consulting a specialist.\n"
|
572 |
+
]
|
573 |
+
}
|
574 |
+
]
|
575 |
+
},
|
576 |
+
{
|
577 |
+
"cell_type": "code",
|
578 |
+
"source": [
|
579 |
+
"print(handle_user_query(\"What are the symptoms of pneumonia?\")) # Should return correct symptoms\n",
|
580 |
+
"print(handle_user_query(\"Signs of heart attack?\")) # Should return expected symptoms\n",
|
581 |
+
"print(handle_user_query(\"How do I know if I have typhoid?\")) # Should return typhoid symptoms\n",
|
582 |
+
"print(handle_user_query(\"What symptoms should I check for tuberculosis?\")) # Should work\n",
|
583 |
+
"print(handle_user_query(\"Symptoms of dengue?\")) # Should return symptoms of dengue\n"
|
584 |
+
],
|
585 |
+
"metadata": {
|
586 |
+
"colab": {
|
587 |
+
"base_uri": "https://localhost:8080/"
|
588 |
+
},
|
589 |
+
"id": "Oq1xVi93u0nN",
|
590 |
+
"outputId": "9fd0433b-aad4-4246-bb3d-5cc8c23995e1"
|
591 |
+
},
|
592 |
+
"execution_count": 113,
|
593 |
+
"outputs": [
|
594 |
+
{
|
595 |
+
"output_type": "stream",
|
596 |
+
"name": "stdout",
|
597 |
+
"text": [
|
598 |
+
" Fever, cough, chest pain, difficulty breathing\n",
|
599 |
+
" Chest pain, shortness of breath, nausea, pain in the left arm\n",
|
600 |
+
"**Predicted Disease:** Hepatitis E\n",
|
601 |
+
"**Precautions:** stop alcohol consumption, rest, consult doctor, medication\n",
|
602 |
+
"Appointment booked with **Dr. Sunil Agarwal** (Hepatologist) at **Fortis Hospital**.\n",
|
603 |
+
"Contact: 9876543266\n",
|
604 |
+
"<s>\n",
|
605 |
+
"<s>\n"
|
606 |
+
]
|
607 |
+
}
|
608 |
+
]
|
609 |
+
},
|
610 |
+
{
|
611 |
+
"cell_type": "code",
|
612 |
+
"source": [
|
613 |
+
"print(handle_user_query(\"Who should I see for a heart attack?\")) # Should return \"Cardiologist\"\n",
|
614 |
+
"print(handle_user_query(\"Which doctor should I visit for diabetes?\")) # Should return \"Endocrinologist\"\n",
|
615 |
+
"print(handle_user_query(\"Who should I consult for a skin rash?\")) # Should return \"Dermatologist\"\n",
|
616 |
+
"print(handle_user_query(\"What kind of doctor treats pneumonia?\")) # Should return \"Pulmonologist\"\n",
|
617 |
+
"print(handle_user_query(\"Who specializes in treating migraines?\")) # Should return \"Neurologist\"\n"
|
618 |
+
],
|
619 |
+
"metadata": {
|
620 |
+
"colab": {
|
621 |
+
"base_uri": "https://localhost:8080/"
|
622 |
+
},
|
623 |
+
"id": "8IyTjf6Ku1r2",
|
624 |
+
"outputId": "4776d477-59fc-4d0a-8bd6-c6c97c2fdaf7"
|
625 |
+
},
|
626 |
+
"execution_count": 114,
|
627 |
+
"outputs": [
|
628 |
+
{
|
629 |
+
"output_type": "stream",
|
630 |
+
"name": "stdout",
|
631 |
+
"text": [
|
632 |
+
"You should see a **Infectious Diseases** for malaria.\n",
|
633 |
+
"Example: Dr. Rajesh Kumar at Apollo Hospital.\n",
|
634 |
+
"I'm not sure, but you may consult a specialist for better guidance.\n",
|
635 |
+
"I'm not sure, but you may consult a specialist for better guidance.\n",
|
636 |
+
"I'm not sure, but common treatments for kind doctor s pneumonia include medication, therapy, or consulting a specialist.\n",
|
637 |
+
"I'm not sure, but common treatments for specializes ing migraines include medication, therapy, or consulting a specialist.\n"
|
638 |
+
]
|
639 |
+
}
|
640 |
+
]
|
641 |
+
},
|
642 |
+
{
|
643 |
+
"cell_type": "code",
|
644 |
+
"source": [
|
645 |
+
"print(handle_user_query(\"Book an appointment for malaria\")) # Should book doctor for malaria\n",
|
646 |
+
"print(handle_user_query(\"I need a doctor for high blood pressure\")) # Should book doctor for hypertension\n",
|
647 |
+
"print(handle_user_query(\"Schedule a consultation for fever\")) # Should book general physician\n",
|
648 |
+
"print(handle_user_query(\"Find a doctor for diabetes treatment\")) # Should book endocrinologist\n",
|
649 |
+
"print(handle_user_query(\"Book an appointment for pneumonia treatment\")) # Should book pulmonologist\n"
|
650 |
+
],
|
651 |
+
"metadata": {
|
652 |
+
"colab": {
|
653 |
+
"base_uri": "https://localhost:8080/"
|
654 |
+
},
|
655 |
+
"id": "RgbA5722u4Bk",
|
656 |
+
"outputId": "ea5b0b8d-6194-49c2-8e6b-311c06d0cad2"
|
657 |
+
},
|
658 |
+
"execution_count": 115,
|
659 |
+
"outputs": [
|
660 |
+
{
|
661 |
+
"output_type": "stream",
|
662 |
+
"name": "stdout",
|
663 |
+
"text": [
|
664 |
+
"Appointment booked with **Dr. Rajesh Kumar** (Infectious Diseases) at **Apollo Hospital**.\n",
|
665 |
+
"Contact: 9876543210\n",
|
666 |
+
"I'm not sure, but you may consult a specialist for better guidance.\n",
|
667 |
+
"I'm not sure, but you may consult a specialist for better guidance.\n",
|
668 |
+
"I'm not sure, but common treatments for find doctor diabetes include medication, therapy, or consulting a specialist.\n",
|
669 |
+
"Appointment booked with **Dr. Priya Sharma** (Pulmonologist) at **Max Healthcare**.\n",
|
670 |
+
"Contact: 9876543277\n"
|
671 |
+
]
|
672 |
+
}
|
673 |
+
]
|
674 |
+
},
|
675 |
+
{
|
676 |
+
"cell_type": "code",
|
677 |
+
"source": [
|
678 |
+
"print(handle_user_query(\"I have fever, cough, and chills\")) # Should predict disease correctly\n",
|
679 |
+
"print(handle_user_query(\"Experiencing blurry vision and excessive thirst\")) # Should return \"Diabetes\"\n",
|
680 |
+
"print(handle_user_query(\"I am experiencing severe chest pain and difficulty breathing\")) # Should return \"Heart Attack\"\n",
|
681 |
+
"print(handle_user_query(\"Feeling tired, cold, and gaining weight\")) # Should return \"Hypothyroidism\"\n",
|
682 |
+
"print(handle_user_query(\"I have rash, joint pain, and headache\")) # Should return \"Dengue\"\n"
|
683 |
+
],
|
684 |
+
"metadata": {
|
685 |
+
"colab": {
|
686 |
+
"base_uri": "https://localhost:8080/"
|
687 |
+
},
|
688 |
+
"id": "9zhwGv6gu5yc",
|
689 |
+
"outputId": "17cff133-5c44-417a-bc37-4858a72084fd"
|
690 |
+
},
|
691 |
+
"execution_count": 116,
|
692 |
+
"outputs": [
|
693 |
+
{
|
694 |
+
"output_type": "stream",
|
695 |
+
"name": "stdout",
|
696 |
+
"text": [
|
697 |
+
"**Predicted Disease:** Hepatitis E\n",
|
698 |
+
"**Precautions:** stop alcohol consumption, rest, consult doctor, medication\n",
|
699 |
+
"Appointment booked with **Dr. Sunil Agarwal** (Hepatologist) at **Fortis Hospital**.\n",
|
700 |
+
"Contact: 9876543266\n",
|
701 |
+
"**Predicted Disease:** Hepatitis E\n",
|
702 |
+
"**Precautions:** stop alcohol consumption, rest, consult doctor, medication\n",
|
703 |
+
"Appointment booked with **Dr. Sunil Agarwal** (Hepatologist) at **Fortis Hospital**.\n",
|
704 |
+
"Contact: 9876543266\n",
|
705 |
+
"**Predicted Disease:** Hepatitis E\n",
|
706 |
+
"**Precautions:** stop alcohol consumption, rest, consult doctor, medication\n",
|
707 |
+
"Appointment booked with **Dr. Sunil Agarwal** (Hepatologist) at **Fortis Hospital**.\n",
|
708 |
+
"Contact: 9876543266\n",
|
709 |
+
"I'm not sure, but you may consult a specialist for better guidance.\n",
|
710 |
+
"**Predicted Disease:** Hepatitis E\n",
|
711 |
+
"**Precautions:** stop alcohol consumption, rest, consult doctor, medication\n",
|
712 |
+
"Appointment booked with **Dr. Sunil Agarwal** (Hepatologist) at **Fortis Hospital**.\n",
|
713 |
+
"Contact: 9876543266\n"
|
714 |
+
]
|
715 |
+
}
|
716 |
+
]
|
717 |
+
},
|
718 |
+
{
|
719 |
+
"cell_type": "code",
|
720 |
+
"source": [
|
721 |
+
"print(handle_user_query(\"What does a doctor do?\")) # Should return general doctor description\n",
|
722 |
+
"print(handle_user_query(\"What are antibiotics?\")) # Should explain antibiotics\n",
|
723 |
+
"print(handle_user_query(\"How does the immune system work?\")) # Should explain immunity\n",
|
724 |
+
"print(handle_user_query(\"What is the function of the liver?\")) # Should explain liver function\n",
|
725 |
+
"print(handle_user_query(\"Explain how blood pressure works?\")) # Should provide useful explanation\n"
|
726 |
+
],
|
727 |
+
"metadata": {
|
728 |
+
"colab": {
|
729 |
+
"base_uri": "https://localhost:8080/"
|
730 |
+
},
|
731 |
+
"id": "f1_Tt8y3u8wB",
|
732 |
+
"outputId": "6b1104ee-32cb-42fd-95ed-894824a49e33"
|
733 |
+
},
|
734 |
+
"execution_count": 118,
|
735 |
+
"outputs": [
|
736 |
+
{
|
737 |
+
"output_type": "stream",
|
738 |
+
"name": "stdout",
|
739 |
+
"text": [
|
740 |
+
"I'm not sure, but you may consult a specialist for better guidance.\n",
|
741 |
+
"bacterial pneumonia\n",
|
742 |
+
"I'm not sure, but you may consult a specialist for better guidance.\n",
|
743 |
+
"I'm not sure, but you may consult a specialist for better guidance.\n",
|
744 |
+
"I'm not sure, but you may consult a specialist for better guidance.\n"
|
745 |
+
]
|
746 |
+
}
|
747 |
+
]
|
748 |
+
},
|
749 |
+
{
|
750 |
+
"cell_type": "code",
|
751 |
+
"source": [],
|
752 |
+
"metadata": {
|
753 |
+
"id": "hh7-xCtRu-NH"
|
754 |
+
},
|
755 |
+
"execution_count": null,
|
756 |
+
"outputs": []
|
757 |
+
},
|
758 |
+
{
|
759 |
+
"cell_type": "code",
|
760 |
+
"source": [
|
761 |
+
"!pip install streamlit\n"
|
762 |
+
],
|
763 |
+
"metadata": {
|
764 |
+
"colab": {
|
765 |
+
"base_uri": "https://localhost:8080/"
|
766 |
+
},
|
767 |
+
"id": "lj6ZVPRqwKqG",
|
768 |
+
"outputId": "55936a43-a429-4630-bbac-60fa63fa399a"
|
769 |
+
},
|
770 |
+
"execution_count": 128,
|
771 |
+
"outputs": [
|
772 |
+
{
|
773 |
+
"output_type": "stream",
|
774 |
+
"name": "stdout",
|
775 |
+
"text": [
|
776 |
+
"Collecting streamlit\n",
|
777 |
+
" Downloading streamlit-1.43.1-py2.py3-none-any.whl.metadata (8.9 kB)\n",
|
778 |
+
"Requirement already satisfied: altair<6,>=4.0 in /usr/local/lib/python3.11/dist-packages (from streamlit) (5.5.0)\n",
|
779 |
+
"Requirement already satisfied: blinker<2,>=1.0.0 in /usr/local/lib/python3.11/dist-packages (from streamlit) (1.9.0)\n",
|
780 |
+
"Requirement already satisfied: cachetools<6,>=4.0 in /usr/local/lib/python3.11/dist-packages (from streamlit) (5.5.2)\n",
|
781 |
+
"Requirement already satisfied: click<9,>=7.0 in /usr/local/lib/python3.11/dist-packages (from streamlit) (8.1.8)\n",
|
782 |
+
"Requirement already satisfied: numpy<3,>=1.23 in /usr/local/lib/python3.11/dist-packages (from streamlit) (1.26.4)\n",
|
783 |
+
"Requirement already satisfied: packaging<25,>=20 in /usr/local/lib/python3.11/dist-packages (from streamlit) (24.2)\n",
|
784 |
+
"Requirement already satisfied: pandas<3,>=1.4.0 in /usr/local/lib/python3.11/dist-packages (from streamlit) (2.2.2)\n",
|
785 |
+
"Requirement already satisfied: pillow<12,>=7.1.0 in /usr/local/lib/python3.11/dist-packages (from streamlit) (11.1.0)\n",
|
786 |
+
"Requirement already satisfied: protobuf<6,>=3.20 in /usr/local/lib/python3.11/dist-packages (from streamlit) (4.25.6)\n",
|
787 |
+
"Requirement already satisfied: pyarrow>=7.0 in /usr/local/lib/python3.11/dist-packages (from streamlit) (18.1.0)\n",
|
788 |
+
"Requirement already satisfied: requests<3,>=2.27 in /usr/local/lib/python3.11/dist-packages (from streamlit) (2.32.3)\n",
|
789 |
+
"Requirement already satisfied: tenacity<10,>=8.1.0 in /usr/local/lib/python3.11/dist-packages (from streamlit) (9.0.0)\n",
|
790 |
+
"Requirement already satisfied: toml<2,>=0.10.1 in /usr/local/lib/python3.11/dist-packages (from streamlit) (0.10.2)\n",
|
791 |
+
"Requirement already satisfied: typing-extensions<5,>=4.4.0 in /usr/local/lib/python3.11/dist-packages (from streamlit) (4.12.2)\n",
|
792 |
+
"Collecting watchdog<7,>=2.1.5 (from streamlit)\n",
|
793 |
+
" Downloading watchdog-6.0.0-py3-none-manylinux2014_x86_64.whl.metadata (44 kB)\n",
|
794 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m44.3/44.3 kB\u001b[0m \u001b[31m2.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
795 |
+
"\u001b[?25hRequirement already satisfied: gitpython!=3.1.19,<4,>=3.0.7 in /usr/local/lib/python3.11/dist-packages (from streamlit) (3.1.44)\n",
|
796 |
+
"Collecting pydeck<1,>=0.8.0b4 (from streamlit)\n",
|
797 |
+
" Downloading pydeck-0.9.1-py2.py3-none-any.whl.metadata (4.1 kB)\n",
|
798 |
+
"Requirement already satisfied: tornado<7,>=6.0.3 in /usr/local/lib/python3.11/dist-packages (from streamlit) (6.4.2)\n",
|
799 |
+
"Requirement already satisfied: jinja2 in /usr/local/lib/python3.11/dist-packages (from altair<6,>=4.0->streamlit) (3.1.5)\n",
|
800 |
+
"Requirement already satisfied: jsonschema>=3.0 in /usr/local/lib/python3.11/dist-packages (from altair<6,>=4.0->streamlit) (4.23.0)\n",
|
801 |
+
"Requirement already satisfied: narwhals>=1.14.2 in /usr/local/lib/python3.11/dist-packages (from altair<6,>=4.0->streamlit) (1.29.0)\n",
|
802 |
+
"Requirement already satisfied: gitdb<5,>=4.0.1 in /usr/local/lib/python3.11/dist-packages (from gitpython!=3.1.19,<4,>=3.0.7->streamlit) (4.0.12)\n",
|
803 |
+
"Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.11/dist-packages (from pandas<3,>=1.4.0->streamlit) (2.8.2)\n",
|
804 |
+
"Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.11/dist-packages (from pandas<3,>=1.4.0->streamlit) (2025.1)\n",
|
805 |
+
"Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.11/dist-packages (from pandas<3,>=1.4.0->streamlit) (2025.1)\n",
|
806 |
+
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2.27->streamlit) (3.4.1)\n",
|
807 |
+
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2.27->streamlit) (3.10)\n",
|
808 |
+
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2.27->streamlit) (2.3.0)\n",
|
809 |
+
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2.27->streamlit) (2025.1.31)\n",
|
810 |
+
"Requirement already satisfied: smmap<6,>=3.0.1 in /usr/local/lib/python3.11/dist-packages (from gitdb<5,>=4.0.1->gitpython!=3.1.19,<4,>=3.0.7->streamlit) (5.0.2)\n",
|
811 |
+
"Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.11/dist-packages (from jinja2->altair<6,>=4.0->streamlit) (3.0.2)\n",
|
812 |
+
"Requirement already satisfied: attrs>=22.2.0 in /usr/local/lib/python3.11/dist-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit) (25.1.0)\n",
|
813 |
+
"Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /usr/local/lib/python3.11/dist-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit) (2024.10.1)\n",
|
814 |
+
"Requirement already satisfied: referencing>=0.28.4 in /usr/local/lib/python3.11/dist-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit) (0.36.2)\n",
|
815 |
+
"Requirement already satisfied: rpds-py>=0.7.1 in /usr/local/lib/python3.11/dist-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit) (0.23.1)\n",
|
816 |
+
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.11/dist-packages (from python-dateutil>=2.8.2->pandas<3,>=1.4.0->streamlit) (1.17.0)\n",
|
817 |
+
"Downloading streamlit-1.43.1-py2.py3-none-any.whl (9.7 MB)\n",
|
818 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m9.7/9.7 MB\u001b[0m \u001b[31m50.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
819 |
+
"\u001b[?25hDownloading pydeck-0.9.1-py2.py3-none-any.whl (6.9 MB)\n",
|
820 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m6.9/6.9 MB\u001b[0m \u001b[31m57.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
821 |
+
"\u001b[?25hDownloading watchdog-6.0.0-py3-none-manylinux2014_x86_64.whl (79 kB)\n",
|
822 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m79.1/79.1 kB\u001b[0m \u001b[31m5.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
823 |
+
"\u001b[?25hInstalling collected packages: watchdog, pydeck, streamlit\n",
|
824 |
+
"Successfully installed pydeck-0.9.1 streamlit-1.43.1 watchdog-6.0.0\n"
|
825 |
+
]
|
826 |
+
}
|
827 |
+
]
|
828 |
+
},
|
829 |
+
{
|
830 |
+
"cell_type": "code",
|
831 |
+
"source": [
|
832 |
+
"%%writefile app.py\n",
|
833 |
+
"import streamlit as st\n",
|
834 |
+
"import requests\n",
|
835 |
+
"\n",
|
836 |
+
"st.set_page_config(page_title=\"AI Health Assistant\", page_icon=\"π€\")\n",
|
837 |
+
"\n",
|
838 |
+
"st.title(\"π©Ί AI Health Assistant\")\n",
|
839 |
+
"st.write(\"Ask any medical-related questions:\")\n",
|
840 |
+
"\n",
|
841 |
+
"# User Input\n",
|
842 |
+
"user_input = st.text_input(\"Your Question:\")\n",
|
843 |
+
"\n",
|
844 |
+
"# Button to Send Query\n",
|
845 |
+
"if st.button(\"Ask\"):\n",
|
846 |
+
" response = requests.post(\"https://b7da-35-232-247-117.ngrok-free.app/query/\", json={\"user_input\": user_input})\n",
|
847 |
+
" bot_response = response.json().get(\"response\", \"Error fetching response\")\n",
|
848 |
+
"\n",
|
849 |
+
" st.markdown(f\"**π€ Bot:** {bot_response}\")\n"
|
850 |
+
],
|
851 |
+
"metadata": {
|
852 |
+
"colab": {
|
853 |
+
"base_uri": "https://localhost:8080/"
|
854 |
+
},
|
855 |
+
"id": "EDgP-RoV1hxA",
|
856 |
+
"outputId": "57a5b4a3-846a-46fc-eb8b-c1c240132284"
|
857 |
+
},
|
858 |
+
"execution_count": 132,
|
859 |
+
"outputs": [
|
860 |
+
{
|
861 |
+
"output_type": "stream",
|
862 |
+
"name": "stdout",
|
863 |
+
"text": [
|
864 |
+
"Writing app.py\n"
|
865 |
+
]
|
866 |
+
}
|
867 |
+
]
|
868 |
+
},
|
869 |
+
{
|
870 |
+
"cell_type": "markdown",
|
871 |
+
"source": [
|
872 |
+
"β
Steps to Deploy on Hugging Face Spaces\n",
|
873 |
+
"π Step 1: Create a Hugging Face Space\n",
|
874 |
+
"1οΈβ£ Go to Hugging Face Spaces\n",
|
875 |
+
"2οΈβ£ Click \"New Space\"\n",
|
876 |
+
"3οΈβ£ Name the Space (e.g., AI-Health-Assistant)\n",
|
877 |
+
"4οΈβ£ Select \"Streamlit\" as the SDK\n",
|
878 |
+
"5οΈβ£ Click \"Create Space\" β
\n",
|
879 |
+
"\n",
|
880 |
+
"π Step 2: Clone the Repository Locally\n",
|
881 |
+
"After creating the Space, clone it to your local machine or Google Colab:\n",
|
882 |
+
"\n",
|
883 |
+
"```bash\n",
|
884 |
+
"git clone https://huggingface.co/spaces/YOUR_USERNAME/AI-Health-Assistant\n",
|
885 |
+
"cd AI-Health-Assistant\n",
|
886 |
+
"```\n",
|
887 |
+
"Replace YOUR_USERNAME with your Hugging Face username!\n",
|
888 |
+
"\n",
|
889 |
+
"π Step 3: Add app.py (Your Streamlit Chatbot)\n",
|
890 |
+
"Inside the cloned folder, create app.py and paste the following:\n",
|
891 |
+
"\n",
|
892 |
+
"π Step 4: Create requirements.txt\n",
|
893 |
+
"Create a new file requirements.txt inside the same folder and add:\n",
|
894 |
+
"```bash\n",
|
895 |
+
"streamlit\n",
|
896 |
+
"requests\n",
|
897 |
+
"```\n",
|
898 |
+
"\n",
|
899 |
+
"π Step 5: Push Your Code to Hugging Face\n",
|
900 |
+
"Run these commands to push the code:\n",
|
901 |
+
"\n",
|
902 |
+
"```bash\n",
|
903 |
+
"git add .\n",
|
904 |
+
"git commit -m \"Initial commit\"\n",
|
905 |
+
"git push\n",
|
906 |
+
"```\n",
|
907 |
+
"π Your Space will automatically start building!"
|
908 |
+
],
|
909 |
+
"metadata": {
|
910 |
+
"id": "4RG98k__4Yg1"
|
911 |
+
}
|
912 |
+
},
|
913 |
+
{
|
914 |
+
"cell_type": "code",
|
915 |
+
"source": [],
|
916 |
+
"metadata": {
|
917 |
+
"id": "Qem1BlA346Ke"
|
918 |
+
},
|
919 |
+
"execution_count": null,
|
920 |
+
"outputs": []
|
921 |
+
}
|
922 |
+
]
|
923 |
+
}
|