Spaces:
Running
Running
Add respond_and_clear function for handling chat messages and clearing input
Browse files
app.py
CHANGED
|
@@ -139,6 +139,28 @@ def load_vector_store():
|
|
| 139 |
print(f"Error loading knowledge base: {str(e)}")
|
| 140 |
return None
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
# Create interface
|
| 143 |
with gr.Blocks() as demo:
|
| 144 |
with gr.Tabs():
|
|
@@ -151,7 +173,7 @@ with gr.Blocks() as demo:
|
|
| 151 |
with gr.Column(scale=3):
|
| 152 |
chatbot = gr.Chatbot(
|
| 153 |
label="Chat",
|
| 154 |
-
|
| 155 |
avatar_images=["user.png", "assistant.png"]
|
| 156 |
)
|
| 157 |
|
|
@@ -162,6 +184,7 @@ with gr.Blocks() as demo:
|
|
| 162 |
scale=4
|
| 163 |
)
|
| 164 |
submit_btn = gr.Button("Send", variant="primary")
|
|
|
|
| 165 |
|
| 166 |
with gr.Column(scale=1):
|
| 167 |
gr.Markdown("### Knowledge Base Management")
|
|
@@ -170,7 +193,7 @@ with gr.Blocks() as demo:
|
|
| 170 |
|
| 171 |
submit_btn.click(
|
| 172 |
respond_and_clear,
|
| 173 |
-
[msg, chatbot, conversation_id],
|
| 174 |
[chatbot, conversation_id, msg]
|
| 175 |
)
|
| 176 |
build_kb_btn.click(build_kb, None, kb_status)
|
|
|
|
| 139 |
print(f"Error loading knowledge base: {str(e)}")
|
| 140 |
return None
|
| 141 |
|
| 142 |
+
def respond_and_clear(message, history, conversation_id):
|
| 143 |
+
"""Handle chat message and clear input"""
|
| 144 |
+
# Get model parameters from config
|
| 145 |
+
max_tokens = MODEL_CONFIG['parameters']['max_length']
|
| 146 |
+
temperature = MODEL_CONFIG['parameters']['temperature']
|
| 147 |
+
top_p = MODEL_CONFIG['parameters']['top_p']
|
| 148 |
+
|
| 149 |
+
# Use system message from constants
|
| 150 |
+
response_generator = respond(
|
| 151 |
+
message=message,
|
| 152 |
+
history=history,
|
| 153 |
+
conversation_id=conversation_id,
|
| 154 |
+
system_message=DEFAULT_SYSTEM_MESSAGE,
|
| 155 |
+
max_tokens=max_tokens,
|
| 156 |
+
temperature=temperature,
|
| 157 |
+
top_p=top_p
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
# Return first yielded response
|
| 161 |
+
response, conv_id = next(response_generator)
|
| 162 |
+
return response, conv_id, "" # Clear message input
|
| 163 |
+
|
| 164 |
# Create interface
|
| 165 |
with gr.Blocks() as demo:
|
| 166 |
with gr.Tabs():
|
|
|
|
| 173 |
with gr.Column(scale=3):
|
| 174 |
chatbot = gr.Chatbot(
|
| 175 |
label="Chat",
|
| 176 |
+
type="messages", # Use new messages format
|
| 177 |
avatar_images=["user.png", "assistant.png"]
|
| 178 |
)
|
| 179 |
|
|
|
|
| 184 |
scale=4
|
| 185 |
)
|
| 186 |
submit_btn = gr.Button("Send", variant="primary")
|
| 187 |
+
clear_btn = gr.Button("Clear") # Add clear button
|
| 188 |
|
| 189 |
with gr.Column(scale=1):
|
| 190 |
gr.Markdown("### Knowledge Base Management")
|
|
|
|
| 193 |
|
| 194 |
submit_btn.click(
|
| 195 |
respond_and_clear,
|
| 196 |
+
[msg, chatbot, conversation_id],
|
| 197 |
[chatbot, conversation_id, msg]
|
| 198 |
)
|
| 199 |
build_kb_btn.click(build_kb, None, kb_status)
|