Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
import glob
|
| 3 |
-
import math
|
| 4 |
from typing import List, Tuple
|
| 5 |
|
| 6 |
import gradio as gr
|
|
@@ -179,26 +178,15 @@ def build_answer(query: str) -> str:
|
|
| 179 |
|
| 180 |
|
| 181 |
def chat_respond(message: str, history):
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
if isinstance(history, list):
|
| 188 |
-
for item in history:
|
| 189 |
-
if isinstance(item, dict):
|
| 190 |
-
normalized.append(item)
|
| 191 |
-
elif isinstance(item, tuple) and len(item) == 2:
|
| 192 |
-
normalized.append({"role": "user", "content": item[0]})
|
| 193 |
-
normalized.append({"role": "assistant", "content": item[1]})
|
| 194 |
-
|
| 195 |
-
# Append new messages
|
| 196 |
-
normalized.append({"role": "user", "content": message})
|
| 197 |
-
normalized.append({"role": "assistant", "content": answer})
|
| 198 |
-
|
| 199 |
-
# MUST return: (string_answer, list_of_dict_messages)
|
| 200 |
-
return answer, normalized
|
| 201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
|
| 204 |
# -----------------------------
|
|
@@ -207,7 +195,6 @@ def chat_respond(message: str, history):
|
|
| 207 |
|
| 208 |
description = """
|
| 209 |
Ask questions as if you were talking to a knowledge base assistant.
|
| 210 |
-
|
| 211 |
In a real scenario, this assistant would be connected to your own
|
| 212 |
help center or internal documentation. Here, it's using a small demo
|
| 213 |
knowledge base to show how retrieval-based self-service can work.
|
|
@@ -217,21 +204,15 @@ chat = gr.ChatInterface(
|
|
| 217 |
fn=chat_respond,
|
| 218 |
title="Self-Service KB Assistant",
|
| 219 |
description=description,
|
| 220 |
-
|
| 221 |
-
height=420,
|
| 222 |
-
show_copy_button=True,
|
| 223 |
-
type="messages"
|
| 224 |
-
),
|
| 225 |
examples=[
|
| 226 |
"What makes a good knowledge base article?",
|
| 227 |
"How could a KB assistant help agents?",
|
| 228 |
"Why is self-service important for customer support?",
|
| 229 |
],
|
| 230 |
-
cache_examples=False,
|
| 231 |
)
|
| 232 |
|
| 233 |
|
| 234 |
if __name__ == "__main__":
|
| 235 |
-
# On Hugging Face Spaces, you don't need to specify server_name/port,
|
| 236 |
-
# but it's harmless if you do.
|
| 237 |
chat.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import glob
|
|
|
|
| 3 |
from typing import List, Tuple
|
| 4 |
|
| 5 |
import gradio as gr
|
|
|
|
| 178 |
|
| 179 |
|
| 180 |
def chat_respond(message: str, history):
|
| 181 |
+
"""
|
| 182 |
+
Gradio ChatInterface (type='messages') calls this with:
|
| 183 |
+
- message: latest user message (str)
|
| 184 |
+
- history: list of previous messages (handled internally by Gradio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
+
We only need to return the assistant's reply as a string.
|
| 187 |
+
"""
|
| 188 |
+
answer = build_answer(message)
|
| 189 |
+
return answer
|
| 190 |
|
| 191 |
|
| 192 |
# -----------------------------
|
|
|
|
| 195 |
|
| 196 |
description = """
|
| 197 |
Ask questions as if you were talking to a knowledge base assistant.
|
|
|
|
| 198 |
In a real scenario, this assistant would be connected to your own
|
| 199 |
help center or internal documentation. Here, it's using a small demo
|
| 200 |
knowledge base to show how retrieval-based self-service can work.
|
|
|
|
| 204 |
fn=chat_respond,
|
| 205 |
title="Self-Service KB Assistant",
|
| 206 |
description=description,
|
| 207 |
+
type="messages", # use new-style message format
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
examples=[
|
| 209 |
"What makes a good knowledge base article?",
|
| 210 |
"How could a KB assistant help agents?",
|
| 211 |
"Why is self-service important for customer support?",
|
| 212 |
],
|
| 213 |
+
cache_examples=False, # avoid example pre-caching issues on HF Spaces
|
| 214 |
)
|
| 215 |
|
| 216 |
|
| 217 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 218 |
chat.launch()
|