AashitaK commited on
Commit
e7e6723
·
verified ·
1 Parent(s): f610b19

Update utils/chatbot_interface_test2.py

Browse files
Files changed (1) hide show
  1. utils/chatbot_interface_test2.py +144 -0
utils/chatbot_interface_test2.py CHANGED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import logging
4
+ from typing import Optional
5
+ import gradio as gr
6
+ from utils.response_manager import ResponseManager
7
+
8
+ class ChatbotInterface:
9
+ def __init__(self,
10
+ config_path: str = 'config/gradio_config.json',
11
+ model: str = "gpt-4o-mini",
12
+ temperature: float = 0,
13
+ max_output_tokens: int = 800,
14
+ max_num_results: int = 15,
15
+ vector_store_id: Optional[str] = None,
16
+ api_key: Optional[str] = None,
17
+ meta_prompt_file: Optional[str] = None):
18
+ """
19
+ Initialize the ChatbotInterface with configuration and custom parameters for ResponseManager.
20
+ """
21
+ self.config = self.load_config(config_path)
22
+
23
+ self.title = self.config["chatbot_title"]
24
+ self.description = self.config["chatbot_description"]
25
+
26
+ # Use a placeholder in the textbox for a unified look.
27
+ self.input_placeholder = "How can the CIS agent help you?"
28
+
29
+ self.output_label = self.config["chatbot_output_label"]
30
+ self.submit_button = self.config["chatbot_submit_button"]
31
+
32
+ try:
33
+ self.response_manager = ResponseManager(
34
+ model=model,
35
+ temperature=temperature,
36
+ max_output_tokens=max_output_tokens,
37
+ max_num_results=max_num_results,
38
+ vector_store_id=vector_store_id,
39
+ api_key=api_key,
40
+ meta_prompt_file=meta_prompt_file
41
+ )
42
+
43
+ logging.info(
44
+ "ChatbotInterface initialized with the following parameters:\n"
45
+ f" - Model: {model}\n"
46
+ f" - Temperature: {temperature}\n"
47
+ f" - Max Output Tokens: {max_output_tokens}\n"
48
+ f" - Max Number of Results: {max_num_results}\n"
49
+ )
50
+ except Exception as e:
51
+ logging.error(f"Failed to initialize ResponseManager: {e}")
52
+ raise
53
+
54
+ @staticmethod
55
+ def load_config(config_path: str) -> dict:
56
+ """
57
+ Load the configuration for Gradio GUI interface from the JSON file.
58
+ """
59
+ logging.info(f"Loading configuration from {config_path}...")
60
+ if not os.path.exists(config_path):
61
+ logging.error(f"Configuration file not found: {config_path}")
62
+ raise FileNotFoundError(f"Configuration file not found: {config_path}")
63
+
64
+ with open(config_path, 'r') as config_file:
65
+ config = json.load(config_file)
66
+
67
+ required_keys = [
68
+ "chatbot_title",
69
+ "chatbot_description",
70
+ "chatbot_input_label",
71
+ "chatbot_input_placeholder",
72
+ "chatbot_output_label",
73
+ "chatbot_reset_button",
74
+ "chatbot_submit_button"
75
+ ]
76
+ for key in required_keys:
77
+ if key not in config:
78
+ logging.error(f"Missing required configuration key: {key}")
79
+ raise ValueError(f"Missing required configuration key: {key}")
80
+
81
+ logging.info("Configuration loaded successfully.")
82
+ return config
83
+
84
+ def create_interface(self) -> gr.Blocks:
85
+ """
86
+ Create the Gradio Blocks interface that displays a single container including both
87
+ the text input and a small arrow submit button. The interface will clear the text input
88
+ after each message is submitted.
89
+ """
90
+ logging.info("Creating Gradio interface...")
91
+
92
+ # Load custom CSS from file if available.
93
+ css = ""
94
+ css_path = "style.css"
95
+ if os.path.exists(css_path):
96
+ with open(css_path, "r") as f:
97
+ css = f.read()
98
+
99
+ with gr.Blocks() as demo: #css=css
100
+ # Title and description area.
101
+ gr.Markdown(f"## {self.title}\n{self.description}")
102
+
103
+ # Chatbot output area.
104
+ chatbot_output = gr.Chatbot(label=self.output_label, type="messages")
105
+
106
+ # # Define a local function to reset input
107
+ # def reset_output() -> list:
108
+ # """
109
+ # Reset the chatbot output.
110
+ # :return: An empty list to reset the output.
111
+ # """
112
+ # return [], ""
113
+
114
+ # Define a local function to process input
115
+ def process_input(user_message, chat_history):
116
+ """
117
+ Call generate_response with the user's message and chat history.
118
+ Return a tuple with the updated chat history and an empty string to clear the input.
119
+ """
120
+ updated_history = self.response_manager.generate_response(user_message, chat_history)
121
+ return updated_history#, ""
122
+
123
+ user_input = gr.ChatInterface(
124
+ process_input,
125
+ chatbot=chatbot_output,
126
+ type="messages"
127
+ )
128
+ # reset = gr.ClearButton(
129
+ # value="🔄",
130
+ # variant="primary",
131
+ # elem_id="reset-button",
132
+ # size="sm"
133
+ # )
134
+
135
+
136
+ # Bind the reset button click to the reset function
137
+ reset.click(
138
+ fn=reset_output,
139
+ inputs=None,
140
+ outputs=[chatbot_output, user_input]
141
+ )
142
+
143
+ logging.info("Gradio interface created successfully.")
144
+ return demo