Emmanuel Frimpong Asante
commited on
Commit
·
119d04f
1
Parent(s):
8d902e1
update space
Browse files- app.py +1 -2
- chatbot_interface.py +2 -1
- utils.py +38 -0
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# File
|
| 2 |
|
| 3 |
# Import necessary libraries
|
| 4 |
import os
|
|
@@ -12,7 +12,6 @@ from utils import PoultryFarmBot, llama3_response
|
|
| 12 |
from chatbot_interface import build_chatbot_interface # Importing chatbot interface function
|
| 13 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 14 |
|
| 15 |
-
|
| 16 |
# Setup logging for better monitoring
|
| 17 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 18 |
logger = logging.getLogger(__name__)
|
|
|
|
| 1 |
+
# File: main.py
|
| 2 |
|
| 3 |
# Import necessary libraries
|
| 4 |
import os
|
|
|
|
| 12 |
from chatbot_interface import build_chatbot_interface # Importing chatbot interface function
|
| 13 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 14 |
|
|
|
|
| 15 |
# Setup logging for better monitoring
|
| 16 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 17 |
logger = logging.getLogger(__name__)
|
chatbot_interface.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
-
# File
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import logging
|
|
|
|
| 5 |
|
| 6 |
logger = logging.getLogger(__name__)
|
| 7 |
|
|
|
|
| 1 |
+
# File: chatbot_interface.py
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import logging
|
| 5 |
+
from utils import chatbot_response # Import chatbot response function from utils
|
| 6 |
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
utils.py
CHANGED
|
@@ -72,6 +72,44 @@ def llama3_response(user_input, tokenizer, model):
|
|
| 72 |
logger.error(f"Error generating response: {str(e)}")
|
| 73 |
return f"Error generating response: {str(e)}"
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
class PoultryFarmBot:
|
| 76 |
def __init__(self, db):
|
| 77 |
"""
|
|
|
|
| 72 |
logger.error(f"Error generating response: {str(e)}")
|
| 73 |
return f"Error generating response: {str(e)}"
|
| 74 |
|
| 75 |
+
def chatbot_response(image, text, username, password):
|
| 76 |
+
"""
|
| 77 |
+
Handle user input and generate appropriate responses.
|
| 78 |
+
|
| 79 |
+
Args:
|
| 80 |
+
image (numpy.ndarray): Image input for disease detection.
|
| 81 |
+
text (str): Text input for general queries.
|
| 82 |
+
username (str): Username for authentication.
|
| 83 |
+
password (str): Password for authentication.
|
| 84 |
+
|
| 85 |
+
Returns:
|
| 86 |
+
str: Response generated by the chatbot.
|
| 87 |
+
"""
|
| 88 |
+
logger.info("Handling user input for chatbot response.")
|
| 89 |
+
user = bot.authenticate_user(username, password)
|
| 90 |
+
if not user:
|
| 91 |
+
return "Authentication failed. Please check your username and password."
|
| 92 |
+
|
| 93 |
+
user_id = user['_id']
|
| 94 |
+
# If an image is provided, diagnose the disease
|
| 95 |
+
if image is not None:
|
| 96 |
+
logger.info("Image input detected. Proceeding with disease diagnosis.")
|
| 97 |
+
diagnosis, name, status, recom = bot.diagnose_disease(image)
|
| 98 |
+
if name and status and recom:
|
| 99 |
+
logger.info("Diagnosis complete.")
|
| 100 |
+
bot.log_enquiry("image", "Image Enquiry", diagnosis, user_id)
|
| 101 |
+
return diagnosis
|
| 102 |
+
else:
|
| 103 |
+
logger.warning("Diagnosis incomplete.")
|
| 104 |
+
bot.log_enquiry("image", "Image Enquiry", diagnosis, user_id)
|
| 105 |
+
return diagnosis
|
| 106 |
+
else:
|
| 107 |
+
# Generate a response using Llama 3.2 for general text input
|
| 108 |
+
logger.info("Text input detected. Generating response.")
|
| 109 |
+
response = llama3_response(text, tokenizer, model)
|
| 110 |
+
bot.log_enquiry("text", text, response, user_id)
|
| 111 |
+
return response
|
| 112 |
+
|
| 113 |
class PoultryFarmBot:
|
| 114 |
def __init__(self, db):
|
| 115 |
"""
|