Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,105 +1,109 @@
|
|
1 |
import os
|
2 |
import json
|
|
|
3 |
import gradio as gr
|
4 |
-
from utils.
|
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 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
submit = gr.Button(chatbot_submit_button, variant="primary")
|
98 |
-
|
99 |
-
# Submit and Reset button actions
|
100 |
-
submit.click(fn=chat_interaction, inputs=[user_input, chatbot_output], outputs=chatbot_output)
|
101 |
-
user_input.submit(fn=chat_interaction, inputs=[user_input, chatbot_output], outputs=chatbot_output)
|
102 |
-
reset.click(fn=reset_output, inputs=None, outputs=chatbot_output)
|
103 |
|
104 |
if __name__ == "__main__":
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Configure logging to both file and console
|
8 |
+
log_file_path = "logs/chatbot_interface.log"
|
9 |
+
logging.basicConfig(
|
10 |
+
level=logging.INFO,
|
11 |
+
format="%(asctime)s - %(levelname)s - %(message)s",
|
12 |
+
handlers=[
|
13 |
+
logging.FileHandler(log_file_path, mode='a', encoding='utf-8'), # Save logs to a file
|
14 |
+
logging.StreamHandler() # Print logs to the console
|
15 |
+
]
|
16 |
+
)
|
17 |
+
|
18 |
+
class ChatbotInterface:
|
19 |
+
def __init__(self, config_path: str = 'config/gradio_config.json'):
|
20 |
+
"""
|
21 |
+
Initialize the ChatbotInterface with configuration.
|
22 |
+
:param config_path: Path to the configuration JSON file.
|
23 |
+
"""
|
24 |
+
self.config = self.load_config(config_path)
|
25 |
+
self.title = self.config["chatbot_title"]
|
26 |
+
self.description = self.config["chatbot_description"]
|
27 |
+
self.input_label = self.config["chatbot_input_label"]
|
28 |
+
self.input_placeholder = self.config["chatbot_input_placeholder"]
|
29 |
+
self.output_label = self.config["chatbot_output_label"]
|
30 |
+
self.reset_button = self.config["chatbot_reset_button"]
|
31 |
+
self.submit_button = self.config["chatbot_submit_button"]
|
32 |
+
|
33 |
+
@staticmethod
|
34 |
+
def load_config(config_path: str) -> dict:
|
35 |
+
"""
|
36 |
+
Load the configuration for Gradio GUI interface from the JSON file.
|
37 |
+
:param config_path: Path to the configuration JSON file.
|
38 |
+
:return: Configuration dictionary.
|
39 |
+
"""
|
40 |
+
if not os.path.exists(config_path):
|
41 |
+
logging.error(f"Configuration file not found: {config_path}")
|
42 |
+
raise FileNotFoundError(f"Configuration file not found: {config_path}")
|
43 |
+
|
44 |
+
with open(config_path, 'r') as config_file:
|
45 |
+
config = json.load(config_file)
|
46 |
+
|
47 |
+
required_keys = [
|
48 |
+
"chatbot_title", "chatbot_description", "chatbot_input_label",
|
49 |
+
"chatbot_input_placeholder", "chatbot_output_label",
|
50 |
+
"chatbot_reset_button", "chatbot_submit_button"
|
51 |
+
]
|
52 |
+
for key in required_keys:
|
53 |
+
if key not in config:
|
54 |
+
logging.error(f"Missing required configuration key: {key}")
|
55 |
+
raise ValueError(f"Missing required configuration key: {key}")
|
56 |
+
|
57 |
+
logging.info("Configuration loaded successfully.")
|
58 |
+
return config
|
59 |
+
|
60 |
+
def reset_output() -> list:
|
61 |
+
"""
|
62 |
+
Reset the chatbot output.
|
63 |
+
:return: An empty list to reset the output.
|
64 |
+
"""
|
65 |
+
return []
|
66 |
+
|
67 |
+
def create_interface(self) -> gr.Blocks:
|
68 |
+
"""
|
69 |
+
Create the Gradio Blocks interface.
|
70 |
+
:return: A Gradio Blocks interface object.
|
71 |
+
"""
|
72 |
+
with gr.Blocks() as demo:
|
73 |
+
gr.Markdown(f"## {self.title}\n{self.description}")
|
74 |
+
|
75 |
+
# Chatbot history component
|
76 |
+
chatbot_output = gr.Chatbot(label=self.output_label)
|
77 |
+
|
78 |
+
# User input
|
79 |
+
user_input = gr.Textbox(
|
80 |
+
lines=2,
|
81 |
+
label=self.input_label,
|
82 |
+
placeholder=self.input_placeholder
|
83 |
+
)
|
84 |
+
|
85 |
+
# Buttons
|
86 |
+
with gr.Row():
|
87 |
+
reset = gr.Button(self.reset_button, variant="secondary")
|
88 |
+
submit = gr.Button(self.submit_button, variant="primary")
|
89 |
+
|
90 |
+
# Button actions
|
91 |
+
submit.click(fn=conversation, inputs=[user_input, chatbot_output], outputs=chatbot_output)
|
92 |
+
user_input.submit(fn=conversation, inputs=[user_input, chatbot_output], outputs=chatbot_output)
|
93 |
+
reset.click(fn=self.reset_output, inputs=None, outputs=chatbot_output)
|
94 |
+
|
95 |
+
logging.info("Gradio interface created successfully.")
|
96 |
+
return demo
|
97 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
if __name__ == "__main__":
|
100 |
+
try:
|
101 |
+
# Instantiate the ChatbotInterface class and create the interface
|
102 |
+
chatbot_interface = ChatbotInterface()
|
103 |
+
demo = chatbot_interface.create_interface()
|
104 |
+
|
105 |
+
# Launch the Gradio interface
|
106 |
+
logging.info("Launching the Gradio interface...")
|
107 |
+
demo.launch()
|
108 |
+
except Exception as e:
|
109 |
+
logging.error(f"An error occurred: {e}")
|