noobmaster1246 commited on
Commit
f188b5f
·
verified ·
1 Parent(s): 92c1ab5

Upload 7 files

Browse files
Files changed (8) hide show
  1. .gitattributes +1 -0
  2. Dockerfile +13 -0
  3. ai.py +120 -0
  4. app.py +41 -0
  5. data.csv +3 -0
  6. frequency_dictionary_en_82_765.txt +0 -0
  7. model.pth +3 -0
  8. symptoms.json +379 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ data.csv filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /app
4
+
5
+ RUN pip install torch pandas scikit-learn sentence-transformers transformers symspellpy flask
6
+
7
+ COPY . .
8
+
9
+ ENV PORT=5000
10
+
11
+ EXPOSE ${PORT}
12
+
13
+ CMD ["python", "app.py"]
ai.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import numpy as np
4
+ import pandas as pd
5
+ from sklearn.preprocessing import LabelEncoder, StandardScaler
6
+ from sentence_transformers import SentenceTransformer, util
7
+ import json
8
+ from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
9
+ from symspellpy import SymSpell, Verbosity
10
+
11
+ device = torch.device("cpu")
12
+
13
+ class DiseaseClassifier(nn.Module):
14
+ def __init__(self, input_size, num_classes, dropout_rate=0.35665610394511454):
15
+ super(DiseaseClassifier, self).__init__()
16
+ self.fc1 = nn.Linear(input_size, 382)
17
+ self.fc2 = nn.Linear(382, 389)
18
+ self.fc3 = nn.Linear(389, 433)
19
+ self.fc4 = nn.Linear(433, num_classes)
20
+ self.activation = nn.LeakyReLU()
21
+ self.dropout = nn.Dropout(dropout_rate)
22
+
23
+ def forward(self, x):
24
+ x = self.activation(self.fc1(x))
25
+ x = self.dropout(x)
26
+ x = self.activation(self.fc2(x))
27
+ x = self.dropout(x)
28
+ x = self.activation(self.fc3(x))
29
+ x = self.dropout(x)
30
+ x = self.fc4(x) # Logits
31
+ return x
32
+
33
+
34
+ class DiseasePredictionModel:
35
+ def __init__(self, ai_model_name="model.pth", data_file="data.csv", symptom_json="symptoms.json", dictionary_file="frequency_dictionary_en_82_765.txt"):
36
+ self.df = pd.read_csv(data_file)
37
+ self.symptom_columns = self.load_symptoms(symptom_json)
38
+ self.label_encoder = LabelEncoder()
39
+ self.label_encoder.fit(self.df.iloc[:, 0])
40
+ self.scaler = StandardScaler()
41
+ self.scaler.fit(self.df.iloc[:, 1:].values)
42
+ self.input_size = len(self.symptom_columns)
43
+ self.num_classes = len(self.label_encoder.classes_)
44
+ self.model = self._load_model(ai_model_name)
45
+ self.SYMPTOM_LIST = self.load_symptoms(symptom_json)
46
+ self.sym_spell = SymSpell(max_dictionary_edit_distance=2, prefix_length=7)
47
+ self.sym_spell.load_dictionary(dictionary_file, term_index=0, count_index=1)
48
+ self.tokenizer = AutoTokenizer.from_pretrained("./biobert_diseases_ner")
49
+ # self.tokenizer.save_pretrained("./biobert_diseases_ner")
50
+ self.nlp_model = AutoModelForTokenClassification.from_pretrained("./biobert_diseases_ner")
51
+ # self.nlp_model.save_pretrained("./biobert_diseases_ner")
52
+ self.ner_pipeline = pipeline("ner", model=self.nlp_model, tokenizer=self.tokenizer, aggregation_strategy="simple")
53
+ self.semantic_model = SentenceTransformer('all-MiniLM-L6-v2')
54
+
55
+ def _load_model(self, ai_model_name):
56
+ model = DiseaseClassifier(self.input_size, self.num_classes).to(device)
57
+ model.load_state_dict(torch.load(ai_model_name, map_location=device, weights_only=True))
58
+ model.eval()
59
+ return model
60
+
61
+ def predict_disease(self, symptoms):
62
+ input_vector = np.zeros(len(self.symptom_columns))
63
+ for symptom in symptoms:
64
+ if symptom in self.symptom_columns:
65
+ input_vector[list(self.symptom_columns).index(symptom)] = 1
66
+
67
+ input_vector = self.scaler.transform([input_vector])
68
+
69
+ input_tensor = torch.tensor(input_vector, dtype=torch.float32).to(device)
70
+
71
+ with torch.no_grad():
72
+ outputs = self.model(input_tensor)
73
+ _, predicted_class = torch.max(outputs, 1)
74
+
75
+ predicted_disease = self.label_encoder.inverse_transform([predicted_class.cpu().numpy()[0]])[0]
76
+ return predicted_disease
77
+
78
+ def load_symptoms(self, json_file):
79
+ with open(json_file, "r", encoding="utf-8") as f:
80
+ return json.load(f)
81
+
82
+ def correct_text(self, text):
83
+ words = text.split()
84
+ corrected_words = []
85
+
86
+ for word in words:
87
+ if word.lower() in [symptom.lower() for symptom in self.SYMPTOM_LIST]:
88
+ corrected_words.append(word)
89
+ else:
90
+ suggestions = self.sym_spell.lookup(word, Verbosity.CLOSEST, max_edit_distance=2)
91
+ if suggestions:
92
+ corrected_words.append(suggestions[0].term)
93
+ else:
94
+ corrected_words.append(word)
95
+ return ' '.join(corrected_words)
96
+
97
+ def extract_symptoms(self, text):
98
+ ner_results = self.ner_pipeline(text)
99
+ symptoms = set()
100
+ for entity in ner_results:
101
+ if entity["entity_group"] == "DISEASE":
102
+ symptoms.add(entity["word"].lower())
103
+ return list(symptoms)
104
+
105
+ def match_symptoms(self, extracted_symptoms):
106
+ matched = {}
107
+
108
+ symptom_embeddings = self.semantic_model.encode(self.SYMPTOM_LIST, convert_to_tensor=True)
109
+
110
+ for symptom in extracted_symptoms:
111
+ symptom_embedding = self.semantic_model.encode(symptom, convert_to_tensor=True)
112
+
113
+ similarities = util.pytorch_cos_sim(symptom_embedding, symptom_embeddings)[0]
114
+
115
+ most_similar_idx = similarities.argmax()
116
+ best_match = self.SYMPTOM_LIST[most_similar_idx]
117
+ matched[symptom] = best_match
118
+
119
+ return matched.values()
120
+
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ai import DiseasePredictionModel
2
+ from flask import Flask, request, jsonify
3
+
4
+ app = Flask(__name__)
5
+
6
+ model = DiseasePredictionModel()
7
+
8
+ @app.route("/ai/text", methods=["POST"])
9
+ def ai_text():
10
+ try:
11
+ text = request.json["prompt"]
12
+ print(f"Original text: {text}")
13
+ corrected_text = model.correct_text(text)
14
+ print(f"Corrected text: {corrected_text}")
15
+ extracted_symptoms = model.extract_symptoms(corrected_text)
16
+ print(f"Extracted symptoms: {extracted_symptoms}")
17
+ matched_symptoms = model.match_symptoms(extracted_symptoms)
18
+ print(f"Matched symptoms: {matched_symptoms}")
19
+ if matched_symptoms == []:
20
+ return jsonify({"disease": "No matching symptoms found."})
21
+ predicted_disease = model.predict_disease(matched_symptoms)
22
+ print(f"Predicted disease: {predicted_disease}")
23
+ return jsonify({"disease": predicted_disease})
24
+ except:
25
+ return 400
26
+
27
+
28
+
29
+ @app.route("/ai/list", methods=["POST"])
30
+ def ai_list():
31
+ body = request.get_json()
32
+ try:
33
+ data = body["symptoms"]
34
+ print(data)
35
+ out = model.predict_disease(data)
36
+ return jsonify({"output":out}),200
37
+ except:
38
+ return jsonify({"status": "error"}),400
39
+
40
+ if __name__ == "__main__":
41
+ app.run(debug=True, host="0.0.0.0",port=7860)
data.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dc86d726e28e70799fbe8555df10d22a7981844729ef69b0dd95a631ff832b5f
3
+ size 190768300
frequency_dictionary_en_82_765.txt ADDED
The diff for this file is too large to render. See raw diff
 
