Spaces:
Build error
Build error
Create chatbot_interface.py
Browse files- utils/chatbot_interface.py +85 -0
utils/chatbot_interface.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import logging
|
4 |
+
import gradio as gr
|
5 |
+
from utils.chatbot_logic import conversation # Import the chatbot logic
|
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 |
+
|
22 |
+
@staticmethod
|
23 |
+
def load_config(config_path: str) -> dict:
|
24 |
+
"""
|
25 |
+
Load the configuration for Gradio GUI interface from the JSON file.
|
26 |
+
:param config_path: Path to the configuration JSON file.
|
27 |
+
:return: Configuration dictionary.
|
28 |
+
"""
|
29 |
+
if not os.path.exists(config_path):
|
30 |
+
logging.error(f"Configuration file not found: {config_path}")
|
31 |
+
raise FileNotFoundError(f"Configuration file not found: {config_path}")
|
32 |
+
|
33 |
+
with open(config_path, 'r') as config_file:
|
34 |
+
config = json.load(config_file)
|
35 |
+
|
36 |
+
required_keys = [
|
37 |
+
"chatbot_title", "chatbot_description", "chatbot_input_label",
|
38 |
+
"chatbot_input_placeholder", "chatbot_output_label",
|
39 |
+
"chatbot_reset_button", "chatbot_submit_button"
|
40 |
+
]
|
41 |
+
for key in required_keys:
|
42 |
+
if key not in config:
|
43 |
+
logging.error(f"Missing required configuration key: {key}")
|
44 |
+
raise ValueError(f"Missing required configuration key: {key}")
|
45 |
+
|
46 |
+
logging.info("Configuration loaded successfully.")
|
47 |
+
return config
|
48 |
+
|
49 |
+
def reset_output(self) -> list:
|
50 |
+
"""
|
51 |
+
Reset the chatbot output.
|
52 |
+
:return: An empty list to reset the output.
|
53 |
+
"""
|
54 |
+
return []
|
55 |
+
|
56 |
+
def create_interface(self) -> gr.Blocks:
|
57 |
+
"""
|
58 |
+
Create the Gradio Blocks interface.
|
59 |
+
:return: A Gradio Blocks interface object.
|
60 |
+
"""
|
61 |
+
with gr.Blocks() as demo:
|
62 |
+
gr.Markdown(f"## {self.title}\n{self.description}")
|
63 |
+
|
64 |
+
# Chatbot history component
|
65 |
+
chatbot_output = gr.Chatbot(label=self.output_label)
|
66 |
+
|
67 |
+
# User input
|
68 |
+
user_input = gr.Textbox(
|
69 |
+
lines=2,
|
70 |
+
label=self.input_label,
|
71 |
+
placeholder=self.input_placeholder
|
72 |
+
)
|
73 |
+
|
74 |
+
# Buttons
|
75 |
+
with gr.Row():
|
76 |
+
reset = gr.Button(self.reset_button, variant="secondary")
|
77 |
+
submit = gr.Button(self.submit_button, variant="primary")
|
78 |
+
|
79 |
+
# Button actions
|
80 |
+
submit.click(fn=conversation, inputs=[user_input, chatbot_output], outputs=chatbot_output)
|
81 |
+
user_input.submit(fn=conversation, inputs=[user_input, chatbot_output], outputs=chatbot_output)
|
82 |
+
reset.click(fn=self.reset_output, inputs=None, outputs=chatbot_output)
|
83 |
+
|
84 |
+
logging.info("Gradio interface created successfully.")
|
85 |
+
return demo
|