Skip to content

Commit 589d15e

Browse files
Fix: Implement symptom analysis results display
Implement the display of disease predictions after analyzing symptoms, as per the provided design.
1 parent c502423 commit 589d15e

File tree

2 files changed

+49
-49
lines changed

2 files changed

+49
-49
lines changed

src/components/symptoms/SymptomChecker.tsx

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -83,48 +83,35 @@ const SymptomChecker = () => {
8383

8484
try {
8585
const symptomNames = selectedSymptoms.map(s => s.name);
86-
8786
const predictions = await predictDisease(symptomNames);
8887

89-
if (predictions.length > 0) {
90-
const topDisease = predictions[0];
91-
92-
const recommendedDoctors = getDoctorsBySpecialty(topDisease.specialist || "General Physician");
93-
94-
const precautions = [
95-
"Consult with a healthcare professional",
96-
"Get adequate rest",
97-
"Stay hydrated",
98-
"Monitor your symptoms",
99-
"Follow prescribed medications if any"
100-
];
88+
if (predictions && predictions.length > 0) {
89+
const recommendedDoctors = getDoctorsBySpecialty(predictions[0].specialist);
10190

102-
const diet = [
103-
"Maintain a balanced diet",
104-
"Include fresh fruits and vegetables",
105-
"Stay hydrated with water",
106-
"Avoid processed foods",
107-
"Consider supplements as recommended by your doctor"
108-
];
109-
11091
setResults({
11192
diseases: predictions,
112-
precautions,
113-
diet,
93+
precautions: [
94+
"Consult with a healthcare professional",
95+
"Get adequate rest",
96+
"Stay hydrated",
97+
"Monitor your symptoms",
98+
"Follow prescribed medications if any"
99+
],
100+
diet: [
101+
"Maintain a balanced diet",
102+
"Include fresh fruits and vegetables",
103+
"Stay hydrated with water",
104+
"Avoid processed foods",
105+
"Consider supplements as recommended by your doctor"
106+
],
114107
doctors: recommendedDoctors
115108
});
116-
} else {
117-
toast({
118-
title: "Analysis Inconclusive",
119-
description: "We couldn't determine a likely condition from your symptoms. Please consult a healthcare professional.",
120-
variant: "destructive",
121-
});
122109
}
123110
} catch (error) {
124-
console.error('Error during analysis:', error);
111+
const errorMessage = error instanceof Error ? error.message : "An error occurred while analyzing your symptoms";
125112
toast({
126-
title: "Error During Analysis",
127-
description: "An error occurred while analyzing your symptoms. Please try again.",
113+
title: "Analysis Error",
114+
description: errorMessage,
128115
variant: "destructive",
129116
});
130117
} finally {

src/utils/diseasePredictor.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import { pipeline } from '@huggingface/transformers';
3+
import { DISEASES } from '@/data/diseases';
34

45
let classifier: any = null;
56

@@ -15,31 +16,43 @@ export const initializeModel = async () => {
1516
};
1617

1718
export const predictDisease = async (symptoms: string[]) => {
19+
// If no symptoms provided, return early
20+
if (!symptoms.length) {
21+
throw new Error("Please select at least one symptom for analysis");
22+
}
23+
1824
const model = await initializeModel();
1925

2026
// Convert symptoms array to a text description
2127
const symptomText = symptoms.join(', ');
2228

23-
// Define possible diseases as candidate labels
24-
const candidateLabels = [
25-
'Common Cold',
26-
'Influenza',
27-
'Seasonal Allergies',
28-
'Migraine',
29-
'Gastroesophageal Reflux Disease',
30-
'Asthma',
31-
'Arthritis',
32-
'Hypertension'
33-
];
29+
// Get disease names as candidate labels
30+
const candidateLabels = DISEASES.map(disease => disease.name);
3431

3532
try {
3633
const result = await model(symptomText, candidateLabels);
37-
return result.labels.map((label: string, index: number) => ({
38-
name: label,
39-
probability: Math.round(result.scores[index] * 100)
40-
}));
34+
35+
// Filter predictions with score > 20%
36+
const predictions = result.labels
37+
.map((label: string, index: number) => ({
38+
name: label,
39+
probability: Math.round(result.scores[index] * 100),
40+
specialist: DISEASES.find(d => d.name === label)?.specialist || 'General Physician',
41+
description: DISEASES.find(d => d.name === label)?.description || ''
42+
}))
43+
.filter(pred => pred.probability > 20)
44+
.sort((a, b) => b.probability - a.probability)
45+
.slice(0, 3); // Top 3 predictions
46+
47+
if (predictions.length === 0) {
48+
throw new Error("No strong matches found for the provided symptoms");
49+
}
50+
51+
return predictions;
4152
} catch (error) {
42-
console.error('Error predicting disease:', error);
43-
return [];
53+
if (error instanceof Error) {
54+
throw error;
55+
}
56+
throw new Error("Error analyzing symptoms");
4457
}
4558
};

0 commit comments

Comments
 (0)