File size: 2,723 Bytes
59b0f06 a2954e9 59b0f06 0f10097 efca706 0c71adf efca706 16955d5 5001611 59b0f06 0f10097 e166c21 efca706 0f10097 59b0f06 0f10097 59b0f06 0f10097 59b0f06 0f10097 59b0f06 0f10097 59b0f06 0f10097 59b0f06 0f10097 59b0f06 0f10097 59b0f06 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import keras
from keras.models import load_model
import gradio as gr
import cv2
import numpy as np
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
from huggingface_hub import login
import os
tok = os.getenv('HF_Token')
login(token=tok)
# Load models
my_model = load_model('models/Final_Chicken_disease_model.h5', compile=True)
auth_model = load_model('models/auth_model.h5', compile=True)
llama_tokenizer = AutoTokenizer.from_pretrained('meta-llama/Meta-Llama-3-8B-Instruct')
llama_model = AutoModelForCausalLM.from_pretrained('meta-llama/Meta-Llama-3-8B-Instruct')
# Dictionaries for disease names, results, and recommendations
name_disease = {0: 'Coccidiosis', 1: 'Healthy', 2: 'New Castle Disease', 3: 'Salmonella'}
result = {0: 'Critical', 1: 'No issue', 2: 'Critical', 3: 'Critical'}
recommend = {0: 'Panadol', 1: 'You have no need of Medicine', 2: 'Paracetamol', 3: 'Ponston'}
def predict(image):
# Preprocess the image for the authentication model
image_check = cv2.resize(image, (224, 224))
image_check = np.expand_dims(image_check, axis=0) # Add batch dimension
indx = auth_model.predict(image_check).argmax()
if indx == 0: # If the image is recognized as a chicken disease image
# Preprocess the image for the disease prediction model
image = cv2.resize(image, (224, 224))
image = np.expand_dims(image, axis=0) # Add batch dimension
indx = my_model.predict(image).argmax()
name = name_disease.get(indx)
status = result.get(indx)
recom = recommend.get(indx)
else: # If the image is not recognized as a chicken disease image
name = 'Unknown Image'
status = 'N/A'
recom = 'N/A'
return f"Chicken is {status}, the disease it has is {name}, the recommended medication is {recom}"
def chat_response(user_input):
inputs = llama_tokenizer(user_input, return_tensors='pt')
outputs = llama_model.generate(inputs['input_ids'], max_length=500, do_sample=True)
response = llama_tokenizer.decode(outputs[0], skip_special_tokens=True)
return response
def combined_interface(image, text):
if image is not None:
return predict(image)
elif text:
return chat_response(text)
else:
return "Please provide an image or ask a question."
# Create Gradio interface
interface = gr.Interface(
fn=combined_interface,
inputs=[gr.inputs.Image(label='Upload Image', optional=True),
gr.inputs.Textbox(label='Ask a question', optional=True)],
outputs=gr.Textbox(label="Response"),
examples=[['disease.jpg', ''], ['', 'What should I do if my chicken is sick?']]
)
# Launch the interface
interface.launch(debug=True)
|