Spaces:
Sleeping
Sleeping
Create chatbot_interface3.py
Browse files- utils/chatbot_interface3.py +160 -0
utils/chatbot_interface3.py
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
:param config_path: Path to the configuration JSON file.
|
21 |
+
:param model: The OpenAI model to use (default: 'gpt-4o-mini').
|
22 |
+
:param temperature: The temperature for response generation (default: 0).
|
23 |
+
:param max_output_tokens: The maximum number of output tokens (default: 800).
|
24 |
+
:param max_num_results: The maximum number of search results to return (default: 15).
|
25 |
+
:param vector_store_id: The ID of the vector store to use for file search.
|
26 |
+
:param api_key: The OpenAI API key for authentication.
|
27 |
+
:param meta_prompt_file: Path to the meta prompt file .
|
28 |
+
"""
|
29 |
+
self.config = self.load_config(config_path)
|
30 |
+
self.title = self.config["chatbot_title"]
|
31 |
+
self.description = self.config["chatbot_description"]
|
32 |
+
self.input_label = self.config["chatbot_input_label"]
|
33 |
+
self.input_placeholder = self.config["chatbot_input_placeholder"]
|
34 |
+
self.output_label = self.config["chatbot_output_label"]
|
35 |
+
self.reset_button = self.config["chatbot_reset_button"]
|
36 |
+
self.submit_button = self.config["chatbot_submit_button"]
|
37 |
+
|
38 |
+
# Initialize ResponseManager with custom parameters
|
39 |
+
try:
|
40 |
+
self.response_manager = ResponseManager(
|
41 |
+
model=model,
|
42 |
+
temperature=temperature,
|
43 |
+
max_output_tokens=max_output_tokens,
|
44 |
+
max_num_results=max_num_results,
|
45 |
+
vector_store_id=vector_store_id,
|
46 |
+
api_key=api_key,
|
47 |
+
meta_prompt_file=meta_prompt_file
|
48 |
+
)
|
49 |
+
logging.info(
|
50 |
+
"ChatbotInterface initialized with the following parameters:\n"
|
51 |
+
f" - Model: {model}\n"
|
52 |
+
f" - Temperature: {temperature}\n"
|
53 |
+
f" - Max Output Tokens: {max_output_tokens}\n"
|
54 |
+
f" - Max Number of Results: {max_num_results}\n"
|
55 |
+
f" - Vector Store ID: {vector_store_id}\n"
|
56 |
+
f" - API Key: {'Provided' if api_key else 'Not Provided'}\n"
|
57 |
+
f" - Meta Prompt File: {meta_prompt_file or 'Default'}"
|
58 |
+
)
|
59 |
+
except Exception as e:
|
60 |
+
logging.error(f"Failed to initialize ResponseManager: {e}")
|
61 |
+
raise
|
62 |
+
|
63 |
+
|
64 |
+
@staticmethod
|
65 |
+
def load_config(config_path: str) -> dict:
|
66 |
+
"""
|
67 |
+
Load the configuration for Gradio GUI interface from the JSON file.
|
68 |
+
:param config_path: Path to the configuration JSON file.
|
69 |
+
:return: Configuration dictionary.
|
70 |
+
"""
|
71 |
+
logging.info(f"Loading configuration from {config_path}...")
|
72 |
+
if not os.path.exists(config_path):
|
73 |
+
logging.error(f"Configuration file not found: {config_path}")
|
74 |
+
raise FileNotFoundError(f"Configuration file not found: {config_path}")
|
75 |
+
|
76 |
+
with open(config_path, 'r') as config_file:
|
77 |
+
config = json.load(config_file)
|
78 |
+
|
79 |
+
required_keys = [
|
80 |
+
"chatbot_title", "chatbot_description", "chatbot_input_label",
|
81 |
+
"chatbot_input_placeholder", "chatbot_output_label",
|
82 |
+
"chatbot_reset_button", "chatbot_submit_button"
|
83 |
+
]
|
84 |
+
for key in required_keys:
|
85 |
+
if key not in config:
|
86 |
+
logging.error(f"Missing required configuration key: {key}")
|
87 |
+
raise ValueError(f"Missing required configuration key: {key}")
|
88 |
+
|
89 |
+
logging.info("Configuration loaded successfully.")
|
90 |
+
return config
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
+
def create_interface(self) -> gr.Blocks:
|
95 |
+
"""
|
96 |
+
Create the Gradio Blocks interface that displays a single container including both
|
97 |
+
the text input and a small arrow submit button. The interface will clear the text input
|
98 |
+
after each message is submitted.
|
99 |
+
"""
|
100 |
+
logging.info("Creating Gradio interface...")
|
101 |
+
|
102 |
+
|
103 |
+
with gr.Blocks() as demo:
|
104 |
+
# Title and description area.
|
105 |
+
gr.Markdown(f"## {self.title}\n{self.description}")
|
106 |
+
|
107 |
+
# Chatbot output area.
|
108 |
+
chatbot_output = gr.Chatbot(label=self.output_label, type="messages")
|
109 |
+
|
110 |
+
# Use a gr.Row container as the input box with an integrated submit button.
|
111 |
+
with gr.Row(elem_id="input-container"):
|
112 |
+
user_input = gr.Textbox(
|
113 |
+
lines=2,
|
114 |
+
label=self.input_label,
|
115 |
+
placeholder=self.input_placeholder,
|
116 |
+
elem_id="chat-input"
|
117 |
+
)
|
118 |
+
|
119 |
+
reset = gr.ClearButton(
|
120 |
+
value="🔄",
|
121 |
+
variant="secondary",
|
122 |
+
elem_id="reset-button",
|
123 |
+
size="md"
|
124 |
+
)
|
125 |
+
|
126 |
+
# Define a local function to reset input
|
127 |
+
def reset_output() -> list:
|
128 |
+
"""
|
129 |
+
Reset the chatbot output.
|
130 |
+
:return: An empty list to reset the output.
|
131 |
+
"""
|
132 |
+
return [], ""
|
133 |
+
|
134 |
+
# Define a local function to process input
|
135 |
+
def process_input(user_message, chat_history):
|
136 |
+
"""
|
137 |
+
Call generate_response with the user's message and chat history.
|
138 |
+
Return a tuple with the updated chat history and an empty string to clear the input.
|
139 |
+
"""
|
140 |
+
updated_history = self.response_manager.generate_response(user_message, chat_history)
|
141 |
+
return updated_history, ""
|
142 |
+
|
143 |
+
|
144 |
+
# Bind the reset button click to the reset function
|
145 |
+
reset.click(
|
146 |
+
fn=reset_output,
|
147 |
+
inputs=None,
|
148 |
+
outputs=[chatbot_output, user_input]
|
149 |
+
)
|
150 |
+
|
151 |
+
|
152 |
+
# Bind the Enter key (textbox submit) to the same processing function
|
153 |
+
user_input.submit(
|
154 |
+
fn=process_input,
|
155 |
+
inputs=[user_input, chatbot_output],
|
156 |
+
outputs=[chatbot_output, user_input]
|
157 |
+
)
|
158 |
+
|
159 |
+
logging.info("Gradio interface created successfully.")
|
160 |
+
return demo
|