Emmanuel Frimpong Asante commited on
Commit
43bc0fa
·
1 Parent(s): ed234f0

"Update space"

Browse files

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

Files changed (1) hide show
  1. app.py +27 -24
app.py CHANGED
@@ -19,7 +19,7 @@ auth_model = load_model('models/auth_model.h5', compile=True)
19
  # Check for GPU availability and load the model accordingly
20
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
21
 
22
- # Load the tokenizer and model, ensuring they run on the correct device
23
  llama_tokenizer = AutoTokenizer.from_pretrained('meta-llama/Meta-Llama-3-8B-Instruct')
24
  llama_model = AutoModelForCausalLM.from_pretrained(
25
  'meta-llama/Meta-Llama-3-8B-Instruct',
@@ -48,46 +48,49 @@ def predict(image):
48
  status = result.get(indx)
49
  recom = recommend.get(indx)
50
 
51
- return f"The chicken is in a {status} condition, diagnosed with {name}. The recommended medication is {recom}."
 
52
  else: # If the image is not recognized as a chicken disease image
53
  return (
54
  "The uploaded image is not recognized as a chicken or does not appear to be related to any known chicken diseases. "
55
  "Please ensure the image is clear and shows a chicken or its symptoms to receive a proper diagnosis."
56
- )
57
 
58
 
59
- def chat_response(user_input):
60
- inputs = llama_tokenizer(user_input, return_tensors='pt').to(device) # Ensure tensors are on the right device
61
- outputs = llama_model.generate(inputs['input_ids'], max_length=500, do_sample=True)
62
- response = llama_tokenizer.decode(outputs[0], skip_special_tokens=True)
63
- return response
64
-
65
-
66
- def combined_interface(image, text):
67
  if image is not None:
68
- return predict(image)
 
 
 
 
 
 
 
 
 
 
 
 
69
  elif text:
 
70
  return chat_response(text)
71
  else:
72
  return "Please provide an image or ask a question."
73
 
74
 
75
- example_image_path = 'disease.jpg' # Replace with actual file path
76
-
77
- # Check if the example image exists and is a file
78
- if not os.path.isfile(example_image_path):
79
- print(f"Warning: Example image file not found: {example_image_path}")
80
- example_image_path = None # Disable the example
81
 
82
- # Only include the example if the path is valid
83
- examples = [[example_image_path, ''], ['', 'What should I do if my chicken is sick?']] if example_image_path else None
84
 
85
- # Create Gradio interface
86
  interface = gr.Interface(
87
- fn=combined_interface,
88
  inputs=[gr.Image(label='Upload Image'), gr.Textbox(label='Ask a question')],
89
- outputs=gr.Textbox(label="Response"),
90
- # examples=examples # Use examples only if valid
91
  )
92
 
93
  # Launch the interface
 
19
  # Check for GPU availability and load the model accordingly
20
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
21
 
22
+ # Load the tokenizer and LLaMA model, ensuring they run on the correct device
23
  llama_tokenizer = AutoTokenizer.from_pretrained('meta-llama/Meta-Llama-3-8B-Instruct')
24
  llama_model = AutoModelForCausalLM.from_pretrained(
25
  'meta-llama/Meta-Llama-3-8B-Instruct',
 
48
  status = result.get(indx)
49
  recom = recommend.get(indx)
50
 
51
+ diagnosis = f"The chicken is in a {status} condition, diagnosed with {name}. The recommended medication is {recom}."
52
+ return diagnosis, name, status, recom
53
  else: # If the image is not recognized as a chicken disease image
54
  return (
55
  "The uploaded image is not recognized as a chicken or does not appear to be related to any known chicken diseases. "
56
  "Please ensure the image is clear and shows a chicken or its symptoms to receive a proper diagnosis."
57
+ ), None, None, None
58
 
59
 
60
+ def generate_combined_response(image, text):
 
 
 
 
 
 
 
61
  if image is not None:
62
+ diagnosis, name, status, recom = predict(image)
63
+
64
+ if name and status and recom: # If the disease is recognized
65
+ # Generate a response using LLaMA based on the diagnosis
66
+ context = f"The chicken is in a {status} condition, diagnosed with {name}. The recommended medication is {recom}. "
67
+ if text:
68
+ context += f"Additionally, the user asked: '{text}'"
69
+ inputs = llama_tokenizer(context, return_tensors='pt').to(device)
70
+ outputs = llama_model.generate(inputs['input_ids'], max_length=500, do_sample=True)
71
+ advice = llama_tokenizer.decode(outputs[0], skip_special_tokens=True)
72
+ return diagnosis + "\n\nAdditional Advice: " + advice
73
+ else:
74
+ return diagnosis
75
  elif text:
76
+ # Only text input provided, generate response using LLaMA
77
  return chat_response(text)
78
  else:
79
  return "Please provide an image or ask a question."
80
 
81
 
82
+ def chat_response(user_input):
83
+ inputs = llama_tokenizer(user_input, return_tensors='pt').to(device)
84
+ outputs = llama_model.generate(inputs['input_ids'], max_length=500, do_sample=True)
85
+ response = llama_tokenizer.decode(outputs[0], skip_special_tokens=True)
86
+ return response
 
87
 
 
 
88
 
89
+ # Gradio Interface
90
  interface = gr.Interface(
91
+ fn=generate_combined_response,
92
  inputs=[gr.Image(label='Upload Image'), gr.Textbox(label='Ask a question')],
93
+ outputs=gr.Textbox(label="Response")
 
94
  )
95
 
96
  # Launch the interface