Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import chromadb
|
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
import uvicorn
|
| 8 |
import requests
|
|
|
|
| 9 |
# Define FastAPI app
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
@@ -120,34 +121,50 @@ class SelectedSymptomsQuery(BaseModel):
|
|
| 120 |
|
| 121 |
@app.post("/find_disease")
|
| 122 |
def find_disease(query: SelectedSymptomsQuery):
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
# Sort diseases by the number of matching symptoms in descending order
|
| 128 |
-
matching_diseases['match_count'] = matching_diseases['Symptoms'].apply(lambda x: sum(s in selected_symptoms for s in x))
|
| 129 |
-
matching_diseases = matching_diseases.sort_values(by='match_count', ascending=False)
|
| 130 |
-
|
| 131 |
-
# Create a list of disease information
|
| 132 |
disease_list = []
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
class DiseaseListQuery(BaseModel):
|
| 152 |
disease_list: list
|
| 153 |
|
|
|
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
import uvicorn
|
| 8 |
import requests
|
| 9 |
+
from itertools import combinations
|
| 10 |
# Define FastAPI app
|
| 11 |
app = FastAPI()
|
| 12 |
|
|
|
|
| 121 |
|
| 122 |
@app.post("/find_disease")
|
| 123 |
def find_disease(query: SelectedSymptomsQuery):
|
| 124 |
+
SelectedSymptoms = query.selected_symptoms
|
| 125 |
+
all_selected_symptoms.extend(SelectedSymptoms)
|
| 126 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
disease_list = []
|
| 128 |
+
symptoms_list = []
|
| 129 |
+
unique_symptoms_list = []
|
| 130 |
+
|
| 131 |
+
# Combine all the symptoms we already know (all_symptoms + selected symptoms)
|
| 132 |
+
known_symptoms = {symptom.lower() for symptom_set in all_symptoms for symptom in symptom_set}
|
| 133 |
+
known_symptoms.update([symptom.lower() for symptom in SelectedSymptoms])
|
| 134 |
+
|
| 135 |
+
# Generate combinations of symptoms from all_symptoms and selected symptoms
|
| 136 |
+
for symptoms_set in all_symptoms:
|
| 137 |
+
for i in range(1, len(symptoms_set) + 1): # Generate combinations with all lengths
|
| 138 |
+
for symptom_combination in combinations(symptoms_set, i):
|
| 139 |
+
temp = list(symptom_combination) + SelectedSymptoms # Combine with selected symptoms
|
| 140 |
+
|
| 141 |
+
# Search for diseases that match the combination
|
| 142 |
+
matching_diseases = df[df['Symptoms'].apply(lambda x: all(s in x for s in temp))]
|
| 143 |
+
|
| 144 |
+
for _, row in matching_diseases.iterrows():
|
| 145 |
+
disease_info = {
|
| 146 |
+
'Disease': row['Name'],
|
| 147 |
+
'Symptoms': row['Symptoms'],
|
| 148 |
+
'Treatments': row['Treatments']
|
| 149 |
+
}
|
| 150 |
+
disease_list.append(disease_info)
|
| 151 |
+
|
| 152 |
+
symptoms_info = row['Symptoms']
|
| 153 |
+
symptoms_list.append(symptoms_info)
|
| 154 |
+
|
| 155 |
+
# Flatten the list of symptoms and remove duplicates, excluding known symptoms
|
| 156 |
+
for symptoms in symptoms_list:
|
| 157 |
+
for symptom in symptoms:
|
| 158 |
+
symptom_lower = symptom.lower()
|
| 159 |
+
if symptom_lower not in known_symptoms and symptom_lower not in unique_symptoms_list:
|
| 160 |
+
unique_symptoms_list.append(symptom_lower)
|
| 161 |
+
|
| 162 |
+
return {
|
| 163 |
+
"unique_symptoms_list": unique_symptoms_list,
|
| 164 |
+
"all_selected_symptoms": all_selected_symptoms,
|
| 165 |
+
"all_symptoms": all_symptoms,
|
| 166 |
+
"disease_list": disease_list
|
| 167 |
+
}
|
| 168 |
class DiseaseListQuery(BaseModel):
|
| 169 |
disease_list: list
|
| 170 |
|