|
|
|
|
|
import gradio as gr |
|
import logging |
|
from services.utils import chatbot_response |
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
def build_chatbot_interface(user): |
|
""" |
|
Build the Gradio interface for the chatbot. |
|
|
|
Args: |
|
user (dict): Authenticated user information. |
|
|
|
Returns: |
|
gr.Blocks: Gradio Blocks object representing the chatbot interface. |
|
""" |
|
try: |
|
logger.info("Building Gradio chatbot interface.") |
|
with gr.Blocks(theme=gr.themes.Soft(primary_hue="green", neutral_hue="blue")) as chatbot_interface: |
|
gr.Markdown(f"# π Poultry Management Chatbot - Welcome, {user['username']}!") |
|
gr.Markdown("Welcome! This chatbot helps you manage your poultry with ease. You can upload an image for disease diagnosis or ask any questions about poultry management.") |
|
|
|
chat_history = gr.Chatbot() |
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
fecal_image = gr.Image( |
|
label="Upload Image of Poultry Feces (Optional)", |
|
type="numpy", |
|
elem_id="image-upload", |
|
show_label=True, |
|
) |
|
with gr.Column(scale=2): |
|
user_input = gr.Textbox( |
|
label="Ask a question", |
|
placeholder="Ask about poultry management...", |
|
lines=3, |
|
elem_id="user-input", |
|
) |
|
|
|
output_box = gr.Textbox( |
|
label="Response", |
|
placeholder="Response will appear here...", |
|
interactive=False, |
|
lines=10, |
|
elem_id="output-box", |
|
) |
|
|
|
submit_button = gr.Button( |
|
"Submit", |
|
variant="primary", |
|
elem_id="submit-button" |
|
) |
|
|
|
def handle_chatbot_response(image, text): |
|
return chatbot_response(image, text, user['username'], None) |
|
|
|
submit_button.click( |
|
fn=handle_chatbot_response, |
|
inputs=[fecal_image, user_input], |
|
outputs=[output_box] |
|
) |
|
logger.info("Chatbot interface built successfully.") |
|
return chatbot_interface |
|
except Exception as e: |
|
logger.error(f"Error building chatbot interface for user {user['username']}: {e}") |
|
raise RuntimeError("Could not build the chatbot interface. Please check the configuration.") |
|
|