AashitaK commited on
Commit
1380137
·
verified ·
1 Parent(s): 016111a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -87
app.py CHANGED
@@ -1,11 +1,8 @@
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",
@@ -15,88 +12,6 @@ logging.basicConfig(
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(self) -> 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
- user_input.submit(fn=conversation, inputs=[user_input, chatbot_output], outputs=chatbot_output)
86
- # Buttons
87
- with gr.Row():
88
- reset = gr.Button(self.reset_button, variant="secondary")
89
- submit = gr.Button(self.submit_button, variant="primary")
90
-
91
- # Button actions
92
- submit.click(fn=conversation, inputs=[user_input, chatbot_output], outputs=chatbot_output)
93
- user_input.submit(fn=conversation, inputs=[user_input, chatbot_output], outputs=chatbot_output)
94
- reset.click(fn=self.reset_output, inputs=None, outputs=chatbot_output)
95
-
96
- logging.info("Gradio interface created successfully.")
97
- return demo
98
-
99
-
100
  if __name__ == "__main__":
101
  try:
102
  # Instantiate the ChatbotInterface class and create the interface
 
 
 
1
  import logging
2
+ from utils.chatbot_interface import ChatbotInterface
 
3
 
4
  # Configure logging to both file and console
5
+ log_file_path = "logs/chatbot_interface.log"
6
  logging.basicConfig(
7
  level=logging.INFO,
8
  format="%(asctime)s - %(levelname)s - %(message)s",
 
12
  ]
13
  )
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  if __name__ == "__main__":
16
  try:
17
  # Instantiate the ChatbotInterface class and create the interface