Emmanuel Frimpong Asante commited on
Commit
42a4489
·
1 Parent(s): 925904e

"Update space"

Browse files

Signed-off-by: Emmanuel Frimpong Asante <[email protected]>

Files changed (1) hide show
  1. app.py +155 -67
app.py CHANGED
@@ -7,6 +7,9 @@ import gradio as gr
7
  import cv2
8
  import numpy as np
9
  from huggingface_hub import login
 
 
 
10
 
11
  # Ensure the HF token is set
12
  tok = os.getenv('HF_Token')
@@ -22,27 +25,32 @@ print("TensorFlow version:", tf.__version__)
22
  print("Eager execution:", tf.executing_eagerly())
23
  print("TensorFlow GPU Available:", tf.config.list_physical_devices('GPU'))
24
 
25
- # Set TensorFlow to use mixed precision to leverage the T4 GPU's capabilities
26
  from tensorflow.keras import mixed_precision
27
 
28
- policy = mixed_precision.Policy('mixed_float16')
29
- mixed_precision.set_global_policy(policy)
 
 
 
 
30
 
31
- # Load TensorFlow/Keras models with GPU support if available
32
  try:
33
- with tf.device('/GPU:0'): # Use GPU if available
 
34
  my_model = load_model('models/Final_Chicken_disease_model.h5', compile=True)
35
  auth_model = load_model('models/auth_model.h5', compile=True)
36
- print("Models loaded successfully.")
37
  except Exception as e:
38
  print(f"Error loading models: {e}")
39
  raise
40
 
41
- # Set PyTorch device
42
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
43
  print(f"Using device: {device}")
44
 
45
- # Load the tokenizer and LLaMA model, ensuring they run on the GPU
46
  llama_tokenizer = AutoTokenizer.from_pretrained('meta-llama/Meta-Llama-3-8B-Instruct')
