Spaces:
Build error
Build error
Update utils/chatbot_interface.py
Browse files- utils/chatbot_interface.py +89 -33
utils/chatbot_interface.py
CHANGED
|
@@ -1,36 +1,92 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
logging.error("An error occurred while generating a response: %s", str(e))
|
| 35 |
-
history.append((query, f"An error occurred: {str(e)}"))
|
| 36 |
-
return history
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import logging
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from utils.response_manager import ResponseManager
|
| 6 |
+
|
| 7 |
+
class ChatbotInterface:
|
| 8 |
+
def __init__(self, config_path: str = 'config/gradio_config.json'):
|
| 9 |
+
"""
|
| 10 |
+
Initialize the ChatbotInterface with configuration.
|
| 11 |
+
:param config_path: Path to the configuration JSON file.
|
| 12 |
+
"""
|
| 13 |
+
self.config = self.load_config(config_path)
|
| 14 |
+
self.title = self.config["chatbot_title"]
|
| 15 |
+
self.description = self.config["chatbot_description"]
|
| 16 |
+
self.input_label = self.config["chatbot_input_label"]
|
| 17 |
+
self.input_placeholder = self.config["chatbot_input_placeholder"]
|
| 18 |
+
self.output_label = self.config["chatbot_output_label"]
|
| 19 |
+
self.reset_button = self.config["chatbot_reset_button"]
|
| 20 |
+
self.submit_button = self.config["chatbot_submit_button"]
|
| 21 |
+
self.response_manager = ResponseManager()
|
| 22 |
+
self.conversation = self.response_manager.conversation # Shortcut to the conversation method
|
| 23 |
+
logging.info("ChatbotInterface initialized with configuration.")
|
| 24 |
+
|
| 25 |
+
@staticmethod
|
| 26 |
+
def load_config(config_path: str) -> dict:
|
| 27 |
+
"""
|
| 28 |
+
Load the configuration for Gradio GUI interface from the JSON file.
|
| 29 |
+
:param config_path: Path to the configuration JSON file.
|
| 30 |
+
:return: Configuration dictionary.
|
| 31 |
+
"""
|
| 32 |
+
logging.info(f"Loading configuration from {config_path}...")
|
| 33 |
+
if not os.path.exists(config_path):
|
| 34 |
+
logging.error(f"Configuration file not found: {config_path}")
|
| 35 |
+
raise FileNotFoundError(f"Configuration file not found: {config_path}")
|
| 36 |
+
|
| 37 |
+
with open(config_path, 'r') as config_file:
|
| 38 |
+
config = json.load(config_file)
|
| 39 |
+
|
| 40 |
+
required_keys = [
|
| 41 |
+
"chatbot_title", "chatbot_description", "chatbot_input_label",
|
| 42 |
+
"chatbot_input_placeholder", "chatbot_output_label",
|
| 43 |
+
"chatbot_reset_button", "chatbot_submit_button"
|
| 44 |
+
]
|
| 45 |
+
for key in required_keys:
|
| 46 |
+
if key not in config:
|
| 47 |
+
logging.error(f"Missing required configuration key: {key}")
|
| 48 |
+
raise ValueError(f"Missing required configuration key: {key}")
|
| 49 |
+
|
| 50 |
+
logging.info("Configuration loaded successfully.")
|
| 51 |
+
return config
|
| 52 |
+
|
| 53 |
+
def reset_output(self) -> list:
|
| 54 |
+
"""
|
| 55 |
+
Reset the chatbot output.
|
| 56 |
+
:return: An empty list to reset the output.
|
| 57 |
+
"""
|
| 58 |
+
return []
|
| 59 |
+
|
| 60 |
+
def create_interface(self) -> gr.Blocks:
|
| 61 |
+
"""
|
| 62 |
+
Create the Gradio Blocks interface.
|
| 63 |
+
:return: A Gradio Blocks interface object.
|
| 64 |
+
"""
|
| 65 |
+
logging.info("Creating Gradio interface...")
|
| 66 |
+
|
| 67 |
+
# Define the Gradio Blocks interface
|
| 68 |
+
with gr.Blocks() as demo:
|
| 69 |
+
gr.Markdown(f"## {self.title}\n{self.description}")
|
| 70 |
+
|
| 71 |
+
# Chatbot history component
|
| 72 |
+
chatbot_output = gr.Chatbot(label=self.output_label)
|
| 73 |
+
|
| 74 |
+
# User input
|
| 75 |
+
user_input = gr.Textbox(
|
| 76 |
+
lines=2,
|
| 77 |
+
label=self.input_label,
|
| 78 |
+
placeholder=self.input_placeholder
|
| 79 |
)
|
| 80 |
|
| 81 |
+
# Buttons
|
| 82 |
+
with gr.Row():
|
| 83 |
+
reset = gr.Button(self.reset_button, variant="secondary")
|
| 84 |
+
submit = gr.Button(self.submit_button, variant="primary")
|
| 85 |
+
|
| 86 |
+
# Button actions
|
| 87 |
+
submit.click(fn=self.conversation, inputs=[user_input, chatbot_output], outputs=chatbot_output)
|
| 88 |
+
user_input.submit(fn=self.conversation, inputs=[user_input, chatbot_output], outputs=chatbot_output)
|
| 89 |
+
reset.click(fn=self.reset_output, inputs=None, outputs=chatbot_output)
|
| 90 |
|
| 91 |
+
logging.info("Gradio interface created successfully.")
|
| 92 |
+
return demo
|
|
|
|
|
|
|
|
|