maxiaolong03
commited on
Commit
·
ed56ef4
1
Parent(s):
810a793
add files
Browse files
app.py
CHANGED
|
@@ -18,21 +18,20 @@ This script provides a Gradio interface for interacting with a chatbot based on
|
|
| 18 |
|
| 19 |
import argparse
|
| 20 |
import base64
|
| 21 |
-
|
| 22 |
-
from datetime import datetime
|
| 23 |
-
from functools import partial
|
| 24 |
import hashlib
|
| 25 |
import json
|
| 26 |
import logging
|
| 27 |
-
import faiss
|
| 28 |
import os
|
| 29 |
-
from argparse import ArgumentParser
|
| 30 |
import textwrap
|
| 31 |
-
import
|
|
|
|
|
|
|
|
|
|
| 32 |
|
|
|
|
| 33 |
import gradio as gr
|
| 34 |
import numpy as np
|
| 35 |
-
|
| 36 |
from bot_requests import BotClient
|
| 37 |
|
| 38 |
os.environ["NO_PROXY"] = "localhost,127.0.0.1" # Disable proxy
|
|
@@ -40,13 +39,15 @@ os.environ["NO_PROXY"] = "localhost,127.0.0.1" # Disable proxy
|
|
| 40 |
logging.root.setLevel(logging.INFO)
|
| 41 |
|
| 42 |
FILE_URL_DEFAULT = "data/coffee.txt"
|
| 43 |
-
RELEVANT_PASSAGE_DEFAULT = textwrap.dedent(
|
|
|
|
| 44 |
1675年时,英格兰就有3000多家咖啡馆;启蒙运动时期,咖啡馆成为民众深入讨论宗教和政治的聚集地,
|
| 45 |
1670年代的英国国王查理二世就曾试图取缔咖啡馆。这一时期的英国人认为咖啡具有药用价值,
|
| 46 |
甚至名医也会推荐将咖啡用于医疗。"""
|
| 47 |
)
|
| 48 |
|
| 49 |
-
QUERY_REWRITE_PROMPT = textwrap.dedent(
|
|
|
|
| 50 |
【当前时间】
|
| 51 |
{TIMESTAMP}
|
| 52 |
|
|
@@ -108,21 +109,13 @@ def get_args() -> argparse.Namespace:
|
|
| 108 |
"""
|
| 109 |
parser = ArgumentParser(description="ERNIE models web chat demo.")
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
parser.add_argument(
|
| 112 |
-
"--
|
| 113 |
-
|
| 114 |
-
parser.add_argument(
|
| 115 |
-
"--server-name", type=str, default="0.0.0.0", help="Demo server name."
|
| 116 |
-
)
|
| 117 |
-
parser.add_argument(
|
| 118 |
-
"--max_char", type=int, default=20000, help="Maximum character limit for messages."
|
| 119 |
-
)
|
| 120 |
-
parser.add_argument(
|
| 121 |
-
"--max_retry_num", type=int, default=3, help="Maximum retry number for request."
|
| 122 |
-
)
|
| 123 |
-
parser.add_argument(
|
| 124 |
-
"--model_map",
|
| 125 |
-
type=str,
|
| 126 |
default="{\"ernie-4.5-turbo-128k-preview\": \"https://qianfan.baidubce.com/v2\"}",
|
| 127 |
help="""JSON string defining model name to endpoint mappings.
|
| 128 |
Required Format:
|
|
@@ -133,56 +126,18 @@ def get_args() -> argparse.Namespace:
|
|
| 133 |
- Specify ONE model endpoint in JSON format.
|
| 134 |
- Prefix determines model capabilities:
|
| 135 |
* ERNIE-4.5: Text-only model
|
| 136 |
-
"""
|
| 137 |
-
)
|
| 138 |
-
parser.add_argument(
|
| 139 |
-
"--embedding_service_url",
|
| 140 |
-
type=str,
|
| 141 |
-
default="https://qianfan.baidubce.com/v2",
|
| 142 |
-
help="Embedding service url."
|
| 143 |
)
|
| 144 |
parser.add_argument(
|
| 145 |
-
"--
|
| 146 |
-
type=str,
|
| 147 |
-
default=os.environ.get("API_KEY"),
|
| 148 |
-
help="Qianfan API key.",
|
| 149 |
-
)
|
| 150 |
-
parser.add_argument(
|
| 151 |
-
"--embedding_model",
|
| 152 |
-
type=str,
|
| 153 |
-
default="embedding-v1",
|
| 154 |
-
help="Embedding model name."
|
| 155 |
-
)
|
| 156 |
-
parser.add_argument(
|
| 157 |
-
"--embedding_dim",
|
| 158 |
-
type=int,
|
| 159 |
-
default=384,
|
| 160 |
-
help="Dimension of the embedding vector."
|
| 161 |
-
)
|
| 162 |
-
parser.add_argument(
|
| 163 |
-
"--chunk_size",
|
| 164 |
-
type=int,
|
| 165 |
-
default=512,
|
| 166 |
-
help="Chunk size for splitting long documents."
|
| 167 |
-
)
|
| 168 |
-
parser.add_argument(
|
| 169 |
-
"--top_k",
|
| 170 |
-
type=int,
|
| 171 |
-
default=3,
|
| 172 |
-
help="Top k results to retrieve."
|
| 173 |
-
)
|
| 174 |
-
parser.add_argument(
|
| 175 |
-
"--faiss_index_path",
|
| 176 |
-
type=str,
|
| 177 |
-
default="data/faiss_index",
|
| 178 |
-
help="Faiss index path."
|
| 179 |
-
)
|
| 180 |
-
parser.add_argument(
|
| 181 |
-
"--text_db_path",
|
| 182 |
-
type=str,
|
| 183 |
-
default="data/text_db.jsonl",
|
| 184 |
-
help="Text database path."
|
| 185 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
args = parser.parse_args()
|
| 188 |
try:
|
|
@@ -192,7 +147,7 @@ def get_args() -> argparse.Namespace:
|
|
| 192 |
if len(args.model_map) < 1:
|
| 193 |
raise ValueError("model_map must contain at least one model configuration")
|
| 194 |
except json.JSONDecodeError as e:
|
| 195 |
-
raise ValueError("Invalid JSON format for --
|
| 196 |
|
| 197 |
return args
|
| 198 |
|
|
@@ -202,6 +157,7 @@ class FaissTextDatabase:
|
|
| 202 |
A vector database for text retrieval using FAISS.
|
| 203 |
Provides efficient similarity search and document management capabilities.
|
| 204 |
"""
|
|
|
|
| 205 |
def __init__(self, args, bot_client: BotClient):
|
| 206 |
"""
|
| 207 |
Initialize the FaissTextDatabase.
|
|
@@ -212,14 +168,14 @@ class FaissTextDatabase:
|
|
| 212 |
embedding_dim: dimension of the embedding vector
|
| 213 |
"""
|
| 214 |
self.logger = logging.getLogger(__name__)
|
| 215 |
-
|
| 216 |
self.bot_client = bot_client
|
| 217 |
self.embedding_dim = getattr(args, "embedding_dim", 384)
|
| 218 |
self.top_k = getattr(args, "top_k", 3)
|
| 219 |
self.context_size = getattr(args, "context_size", 2)
|
| 220 |
self.faiss_index_path = getattr(args, "faiss_index_path", "data/faiss_index")
|
| 221 |
self.text_db_path = getattr(args, "text_db_path", "data/text_db.jsonl")
|
| 222 |
-
|
| 223 |
# If faiss_index_path exists, load it and text_db_path
|
| 224 |
if os.path.exists(self.faiss_index_path) and os.path.exists(self.text_db_path):
|
| 225 |
self.index = faiss.read_index(self.faiss_index_path)
|
|
@@ -227,11 +183,8 @@ class FaissTextDatabase:
|
|
| 227 |
self.text_db = json.load(f)
|
| 228 |
else:
|
| 229 |
self.index = faiss.IndexFlatIP(self.embedding_dim)
|
| 230 |
-
self.text_db = {
|
| 231 |
-
|
| 232 |
-
"chunks": [] # Save chunks
|
| 233 |
-
}
|
| 234 |
-
|
| 235 |
def calculate_md5(self, file_path: str) -> str:
|
| 236 |
"""
|
| 237 |
Calculate the MD5 hash of a file
|
|
@@ -257,51 +210,51 @@ class FaissTextDatabase:
|
|
| 257 |
"""
|
| 258 |
file_md5 = self.calculate_md5(file_path)
|
| 259 |
return file_md5 in self.text_db["file_md5s"]
|
| 260 |
-
|
| 261 |
-
def add_embeddings(
|
| 262 |
-
|
|
|
|
| 263 |
"""
|
| 264 |
Stores document embeddings in FAISS database after checking for duplicates.
|
| 265 |
Generates embeddings for each text segment, updates the FAISS index and metadata database,
|
| 266 |
and persists changes to disk. Includes optional progress tracking for Gradio interfaces.
|
| 267 |
-
|
| 268 |
Args:
|
| 269 |
file_path: the path of the source file
|
| 270 |
segments: the list of segments
|
| 271 |
progress_bar: the progress bar object
|
| 272 |
-
|
| 273 |
Returns:
|
| 274 |
bool: whether the operation was successful
|
| 275 |
"""
|
| 276 |
file_md5 = self.calculate_md5(file_path)
|
| 277 |
if file_md5 in self.text_db["file_md5s"]:
|
| 278 |
-
self.logger.info("File already processed: {file_path} (MD5: {file_md5})"
|
| 279 |
-
file_path=file_path,
|
| 280 |
-
file_md5=file_md5
|
| 281 |
-
))
|
| 282 |
return False
|
| 283 |
-
|
| 284 |
# Generate embeddings
|
| 285 |
vectors = []
|
| 286 |
file_name = os.path.basename(file_path)
|
| 287 |
file_txt = "".join(file_name.split(".")[:-1])[:30]
|
| 288 |
-
for i, segment in
|
| 289 |
vectors.append(self.bot_client.embed_fn(file_txt + "\n" + segment))
|
| 290 |
if progress_bar is not None:
|
| 291 |
progress_bar((i + 1) / len(segments), desc=file_name + " Processing...")
|
| 292 |
vectors = np.array(vectors)
|
| 293 |
self.index.add(vectors.astype('float32'))
|
| 294 |
-
|
| 295 |
start_id = len(self.text_db["chunks"])
|
| 296 |
for i, text in enumerate(segments):
|
| 297 |
-
self.text_db["chunks"].append(
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
|
|
|
|
|
|
| 305 |
self.text_db["file_md5s"].append(file_md5)
|
| 306 |
if save_file:
|
| 307 |
self.save()
|
|
@@ -312,10 +265,10 @@ class FaissTextDatabase:
|
|
| 312 |
Finds the most relevant text chunks for multiple queries and includes surrounding context.
|
| 313 |
Uses FAISS to find the closest matching embeddings, then retrieves adjacent chunks
|
| 314 |
from the same source document to provide better context understanding.
|
| 315 |
-
|
| 316 |
Args:
|
| 317 |
query_list: list of input query strings
|
| 318 |
-
|
| 319 |
Returns:
|
| 320 |
str: the concatenated output string
|
| 321 |
"""
|
|
@@ -325,51 +278,55 @@ class FaissTextDatabase:
|
|
| 325 |
query_vector = np.array([self.bot_client.embed_fn(query)]).astype('float32')
|
| 326 |
_, indices = self.index.search(query_vector, self.top_k)
|
| 327 |
all_indices.extend(indices[0].tolist())
|
| 328 |
-
|
| 329 |
# Step 2: Remove duplicate indices
|
| 330 |
-
unique_indices = sorted(
|
| 331 |
self.logger.info(f"Retrieved indices: {all_indices}")
|
| 332 |
self.logger.info(f"Unique indices after deduplication: {unique_indices}")
|
| 333 |
-
|
| 334 |
# Step 3: Expand each index with context (within same file boundaries)
|
| 335 |
expanded_indices = set()
|
| 336 |
file_boundaries = {} # {file_md5: (start_idx, end_idx)}
|
| 337 |
for target_idx in unique_indices:
|
| 338 |
target_chunk = self.text_db["chunks"][target_idx]
|
| 339 |
target_file_md5 = target_chunk["file_md5"]
|
| 340 |
-
|
| 341 |
if target_file_md5 not in file_boundaries:
|
| 342 |
file_start = target_idx
|
| 343 |
while file_start > 0 and self.text_db["chunks"][file_start - 1]["file_md5"] == target_file_md5:
|
| 344 |
file_start -= 1
|
| 345 |
file_end = target_idx
|
| 346 |
-
while (
|
| 347 |
-
self.text_db["chunks"]
|
|
|
|
|
|
|
| 348 |
file_end += 1
|
| 349 |
else:
|
| 350 |
file_start, file_end = file_boundaries[target_file_md5]
|
| 351 |
-
|
| 352 |
# Calculate context range within file boundaries
|
| 353 |
start = max(file_start, target_idx - self.context_size)
|
| 354 |
end = min(file_end, target_idx + self.context_size)
|
| 355 |
-
|
| 356 |
for pos in range(start, end + 1):
|
| 357 |
expanded_indices.add(pos)
|
| 358 |
-
|
| 359 |
# Step 4: Sort and merge continuous chunks
|
| 360 |
-
sorted_indices = sorted(
|
| 361 |
groups = []
|
| 362 |
current_group = [sorted_indices[0]]
|
| 363 |
for i in range(1, len(sorted_indices)):
|
| 364 |
-
if (
|
| 365 |
-
|
| 366 |
-
self.text_db["chunks"][sorted_indices[i
|
|
|
|
|
|
|
| 367 |
current_group.append(sorted_indices[i])
|
| 368 |
else:
|
| 369 |
groups.append(current_group)
|
| 370 |
current_group = [sorted_indices[i]]
|
| 371 |
groups.append(current_group)
|
| 372 |
-
|
| 373 |
# Step 5: Create merged text for each group
|
| 374 |
result = ""
|
| 375 |
for idx, group in enumerate(groups):
|
|
@@ -377,22 +334,23 @@ class FaissTextDatabase:
|
|
| 377 |
for idx in group:
|
| 378 |
result += self.text_db["chunks"][idx]["text"] + "\n"
|
| 379 |
self.logger.info(f"Merged chunk range: {group[0]}-{group[-1]}")
|
| 380 |
-
|
| 381 |
return result
|
| 382 |
-
|
| 383 |
def save(self) -> None:
|
| 384 |
"""Save the database to disk."""
|
| 385 |
faiss.write_index(self.index, self.faiss_index_path)
|
| 386 |
-
|
| 387 |
with open(self.text_db_path, 'w', encoding='utf-8') as f:
|
| 388 |
json.dump(self.text_db, f, ensure_ascii=False, indent=2)
|
| 389 |
|
| 390 |
|
| 391 |
-
class GradioEvents
|
| 392 |
"""
|
| 393 |
Manages event handling and UI interactions for Gradio applications.
|
| 394 |
Provides methods to process user inputs, trigger callbacks, and update interface components.
|
| 395 |
"""
|
|
|
|
| 396 |
@staticmethod
|
| 397 |
def get_history_conversation(task_history: list) -> tuple:
|
| 398 |
"""
|
|
@@ -412,16 +370,16 @@ class GradioEvents(object):
|
|
| 412 |
for query_h, response_h in task_history:
|
| 413 |
conversation.append({"role": "user", "content": query_h})
|
| 414 |
conversation.append({"role": "assistant", "content": response_h})
|
| 415 |
-
conversation_str += "user:\n{
|
| 416 |
return conversation, conversation_str
|
| 417 |
|
| 418 |
@staticmethod
|
| 419 |
def chat_stream(
|
| 420 |
-
query: str,
|
| 421 |
-
task_history: list,
|
| 422 |
-
model: str,
|
| 423 |
faiss_db: FaissTextDatabase,
|
| 424 |
-
bot_client: BotClient,
|
| 425 |
) -> dict:
|
| 426 |
"""
|
| 427 |
Streams chatbot responses by processing queries with context from history and FAISS database.
|
|
@@ -439,11 +397,10 @@ class GradioEvents(object):
|
|
| 439 |
dict: A dictionary containing the event type and its corresponding content.
|
| 440 |
"""
|
| 441 |
conversation, conversation_str = GradioEvents.get_history_conversation(task_history)
|
| 442 |
-
conversation_str += "user:\n{query}\n"
|
| 443 |
|
| 444 |
search_info_message = QUERY_REWRITE_PROMPT.format(
|
| 445 |
-
TIMESTAMP=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 446 |
-
CONVERSATION=conversation_str
|
| 447 |
)
|
| 448 |
search_conversation = [{"role": "user", "content": search_info_message}]
|
| 449 |
search_info_result = GradioEvents.get_sub_query(search_conversation, model, bot_client)
|
|
@@ -453,11 +410,11 @@ class GradioEvents(object):
|
|
| 453 |
if search_info_result.get("query", []):
|
| 454 |
relevant_passages = faiss_db.search_with_context(search_info_result["query"])
|
| 455 |
yield {"type": "relevant_passage", "content": relevant_passages}
|
| 456 |
-
|
| 457 |
query = ANSWER_PROMPT.format(
|
| 458 |
-
DOC_CONTENT=relevant_passages,
|
| 459 |
-
TIMESTAMP=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 460 |
-
CONVERSATION=conversation_str
|
| 461 |
)
|
| 462 |
|
| 463 |
conversation.append({"role": "user", "content": query})
|
|
@@ -466,22 +423,22 @@ class GradioEvents(object):
|
|
| 466 |
for chunk in bot_client.process_stream(model, req_data):
|
| 467 |
if "error" in chunk:
|
| 468 |
raise Exception(chunk["error"])
|
| 469 |
-
|
| 470 |
message = chunk.get("choices", [{}])[0].get("delta", {})
|
| 471 |
content = message.get("content", "")
|
| 472 |
-
|
| 473 |
if content:
|
| 474 |
yield {"type": "answer", "content": content}
|
| 475 |
-
|
| 476 |
except Exception as e:
|
| 477 |
raise gr.Error("Exception: " + repr(e))
|
| 478 |
|
| 479 |
@staticmethod
|
| 480 |
def predict_stream(
|
| 481 |
-
query: str,
|
| 482 |
-
chatbot: list,
|
| 483 |
task_history: list,
|
| 484 |
-
model: str,
|
| 485 |
faiss_db: FaissTextDatabase,
|
| 486 |
bot_client: BotClient,
|
| 487 |
) -> tuple:
|
|
@@ -493,7 +450,7 @@ class GradioEvents(object):
|
|
| 493 |
Args:
|
| 494 |
query (str): The content of the user's input query.
|
| 495 |
chatbot (list): The chatbot's historical message list.
|
| 496 |
-
task_history (list): The task history record list.
|
| 497 |
model (Model): The model used to generate responses.
|
| 498 |
bot_client (object): The chatbot client object.
|
| 499 |
faiss_db (FaissTextDatabase): The FAISS database instance.
|
|
@@ -503,16 +460,16 @@ class GradioEvents(object):
|
|
| 503 |
"""
|
| 504 |
query = query if query else QUERY_DEFAULT
|
| 505 |
|
| 506 |
-
logging.info("User: {}"
|
| 507 |
-
chatbot.append({"role": "user", "content": query})
|
| 508 |
-
|
| 509 |
# First yield the chatbot with user message
|
| 510 |
yield chatbot, None
|
| 511 |
|
| 512 |
new_texts = GradioEvents.chat_stream(
|
| 513 |
-
query,
|
| 514 |
-
task_history,
|
| 515 |
-
model,
|
| 516 |
faiss_db,
|
| 517 |
bot_client,
|
| 518 |
)
|
|
@@ -522,7 +479,7 @@ class GradioEvents(object):
|
|
| 522 |
for new_text in new_texts:
|
| 523 |
if not isinstance(new_text, dict):
|
| 524 |
continue
|
| 525 |
-
|
| 526 |
if new_text.get("type") == "embedding":
|
| 527 |
current_relevant_passage = new_text["content"]
|
| 528 |
yield chatbot, current_relevant_passage
|
|
@@ -533,24 +490,24 @@ class GradioEvents(object):
|
|
| 533 |
continue
|
| 534 |
elif new_text.get("type") == "answer":
|
| 535 |
response += new_text["content"]
|
| 536 |
-
|
| 537 |
# Remove previous message if exists
|
| 538 |
if chatbot[-1].get("role") == "assistant":
|
| 539 |
chatbot.pop(-1)
|
| 540 |
-
|
| 541 |
if response:
|
| 542 |
chatbot.append({"role": "assistant", "content": response})
|
| 543 |
yield chatbot, current_relevant_passage
|
| 544 |
|
| 545 |
-
logging.info("History: {}"
|
| 546 |
-
task_history.append((query, response))
|
| 547 |
-
logging.info("ERNIE models: {}"
|
| 548 |
|
| 549 |
@staticmethod
|
| 550 |
def regenerate(
|
| 551 |
-
chatbot: list,
|
| 552 |
-
task_history: list,
|
| 553 |
-
model: str,
|
| 554 |
faiss_db: FaissTextDatabase,
|
| 555 |
bot_client: BotClient,
|
| 556 |
) -> tuple:
|
|
@@ -576,15 +533,14 @@ class GradioEvents(object):
|
|
| 576 |
chatbot.pop(-1)
|
| 577 |
chatbot.pop(-1)
|
| 578 |
|
| 579 |
-
|
| 580 |
-
item[0],
|
| 581 |
-
chatbot,
|
| 582 |
-
task_history,
|
| 583 |
-
model,
|
| 584 |
faiss_db,
|
| 585 |
bot_client,
|
| 586 |
-
)
|
| 587 |
-
yield chunk, relevant_passage
|
| 588 |
|
| 589 |
@staticmethod
|
| 590 |
def reset_user_input() -> gr.update:
|
|
@@ -605,19 +561,15 @@ class GradioEvents(object):
|
|
| 605 |
tuple: A named tuple containing the updated values for chatbot, task_history, file_btn, and relevant_passage
|
| 606 |
"""
|
| 607 |
GradioEvents.gc()
|
| 608 |
-
|
| 609 |
-
reset_result = namedtuple("reset_result",
|
| 610 |
-
["chatbot",
|
| 611 |
-
"task_history",
|
| 612 |
-
"file_btn",
|
| 613 |
-
"relevant_passage"])
|
| 614 |
return reset_result(
|
| 615 |
[], # clear chatbot
|
| 616 |
[], # clear task_history
|
| 617 |
gr.update(value=None), # clear file_btn
|
| 618 |
-
gr.update(value=None) # reset relevant_passage
|
| 619 |
)
|
| 620 |
-
|
| 621 |
@staticmethod
|
| 622 |
def gc():
|
| 623 |
"""
|
|
@@ -644,7 +596,7 @@ class GradioEvents(object):
|
|
| 644 |
extension = image_path.split(".")[-1]
|
| 645 |
with open(image_path, "rb") as image_file:
|
| 646 |
base64_image = base64.b64encode(image_file.read()).decode("utf-8")
|
| 647 |
-
url = "data:image/{
|
| 648 |
return url
|
| 649 |
|
| 650 |
@staticmethod
|
|
@@ -696,20 +648,20 @@ class GradioEvents(object):
|
|
| 696 |
|
| 697 |
if len(line) <= chunk_size:
|
| 698 |
return line, ""
|
| 699 |
-
|
| 700 |
# Search from chunk_size position backwards
|
| 701 |
split_pos = chunk_size
|
| 702 |
for i in range(chunk_size, 0, -1):
|
| 703 |
if line[i] in PUNCTUATIONS:
|
| 704 |
split_pos = i + 1 # Include punctuation
|
| 705 |
break
|
| 706 |
-
|
| 707 |
# Fallback to whitespace if no punctuation found
|
| 708 |
if split_pos == chunk_size:
|
| 709 |
split_pos = line.rfind(" ", 0, chunk_size)
|
| 710 |
if split_pos == -1:
|
| 711 |
split_pos = chunk_size # Hard split
|
| 712 |
-
|
| 713 |
return line[:split_pos], line[split_pos:]
|
| 714 |
|
| 715 |
@staticmethod
|
|
@@ -735,7 +687,7 @@ class GradioEvents(object):
|
|
| 735 |
chunks = []
|
| 736 |
current_chunk = []
|
| 737 |
current_length = 0
|
| 738 |
-
|
| 739 |
for line in lines:
|
| 740 |
# If adding this line would exceed chunk size (and we have content)
|
| 741 |
if current_length + len(line) > chunk_size and current_chunk:
|
|
@@ -747,22 +699,19 @@ class GradioEvents(object):
|
|
| 747 |
while len(line) > chunk_size:
|
| 748 |
head, line = GradioEvents.split_oversized_line(line, chunk_size)
|
| 749 |
chunks.append(head)
|
| 750 |
-
|
| 751 |
# Add remaining line content
|
| 752 |
if line:
|
| 753 |
current_chunk.append(line)
|
| 754 |
current_length += len(line) + 1
|
| 755 |
-
|
| 756 |
if current_chunk:
|
| 757 |
chunks.append("\n".join(current_chunk))
|
| 758 |
return chunks
|
| 759 |
|
| 760 |
@staticmethod
|
| 761 |
def file_upload(
|
| 762 |
-
files_url: list,
|
| 763 |
-
chunk_size: int,
|
| 764 |
-
faiss_db: FaissTextDatabase,
|
| 765 |
-
progress_bar: gr.Progress = gr.Progress()
|
| 766 |
) -> str:
|
| 767 |
"""
|
| 768 |
Uploads and processes multiple files by splitting them into semantically meaningful chunks,
|
|
@@ -783,13 +732,18 @@ class GradioEvents(object):
|
|
| 783 |
for file_url in files_url:
|
| 784 |
if not GradioEvents.save_file_to_db(file_url, chunk_size, faiss_db, progress_bar):
|
| 785 |
file_name = os.path.basename(file_url)
|
| 786 |
-
gr.Info("{} already processed."
|
| 787 |
|
| 788 |
yield gr.update(visible=False)
|
| 789 |
|
| 790 |
@staticmethod
|
| 791 |
-
def save_file_to_db(
|
| 792 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 793 |
"""
|
| 794 |
Processes and indexes document content into FAISS database with semantic-aware chunking.
|
| 795 |
Handles file validation, text segmentation, embedding generation and storage operations.
|
|
@@ -804,31 +758,31 @@ class GradioEvents(object):
|
|
| 804 |
bool: True if the file was saved successfully, otherwise False.
|
| 805 |
"""
|
| 806 |
if not os.path.exists(file_url):
|
| 807 |
-
logging.error("File not found: {}"
|
| 808 |
return False
|
| 809 |
|
| 810 |
file_name = os.path.basename(file_url)
|
| 811 |
if not faiss_db.is_file_processed(file_url):
|
| 812 |
-
logging.info("{} not processed yet, processing now..."
|
| 813 |
try:
|
| 814 |
segments = GradioEvents.split_text_into_chunks(file_url, chunk_size)
|
| 815 |
faiss_db.add_embeddings(file_url, segments, progress_bar, save_file)
|
| 816 |
|
| 817 |
-
logging.info("{} processed successfully."
|
| 818 |
return True
|
| 819 |
except Exception as e:
|
| 820 |
-
logging.error("Error processing {}: {}"
|
| 821 |
-
gr.Error("Error processing file: {}"
|
| 822 |
raise
|
| 823 |
else:
|
| 824 |
-
logging.info("{} already processed."
|
| 825 |
return False
|
| 826 |
|
| 827 |
|
| 828 |
def launch_demo(args: argparse.Namespace, bot_client: BotClient, faiss_db_template: FaissTextDatabase):
|
| 829 |
"""
|
| 830 |
Launch demo program
|
| 831 |
-
|
| 832 |
Args:
|
| 833 |
args (argparse.Namespace): argparse Namespace object containing parsed command line arguments
|
| 834 |
bot_client (BotClient): Bot client instance
|
|
@@ -855,38 +809,41 @@ def launch_demo(args: argparse.Namespace, bot_client: BotClient, faiss_db_templa
|
|
| 855 |
}
|
| 856 |
"""
|
| 857 |
with gr.Blocks(css=css) as demo:
|
| 858 |
-
model_name = gr.State(
|
| 859 |
faiss_db = gr.State(copy.deepcopy(faiss_db_template))
|
| 860 |
|
| 861 |
logo_url = GradioEvents.get_image_url("assets/logo.png")
|
| 862 |
-
gr.Markdown(
|
| 863 |
-
|
| 864 |
-
|
|
|
|
|
|
|
| 865 |
gr.Markdown(
|
| 866 |
"""\
|
| 867 |
<center><font size=3>This demo is based on ERNIE models. \
|
| 868 |
(本演示基于文心大模型实现。)</center>"""
|
| 869 |
)
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
|
|
|
|
|
|
|
|
|
|
| 874 |
)
|
| 875 |
|
|
|
|
|
|
|
| 876 |
with gr.Row(equal_height=True):
|
| 877 |
file_btn = gr.File(
|
| 878 |
-
label="Knowledge Base Upload (System default will be used if none provided. Accepted formats: TXT, MD)",
|
| 879 |
-
height="150px",
|
| 880 |
file_types=[".txt", ".md"],
|
| 881 |
elem_id="file-upload",
|
| 882 |
-
file_count="multiple"
|
| 883 |
)
|
| 884 |
relevant_passage = gr.Textbox(
|
| 885 |
-
label="Relevant Passage",
|
| 886 |
-
lines=5,
|
| 887 |
-
max_lines=5,
|
| 888 |
-
placeholder=RELEVANT_PASSAGE_DEFAULT,
|
| 889 |
-
interactive=False
|
| 890 |
)
|
| 891 |
with gr.Row():
|
| 892 |
progress_bar = gr.Textbox(label="Progress", visible=False)
|
|
@@ -897,21 +854,15 @@ def launch_demo(args: argparse.Namespace, bot_client: BotClient, faiss_db_templa
|
|
| 897 |
empty_btn = gr.Button("🧹 Clear History(清除历史)")
|
| 898 |
submit_btn = gr.Button("🚀 Submit(发送)", elem_id="submit-button")
|
| 899 |
regen_btn = gr.Button("🤔️ Regenerate(重试)")
|
| 900 |
-
|
| 901 |
task_history = gr.State([])
|
| 902 |
-
|
| 903 |
-
predict_with_clients = partial(
|
| 904 |
-
|
| 905 |
-
bot_client=bot_client
|
| 906 |
-
)
|
| 907 |
-
regenerate_with_clients = partial(
|
| 908 |
-
GradioEvents.regenerate,
|
| 909 |
-
bot_client=bot_client
|
| 910 |
-
)
|
| 911 |
file_upload_with_clients = partial(
|
| 912 |
GradioEvents.file_upload,
|
| 913 |
)
|
| 914 |
-
|
| 915 |
chunk_size = gr.State(args.chunk_size)
|
| 916 |
file_btn.change(
|
| 917 |
fn=file_upload_with_clients,
|
|
@@ -919,34 +870,30 @@ def launch_demo(args: argparse.Namespace, bot_client: BotClient, faiss_db_templa
|
|
| 919 |
outputs=[progress_bar],
|
| 920 |
)
|
| 921 |
query.submit(
|
| 922 |
-
predict_with_clients,
|
| 923 |
-
inputs=[query, chatbot, task_history, model_name, faiss_db],
|
| 924 |
outputs=[chatbot, relevant_passage],
|
| 925 |
-
show_progress=True
|
| 926 |
)
|
| 927 |
query.submit(GradioEvents.reset_user_input, [], [query])
|
| 928 |
submit_btn.click(
|
| 929 |
-
predict_with_clients,
|
| 930 |
inputs=[query, chatbot, task_history, model_name, faiss_db],
|
| 931 |
outputs=[chatbot, relevant_passage],
|
| 932 |
show_progress=True,
|
| 933 |
)
|
| 934 |
submit_btn.click(GradioEvents.reset_user_input, [], [query])
|
| 935 |
empty_btn.click(
|
| 936 |
-
GradioEvents.reset_state,
|
| 937 |
-
outputs=[chatbot, task_history, file_btn, relevant_passage], show_progress=True
|
| 938 |
)
|
| 939 |
regen_btn.click(
|
| 940 |
-
regenerate_with_clients,
|
| 941 |
inputs=[chatbot, task_history, model_name, faiss_db],
|
| 942 |
outputs=[chatbot, relevant_passage],
|
| 943 |
-
show_progress=True
|
| 944 |
)
|
| 945 |
|
| 946 |
-
demo.queue().launch(
|
| 947 |
-
server_port=args.server_port,
|
| 948 |
-
server_name=args.server_name
|
| 949 |
-
)
|
| 950 |
|
| 951 |
|
| 952 |
def main():
|
|
@@ -960,5 +907,6 @@ def main():
|
|
| 960 |
|
| 961 |
launch_demo(args, bot_client, faiss_db)
|
| 962 |
|
|
|
|
| 963 |
if __name__ == "__main__":
|
| 964 |
main()
|
|
|
|
| 18 |
|
| 19 |
import argparse
|
| 20 |
import base64
|
| 21 |
+
import copy
|
|
|
|
|
|
|
| 22 |
import hashlib
|
| 23 |
import json
|
| 24 |
import logging
|
|
|
|
| 25 |
import os
|
|
|
|
| 26 |
import textwrap
|
| 27 |
+
from argparse import ArgumentParser
|
| 28 |
+
from collections import namedtuple
|
| 29 |
+
from datetime import datetime
|
| 30 |
+
from functools import partial
|
| 31 |
|
| 32 |
+
import faiss
|
| 33 |
import gradio as gr
|
| 34 |
import numpy as np
|
|
|
|
| 35 |
from bot_requests import BotClient
|
| 36 |
|
| 37 |
os.environ["NO_PROXY"] = "localhost,127.0.0.1" # Disable proxy
|
|
|
|
| 39 |
logging.root.setLevel(logging.INFO)
|
| 40 |
|
| 41 |
FILE_URL_DEFAULT = "data/coffee.txt"
|
| 42 |
+
RELEVANT_PASSAGE_DEFAULT = textwrap.dedent(
|
| 43 |
+
"""\
|
| 44 |
1675年时,英格兰就有3000多家咖啡馆;启蒙运动时期,咖啡馆成为民众深入讨论宗教和政治的聚集地,
|
| 45 |
1670年代的英国国王查理二世就曾试图取缔咖啡馆。这一时期的英国人认为咖啡具有药用价值,
|
| 46 |
甚至名医也会推荐将咖啡用于医疗。"""
|
| 47 |
)
|
| 48 |
|
| 49 |
+
QUERY_REWRITE_PROMPT = textwrap.dedent(
|
| 50 |
+
"""\
|
| 51 |
【当前时间】
|
| 52 |
{TIMESTAMP}
|
| 53 |
|
|
|
|
| 109 |
"""
|
| 110 |
parser = ArgumentParser(description="ERNIE models web chat demo.")
|
| 111 |
|
| 112 |
+
parser.add_argument("--server-port", type=int, default=7860, help="Demo server port.")
|
| 113 |
+
parser.add_argument("--server-name", type=str, default="0.0.0.0", help="Demo server name.")
|
| 114 |
+
parser.add_argument("--max_char", type=int, default=20000, help="Maximum character limit for messages.")
|
| 115 |
+
parser.add_argument("--max_retry_num", type=int, default=3, help="Maximum retry number for request.")
|
| 116 |
parser.add_argument(
|
| 117 |
+
"--model_map",
|
| 118 |
+
type=str,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
default="{\"ernie-4.5-turbo-128k-preview\": \"https://qianfan.baidubce.com/v2\"}",
|
| 120 |
help="""JSON string defining model name to endpoint mappings.
|
| 121 |
Required Format:
|
|
|
|
| 126 |
- Specify ONE model endpoint in JSON format.
|
| 127 |
- Prefix determines model capabilities:
|
| 128 |
* ERNIE-4.5: Text-only model
|
| 129 |
+
""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
)
|
| 131 |
parser.add_argument(
|
| 132 |
+
"--embedding_service_url", type=str, default="https://qianfan.baidubce.com/v2", help="Embedding service url."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
)
|
| 134 |
+
parser.add_argument("--qianfan_api_key", type=str, default=os.environ.get("API_KEY"), help="Qianfan API key.")
|
| 135 |
+
parser.add_argument("--embedding_model", type=str, default="embedding-v1", help="Embedding model name.")
|
| 136 |
+
parser.add_argument("--embedding_dim", type=int, default=384, help="Dimension of the embedding vector.")
|
| 137 |
+
parser.add_argument("--chunk_size", type=int, default=512, help="Chunk size for splitting long documents.")
|
| 138 |
+
parser.add_argument("--top_k", type=int, default=3, help="Top k results to retrieve.")
|
| 139 |
+
parser.add_argument("--faiss_index_path", type=str, default="data/faiss_index", help="Faiss index path.")
|
| 140 |
+
parser.add_argument("--text_db_path", type=str, default="data/text_db.jsonl", help="Text database path.")
|
| 141 |
|
| 142 |
args = parser.parse_args()
|
| 143 |
try:
|
|
|
|
| 147 |
if len(args.model_map) < 1:
|
| 148 |
raise ValueError("model_map must contain at least one model configuration")
|
| 149 |
except json.JSONDecodeError as e:
|
| 150 |
+
raise ValueError("Invalid JSON format for --model_map") from e
|
| 151 |
|
| 152 |
return args
|
| 153 |
|
|
|
|
| 157 |
A vector database for text retrieval using FAISS.
|
| 158 |
Provides efficient similarity search and document management capabilities.
|
| 159 |
"""
|
| 160 |
+
|
| 161 |
def __init__(self, args, bot_client: BotClient):
|
| 162 |
"""
|
| 163 |
Initialize the FaissTextDatabase.
|
|
|
|
| 168 |
embedding_dim: dimension of the embedding vector
|
| 169 |
"""
|
| 170 |
self.logger = logging.getLogger(__name__)
|
| 171 |
+
|
| 172 |
self.bot_client = bot_client
|
| 173 |
self.embedding_dim = getattr(args, "embedding_dim", 384)
|
| 174 |
self.top_k = getattr(args, "top_k", 3)
|
| 175 |
self.context_size = getattr(args, "context_size", 2)
|
| 176 |
self.faiss_index_path = getattr(args, "faiss_index_path", "data/faiss_index")
|
| 177 |
self.text_db_path = getattr(args, "text_db_path", "data/text_db.jsonl")
|
| 178 |
+
|
| 179 |
# If faiss_index_path exists, load it and text_db_path
|
| 180 |
if os.path.exists(self.faiss_index_path) and os.path.exists(self.text_db_path):
|
| 181 |
self.index = faiss.read_index(self.faiss_index_path)
|
|
|
|
| 183 |
self.text_db = json.load(f)
|
| 184 |
else:
|
| 185 |
self.index = faiss.IndexFlatIP(self.embedding_dim)
|
| 186 |
+
self.text_db = {"file_md5s": [], "chunks": []} # Save file_md5s to avoid duplicates # Save chunks
|
| 187 |
+
|
|
|
|
|
|
|
|
|
|
| 188 |
def calculate_md5(self, file_path: str) -> str:
|
| 189 |
"""
|
| 190 |
Calculate the MD5 hash of a file
|
|
|
|
| 210 |
"""
|
| 211 |
file_md5 = self.calculate_md5(file_path)
|
| 212 |
return file_md5 in self.text_db["file_md5s"]
|
| 213 |
+
|
| 214 |
+
def add_embeddings(
|
| 215 |
+
self, file_path: str, segments: list[str], progress_bar: gr.Progress = None, save_file: bool = False
|
| 216 |
+
) -> bool:
|
| 217 |
"""
|
| 218 |
Stores document embeddings in FAISS database after checking for duplicates.
|
| 219 |
Generates embeddings for each text segment, updates the FAISS index and metadata database,
|
| 220 |
and persists changes to disk. Includes optional progress tracking for Gradio interfaces.
|
| 221 |
+
|
| 222 |
Args:
|
| 223 |
file_path: the path of the source file
|
| 224 |
segments: the list of segments
|
| 225 |
progress_bar: the progress bar object
|
| 226 |
+
|
| 227 |
Returns:
|
| 228 |
bool: whether the operation was successful
|
| 229 |
"""
|
| 230 |
file_md5 = self.calculate_md5(file_path)
|
| 231 |
if file_md5 in self.text_db["file_md5s"]:
|
| 232 |
+
self.logger.info(f"File already processed: {file_path} (MD5: {file_md5})")
|
|
|
|
|
|
|
|
|
|
| 233 |
return False
|
| 234 |
+
|
| 235 |
# Generate embeddings
|
| 236 |
vectors = []
|
| 237 |
file_name = os.path.basename(file_path)
|
| 238 |
file_txt = "".join(file_name.split(".")[:-1])[:30]
|
| 239 |
+
for i, segment in enumerate(segments):
|
| 240 |
vectors.append(self.bot_client.embed_fn(file_txt + "\n" + segment))
|
| 241 |
if progress_bar is not None:
|
| 242 |
progress_bar((i + 1) / len(segments), desc=file_name + " Processing...")
|
| 243 |
vectors = np.array(vectors)
|
| 244 |
self.index.add(vectors.astype('float32'))
|
| 245 |
+
|
| 246 |
start_id = len(self.text_db["chunks"])
|
| 247 |
for i, text in enumerate(segments):
|
| 248 |
+
self.text_db["chunks"].append(
|
| 249 |
+
{
|
| 250 |
+
"file_md5": file_md5,
|
| 251 |
+
"file_name": file_name,
|
| 252 |
+
"file_txt": file_txt,
|
| 253 |
+
"text": text,
|
| 254 |
+
"vector_id": start_id + i,
|
| 255 |
+
}
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
self.text_db["file_md5s"].append(file_md5)
|
| 259 |
if save_file:
|
| 260 |
self.save()
|
|
|
|
| 265 |
Finds the most relevant text chunks for multiple queries and includes surrounding context.
|
| 266 |
Uses FAISS to find the closest matching embeddings, then retrieves adjacent chunks
|
| 267 |
from the same source document to provide better context understanding.
|
| 268 |
+
|
| 269 |
Args:
|
| 270 |
query_list: list of input query strings
|
| 271 |
+
|
| 272 |
Returns:
|
| 273 |
str: the concatenated output string
|
| 274 |
"""
|
|
|
|
| 278 |
query_vector = np.array([self.bot_client.embed_fn(query)]).astype('float32')
|
| 279 |
_, indices = self.index.search(query_vector, self.top_k)
|
| 280 |
all_indices.extend(indices[0].tolist())
|
| 281 |
+
|
| 282 |
# Step 2: Remove duplicate indices
|
| 283 |
+
unique_indices = sorted(set(all_indices))
|
| 284 |
self.logger.info(f"Retrieved indices: {all_indices}")
|
| 285 |
self.logger.info(f"Unique indices after deduplication: {unique_indices}")
|
| 286 |
+
|
| 287 |
# Step 3: Expand each index with context (within same file boundaries)
|
| 288 |
expanded_indices = set()
|
| 289 |
file_boundaries = {} # {file_md5: (start_idx, end_idx)}
|
| 290 |
for target_idx in unique_indices:
|
| 291 |
target_chunk = self.text_db["chunks"][target_idx]
|
| 292 |
target_file_md5 = target_chunk["file_md5"]
|
| 293 |
+
|
| 294 |
if target_file_md5 not in file_boundaries:
|
| 295 |
file_start = target_idx
|
| 296 |
while file_start > 0 and self.text_db["chunks"][file_start - 1]["file_md5"] == target_file_md5:
|
| 297 |
file_start -= 1
|
| 298 |
file_end = target_idx
|
| 299 |
+
while (
|
| 300 |
+
file_end < len(self.text_db["chunks"]) - 1
|
| 301 |
+
and self.text_db["chunks"][file_end + 1]["file_md5"] == target_file_md5
|
| 302 |
+
):
|
| 303 |
file_end += 1
|
| 304 |
else:
|
| 305 |
file_start, file_end = file_boundaries[target_file_md5]
|
| 306 |
+
|
| 307 |
# Calculate context range within file boundaries
|
| 308 |
start = max(file_start, target_idx - self.context_size)
|
| 309 |
end = min(file_end, target_idx + self.context_size)
|
| 310 |
+
|
| 311 |
for pos in range(start, end + 1):
|
| 312 |
expanded_indices.add(pos)
|
| 313 |
+
|
| 314 |
# Step 4: Sort and merge continuous chunks
|
| 315 |
+
sorted_indices = sorted(expanded_indices)
|
| 316 |
groups = []
|
| 317 |
current_group = [sorted_indices[0]]
|
| 318 |
for i in range(1, len(sorted_indices)):
|
| 319 |
+
if (
|
| 320 |
+
sorted_indices[i] == sorted_indices[i - 1] + 1
|
| 321 |
+
and self.text_db["chunks"][sorted_indices[i]]["file_md5"]
|
| 322 |
+
== self.text_db["chunks"][sorted_indices[i - 1]]["file_md5"]
|
| 323 |
+
):
|
| 324 |
current_group.append(sorted_indices[i])
|
| 325 |
else:
|
| 326 |
groups.append(current_group)
|
| 327 |
current_group = [sorted_indices[i]]
|
| 328 |
groups.append(current_group)
|
| 329 |
+
|
| 330 |
# Step 5: Create merged text for each group
|
| 331 |
result = ""
|
| 332 |
for idx, group in enumerate(groups):
|
|
|
|
| 334 |
for idx in group:
|
| 335 |
result += self.text_db["chunks"][idx]["text"] + "\n"
|
| 336 |
self.logger.info(f"Merged chunk range: {group[0]}-{group[-1]}")
|
| 337 |
+
|
| 338 |
return result
|
| 339 |
+
|
| 340 |
def save(self) -> None:
|
| 341 |
"""Save the database to disk."""
|
| 342 |
faiss.write_index(self.index, self.faiss_index_path)
|
| 343 |
+
|
| 344 |
with open(self.text_db_path, 'w', encoding='utf-8') as f:
|
| 345 |
json.dump(self.text_db, f, ensure_ascii=False, indent=2)
|
| 346 |
|
| 347 |
|
| 348 |
+
class GradioEvents:
|
| 349 |
"""
|
| 350 |
Manages event handling and UI interactions for Gradio applications.
|
| 351 |
Provides methods to process user inputs, trigger callbacks, and update interface components.
|
| 352 |
"""
|
| 353 |
+
|
| 354 |
@staticmethod
|
| 355 |
def get_history_conversation(task_history: list) -> tuple:
|
| 356 |
"""
|
|
|
|
| 370 |
for query_h, response_h in task_history:
|
| 371 |
conversation.append({"role": "user", "content": query_h})
|
| 372 |
conversation.append({"role": "assistant", "content": response_h})
|
| 373 |
+
conversation_str += f"user:\n{query_h}\n assistant:\n{response_h}\n "
|
| 374 |
return conversation, conversation_str
|
| 375 |
|
| 376 |
@staticmethod
|
| 377 |
def chat_stream(
|
| 378 |
+
query: str,
|
| 379 |
+
task_history: list,
|
| 380 |
+
model: str,
|
| 381 |
faiss_db: FaissTextDatabase,
|
| 382 |
+
bot_client: BotClient,
|
| 383 |
) -> dict:
|
| 384 |
"""
|
| 385 |
Streams chatbot responses by processing queries with context from history and FAISS database.
|
|
|
|
| 397 |
dict: A dictionary containing the event type and its corresponding content.
|
| 398 |
"""
|
| 399 |
conversation, conversation_str = GradioEvents.get_history_conversation(task_history)
|
| 400 |
+
conversation_str += f"user:\n{query}\n"
|
| 401 |
|
| 402 |
search_info_message = QUERY_REWRITE_PROMPT.format(
|
| 403 |
+
TIMESTAMP=datetime.now().strftime("%Y-%m-%d %H:%M:%S"), CONVERSATION=conversation_str
|
|
|
|
| 404 |
)
|
| 405 |
search_conversation = [{"role": "user", "content": search_info_message}]
|
| 406 |
search_info_result = GradioEvents.get_sub_query(search_conversation, model, bot_client)
|
|
|
|
| 410 |
if search_info_result.get("query", []):
|
| 411 |
relevant_passages = faiss_db.search_with_context(search_info_result["query"])
|
| 412 |
yield {"type": "relevant_passage", "content": relevant_passages}
|
| 413 |
+
|
| 414 |
query = ANSWER_PROMPT.format(
|
| 415 |
+
DOC_CONTENT=relevant_passages,
|
| 416 |
+
TIMESTAMP=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 417 |
+
CONVERSATION=conversation_str,
|
| 418 |
)
|
| 419 |
|
| 420 |
conversation.append({"role": "user", "content": query})
|
|
|
|
| 423 |
for chunk in bot_client.process_stream(model, req_data):
|
| 424 |
if "error" in chunk:
|
| 425 |
raise Exception(chunk["error"])
|
| 426 |
+
|
| 427 |
message = chunk.get("choices", [{}])[0].get("delta", {})
|
| 428 |
content = message.get("content", "")
|
| 429 |
+
|
| 430 |
if content:
|
| 431 |
yield {"type": "answer", "content": content}
|
| 432 |
+
|
| 433 |
except Exception as e:
|
| 434 |
raise gr.Error("Exception: " + repr(e))
|
| 435 |
|
| 436 |
@staticmethod
|
| 437 |
def predict_stream(
|
| 438 |
+
query: str,
|
| 439 |
+
chatbot: list,
|
| 440 |
task_history: list,
|
| 441 |
+
model: str,
|
| 442 |
faiss_db: FaissTextDatabase,
|
| 443 |
bot_client: BotClient,
|
| 444 |
) -> tuple:
|
|
|
|
| 450 |
Args:
|
| 451 |
query (str): The content of the user's input query.
|
| 452 |
chatbot (list): The chatbot's historical message list.
|
| 453 |
+
task_history (list): The task history record list.
|
| 454 |
model (Model): The model used to generate responses.
|
| 455 |
bot_client (object): The chatbot client object.
|
| 456 |
faiss_db (FaissTextDatabase): The FAISS database instance.
|
|
|
|
| 460 |
"""
|
| 461 |
query = query if query else QUERY_DEFAULT
|
| 462 |
|
| 463 |
+
logging.info(f"User: {query}")
|
| 464 |
+
chatbot.append({"role": "user", "content": query})
|
| 465 |
+
|
| 466 |
# First yield the chatbot with user message
|
| 467 |
yield chatbot, None
|
| 468 |
|
| 469 |
new_texts = GradioEvents.chat_stream(
|
| 470 |
+
query,
|
| 471 |
+
task_history,
|
| 472 |
+
model,
|
| 473 |
faiss_db,
|
| 474 |
bot_client,
|
| 475 |
)
|
|
|
|
| 479 |
for new_text in new_texts:
|
| 480 |
if not isinstance(new_text, dict):
|
| 481 |
continue
|
| 482 |
+
|
| 483 |
if new_text.get("type") == "embedding":
|
| 484 |
current_relevant_passage = new_text["content"]
|
| 485 |
yield chatbot, current_relevant_passage
|
|
|
|
| 490 |
continue
|
| 491 |
elif new_text.get("type") == "answer":
|
| 492 |
response += new_text["content"]
|
| 493 |
+
|
| 494 |
# Remove previous message if exists
|
| 495 |
if chatbot[-1].get("role") == "assistant":
|
| 496 |
chatbot.pop(-1)
|
| 497 |
+
|
| 498 |
if response:
|
| 499 |
chatbot.append({"role": "assistant", "content": response})
|
| 500 |
yield chatbot, current_relevant_passage
|
| 501 |
|
| 502 |
+
logging.info(f"History: {task_history}")
|
| 503 |
+
task_history.append((query, response))
|
| 504 |
+
logging.info(f"ERNIE models: {response}")
|
| 505 |
|
| 506 |
@staticmethod
|
| 507 |
def regenerate(
|
| 508 |
+
chatbot: list,
|
| 509 |
+
task_history: list,
|
| 510 |
+
model: str,
|
| 511 |
faiss_db: FaissTextDatabase,
|
| 512 |
bot_client: BotClient,
|
| 513 |
) -> tuple:
|
|
|
|
| 533 |
chatbot.pop(-1)
|
| 534 |
chatbot.pop(-1)
|
| 535 |
|
| 536 |
+
yield from GradioEvents.predict_stream(
|
| 537 |
+
item[0],
|
| 538 |
+
chatbot,
|
| 539 |
+
task_history,
|
| 540 |
+
model,
|
| 541 |
faiss_db,
|
| 542 |
bot_client,
|
| 543 |
+
)
|
|
|
|
| 544 |
|
| 545 |
@staticmethod
|
| 546 |
def reset_user_input() -> gr.update:
|
|
|
|
| 561 |
tuple: A named tuple containing the updated values for chatbot, task_history, file_btn, and relevant_passage
|
| 562 |
"""
|
| 563 |
GradioEvents.gc()
|
| 564 |
+
|
| 565 |
+
reset_result = namedtuple("reset_result", ["chatbot", "task_history", "file_btn", "relevant_passage"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 566 |
return reset_result(
|
| 567 |
[], # clear chatbot
|
| 568 |
[], # clear task_history
|
| 569 |
gr.update(value=None), # clear file_btn
|
| 570 |
+
gr.update(value=None), # reset relevant_passage
|
| 571 |
)
|
| 572 |
+
|
| 573 |
@staticmethod
|
| 574 |
def gc():
|
| 575 |
"""
|
|
|
|
| 596 |
extension = image_path.split(".")[-1]
|
| 597 |
with open(image_path, "rb") as image_file:
|
| 598 |
base64_image = base64.b64encode(image_file.read()).decode("utf-8")
|
| 599 |
+
url = f"data:image/{extension};base64,{base64_image}"
|
| 600 |
return url
|
| 601 |
|
| 602 |
@staticmethod
|
|
|
|
| 648 |
|
| 649 |
if len(line) <= chunk_size:
|
| 650 |
return line, ""
|
| 651 |
+
|
| 652 |
# Search from chunk_size position backwards
|
| 653 |
split_pos = chunk_size
|
| 654 |
for i in range(chunk_size, 0, -1):
|
| 655 |
if line[i] in PUNCTUATIONS:
|
| 656 |
split_pos = i + 1 # Include punctuation
|
| 657 |
break
|
| 658 |
+
|
| 659 |
# Fallback to whitespace if no punctuation found
|
| 660 |
if split_pos == chunk_size:
|
| 661 |
split_pos = line.rfind(" ", 0, chunk_size)
|
| 662 |
if split_pos == -1:
|
| 663 |
split_pos = chunk_size # Hard split
|
| 664 |
+
|
| 665 |
return line[:split_pos], line[split_pos:]
|
| 666 |
|
| 667 |
@staticmethod
|
|
|
|
| 687 |
chunks = []
|
| 688 |
current_chunk = []
|
| 689 |
current_length = 0
|
| 690 |
+
|
| 691 |
for line in lines:
|
| 692 |
# If adding this line would exceed chunk size (and we have content)
|
| 693 |
if current_length + len(line) > chunk_size and current_chunk:
|
|
|
|
| 699 |
while len(line) > chunk_size:
|
| 700 |
head, line = GradioEvents.split_oversized_line(line, chunk_size)
|
| 701 |
chunks.append(head)
|
| 702 |
+
|
| 703 |
# Add remaining line content
|
| 704 |
if line:
|
| 705 |
current_chunk.append(line)
|
| 706 |
current_length += len(line) + 1
|
| 707 |
+
|
| 708 |
if current_chunk:
|
| 709 |
chunks.append("\n".join(current_chunk))
|
| 710 |
return chunks
|
| 711 |
|
| 712 |
@staticmethod
|
| 713 |
def file_upload(
|
| 714 |
+
files_url: list, chunk_size: int, faiss_db: FaissTextDatabase, progress_bar: gr.Progress = gr.Progress()
|
|
|
|
|
|
|
|
|
|
| 715 |
) -> str:
|
| 716 |
"""
|
| 717 |
Uploads and processes multiple files by splitting them into semantically meaningful chunks,
|
|
|
|
| 732 |
for file_url in files_url:
|
| 733 |
if not GradioEvents.save_file_to_db(file_url, chunk_size, faiss_db, progress_bar):
|
| 734 |
file_name = os.path.basename(file_url)
|
| 735 |
+
gr.Info(f"{file_name} already processed.")
|
| 736 |
|
| 737 |
yield gr.update(visible=False)
|
| 738 |
|
| 739 |
@staticmethod
|
| 740 |
+
def save_file_to_db(
|
| 741 |
+
file_url: str,
|
| 742 |
+
chunk_size: int,
|
| 743 |
+
faiss_db: FaissTextDatabase,
|
| 744 |
+
progress_bar: gr.Progress = None,
|
| 745 |
+
save_file: bool = False,
|
| 746 |
+
):
|
| 747 |
"""
|
| 748 |
Processes and indexes document content into FAISS database with semantic-aware chunking.
|
| 749 |
Handles file validation, text segmentation, embedding generation and storage operations.
|
|
|
|
| 758 |
bool: True if the file was saved successfully, otherwise False.
|
| 759 |
"""
|
| 760 |
if not os.path.exists(file_url):
|
| 761 |
+
logging.error(f"File not found: {file_url}")
|
| 762 |
return False
|
| 763 |
|
| 764 |
file_name = os.path.basename(file_url)
|
| 765 |
if not faiss_db.is_file_processed(file_url):
|
| 766 |
+
logging.info(f"{file_url} not processed yet, processing now...")
|
| 767 |
try:
|
| 768 |
segments = GradioEvents.split_text_into_chunks(file_url, chunk_size)
|
| 769 |
faiss_db.add_embeddings(file_url, segments, progress_bar, save_file)
|
| 770 |
|
| 771 |
+
logging.info(f"{file_url} processed successfully.")
|
| 772 |
return True
|
| 773 |
except Exception as e:
|
| 774 |
+
logging.error(f"Error processing {file_url}: {e!s}")
|
| 775 |
+
gr.Error(f"Error processing file: {file_name}")
|
| 776 |
raise
|
| 777 |
else:
|
| 778 |
+
logging.info(f"{file_url} already processed.")
|
| 779 |
return False
|
| 780 |
|
| 781 |
|
| 782 |
def launch_demo(args: argparse.Namespace, bot_client: BotClient, faiss_db_template: FaissTextDatabase):
|
| 783 |
"""
|
| 784 |
Launch demo program
|
| 785 |
+
|
| 786 |
Args:
|
| 787 |
args (argparse.Namespace): argparse Namespace object containing parsed command line arguments
|
| 788 |
bot_client (BotClient): Bot client instance
|
|
|
|
| 809 |
}
|
| 810 |
"""
|
| 811 |
with gr.Blocks(css=css) as demo:
|
| 812 |
+
model_name = gr.State(next(iter(args.model_map.keys())))
|
| 813 |
faiss_db = gr.State(copy.deepcopy(faiss_db_template))
|
| 814 |
|
| 815 |
logo_url = GradioEvents.get_image_url("assets/logo.png")
|
| 816 |
+
gr.Markdown(
|
| 817 |
+
f"""\
|
| 818 |
+
<p align="center"><img src="{logo_url}" \
|
| 819 |
+
style="height: 60px"/><p>"""
|
| 820 |
+
)
|
| 821 |
gr.Markdown(
|
| 822 |
"""\
|
| 823 |
<center><font size=3>This demo is based on ERNIE models. \
|
| 824 |
(本演示基于文心大模型实现。)</center>"""
|
| 825 |
)
|
| 826 |
+
gr.Markdown(
|
| 827 |
+
"""\
|
| 828 |
+
<center><font size=3> <a href="https://ernie.baidu.com/">ERNIE Bot</a> | \
|
| 829 |
+
<a href="https://github.com/PaddlePaddle/ERNIE">GitHub</a> | \
|
| 830 |
+
<a href="https://huggingface.co/baidu">Hugging Face</a> | \
|
| 831 |
+
<a href="https://aistudio.baidu.com/modelsoverview">BAIDU AI Studio</a> | \
|
| 832 |
+
<a href="https://yiyan.baidu.com/blog/publication/">Technical Report</a></center>"""
|
| 833 |
)
|
| 834 |
|
| 835 |
+
chatbot = gr.Chatbot(label="ERNIE", type="messages")
|
| 836 |
+
|
| 837 |
with gr.Row(equal_height=True):
|
| 838 |
file_btn = gr.File(
|
| 839 |
+
label="Knowledge Base Upload (System default will be used if none provided. Accepted formats: TXT, MD)",
|
| 840 |
+
height="150px",
|
| 841 |
file_types=[".txt", ".md"],
|
| 842 |
elem_id="file-upload",
|
| 843 |
+
file_count="multiple",
|
| 844 |
)
|
| 845 |
relevant_passage = gr.Textbox(
|
| 846 |
+
label="Relevant Passage", lines=5, max_lines=5, placeholder=RELEVANT_PASSAGE_DEFAULT, interactive=False
|
|
|
|
|
|
|
|
|
|
|
|
|
| 847 |
)
|
| 848 |
with gr.Row():
|
| 849 |
progress_bar = gr.Textbox(label="Progress", visible=False)
|
|
|
|
| 854 |
empty_btn = gr.Button("🧹 Clear History(清除历史)")
|
| 855 |
submit_btn = gr.Button("🚀 Submit(发送)", elem_id="submit-button")
|
| 856 |
regen_btn = gr.Button("🤔️ Regenerate(重试)")
|
| 857 |
+
|
| 858 |
task_history = gr.State([])
|
| 859 |
+
|
| 860 |
+
predict_with_clients = partial(GradioEvents.predict_stream, bot_client=bot_client)
|
| 861 |
+
regenerate_with_clients = partial(GradioEvents.regenerate, bot_client=bot_client)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 862 |
file_upload_with_clients = partial(
|
| 863 |
GradioEvents.file_upload,
|
| 864 |
)
|
| 865 |
+
|
| 866 |
chunk_size = gr.State(args.chunk_size)
|
| 867 |
file_btn.change(
|
| 868 |
fn=file_upload_with_clients,
|
|
|
|
| 870 |
outputs=[progress_bar],
|
| 871 |
)
|
| 872 |
query.submit(
|
| 873 |
+
predict_with_clients,
|
| 874 |
+
inputs=[query, chatbot, task_history, model_name, faiss_db],
|
| 875 |
outputs=[chatbot, relevant_passage],
|
| 876 |
+
show_progress=True,
|
| 877 |
)
|
| 878 |
query.submit(GradioEvents.reset_user_input, [], [query])
|
| 879 |
submit_btn.click(
|
| 880 |
+
predict_with_clients,
|
| 881 |
inputs=[query, chatbot, task_history, model_name, faiss_db],
|
| 882 |
outputs=[chatbot, relevant_passage],
|
| 883 |
show_progress=True,
|
| 884 |
)
|
| 885 |
submit_btn.click(GradioEvents.reset_user_input, [], [query])
|
| 886 |
empty_btn.click(
|
| 887 |
+
GradioEvents.reset_state, outputs=[chatbot, task_history, file_btn, relevant_passage], show_progress=True
|
|
|
|
| 888 |
)
|
| 889 |
regen_btn.click(
|
| 890 |
+
regenerate_with_clients,
|
| 891 |
inputs=[chatbot, task_history, model_name, faiss_db],
|
| 892 |
outputs=[chatbot, relevant_passage],
|
| 893 |
+
show_progress=True,
|
| 894 |
)
|
| 895 |
|
| 896 |
+
demo.queue().launch(server_port=args.server_port, server_name=args.server_name)
|
|
|
|
|
|
|
|
|
|
| 897 |
|
| 898 |
|
| 899 |
def main():
|
|
|
|
| 907 |
|
| 908 |
launch_demo(args, bot_client, faiss_db)
|
| 909 |
|
| 910 |
+
|
| 911 |
if __name__ == "__main__":
|
| 912 |
main()
|