Emmanuel Frimpong Asante
commited on
Commit
·
ad9f33c
1
Parent(s):
ec08f1b
update space
Browse files
app.py
CHANGED
@@ -20,6 +20,35 @@ dotenv.load_dotenv()
|
|
20 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
21 |
logger = logging.getLogger(__name__)
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
# Hugging Face token setup
|
24 |
tok = os.getenv('HF_TOKEN')
|
25 |
if tok:
|
@@ -29,14 +58,6 @@ if tok:
|
|
29 |
else:
|
30 |
logger.warning("Hugging Face token not found in environment variables.")
|
31 |
|
32 |
-
# MongoDB Setup
|
33 |
-
MONGO_URI = os.getenv("MONGO_URI")
|
34 |
-
logger.info("Connecting to MongoDB.")
|
35 |
-
client = MongoClient(MONGO_URI)
|
36 |
-
db = client.poultry_farm # Connect to the 'poultry_farm' database
|
37 |
-
enquiries_collection = db.enquiries # Collection to store farmer enquiries
|
38 |
-
users_collection = db.users # Collection to store user credentials
|
39 |
-
|
40 |
# GPU Setup for TensorFlow
|
41 |
logger.info("TensorFlow version: %s", tf.__version__)
|
42 |
logger.info("Eager execution: %s", tf.executing_eagerly())
|
@@ -348,65 +369,6 @@ def build_gradio_interface():
|
|
348 |
gr.Blocks: Gradio Blocks object representing the chatbot interface.
|
349 |
"""
|
350 |
logger.info("Building Gradio interface.")
|
351 |
-
with gr.Blocks(theme=gr.themes.
|
352 |
gr.Markdown("# 🐔 Poultry Management Chatbot")
|
353 |
-
gr.Markdown("This chatbot helps you manage your poultry
|
354 |
-
|
355 |
-
with gr.Row():
|
356 |
-
with gr.Column(scale=1):
|
357 |
-
fecal_image = gr.Image(
|
358 |
-
label="Upload Image of Poultry Feces (Optional)",
|
359 |
-
type="numpy",
|
360 |
-
elem_id="image-upload",
|
361 |
-
show_label=True,
|
362 |
-
)
|
363 |
-
with gr.Column(scale=2):
|
364 |
-
user_input = gr.Textbox(
|
365 |
-
label="Ask a question",
|
366 |
-
placeholder="Ask about poultry management...",
|
367 |
-
lines=3,
|
368 |
-
elem_id="user-input",
|
369 |
-
)
|
370 |
-
username = gr.Textbox(
|
371 |
-
label="Username",
|
372 |
-
placeholder="Enter your username",
|
373 |
-
lines=1,
|
374 |
-
elem_id="username-input",
|
375 |
-
)
|
376 |
-
password = gr.Textbox(
|
377 |
-
label="Password",
|
378 |
-
placeholder="Enter your password",
|
379 |
-
type="password",
|
380 |
-
lines=1,
|
381 |
-
elem_id="password-input",
|
382 |
-
)
|
383 |
-
|
384 |
-
output_box = gr.Textbox(
|
385 |
-
label="Response",
|
386 |
-
placeholder="Response will appear here...",
|
387 |
-
interactive=False,
|
388 |
-
lines=10,
|
389 |
-
elem_id="output-box",
|
390 |
-
)
|
391 |
-
|
392 |
-
submit_button = gr.Button(
|
393 |
-
"Submit",
|
394 |
-
variant="primary",
|
395 |
-
elem_id="submit-button"
|
396 |
-
)
|
397 |
-
# Connect the submit button to the chatbot response function
|
398 |
-
submit_button.click(
|
399 |
-
fn=chatbot_response,
|
400 |
-
inputs=[fecal_image, user_input, username, password],
|
401 |
-
outputs=[output_box]
|
402 |
-
)
|
403 |
-
logger.info("Gradio interface built successfully.")
|
404 |
-
return chatbot_interface
|
405 |
-
|
406 |
-
# Launch the Gradio interface
|
407 |
-
if __name__ == "__main__":
|
408 |
-
logger.info("Launching Gradio interface.")
|
409 |
-
interface = build_gradio_interface()
|
410 |
-
# Launch the interface with queuing enabled for concurrent requests
|
411 |
-
interface.queue().launch(debug=True, share=True)
|
412 |
-
logger.info("Gradio interface launched.")
|
|
|
20 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
21 |
logger = logging.getLogger(__name__)
|
22 |
|
23 |
+
# MongoDB Setup for logging and audit
|
24 |
+
MONGO_URI = os.getenv("MONGO_URI")
|
25 |
+
logger.info("Connecting to MongoDB.")
|
26 |
+
client = MongoClient(MONGO_URI)
|
27 |
+
db = client.poultry_farm # Connect to the 'poultry_farm' database
|
28 |
+
enquiries_collection = db.enquiries # Collection to store farmer enquiries
|
29 |
+
users_collection = db.users # Collection to store user credentials
|
30 |
+
logs_collection = db.logs # Collection to store application logs
|
31 |
+
|
32 |
+
def log_to_db(level, message):
|
33 |
+
log_entry = {
|
34 |
+
"level": level,
|
35 |
+
"message": message,
|
36 |
+
"timestamp": datetime.utcnow()
|
37 |
+
}
|
38 |
+
logs_collection.insert_one(log_entry)
|
39 |
+
|
40 |
+
# Override logger methods to also log to MongoDB
|
41 |
+
class MongoHandler(logging.Handler):
|
42 |
+
def emit(self, record):
|
43 |
+
log_entry = self.format(record)
|
44 |
+
log_to_db(record.levelname, log_entry)
|
45 |
+
|
46 |
+
mongo_handler = MongoHandler()
|
47 |
+
mongo_handler.setLevel(logging.INFO)
|
48 |
+
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
49 |
+
mongo_handler.setFormatter(formatter)
|
50 |
+
logger.addHandler(mongo_handler)
|
51 |
+
|
52 |
# Hugging Face token setup
|
53 |
tok = os.getenv('HF_TOKEN')
|
54 |
if tok:
|
|
|
58 |
else:
|
59 |
logger.warning("Hugging Face token not found in environment variables.")
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
# GPU Setup for TensorFlow
|
62 |
logger.info("TensorFlow version: %s", tf.__version__)
|
63 |
logger.info("Eager execution: %s", tf.executing_eagerly())
|
|
|
369 |
gr.Blocks: Gradio Blocks object representing the chatbot interface.
|
370 |
"""
|
371 |
logger.info("Building Gradio interface.")
|
372 |
+
with gr.Blocks(theme=gr.themes.Base()):
|
373 |
gr.Markdown("# 🐔 Poultry Management Chatbot")
|
374 |
+
gr.Markdown("Welcome! This chatbot helps you manage your poultry with ease. You
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|