Emmanuel Frimpong Asante commited on
Commit
fa011fc
·
1 Parent(s): 8a97979

"Update space"

Browse files

Signed-off-by: Emmanuel Frimpong Asante <frimpongasante50@gmail.com>

Files changed (1) hide show
  1. app.py +33 -43
app.py CHANGED
@@ -87,24 +87,11 @@ class PoultryFarmBot:
87
  diagnosis = f"The fecal sample indicates the poultry animal is in a {status} condition, diagnosed with {name}. The recommended action is {recom}."
88
  return diagnosis, name, status, recom
89
 
90
- # Diagnose Disease Using Fecal Image or Symptoms
91
- def diagnose_disease(self, image=None, symptoms=None):
92
  if image is not None and image.size > 0: # Ensure image is valid and has elements
93
  return self.predict(image)
94
- elif symptoms:
95
- # Generate diagnosis using GPT-based model based on the provided symptoms
96
- context = f"Symptoms: {symptoms}."
97
- response = openai.ChatCompletion.create(
98
- model="gpt-3.5-turbo",
99
- messages=[
100
- {"role": "system", "content": "You are an advanced poultry farm management system."},
101
- {"role": "user", "content": context}
102
- ],
103
- max_tokens=150
104
- )
105
- diagnosis = response['choices'][0]['message']['content'].strip()
106
- return diagnosis, None, None, None
107
- return "Please provide an image of poultry fecal matter or describe the symptoms.", None, None, None
108
 
109
  # Initialize the bot instance
110
  bot = PoultryFarmBot()
@@ -131,46 +118,49 @@ def mistral_response(user_input):
131
  except Exception as e:
132
  return f"Error generating response: {str(e)}"
133
 
134
- # Gradio interface for disease diagnosis and chatbot response
135
- def generate_combined_response(image, text):
136
- diagnosis, name, status, recom = bot.diagnose_disease(image=image, symptoms=text)
137
- if name and status and recom:
138
- context = f"The fecal sample indicates the poultry animal is in a {status} condition, diagnosed with {name}. The recommended action is {recom}. "
139
- if text:
140
- context += f"Additionally, the user asked: '{text}'"
141
- advice = mistral_response(context)
142
- return diagnosis + "\n\nAdditional Advice: " + advice
143
- else:
144
- return diagnosis
145
-
146
- # Gradio interface styling and layout
147
- with gr.Blocks(theme=gr.themes.Soft(primary_hue="green", neutral_hue="slate")) as diagnosis_interface:
148
- gr.Markdown("# 🐔 Poultry Fecal Diagnostic Assistant")
 
 
 
149
  gr.Markdown(
150
- "Upload an image of poultry fecal matter or describe symptoms to get a diagnosis and advice."
151
  )
152
 
153
  with gr.Row():
154
  with gr.Column(scale=1):
155
  fecal_image = gr.Image(
156
- label="Upload Image of Poultry Feces",
157
  type="numpy",
158
  elem_id="image-upload",
159
  show_label=True,
160
  )
161
  with gr.Column(scale=2):
162
- symptom_text = gr.Textbox(
163
- label="Describe Symptoms or Ask a Question",
164
- placeholder="Enter symptoms of the poultry...",
165
  lines=3,
166
- elem_id="symptom-textbox",
167
  )
168
 
169
  output_box = gr.Textbox(
170
- label="Diagnosis and Advice",
171
- placeholder="The diagnosis and advice will appear here...",
172
  interactive=False,
173
- lines=6,
174
  elem_id="output-box",
175
  )
176
 
@@ -180,11 +170,11 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="green", neutral_hue="slate")) a
180
  elem_id="submit-button"
181
  )
182
  submit_button.click(
183
- fn=generate_combined_response,
184
- inputs=[fecal_image, symptom_text],
185
  outputs=[output_box],
186
  )
187
 
188
  # Launch the Gradio interface
189
  if __name__ == "__main__":
190
- diagnosis_interface.launch(debug=True, share=True)
 
87
  diagnosis = f"The fecal sample indicates the poultry animal is in a {status} condition, diagnosed with {name}. The recommended action is {recom}."
88
  return diagnosis, name, status, recom
89
 
90
+ # Diagnose Disease Using Fecal Image
91
+ def diagnose_disease(self, image):
92
  if image is not None and image.size > 0: # Ensure image is valid and has elements
93
  return self.predict(image)
94
+ return "Please provide an image of poultry fecal matter for disease detection.", None, None, None
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  # Initialize the bot instance
97
  bot = PoultryFarmBot()
 
118
  except Exception as e:
119
  return f"Error generating response: {str(e)}"
120
 
121
+
122
+ # Main chatbot function: handles both generative AI and disease detection
123
+ def chatbot_response(image, text):
124
+ # If an image is provided, perform disease detection
125
+ if image is not None:
126
+ diagnosis, name, status, recom = bot.diagnose_disease(image)
127
+ if name and status and recom:
128
+ return diagnosis
129
+ else:
130
+ return diagnosis # Return only the diagnostic message if no disease found
131
+
132
+ # If no image is provided, proceed with generative AI response
133
+ return mistral_response(text)
134
+
135
+
136
+ # Gradio interface styling and layout with ChatGPT-like theme
137
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="green", neutral_hue="slate")) as chatbot_interface:
138
+ gr.Markdown("# 🐔 Poultry Management Chatbot")
139
  gr.Markdown(
140
+ "This chatbot can help you manage your poultry with conversational AI. Upload an image of poultry fecal matter for disease detection or just ask questions!"
141
  )
142
 
143
  with gr.Row():
144
  with gr.Column(scale=1):
145
  fecal_image = gr.Image(
146
+ label="Upload Image of Poultry Feces (Optional)",
147
  type="numpy",
148
  elem_id="image-upload",
149
  show_label=True,
150
  )
151
  with gr.Column(scale=2):
152
+ user_input = gr.Textbox(
153
+ label="Type your question or chat with the assistant",
154
+ placeholder="Ask a question about poultry management...",
155
  lines=3,
156
+ elem_id="user-input",
157
  )
158
 
159
  output_box = gr.Textbox(
160
+ label="Response",
161
+ placeholder="The response will appear here...",
162
  interactive=False,
163
+ lines=10,
164
  elem_id="output-box",
165
  )
166
 
 
170
  elem_id="submit-button"
171
  )
172
  submit_button.click(
173
+ fn=chatbot_response,
174
+ inputs=[fecal_image, user_input],
175
  outputs=[output_box],
176
  )
177
 
178
  # Launch the Gradio interface
179
  if __name__ == "__main__":
180
+ chatbot_interface.launch(debug=True, share=True)