Emmanuel Frimpong Asante
commited on
Commit
·
1cef079
1
Parent(s):
9a9bdef
"Update space" chatgptv1
Browse filesSigned-off-by: Emmanuel Frimpong Asante <frimpongasante50@gmail.com>
- app.py +65 -73
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
import os
|
| 2 |
-
import openai
|
| 3 |
import tensorflow as tf
|
| 4 |
-
import torch
|
| 5 |
from keras.models import load_model
|
| 6 |
import gradio as gr
|
| 7 |
import cv2
|
| 8 |
import numpy as np
|
| 9 |
from huggingface_hub import login
|
| 10 |
-
from
|
|
|
|
| 11 |
import json
|
| 12 |
import requests
|
| 13 |
|
|
@@ -17,10 +17,15 @@ if tok:
|
|
| 17 |
login(token=tok, add_to_git_credential=True)
|
| 18 |
else:
|
| 19 |
print("Warning: Hugging Face token not found in environment variables.")
|
| 20 |
-
|
| 21 |
# Set your OpenAI API key
|
| 22 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# Check GPU availability for TensorFlow
|
| 25 |
print("TensorFlow version:", tf.__version__)
|
| 26 |
print("Eager execution:", tf.executing_eagerly())
|
|
@@ -47,18 +52,9 @@ except Exception as e:
|
|
| 47 |
print(f"Error loading models: {e}")
|
| 48 |
raise
|
| 49 |
|
| 50 |
-
# Dictionaries for disease names, results, and recommendations
|
| 51 |
-
name_disease = {0: 'Coccidiosis', 1: 'Healthy', 2: 'New Castle Disease', 3: 'Salmonella'}
|
| 52 |
-
result = {0: 'Critical', 1: 'No issue', 2: 'Critical', 3: 'Critical'}
|
| 53 |
-
recommend = {0: 'Panadol', 1: 'You have no need of Medicine', 2: 'Paracetamol', 3: 'Ponston'}
|
| 54 |
-
|
| 55 |
-
|
| 56 |
class PoultryFarmBot:
|
| 57 |
def __init__(self):
|
| 58 |
-
self.
|
| 59 |
-
self.medicine_inventory = {"Panadol": 100, "Paracetamol": 50}
|
| 60 |
-
self.chicken_health = {}
|
| 61 |
-
self.reports = []
|
| 62 |
|
| 63 |
# Health Monitoring and Disease Diagnosis
|
| 64 |
def preprocess_image(self, image):
|
|
@@ -87,7 +83,7 @@ class PoultryFarmBot:
|
|
| 87 |
return diagnosis, name, status, recom
|
| 88 |
else: # If the image is not recognized as a chicken disease image
|
| 89 |
return (
|
| 90 |
-
"The uploaded image is not recognized as a chicken
|
| 91 |
"Please ensure the image is clear and shows a chicken or its symptoms to receive a proper diagnosis."
|
| 92 |
), None, None, None
|
| 93 |
|
|
@@ -95,51 +91,68 @@ class PoultryFarmBot:
|
|
| 95 |
if image is not None and image.size > 0: # Ensure image is valid and has elements
|
| 96 |
return self.predict(image)
|
| 97 |
elif symptoms:
|
| 98 |
-
#
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
status = "N/A"
|
| 112 |
-
recom = "Consult a veterinarian for further diagnosis."
|
| 113 |
-
diagnosis = f"Symptoms are not conclusive. {recom}"
|
| 114 |
-
|
| 115 |
-
return diagnosis, name, status, recom
|
| 116 |
-
|
| 117 |
return "Please provide an image or describe the symptoms.", None, None, None
|
| 118 |
|
| 119 |
# Inventory Management
|
| 120 |
def track_inventory(self, item, usage):
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
| 124 |
return f"{item} inventory is low, please reorder."
|
| 125 |
-
return f"{item} inventory updated. Current inventory: {
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
# Reporting and Analytics
|
| 134 |
def generate_report(self):
|
| 135 |
report = {
|
| 136 |
"date": str(datetime.now()),
|
| 137 |
-
"feed_inventory": self.
|
| 138 |
-
"
|
| 139 |
-
"
|
| 140 |
-
"health_reports": self.
|
| 141 |
}
|
| 142 |
-
self.reports.
|
| 143 |
return json.dumps(report, indent=4)
|
| 144 |
|
| 145 |
# Integration with External Systems
|
|
@@ -164,29 +177,8 @@ class PoultryFarmBot:
|
|
| 164 |
return "Unknown emergency type."
|
| 165 |
|
| 166 |
|
| 167 |
-
#
|
| 168 |
-
|
| 169 |
-
#
|
| 170 |
-
# # Health Monitoring and Disease Diagnosis
|
| 171 |
-
# image = None # Replace with actual image input
|
| 172 |
-
# symptoms = "coughing and sneezing"
|
| 173 |
-
# diagnosis_result = bot.diagnose_disease(image=image, symptoms=symptoms)
|
| 174 |
-
# print(diagnosis_result)
|
| 175 |
-
#
|
| 176 |
-
# # Inventory Management
|
| 177 |
-
# print(bot.track_inventory("feed", 50))
|
| 178 |
-
# print(bot.track_inventory("Panadol", 10))
|
| 179 |
-
#
|
| 180 |
-
# # Reporting and Analytics
|
| 181 |
-
# print(bot.generate_report())
|
| 182 |
-
#
|
| 183 |
-
# # Integration with External Systems
|
| 184 |
-
# data_to_send = {"temperature": 32, "humidity": 35}
|
| 185 |
-
# print(bot.integrate_with_external_system("https://api.external-system.com/data", data_to_send))
|
| 186 |
-
#
|
| 187 |
-
# # Emergency Handling
|
| 188 |
-
# print(bot.handle_emergency("disease_outbreak"))
|
| 189 |
-
|
| 190 |
|
| 191 |
# Function to generate a response using OpenAI's GPT model
|
| 192 |
def generate_combined_response(image, text):
|
|
@@ -199,7 +191,7 @@ def generate_combined_response(image, text):
|
|
| 199 |
|
| 200 |
# Use OpenAI's GPT model to generate additional advice
|
| 201 |
response = openai.ChatCompletion.create(
|
| 202 |
-
model="gpt-
|
| 203 |
messages=[
|
| 204 |
{"role": "system", "content": "You are an expert poultry farm management assistant."},
|
| 205 |
{"role": "user", "content": context}
|
|
|
|
| 1 |
import os
|
| 2 |
+
import openai
|
| 3 |
import tensorflow as tf
|
|
|
|
| 4 |
from keras.models import load_model
|
| 5 |
import gradio as gr
|
| 6 |
import cv2
|
| 7 |
import numpy as np
|
| 8 |
from huggingface_hub import login
|
| 9 |
+
from pymongo import MongoClient
|
| 10 |
+
from datetime import datetime
|
| 11 |
import json
|
| 12 |
import requests
|
| 13 |
|
|
|
|
| 17 |
login(token=tok, add_to_git_credential=True)
|
| 18 |
else:
|
| 19 |
print("Warning: Hugging Face token not found in environment variables.")
|
| 20 |
+
|
| 21 |
# Set your OpenAI API key
|
| 22 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 23 |
|
| 24 |
+
# MongoDB Setup
|
| 25 |
+
MONGO_URI = os.getenv("MONGO_URI", "mongodb://localhost:27017/")
|
| 26 |
+
client = MongoClient(MONGO_URI)
|
| 27 |
+
db = client.poultry_farm # Database
|
| 28 |
+
|
| 29 |
# Check GPU availability for TensorFlow
|
| 30 |
print("TensorFlow version:", tf.__version__)
|
| 31 |
print("Eager execution:", tf.executing_eagerly())
|
|
|
|
| 52 |
print(f"Error loading models: {e}")
|
| 53 |
raise
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
class PoultryFarmBot:
|
| 56 |
def __init__(self):
|
| 57 |
+
self.db = db # MongoDB database
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# Health Monitoring and Disease Diagnosis
|
| 60 |
def preprocess_image(self, image):
|
|
|
|
| 83 |
return diagnosis, name, status, recom
|
| 84 |
else: # If the image is not recognized as a chicken disease image
|
| 85 |
return (
|
| 86 |
+
"The uploaded image is not recognized as a chicken or does not appear to be related to any known chicken diseases. "
|
| 87 |
"Please ensure the image is clear and shows a chicken or its symptoms to receive a proper diagnosis."
|
| 88 |
), None, None, None
|
| 89 |
|
|
|
|
| 91 |
if image is not None and image.size > 0: # Ensure image is valid and has elements
|
| 92 |
return self.predict(image)
|
| 93 |
elif symptoms:
|
| 94 |
+
# Generate diagnosis using ChatGPT based on the provided symptoms
|
| 95 |
+
context = f"Symptoms: {symptoms}."
|
| 96 |
+
response = openai.ChatCompletion.create(
|
| 97 |
+
model="gpt-3.5-turbo", # Use GPT-4 if you have access
|
| 98 |
+
messages=[
|
| 99 |
+
{"role": "system",
|
| 100 |
+
"content": "You are an advanced poultry farm management system, helping poultry farmers manage their flocks efficiently."},
|
| 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 or describe the symptoms.", None, None, None
|
| 108 |
|
| 109 |
# Inventory Management
|
| 110 |
def track_inventory(self, item, usage):
|
| 111 |
+
collection = self.db.inventory
|
| 112 |
+
inventory_item = collection.find_one({"item": item})
|
| 113 |
+
if inventory_item:
|
| 114 |
+
new_quantity = inventory_item["quantity"] - usage
|
| 115 |
+
collection.update_one({"item": item}, {"$set": {"quantity": new_quantity}})
|
| 116 |
+
if new_quantity < 10:
|
| 117 |
return f"{item} inventory is low, please reorder."
|
| 118 |
+
return f"{item} inventory updated. Current inventory: {new_quantity} units."
|
| 119 |
+
else:
|
| 120 |
+
return f"Item {item} not recognized in inventory."
|
| 121 |
+
|
| 122 |
+
def add_inventory_item(self, item, quantity):
|
| 123 |
+
collection = self.db.inventory
|
| 124 |
+
if collection.find_one({"item": item}):
|
| 125 |
+
collection.update_one({"item": item}, {"$inc": {"quantity": quantity}})
|
| 126 |
+
else:
|
| 127 |
+
collection.insert_one({"item": item, "quantity": quantity})
|
| 128 |
+
return f"Added {quantity} units of {item} to the inventory."
|
| 129 |
+
|
| 130 |
+
# Chicken and Egg Management
|
| 131 |
+
def add_chicken(self, chicken_id, breed, age):
|
| 132 |
+
collection = self.db.chickens
|
| 133 |
+
collection.insert_one({"chicken_id": chicken_id, "breed": breed, "age": age})
|
| 134 |
+
return f"Chicken {chicken_id} added to the database."
|
| 135 |
+
|
| 136 |
+
def update_chicken(self, chicken_id, update_data):
|
| 137 |
+
collection = self.db.chickens
|
| 138 |
+
collection.update_one({"chicken_id": chicken_id}, {"$set": update_data})
|
| 139 |
+
return f"Chicken {chicken_id} updated."
|
| 140 |
+
|
| 141 |
+
def add_eggs(self, quantity):
|
| 142 |
+
collection = self.db.eggs
|
| 143 |
+
collection.insert_one({"date": datetime.now(), "quantity": quantity})
|
| 144 |
+
return f"Added {quantity} eggs to the database."
|
| 145 |
|
| 146 |
# Reporting and Analytics
|
| 147 |
def generate_report(self):
|
| 148 |
report = {
|
| 149 |
"date": str(datetime.now()),
|
| 150 |
+
"feed_inventory": list(self.db.inventory.find({})),
|
| 151 |
+
"chickens": list(self.db.chickens.find({})),
|
| 152 |
+
"eggs_collected": list(self.db.eggs.find({})),
|
| 153 |
+
"health_reports": list(self.db.health_reports.find({}))
|
| 154 |
}
|
| 155 |
+
self.db.reports.insert_one(report)
|
| 156 |
return json.dumps(report, indent=4)
|
| 157 |
|
| 158 |
# Integration with External Systems
|
|
|
|
| 177 |
return "Unknown emergency type."
|
| 178 |
|
| 179 |
|
| 180 |
+
# Initialize the bot instance
|
| 181 |
+
bot = PoultryFarmBot()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
# Function to generate a response using OpenAI's GPT model
|
| 184 |
def generate_combined_response(image, text):
|
|
|
|
| 191 |
|
| 192 |
# Use OpenAI's GPT model to generate additional advice
|
| 193 |
response = openai.ChatCompletion.create(
|
| 194 |
+
model="gpt-3.5-turbo", # Use GPT-4 or gpt-3.5-turbo based on your API access
|
| 195 |
messages=[
|
| 196 |
{"role": "system", "content": "You are an expert poultry farm management assistant."},
|
| 197 |
{"role": "user", "content": context}
|
requirements.txt
CHANGED
|
@@ -7,3 +7,4 @@ numpy~=1.23.5
|
|
| 7 |
torchvision
|
| 8 |
accelerate
|
| 9 |
openai==0.28
|
|
|
|
|
|
| 7 |
torchvision
|
| 8 |
accelerate
|
| 9 |
openai==0.28
|
| 10 |
+
pymongo
|