Emmanuel Frimpong Asante
commited on
Commit
·
3abbae6
1
Parent(s):
5aeb365
"Update space"
Browse filesSigned-off-by: Emmanuel Frimpong Asante <[email protected]>
services/disease_detection.py
CHANGED
@@ -4,11 +4,20 @@ import cv2
|
|
4 |
import numpy as np
|
5 |
import tensorflow as tf
|
6 |
from keras.models import load_model
|
|
|
7 |
|
8 |
# Check your model optimizer configuration
|
9 |
optimizer = tf.keras.optimizers.Adam() # Default optimizer without weight_decay
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Mapping of disease labels to disease names, health status, and treatment recommendations
|
14 |
name_disease = {0: 'Coccidiosis', 1: 'Healthy', 2: 'New Castle Disease', 3: 'Salmonella'}
|
@@ -20,7 +29,6 @@ recommend = {
|
|
20 |
3: 'Ponston' # Treatment for Salmonella
|
21 |
}
|
22 |
|
23 |
-
|
24 |
class PoultryFarmBot:
|
25 |
def __init__(self, db):
|
26 |
"""
|
@@ -64,10 +72,54 @@ class PoultryFarmBot:
|
|
64 |
|
65 |
# Get the disease name, status, and recommended treatment
|
66 |
name = name_disease.get(indx, "Unknown disease")
|
67 |
-
status = result.get(indx, "
|
68 |
-
recom = recommend.get(indx, "
|
69 |
|
70 |
return name, status, recom
|
71 |
except Exception as e:
|
72 |
print(f"Error during disease prediction: {e}")
|
73 |
return "Prediction failed.", None, None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import numpy as np
|
5 |
import tensorflow as tf
|
6 |
from keras.models import load_model
|
7 |
+
from services.llama_service import llama2_response
|
8 |
|
9 |
# Check your model optimizer configuration
|
10 |
optimizer = tf.keras.optimizers.Adam() # Default optimizer without weight_decay
|
11 |
+
|
12 |
+
# Load TensorFlow/Keras models with GPU support if available, otherwise use CPU
|
13 |
+
try:
|
14 |
+
device_name = '/GPU:0' if len(tf.config.list_physical_devices('GPU')) > 0 else '/CPU:0'
|
15 |
+
with tf.device(device_name):
|
16 |
+
my_model = load_model('models/Final_Chicken_disease_model.h5', compile=True)
|
17 |
+
auth_model = load_model('models/auth_model.h5', compile=True)
|
18 |
+
print(f"Models loaded successfully on {device_name}.")
|
19 |
+
except Exception as e:
|
20 |
+
print(f"Error loading models: {e}")
|
21 |
|
22 |
# Mapping of disease labels to disease names, health status, and treatment recommendations
|
23 |
name_disease = {0: 'Coccidiosis', 1: 'Healthy', 2: 'New Castle Disease', 3: 'Salmonella'}
|
|
|
29 |
3: 'Ponston' # Treatment for Salmonella
|
30 |
}
|
31 |
|
|
|
32 |
class PoultryFarmBot:
|
33 |
def __init__(self, db):
|
34 |
"""
|
|
|
72 |
|
73 |
# Get the disease name, status, and recommended treatment
|
74 |
name = name_disease.get(indx, "Unknown disease")
|
75 |
+
status = result.get(indx, "Unknown condition")
|
76 |
+
recom = recommend.get(indx, "No recommendation available")
|
77 |
|
78 |
return name, status, recom
|
79 |
except Exception as e:
|
80 |
print(f"Error during disease prediction: {e}")
|
81 |
return "Prediction failed.", None, None, None
|
82 |
+
|
83 |
+
# Generate a detailed response using Llama 2 for disease information and recommendations
|
84 |
+
def generate_disease_response(self, disease_name, status, recommendation):
|
85 |
+
"""
|
86 |
+
Generates a detailed response using Llama 2 model for disease information and recommendations.
|
87 |
+
|
88 |
+
:param disease_name: The name of the detected disease.
|
89 |
+
:param status: The health status of the poultry.
|
90 |
+
:param recommendation: The treatment recommendation.
|
91 |
+
:return: A detailed response with disease information.
|
92 |
+
"""
|
93 |
+
try:
|
94 |
+
prompt = (
|
95 |
+
f"The disease detected is {disease_name}, classified as {status}. "
|
96 |
+
f"Recommended action: {recommendation}. "
|
97 |
+
f"Here is some information about {disease_name}: causes, symptoms, and treatment methods "
|
98 |
+
"to effectively manage this condition on a poultry farm."
|
99 |
+
)
|
100 |
+
response = llama2_response(prompt)
|
101 |
+
# Post-process to remove the prompt if accidentally included in the response
|
102 |
+
return response.replace(prompt, "").strip()
|
103 |
+
except Exception as e:
|
104 |
+
print(f"Error generating detailed response: {e}")
|
105 |
+
return "Unable to generate detailed response."
|
106 |
+
|
107 |
+
# Diagnose Disease Using Fecal Image
|
108 |
+
def diagnose_disease(self, image):
|
109 |
+
"""
|
110 |
+
Diagnose a disease using a fecal image.
|
111 |
+
|
112 |
+
:param image: The image of poultry feces to detect a disease.
|
113 |
+
:return: Tuple of diagnosis result and detailed response if applicable.
|
114 |
+
"""
|
115 |
+
if image is not None and image.size > 0: # Ensure the image is valid and has elements
|
116 |
+
disease_name, status, recommendation = self.predict(image)
|
117 |
+
if disease_name and status and recommendation:
|
118 |
+
detailed_response = self.generate_disease_response(disease_name, status, recommendation)
|
119 |
+
return {
|
120 |
+
"disease": disease_name,
|
121 |
+
"status": status,
|
122 |
+
"recommendation": recommendation,
|
123 |
+
"detailed_response": detailed_response
|
124 |
+
}
|
125 |
+
return "Please provide an image of poultry fecal matter for disease detection.", None, None, None
|