47
  llama_model = AutoModelForCausalLM.from_pretrained(
48
  'meta-llama/Meta-Llama-3-8B-Instruct',
@@ -59,79 +67,159 @@ result = {0: 'Critical', 1: 'No issue', 2: 'Critical', 3: 'Critical'}
59
  recommend = {0: 'Panadol', 1: 'You have no need of Medicine', 2: 'Paracetamol', 3: 'Ponston'}
60
 
61
 
62
- def preprocess_image(image):
63
- try:
64
- image_check = cv2.resize(image, (224, 224))
65
- image_check = np.expand_dims(image_check, axis=0) # Add batch dimension
66
- return image_check
67
- except Exception as e:
68
- print(f"Error in image preprocessing: {e}")
69
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
 
72
- def predict(image):
73
- image_check = preprocess_image(image)
74
- if image_check is None:
75
- return "Image preprocessing failed.", None, None, None
76
 
77
- indx = auth_model.predict(image_check).argmax()
 
 
 
 
78
 
79
- if indx == 0: # If the image is recognized as a chicken disease image
80
- indx = my_model.predict(image_check).argmax()
81
- name = name_disease.get(indx)
82
- status = result.get(indx)
83
- recom = recommend.get(indx)
84
 
85
- diagnosis = f"The chicken is in a {status} condition, diagnosed with {name}. The recommended medication is {recom}."
86
- return diagnosis, name, status, recom
87
- else: # If the image is not recognized as a chicken disease image
88
- return (
89
- "The uploaded image is not recognized as a chicken or does not appear to be related to any known chicken diseases. "
90
- "Please ensure the image is clear and shows a chicken or its symptoms to receive a proper diagnosis."
91
- ), None, None, None
92
 
 
 
93
 
94
- def generate_combined_response(image, text):
95
- if image is not None:
96
- diagnosis, name, status, recom = predict(image)
97
-
98
- if name and status and recom: # If the disease is recognized
99
- context = f"The chicken is in a {status} condition, diagnosed with {name}. The recommended medication is {recom}. "
100
- if text:
101
- context += f"Additionally, the user asked: '{text}'"
102
- inputs = llama_tokenizer(context, return_tensors='pt', padding=True).to(device)
103
- outputs = llama_model.generate(
104
- inputs['input_ids'],
105
- attention_mask=inputs['attention_mask'], # Pass attention mask
106
- max_length=500,
107
- do_sample=True
108
- )
109
- advice = llama_tokenizer.decode(outputs[0], skip_special_tokens=True)
110
- return diagnosis + "\n\nAdditional Advice: " + advice
111
- else:
112
- return diagnosis
113
- elif text:
114
- return chat_response(text)
115
- else:
116
- return "Please provide an image or ask a question."
117
 
118
 
119
- def chat_response(user_input):
120
- inputs = llama_tokenizer(user_input, return_tensors='pt', padding=True).to(device)
121
- outputs = llama_model.generate(
122
- inputs['input_ids'],
123
- attention_mask=inputs['attention_mask'], # Pass attention mask
124
- max_length=500,
125
- do_sample=True
126
- )
127
- response = llama_tokenizer.decode(outputs[0], skip_special_tokens=True)
128
- return response
 
 
 
 
 
 
 
 
 
129
 
130
 
131
  # Gradio Interface
132
  interface = gr.Interface(
133
  fn=generate_combined_response,
134
- inputs=[gr.Image(label='Upload Image'), gr.Textbox(label='Ask a question')],
135
  outputs=gr.Textbox(label="Response")
136
  )
137
 
 
7
  import cv2
8
  import numpy as np
9
  from huggingface_hub import login
10
+ from datetime import datetime, timedelta
11
+ import json
12
+ import requests
13
 
14
  # Ensure the HF token is set
15
  tok = os.getenv('HF_Token')
 
25
  print("Eager execution:", tf.executing_eagerly())
26
  print("TensorFlow GPU Available:", tf.config.list_physical_devices('GPU'))
27
 
28
+ # Set TensorFlow to use mixed precision to leverage the T4 GPU's capabilities when available
29
  from tensorflow.keras import mixed_precision
30
 
31
+ if len(tf.config.list_physical_devices('GPU')) > 0:
32
+ policy = mixed_precision.Policy('mixed_float16')
33
+ mixed_precision.set_global_policy(policy)
34
+ print("Using mixed precision with GPU")
35
+ else:
36
+ print("Using CPU without mixed precision")
37
 
38
+ # Load TensorFlow/Keras models with GPU support if available, otherwise use CPU
39
  try:
40
+ device_name = '/GPU:0' if len(tf.config.list_physical_devices('GPU')) > 0 else '/CPU:0'
41
+ with tf.device(device_name): # Use GPU if available, otherwise CPU
42
  my_model = load_model('models/Final_Chicken_disease_model.h5', compile=True)
43
  auth_model = load_model('models/auth_model.h5', compile=True)
44
+ print(f"Models loaded successfully on {device_name}.")
45
  except Exception as e:
46
  print(f"Error loading models: {e}")
47
  raise
48
 
49
+ # Set PyTorch device to GPU if available, otherwise CPU
50
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
51
  print(f"Using device: {device}")
52
 
53
+ # Load the tokenizer and LLaMA model, ensuring they run on the correct device
54
  llama_tokenizer = AutoTokenizer.from_pretrained('meta-llama/Meta-Llama-3-8B-Instruct')
55
  llama_model = AutoModelForCausalLM.from_pretrained(
56
  'meta-llama/Meta-Llama-3-8B-Instruct',
 
67
  recommend = {0: 'Panadol', 1: 'You have no need of Medicine', 2: 'Paracetamol', 3: 'Ponston'}
68
 
69
 
70
+ class PoultryFarmBot:
71
+ def __init__(self):
72
+ self.feed_inventory = 1000 # kg
73
+ self.medicine_inventory = {"Panadol": 100, "Paracetamol": 50}
74
+ self.chicken_health = {}
75
+ self.reports = []
76
+
77
+ # Health Monitoring and Disease Diagnosis
78
+ def preprocess_image(self, image):
79
+ try:
80
+ image_check = cv2.resize(image, (224, 224))
81
+ image_check = np.expand_dims(image_check, axis=0) # Add batch dimension
82
+ return image_check
83
+ except Exception as e:
84
+ print(f"Error in image preprocessing: {e}")
85
+ return None
86
+
87
+ def predict(self, image):
88
+ image_check = self.preprocess_image(image)
89
+ if image_check is None:
90
+ return "Image preprocessing failed.", None, None, None
91
+
92
+ indx = auth_model.predict(image_check).argmax()
93
+
94
+ if indx == 0: # If the image is recognized as a chicken disease image
95
+ indx = my_model.predict(image_check).argmax()
96
+ name = name_disease.get(indx)
97
+ status = result.get(indx)
98
+ recom = recommend.get(indx)
99
+
100
+ diagnosis = f"The chicken is in a {status} condition, diagnosed with {name}. The recommended medication is {recom}."
101
+ return diagnosis, name, status, recom
102
+ else: # If the image is not recognized as a chicken disease image
103
+ return (
104
+ "The uploaded image is not recognized as a chicken or does not appear to be related to any known chicken diseases. "
105
+ "Please ensure the image is clear and shows a chicken or its symptoms to receive a proper diagnosis."
106
+ ), None, None, None
107
+
108
+ def diagnose_disease(self, image=None, symptoms=None):
109
+ if image:
110
+ return self.predict(image)
111
+ elif symptoms:
112
+ # Simulate symptom-based diagnosis
113
+ return "Based on symptoms, the chicken might have Newcastle Disease."
114
+ return "Please provide an image or describe the symptoms."
115
+
116
+ # Inventory Management
117
+ def track_inventory(self, item, usage):
118
+ if item in self.medicine_inventory:
119
+ self.medicine_inventory[item] -= usage
120
+ if self.medicine_inventory[item] < 10:
121
+ return f"{item} inventory is low, please reorder."
122
+ return f"{item} inventory updated. Current inventory: {self.medicine_inventory[item]} units."
123
+ elif item == "feed":
124
+ self.feed_inventory -= usage
125
+ if self.feed_inventory < 100:
126
+ return "Feed inventory is low, please reorder."
127
+ return f"Feed inventory updated. Current inventory: {self.feed_inventory} kg."
128
+ return "Item not recognized in inventory."
129
+
130
+ # Reporting and Analytics
131
+ def generate_report(self):
132
+ report = {
133
+ "date": str(datetime.now()),
134
+ "feed_inventory": self.feed_inventory,
135
+ "medicine_inventory": self.medicine_inventory,
136
+ "chickens_monitored": len(self.chicken_health),
137
+ "health_reports": self.chicken_health
138
+ }
139
+ self.reports.append(report)
140
+ return json.dumps(report, indent=4)
141
+
142
+ # IoT Device Integration (Temperature and Humidity Monitoring)
143
+ def monitor_environment(self, temperature, humidity):
144
+ if temperature > 30:
145
+ return "Temperature too high, increase ventilation."
146
+ if humidity < 40:
147
+ return "Humidity too low, consider using a humidifier."
148
+ return "Environmental conditions are optimal."
149
+
150
+ # Integration with External Systems
151
+ def integrate_with_external_system(self, system_url, data):
152
+ try:
153
+ response = requests.post(system_url, json=data)
154
+ if response.status_code == 200:
155
+ return "Data successfully sent to external system."
156
+ else:
157
+ return f"Failed to send data. Status code: {response.status_code}"
158
+ except Exception as e:
159
+ return f"Integration failed with error: {str(e)}"
160
+
161
+ # Emergency Handling
162
+ def handle_emergency(self, emergency_type):
163
+ if emergency_type == "disease_outbreak":
164
+ return "Disease outbreak detected. Isolate affected chickens and contact a veterinarian immediately."
165
+ elif emergency_type == "equipment_failure":
166
+ return "Equipment failure detected. Check the equipment immediately and perform necessary repairs."
167
+ else:
168
+ return "Unknown emergency type."
169
 
170
 
171
+ # Example usage of the chatbot with integrated Health Monitoring and Disease Diagnosis
172
+ bot = PoultryFarmBot()
 
 
173
 
174
+ # Health Monitoring and Disease Diagnosis
175
+ image = None # Replace with actual image input
176
+ symptoms = "coughing and sneezing"
177
+ diagnosis_result = bot.diagnose_disease(image=image, symptoms=symptoms)
178
+ print(diagnosis_result)
179
 
180
+ # Inventory Management
181
+ print(bot.track_inventory("feed", 50))
182
+ print(bot.track_inventory("Panadol", 10))
 
 
183
 
184
+ # Reporting and Analytics
185
+ print(bot.generate_report())
 
 
 
 
 
186
 
187
+ # IoT Device Integration (Temperature and Humidity Monitoring)
188
+ print(bot.monitor_environment(32, 35))
189
 
190
+ # Integration with External Systems
191
+ data_to_send = {"temperature": 32, "humidity": 35}
192
+ print(bot.integrate_with_external_system("https://api.external-system.com/data", data_to_send))
193
+
194
+ # Emergency Handling
195
+ print(bot.handle_emergency("disease_outbreak"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
 
197
 
198
+ # Gradio Interface for Health Monitoring
199
+ def generate_combined_response(image, text):
200
+ diagnosis, name, status, recom = bot.diagnose_disease(image=image, symptoms=text)
201
+
202
+ if name and status and recom: # If the disease is recognized
203
+ context = f"The chicken is in a {status} condition, diagnosed with {name}. The recommended medication is {recom}. "
204
+ if text:
205
+ context += f"Additionally, the user asked: '{text}'"
206
+ inputs = llama_tokenizer(context, return_tensors='pt', padding=True).to(device)
207
+ outputs = llama_model.generate(
208
+ inputs['input_ids'],
209
+ attention_mask=inputs['attention_mask'], # Pass attention mask
210
+ max_length=500,
211
+ do_sample=True
212
+ )
213
+ advice = llama_tokenizer.decode(outputs[0], skip_special_tokens=True)
214
+ return diagnosis + "\n\nAdditional Advice: " + advice
215
+ else:
216
+ return diagnosis
217
 
218
 
219
  # Gradio Interface
220
  interface = gr.Interface(
221
  fn=generate_combined_response,
222
+ inputs=[gr.Image(label='Upload Image'), gr.Textbox(label='Describe symptoms or ask a question')],
223
  outputs=gr.Textbox(label="Response")
224
  )
225