Emmanuel Frimpong Asante commited on
Commit
0f10097
·
1 Parent(s): c3920ae

Update space

Browse files
Files changed (2) hide show
  1. app.py +49 -14
  2. requirements.txt +2 -1
app.py CHANGED
@@ -2,34 +2,69 @@ import keras
2
  from keras.models import load_model
3
  import gradio as gr
4
  import cv2
 
 
 
5
 
6
- my_model = load_model('Final_Chicken_disease_model.h5', compile=True)
7
- auth_model = load_model('auth_model.h5', compile=True)
 
 
 
 
 
8
  name_disease = {0: 'Coccidiosis', 1: 'Healthy', 2: 'New Castle Disease', 3: 'Salmonella'}
9
  result = {0: 'Critical', 1: 'No issue', 2: 'Critical', 3: 'Critical'}
10
- recommend = {0: 'Panadol', 1: 'You have no need of Medicine', 2: 'Percetamol', 3: 'Ponston'}
11
 
12
 
13
  def predict(image):
 
14
  image_check = cv2.resize(image, (224, 224))
15
- indx = auth_model.predict(image_check.reshape(1, 224, 224, 3)).argmax()
16
- if indx == 0:
 
 
 
17
  image = cv2.resize(image, (224, 224))
18
- indx = my_model.predict(image.reshape(1, 224, 224, 3)).argmax()
 
 
19
  name = name_disease.get(indx)
20
  status = result.get(indx)
21
  recom = recommend.get(indx)
22
- return name, status, recom
23
- else:
24
  name = 'Unknown Image'
25
  status = 'N/A'
26
  recom = 'N/A'
27
- return name, status, recom
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- interface = gr.Interface(fn=predict, inputs=[gr.Image(label='upload Image')],
31
- outputs=[gr.components.Textbox(label="Disease Name"),
32
- gr.components.Textbox(label="result"),
33
- gr.components.Textbox(label='Medicine Recommend')],
34
- examples=[['disease.jpg'], ['ncd.jpg']])
35
  interface.launch(debug=True)
 
2
  from keras.models import load_model
3
  import gradio as gr
4
  import cv2
5
+ import numpy as np
6
+ from transformers import AutoModelForCausalLM, AutoTokenizer
7
+ import torch
8
 
9
+ # Load models
10
+ my_model = load_model('models/Final_Chicken_disease_model.h5', compile=True)
11
+ auth_model = load_model('models/auth_model.h5', compile=True)
12
+ llama_tokenizer = AutoTokenizer.from_pretrained('huggingface/llama3')
13
+ llama_model = AutoModelForCausalLM.from_pretrained('huggingface/llama3')
14
+
15
+ # Dictionaries for disease names, results, and recommendations
16
  name_disease = {0: 'Coccidiosis', 1: 'Healthy', 2: 'New Castle Disease', 3: 'Salmonella'}
17
  result = {0: 'Critical', 1: 'No issue', 2: 'Critical', 3: 'Critical'}
18
+ recommend = {0: 'Panadol', 1: 'You have no need of Medicine', 2: 'Paracetamol', 3: 'Ponston'}
19
 
20
 
21
  def predict(image):
22
+ # Preprocess the image for the authentication model
23
  image_check = cv2.resize(image, (224, 224))
24
+ image_check = np.expand_dims(image_check, axis=0) # Add batch dimension
25
+ indx = auth_model.predict(image_check).argmax()
26
+
27
+ if indx == 0: # If the image is recognized as a chicken disease image
28
+ # Preprocess the image for the disease prediction model
29
  image = cv2.resize(image, (224, 224))
30
+ image = np.expand_dims(image, axis=0) # Add batch dimension
31
+ indx = my_model.predict(image).argmax()
32
+
33
  name = name_disease.get(indx)
34
  status = result.get(indx)
35
  recom = recommend.get(indx)
36
+ else: # If the image is not recognized as a chicken disease image
 
37
  name = 'Unknown Image'
38
  status = 'N/A'
39
  recom = 'N/A'
 
40
 
41
+ return f"Chicken is {status}, the disease it has is {name}, the recommended medication is {recom}"
42
+
43
+
44
+ def chat_response(user_input):
45
+ inputs = llama_tokenizer(user_input, return_tensors='pt')
46
+ outputs = llama_model.generate(inputs['input_ids'], max_length=500, do_sample=True)
47
+ response = llama_tokenizer.decode(outputs[0], skip_special_tokens=True)
48
+ return response
49
+
50
+
51
+ def combined_interface(image, text):
52
+ if image is not None:
53
+ return predict(image)
54
+ elif text:
55
+ return chat_response(text)
56
+ else:
57
+ return "Please provide an image or ask a question."
58
+
59
+
60
+ # Create Gradio interface
61
+ interface = gr.Interface(
62
+ fn=combined_interface,
63
+ inputs=[gr.inputs.Image(label='Upload Image', optional=True),
64
+ gr.inputs.Textbox(label='Ask a question', optional=True)],
65
+ outputs=gr.Textbox(label="Response"),
66
+ examples=[['disease.jpg', ''], ['', 'What should I do if my chicken is sick?']]
67
+ )
68
 
69
+ # Launch the interface
 
 
 
 
70
  interface.launch(debug=True)
requirements.txt CHANGED
@@ -2,4 +2,5 @@ huggingface_hub==0.22.2
2
  keras
3
  gradio
4
  opencv-python
5
- tensorflow==2.12.0
 
 
2
  keras
3
  gradio
4
  opencv-python
5
+ tensorflow==2.12.0
6
+ transformers