model.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5af45666e8c147fec577d35040d64b347a07b5d2d7194ce61608ce564cec620b
3
+ size 3157604
symptoms.json ADDED
@@ -0,0 +1,379 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ "anxiety and nervousness",
3
+ "depression",
4
+ "shortness of breath",
5
+ "depressive or psychotic symptoms",
6
+ "sharp chest pain",
7
+ "dizziness",
8
+ "insomnia",
9
+ "abnormal involuntary movements",
10
+ "chest tightness",
11
+ "palpitations",
12
+ "irregular heartbeat",
13
+ "breathing fast",
14
+ "hoarse voice",
15
+ "sore throat",
16
+ "difficulty speaking",
17
+ "cough",
18
+ "nasal congestion",
19
+ "throat swelling",
20
+ "diminished hearing",
21
+ "lump in throat",
22
+ "throat feels tight",
23
+ "difficulty in swallowing",
24
+ "skin swelling",
25
+ "retention of urine",
26
+ "groin mass",
27
+ "leg pain",
28
+ "hip pain",
29
+ "suprapubic pain",
30
+ "blood in stool",
31
+ "lack of growth",
32
+ "emotional symptoms",
33
+ "elbow weakness",
34
+ "back weakness",
35
+ "pus in sputum",
36
+ "symptoms of the scrotum and testes",
37
+ "swelling of scrotum",
38
+ "pain in testicles",
39
+ "flatulence",
40
+ "pus draining from ear",
41
+ "jaundice",
42
+ "mass in scrotum",
43
+ "white discharge from eye",
44
+ "irritable infant",
45
+ "abusing alcohol",
46
+ "fainting",
47
+ "hostile behavior",
48
+ "drug abuse",
49
+ "sharp abdominal pain",
50
+ "feeling ill",
51
+ "vomiting",
52
+ "headache",
53
+ "nausea",
54
+ "diarrhea",
55
+ "vaginal itching",
56
+ "vaginal dryness",
57
+ "painful urination",
58
+ "involuntary urination",
59
+ "pain during intercourse",
60
+ "frequent urination",
61
+ "lower abdominal pain",
62
+ "vaginal discharge",
63
+ "blood in urine",
64
+ "hot flashes",
65
+ "intermenstrual bleeding",
66
+ "hand or finger pain",
67
+ "wrist pain",
68
+ "hand or finger swelling",
69
+ "arm pain",
70
+ "wrist swelling",
71
+ "arm stiffness or tightness",
72
+ "arm swelling",
73
+ "hand or finger stiffness or tightness",
74
+ "wrist stiffness or tightness",
75
+ "lip swelling",
76
+ "toothache",
77
+ "abnormal appearing skin",
78
+ "skin lesion",
79
+ "acne or pimples",
80
+ "dry lips",
81
+ "facial pain",
82
+ "mouth ulcer",
83
+ "skin growth",
84
+ "eye deviation",
85
+ "diminished vision",
86
+ "double vision",
87
+ "crosseyed",
88
+ "symptoms of eye",
89
+ "pain in eye",
90
+ "eye moves abnormally",
91
+ "abnormal movement of eyelid",
92
+ "foreign body sensation in eye",
93
+ "irregular appearing scalp",
94
+ "swollen lymph nodes",
95
+ "back pain",
96
+ "neck pain",
97
+ "low back pain",
98
+ "pain of the anus",
99
+ "pain during pregnancy",
100
+ "pelvic pain",
101
+ "impotence",
102
+ "infant spitting up",
103
+ "vomiting blood",
104
+ "regurgitation",
105
+ "burning abdominal pain",
106
+ "restlessness",
107
+ "symptoms of infants",
108
+ "wheezing",
109
+ "peripheral edema",
110
+ "neck mass",
111
+ "ear pain",
112
+ "jaw swelling",
113
+ "mouth dryness",
114
+ "neck swelling",
115
+ "knee pain",
116
+ "foot or toe pain",
117
+ "bowlegged or knockkneed",
118
+ "ankle pain",
119
+ "bones are painful",
120
+ "knee weakness",
121
+ "elbow pain",
122
+ "knee swelling",
123
+ "skin moles",
124
+ "knee lump or mass",
125
+ "weight gain",
126
+ "problems with movement",
127
+ "knee stiffness or tightness",
128
+ "leg swelling",
129
+ "foot or toe swelling",
130
+ "heartburn",
131
+ "smoking problems",
132
+ "muscle pain",
133
+ "infant feeding problem",
134
+ "recent weight loss",
135
+ "problems with shape or size of breast",
136
+ "underweight",
137
+ "difficulty eating",
138
+ "scanty menstrual flow",
139
+ "vaginal pain",
140
+ "vaginal redness",
141
+ "vulvar irritation",
142
+ "weakness",
143
+ "decreased heart rate",
144
+ "increased heart rate",
145
+ "bleeding or discharge from nipple",
146
+ "ringing in ear",
147
+ "plugged feeling in ear",
148
+ "itchy ears",
149
+ "frontal headache",
150
+ "fluid in ear",
151
+ "neck stiffness or tightness",
152
+ "spots or clouds in vision",
153
+ "eye redness",
154
+ "lacrimation",
155
+ "itchiness of eye",
156
+ "blindness",
157
+ "eye burns or stings",
158
+ "itchy eyelid",
159
+ "feeling cold",
160
+ "decreased appetite",
161
+ "excessive appetite",
162
+ "excessive anger",
163
+ "loss of sensation",
164
+ "focal weakness",
165
+ "slurring words",
166
+ "symptoms of the face",
167
+ "disturbance of memory",
168
+ "paresthesia",
169
+ "side pain",
170
+ "fever",
171
+ "shoulder pain",
172
+ "shoulder stiffness or tightness",
173
+ "shoulder weakness",
174
+ "arm cramps or spasms",
175
+ "shoulder swelling",
176
+ "tongue lesions",
177
+ "leg cramps or spasms",
178
+ "abnormal appearing tongue",
179
+ "ache all over",
180
+ "lower body pain",
181
+ "problems during pregnancy",
182
+ "spotting or bleeding during pregnancy",
183
+ "cramps and spasms",
184
+ "upper abdominal pain",
185
+ "stomach bloating",
186
+ "changes in stool appearance",
187
+ "unusual color or odor to urine",
188
+ "kidney mass",
189
+ "swollen abdomen",
190
+ "symptoms of prostate",
191
+ "leg stiffness or tightness",
192
+ "difficulty breathing",
193
+ "rib pain",
194
+ "joint pain",
195
+ "muscle stiffness or tightness",
196
+ "pallor",
197
+ "hand or finger lump or mass",
198
+ "chills",
199
+ "groin pain",
200
+ "fatigue",
201
+ "abdominal distention",
202
+ "regurgitation1",
203
+ "symptoms of the kidneys",
204
+ "melena",
205
+ "flushing",
206
+ "coughing up sputum",
207
+ "seizures",
208
+ "delusions or hallucinations",
209
+ "shoulder cramps or spasms",
210
+ "joint stiffness or tightness",
211
+ "pain or soreness of breast",
212
+ "excessive urination at night",
213
+ "bleeding from eye",
214
+ "rectal bleeding",
215
+ "constipation",
216
+ "temper problems",
217
+ "coryza",
218
+ "wrist weakness",
219
+ "eye strain",
220
+ "hemoptysis",
221
+ "lymphedema",
222
+ "skin on leg or foot looks infected",
223
+ "allergic reaction",
224
+ "congestion in chest",
225
+ "muscle swelling",
226
+ "pus in urine",
227
+ "abnormal size or shape of ear",
228
+ "low back weakness",
229
+ "sleepiness",
230
+ "apnea",
231
+ "abnormal breathing sounds",
232
+ "excessive growth",
233
+ "elbow cramps or spasms",
234
+ "feeling hot and cold",
235
+ "blood clots during menstrual periods",
236
+ "absence of menstruation",
237
+ "pulling at ears",
238
+ "gum pain",
239
+ "redness in ear",
240
+ "fluid retention",
241
+ "flulike syndrome",
242
+ "sinus congestion",
243
+ "painful sinuses",
244
+ "fears and phobias",
245
+ "recent pregnancy",
246
+ "uterine contractions",
247
+ "burning chest pain",
248
+ "back cramps or spasms",
249
+ "stiffness all over",
250
+ "muscle cramps contractures or spasms",
251
+ "low back cramps or spasms",
252
+ "back mass or lump",
253
+ "nosebleed",
254
+ "long menstrual periods",
255
+ "heavy menstrual flow",
256
+ "unpredictable menstruation",
257
+ "painful menstruation",
258
+ "infertility",
259
+ "frequent menstruation",
260
+ "sweating",
261
+ "mass on eyelid",
262
+ "swollen eye",
263
+ "eyelid swelling",
264
+ "eyelid lesion or rash",
265
+ "unwanted hair",
266
+ "symptoms of bladder",
267
+ "irregular appearing nails",
268
+ "itching of skin",
269
+ "hurts to breath",
270
+ "nailbiting",
271
+ "skin dryness peeling scaliness or roughness",
272
+ "skin on arm or hand looks infected",
273
+ "skin irritation",
274
+ "itchy scalp",
275
+ "hip swelling",
276
+ "incontinence of stool",
277
+ "foot or toe cramps or spasms",
278
+ "warts",
279
+ "bumps on penis",
280
+ "too little hair",
281
+ "foot or toe lump or mass",
282
+ "skin rash",
283
+ "mass or swelling around the anus",
284
+ "low back swelling",
285
+ "ankle swelling",
286
+ "hip lump or mass",
287
+ "drainage in throat",
288
+ "dry or flaky scalp",
289
+ "premenstrual tension or irritability",
290
+ "feeling hot",
291
+ "feet turned in",
292
+ "foot or toe stiffness or tightness",
293
+ "pelvic pressure",
294
+ "elbow swelling",
295
+ "elbow stiffness or tightness",
296
+ "early or late onset of menopause",
297
+ "mass on ear",
298
+ "bleeding from ear",
299
+ "hand or finger weakness",
300
+ "low selfesteem",
301
+ "throat irritation",
302
+ "itching of the anus",
303
+ "swollen or red tonsils",
304
+ "irregular belly button",
305
+ "swollen tongue",
306
+ "lip sore",
307
+ "vulvar sore",
308
+ "hip stiffness or tightness",
309
+ "mouth pain",
310
+ "arm weakness",
311
+ "leg lump or mass",
312
+ "disturbance of smell or taste",
313
+ "discharge in stools",
314
+ "penis pain",
315
+ "loss of sex drive",
316
+ "obsessions and compulsions",
317
+ "antisocial behavior",
318
+ "neck cramps or spasms",
319
+ "pupils unequal",
320
+ "poor circulation",
321
+ "thirst",
322
+ "sleepwalking",
323
+ "skin oiliness",
324
+ "sneezing",
325
+ "bladder mass",
326
+ "knee cramps or spasms",
327
+ "premature ejaculation",
328
+ "leg weakness",
329
+ "posture problems",
330
+ "bleeding in mouth",
331
+ "tongue bleeding",
332
+ "change in skin mole size or color",
333
+ "penis redness",
334
+ "penile discharge",
335
+ "shoulder lump or mass",
336
+ "polyuria",
337
+ "cloudy eye",
338
+ "hysterical behavior",
339
+ "arm lump or mass",
340
+ "nightmares",
341
+ "bleeding gums",
342
+ "pain in gums",
343
+ "bedwetting",
344
+ "diaper rash",
345
+ "lump or mass of breast",
346
+ "vaginal bleeding after menopause",
347
+ "infrequent menstruation",
348
+ "mass on vulva",
349
+ "jaw pain",
350
+ "itching of scrotum",
351
+ "postpartum problems of the breast",
352
+ "eyelid retracted",
353
+ "hesitancy",
354
+ "elbow lump or mass",
355
+ "muscle weakness",
356
+ "throat redness",
357
+ "joint swelling",
358
+ "tongue pain",
359
+ "redness in or around nose",
360
+ "wrinkles on skin",
361
+ "foot or toe weakness",
362
+ "hand or finger cramps or spasms",
363
+ "back stiffness or tightness",
364
+ "wrist lump or mass",
365
+ "skin pain",
366
+ "low back stiffness or tightness",
367
+ "low urine output",
368
+ "skin on head or neck looks infected",
369
+ "stuttering or stammering",
370
+ "problems with orgasm",
371
+ "nose deformity",
372
+ "lump over jaw",
373
+ "sore in nose",
374
+ "hip weakness",
375
+ "back swelling",
376
+ "ankle stiffness or tightness",
377
+ "ankle weakness",
378
+ "neck weakness"
379
+ ]